home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n4.zip / ROD.ZIP / GETOFF.C < prev    next >
C/C++ Source or Header  |  1993-02-22  |  6KB  |  267 lines

  1. //*
  2. //* getoff.c:     This utility reads the offsets of variables, named in a list
  3. //*                file, from the named *.MAP file.
  4. //*
  5. //*                "getoff <MAPFILE> <LISTFILE>"
  6. //*                
  7. //*                The utility then overwrites the original list file
  8. //*                with the offsets of the global variables as constants.
  9. //*                The constants file can then be include in the DLL routines.
  10. //*                The use of in-line assembly allows you to reference the
  11. //*                variables.
  12. //*
  13. //*    r.e.haxton 93jan20
  14. //*
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <string.h>
  20.  
  21. #define    VAR_DS_OFFSET    6
  22.  
  23. void main(int argc, char **argv)
  24. {
  25.     /* function prototypes */
  26.     short        GetVarName(FILE *, char *, long *);
  27.     char        *GetVarOffset(FILE *, long *);
  28.  
  29.     void        raiseStr(char *);
  30.  
  31.     /* local variables */
  32.     char        mapFile[50];
  33.     char        listFile[50];
  34.     float        perCent;
  35.       FILE        *mapPtr, *filePtr;
  36.     long        bufStartPos;
  37.  
  38.     static    float fileCount= 0;
  39.     static    float matchCount= 0;
  40.  
  41.     struct LIST_NODE    {
  42.  
  43.         char    name[50];
  44.         char    offset[5];
  45.         struct LIST_NODE *next;
  46.  
  47.     } *tailPtr, *headPtr;
  48.  
  49. //REH--->93jan20     LATER PUT IN CORRECT ERROR CHECKING...
  50. //                        FOR NOW IN HOUSE UTILITY.
  51.  
  52.     /* get file name */
  53.     if(argc < 3) {
  54.  
  55.         printf("Please enter the name of the MAP file you wish to search: ");
  56.         scanf(mapFile, "%s\n");
  57.  
  58.         printf("Please enter the name of the LIST file: ");
  59.         scanf(listFile, "%s\n");
  60.  
  61.     }
  62.                              
  63.     else {
  64.  
  65.         strcpy(mapFile, argv[argc-2]);
  66.         strcpy(listFile, argv[argc-1]);
  67.  
  68.     }
  69.  
  70.     /* open the map file (read) */
  71.     if((mapPtr= fopen(mapFile, "r")) == NULL) {
  72.  
  73.         printf("Can't open file: %s\n");
  74.         exit(-1);
  75.  
  76.     }
  77.     
  78.     /* open the list file (read/write) */
  79.     if((filePtr= fopen(listFile, "r+")) == NULL) {
  80.  
  81.         printf("Can't open file: %s\n");
  82.         exit(-1);
  83.  
  84.     }
  85.  
  86.     /* indicate to user that you're working */
  87.     printf("Reading List File...\n");
  88.  
  89.     /*    alloc space for tailPtr node and read list file */
  90.     if((headPtr= (struct LIST_NODE *) malloc(sizeof(struct LIST_NODE))) == NULL) {
  91.  
  92.         printf("Not Enough Memory!\n");
  93.         exit(-1);
  94.  
  95.     }
  96.  
  97.     /* read list file */
  98.     fscanf(filePtr, "%s", headPtr->name);
  99.  
  100.     /* increment file counter */
  101.     ++fileCount;
  102.  
  103.     headPtr->next= NULL;
  104.     tailPtr= headPtr;
  105.         
  106.     /* read list file until EOF */
  107.     while(!feof(filePtr)) {
  108.  
  109.         if((tailPtr->next= (struct LIST_NODE *) malloc(sizeof(struct LIST_NODE))) == NULL) {
  110.  
  111.             printf("Not Enough Memory!\n");
  112.             exit(1);
  113.  
  114.         }
  115.  
  116.         tailPtr= tailPtr->next;
  117.  
  118.         /* read name from list file */
  119.         fscanf(filePtr, "%s", tailPtr->name);
  120.  
  121.         /* increment file counter */
  122.         ++fileCount;
  123.  
  124.         tailPtr->next= NULL;
  125.         
  126.     }
  127.  
  128.     /* decrement the fileCount by one */
  129.     --fileCount;
  130.  
  131.     /* close list file */
  132.     fclose(filePtr);
  133.  
  134.     /* set up search file pointer */
  135.     tailPtr= headPtr;
  136.  
  137.     /* search map file */
  138.     while(tailPtr->next != NULL) {
  139.  
  140.         /* search for variable string */
  141.         if(GetVarName(mapPtr, tailPtr->name, &bufStartPos)) {
  142.  
  143.             /* grab offset of variable from map file */
  144.             strcpy(tailPtr->offset, GetVarOffset(mapPtr, &bufStartPos));
  145.             tailPtr= tailPtr->next;
  146.  
  147.             /* calculate and print percentage of search done */
  148.             perCent=  ((++matchCount) / fileCount) * 100;
  149.             printf("Percent completed: %3.2f\n", perCent);
  150.  
  151.         }
  152.     }
  153.  
  154.     /* open an include file and output the constants */
  155.     if((filePtr= fopen("consts.h", "w+")) == NULL) {
  156.  
  157.         printf("Can't open file: %s\n");
  158.         exit(-1);
  159.  
  160.     }
  161.  
  162.     /* output var name and offsets to new include file */
  163.     tailPtr= headPtr;
  164.  
  165.     printf("Writing output file...\n");
  166.  
  167.     while(tailPtr->next != NULL) {
  168.  
  169.         /* convert variable name to uppercase */
  170.         raiseStr(tailPtr->name);
  171.  
  172.         fprintf(filePtr, "#define %s\t\t0x%s\n", tailPtr->name, tailPtr->offset);
  173.         tailPtr= tailPtr->next;
  174.  
  175.     }
  176.  
  177.     /* free nodes */
  178.     tailPtr= headPtr;
  179.     headPtr= headPtr->next;
  180.     
  181.     while(tailPtr->next != NULL) {
  182.  
  183.         /* free node */
  184.         free(tailPtr);
  185.  
  186.         tailPtr= headPtr;
  187.         headPtr= headPtr->next;
  188.  
  189.     }
  190.  
  191.     /* close files */
  192.     fclose(filePtr);
  193.     fclose(mapPtr);
  194.  
  195.     printf("Finish\n");
  196.  
  197. }    /* main */
  198.  
  199. //-------------------------------------------------------------------------->
  200.  
  201. //
  202. // This routine keeps track of the search file pointer and grabs the
  203. // names of variables from the name field in the *.map file. 
  204. //
  205.  
  206. short GetVarName(FILE *mapPtr, char *varName, long *prevpos)
  207. {
  208.     /* local variables */
  209.     char        tmpBuffer[80];
  210.     static    long ptrpos= 0;
  211.  
  212.     /* obtain record start position if beginning reset pointer position */
  213.     if((*prevpos= ftell(mapPtr)) == 0)
  214.         ptrpos= 0;
  215.     else
  216.         *prevpos= ptrpos;
  217.  
  218.     /* grab total map line */
  219.     fgets(tmpBuffer, 80, mapPtr);
  220.  
  221.     /* get new position */
  222.     ptrpos= ftell(mapPtr);
  223.  
  224.     /* check for matching string */
  225.     if(strstr(tmpBuffer, varName) != NULL)
  226.         return(1);
  227.  
  228.     else
  229.         return(0);
  230.  
  231. }    /* GetVarName */
  232.  
  233. //-------------------------------------------------------------------------->
  234.  
  235. //
  236. // This routine grabs the offset of the found named variable.
  237. //
  238.  
  239. char  *GetVarOffset(FILE *mapPtr, long *prevPos)
  240. {
  241.     /* local variables */
  242.     char    tmpBuffer[5];
  243.  
  244.     /* seek to and get variable name data offset map file */
  245.     fseek(mapPtr, (*prevPos + VAR_DS_OFFSET), SEEK_SET);
  246.     fgets(tmpBuffer, 5, mapPtr);
  247.  
  248.     /* return offset */
  249.     return(tmpBuffer);
  250.  
  251. }    /* GetVarOffset */
  252.  
  253. //------------------------------------------------------------------------>
  254.  
  255. //
  256. // this routine converts a string to uppercase
  257. //
  258. void    raiseStr(char *string)
  259. {
  260.   /* local variables */
  261.     short        i;
  262.  
  263.     for(i= 0; (i < strlen(string)); i++)
  264.         string[i]= toupper(string[i]);
  265.  
  266. } /* raiseStr */
  267.