home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / scan_def.zip / scan_def.c < prev    next >
C/C++ Source or Header  |  1997-04-23  |  4KB  |  128 lines

  1. //
  2. // scan_def.c
  3. // 22-Apr-97
  4. // (C)1997 Cornel Huth - http://www.40th.com
  5. //
  6. // reads .h file grabbing all #define lines and sorting those by number and ID(name)
  7. // primarily intended to deal with auto-ID generators that don't really do a good
  8. // job of helping you prevent duplicate IDs/numbers from begin generated, such as
  9. // can easily happen when you duplicate/copy controls in a design
  10.  
  11.  
  12. #define INCL_BASE
  13. #include <os2.h>
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #define MAX_DEFINE_LINES 9999           // max #define lines accepted
  20. #define MAX_LINE_LEN 260                // max length of any line read from disk
  21. #define MAX_ID_LEN 50                   // max size of ID name part of #define
  22. #define MAX_NUM_LEN 12                  // max size of "number" part of #define
  23. #define MAX_BUFFER_LEN (MAX_ID_LEN+1+MAX_NUM_LEN+1) // qsort element size
  24. #define FMT_STR "%12.12s %s"            // size should match MAX_NUM_LEN
  25.  
  26. #define MATCH_STR "#define"
  27. #define MATCH_LEN sizeof(MATCH_STR)
  28.  
  29. int compareNum(VOID *arg1, VOID *arg2) {
  30.    return stricmp(arg1, arg2);
  31. }
  32.  
  33. int compareID(VOID *arg1, VOID *arg2) {
  34.    return stricmp( (PVOID)((ULONG)arg1+MAX_NUM_LEN+1), (PVOID)((ULONG)arg2+MAX_NUM_LEN+1));
  35. }
  36.  
  37. int main(int argc, const char *argv[]) {
  38.  
  39.  FILE *file;
  40.  
  41.  CHAR lineBuffer[MAX_LINE_LEN];
  42.  CHAR tID[MAX_LINE_LEN];        // in case tID/tNum larger than what is wanted
  43.  CHAR tNum[MAX_LINE_LEN];
  44.  CHAR *sortBuffer=NULL;
  45.  CHAR *sbPtr=NULL;
  46.  CHAR *tPos;
  47.  int lineNo=0;
  48.  int defNo=0;
  49.  int i;
  50.  
  51.  printf("scan_def 1.0  Copyright (C)1997 Cornel Huth  http://www.40th.com/\n");
  52.  
  53.  if (argc < 2) {
  54.     printf("\n\tUse [G:\\]scan_def filename.h [ > file.lst]\n");
  55.     return 1;
  56.  }
  57.  
  58.  file = fopen(argv[1],"rt");
  59.  if (file==NULL) {
  60.     printf("Cannot open %s\n",argv[1]);
  61.     return 1;
  62.  }
  63.  
  64.  sortBuffer = malloc(MAX_BUFFER_LEN*MAX_DEFINE_LINES);
  65.  if (sortBuffer==NULL) {
  66.     printf("Cannot allocate sortBuffer\n");
  67.     return 1;
  68.  }
  69.  sbPtr = sortBuffer;
  70.  
  71.  printf("\nfile: %s\n",argv[1]);
  72.  printf("Unsorted ======================================\n\n");
  73.  
  74.  fgets(lineBuffer,sizeof(lineBuffer),file);    // read a line
  75.  while (!feof(file)) {
  76.     lineNo++;
  77.  
  78.     // load only #define's (even in comments, #ifdef 0, whatever)
  79.  
  80.     if (tPos=strstr(lineBuffer,MATCH_STR)) {
  81.  
  82.        defNo++;
  83.        if (defNo > MAX_DEFINE_LINES) {
  84.           printf("Exceed %d #define lines\n",MAX_DEFINE_LINES);
  85.           break;
  86.        }
  87.  
  88.        sscanf((tPos+MATCH_LEN),"%s %s",tID,tNum);
  89.  
  90.        // truncate ID/Num if size exceeds program's allocations
  91.  
  92.        if ( strlen(tID) >= MAX_ID_LEN) tID[MAX_ID_LEN] = 0;
  93.        if ( strlen(tNum) >= MAX_NUM_LEN) tNum[MAX_NUM_LEN] = 0;
  94.  
  95.        sprintf(sbPtr,FMT_STR,tNum,tID);
  96.        printf("%4.4d %s\n",defNo,sbPtr);
  97.        sbPtr = (CHAR*) ((ULONG)sbPtr + MAX_BUFFER_LEN);
  98.     }
  99.  
  100.     fgets(lineBuffer,sizeof(lineBuffer),file);  // read another line
  101.  }
  102.  
  103.  // sort by number and display
  104.  
  105.  printf("\n\nSorted by Number ==============================\n\n");
  106.  qsort(sortBuffer,defNo,MAX_BUFFER_LEN,compareNum);
  107.  sbPtr = sortBuffer;
  108.  for (i=0; i < defNo; i++) {
  109.     printf("%4.4d %s\n",i+1,sbPtr);
  110.     sbPtr = (CHAR*) ((ULONG)sbPtr + MAX_BUFFER_LEN);
  111.  }
  112.  
  113.  // sort by ID name and display
  114.  
  115.  printf("\n\nSorted by ID ==================================\n\n");
  116.  qsort(sortBuffer,defNo,MAX_BUFFER_LEN,compareID);
  117.  sbPtr = sortBuffer;
  118.  for (i=0; i < defNo; i++) {
  119.     printf("%4.4d %s\n",i+1,sbPtr);
  120.     sbPtr = (CHAR*) ((ULONG)sbPtr + MAX_BUFFER_LEN);
  121.  }
  122.  
  123.  fclose(file);
  124.  free(sortBuffer);
  125.  
  126.  return 0;
  127. }
  128.