home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / scan_h11.zip / scan_def.c < prev    next >
C/C++ Source or Header  |  1997-06-03  |  5KB  |  169 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. // 3-Jun-97 (chh)
  12. // added * display on duplicate ID lines and number lines
  13. // added -dups switch to show only duplicates
  14.  
  15. #define INCL_BASE
  16. #include <os2.h>
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define MAX_DEFINE_LINES 9999           // max #define lines accepted
  23. #define MAX_LINE_LEN 260                // max length of any line read from disk
  24. #define MAX_ID_LEN 50                   // max size of ID name part of #define
  25. #define MAX_NUM_LEN 12                  // max size of "number" part of #define
  26. #define MAX_BUFFER_LEN (MAX_ID_LEN+1+MAX_NUM_LEN+1) // qsort element size
  27. #define FMT_STR "%12.12s %s"            // size should match MAX_NUM_LEN
  28.  
  29. #define MATCH_STR "#define"
  30. #define MATCH_LEN sizeof(MATCH_STR)
  31.  
  32. int compareNum(VOID *arg1, VOID *arg2) {
  33.    return stricmp(arg1, arg2);
  34. }
  35.  
  36. int compareID(VOID *arg1, VOID *arg2) {
  37.    return stricmp( (PVOID)((ULONG)arg1+MAX_NUM_LEN+1), (PVOID)((ULONG)arg2+MAX_NUM_LEN+1));
  38. }
  39.  
  40. int main(int argc, const char *argv[]) {
  41.  
  42.  FILE *file;
  43.  
  44.  CHAR fName[260];
  45.  CHAR lineBuffer[MAX_LINE_LEN];
  46.  CHAR tID[MAX_LINE_LEN];        // in case tID/tNum larger than what is wanted
  47.  CHAR tNum[MAX_LINE_LEN];
  48.  CHAR tIDlast[MAX_LINE_LEN];
  49.  CHAR tNumLast[MAX_LINE_LEN];
  50.  CHAR *sortBuffer=NULL;
  51.  CHAR *sbPtr=NULL;
  52.  CHAR *tPos;
  53.  int lineNo=0;
  54.  int defNo=0;
  55.  int i;
  56.  int dupsOnly = 0;
  57.  
  58.  printf("scan_def 1.1  Copyright (C)1997 Cornel Huth  http://www.40th.com/\n");
  59.  
  60.  if (argc < 2) {
  61.     printf("\n\tUse [G:\\]scan_def [-dups] filename.h [ > file.lst]\n");
  62.     return 1;
  63.  }
  64.  
  65.  fName[0] = 0;
  66.  for (i=1; i < argc; i++) {
  67.  
  68.     if (*argv[i] == '-') {
  69.        if (strlen(argv[i]) >= 5) {
  70.           if (stricmp(argv[i],"-dups")==0) dupsOnly = 1;
  71.        }
  72.        continue;
  73.     }
  74.     strcpy(fName,argv[i]);
  75.  }
  76.  
  77.  file = fopen(fName,"rt");
  78.  if (file==NULL) {
  79.     printf("Cannot open %s\n",fName);
  80.     return 1;
  81.  }
  82.  
  83.  sortBuffer = malloc(MAX_BUFFER_LEN*MAX_DEFINE_LINES);
  84.  if (sortBuffer==NULL) {
  85.     printf("Cannot allocate sortBuffer\n");
  86.     return 1;
  87.  }
  88.  sbPtr = sortBuffer;
  89.  
  90.  printf("\nfile: %s  ",fName);
  91.  if (dupsOnly) printf("(duplicate entries only)");
  92.  printf("\n");
  93.  
  94.  
  95.  if (dupsOnly == 0) printf("Unsorted ======================================\n\n");
  96.  
  97.  fgets(lineBuffer,sizeof(lineBuffer),file);    // read a line
  98.  while (!feof(file)) {
  99.     lineNo++;
  100.  
  101.     // load only #define's (even in comments, #ifdef 0, whatever)
  102.  
  103.     if (tPos=strstr(lineBuffer,MATCH_STR)) {
  104.  
  105.        defNo++;
  106.        if (defNo > MAX_DEFINE_LINES) {
  107.           printf("Exceed %d #define lines\n",MAX_DEFINE_LINES);
  108.           break;
  109.        }
  110.  
  111.        sscanf((tPos+MATCH_LEN),"%s %s",tID,tNum);
  112.  
  113.        // truncate ID/Num if size exceeds program's allocations
  114.  
  115.        if ( strlen(tID) >= MAX_ID_LEN) tID[MAX_ID_LEN] = 0;
  116.        if ( strlen(tNum) >= MAX_NUM_LEN) tNum[MAX_NUM_LEN] = 0;
  117.  
  118.        sprintf(sbPtr,FMT_STR,tNum,tID);
  119.        if (dupsOnly == 0) printf("%4.4d %s\n",defNo,sbPtr);
  120.        sbPtr = (CHAR*) ((ULONG)sbPtr + MAX_BUFFER_LEN);
  121.     }
  122.  
  123.     fgets(lineBuffer,sizeof(lineBuffer),file);  // read another line
  124.  }
  125.  
  126.  // sort by number and display
  127.  
  128.  printf("\n\nSorted by Number ==============================\n\n");
  129.  qsort(sortBuffer,defNo,MAX_BUFFER_LEN,compareNum);
  130.  sbPtr = sortBuffer;
  131.  
  132.  tNumLast[0] = 0;
  133.  
  134.  for (i=0; i < defNo; i++) {
  135.     if (strnicmp(sbPtr,tNumLast,MAX_NUM_LEN)==0) {
  136.        printf("%4.4d *%s\n",i+1,sbPtr);
  137.     }
  138.     else {
  139.        if (dupsOnly == 0) printf("%4.4d  %s\n",i+1,sbPtr);
  140.     }
  141.     strncpy(tNumLast,sbPtr,MAX_NUM_LEN);
  142.     sbPtr = (CHAR*) ((ULONG)sbPtr + MAX_BUFFER_LEN);
  143.  }
  144.  
  145.  // sort by ID name and display
  146.  
  147.  printf("\n\nSorted by ID ==================================\n\n");
  148.  qsort(sortBuffer,defNo,MAX_BUFFER_LEN,compareID);
  149.  sbPtr = sortBuffer;
  150.  
  151.  tIDlast[0] = 0;
  152.  
  153.  for (i=0; i < defNo; i++) {
  154.  
  155.     if (strnicmp( (PVOID)((ULONG)sbPtr+MAX_NUM_LEN+1), tIDlast, MAX_ID_LEN)==0) {
  156.        printf("%4.4d *%s\n",i+1,sbPtr);
  157.     }
  158.     else {
  159.        if (dupsOnly == 0) printf("%4.4d  %s\n",i+1,sbPtr);
  160.     }
  161.     strncpy(tIDlast, (PVOID)((ULONG)sbPtr+MAX_NUM_LEN+1),MAX_ID_LEN);
  162.     sbPtr = (CHAR*) ((ULONG)sbPtr + MAX_BUFFER_LEN);
  163.  }
  164.  
  165.  fclose(file);
  166.  free(sortBuffer);
  167.  return 0;
  168. }
  169.