home *** CD-ROM | disk | FTP | other *** search
/ Hot Shareware 35 / hot35.iso / ficheros / 9TXT / ZE32V270.ZIP / CINC.CP_ / CINC.CP
Text File  |  1997-07-08  |  11KB  |  491 lines

  1. //-------------------------------------------------------------------------
  2. //--
  3. //-- Copyright (c) 1991-95   Xidicone Pty Ltd
  4. //-- All rights reserved.
  5. //--
  6. //-------------------------------------------------------------------------
  7.  
  8. #include <stdio.h>
  9. #include <direct.h>
  10. #include <stdlib.h>
  11. #include <search.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. //-------------------------------------------------------------------------
  16.  
  17. #define BOOL int
  18.  
  19. #define TRUE  1
  20. #define FALSE 0
  21.  
  22. //-------------------------------------------------------------------------
  23.  
  24. static int  sIndent = 0;
  25.  
  26. static char szIndent[20];
  27. static char szFullName[180];
  28. static char szLineTemp[120];
  29.  
  30. static BOOL fFormat = 0;
  31. static BOOL fParseSystem = 0;
  32.  
  33. static unsigned int sIndex = 0;
  34. static char        *apszIncludeList[200];
  35.  
  36. //-------------------------------------------------------------------------
  37. #define TITLE      "CINC Version 1.2 - Include File Searching Tool\n"
  38.  
  39. #define COPYRIGHT "Copyright (c) 1996 Xidicone Pty Ltd - All rights reserved\n"
  40.  
  41. #define SYNTAX     "\n" \
  42.                   "Syntax: CINC [flags] filename\n"       \
  43.                        "        -s  search system include files as well\n" \
  44.                        "        -f  format the output display\n\n"
  45.  
  46. #define AUTHOR     "Author: Jussi Jumppanen (jussi@ca.com.au)\n"\
  47.                        "        http://ourworld.compuserve.com/homepages/jussi\n\n"
  48.  
  49. //-------------------------------------------------------------------------
  50.  
  51. int CheckFile(char *pszFileName)
  52. {
  53.    int sResult = 0;
  54.  
  55.    if (sIndex < 200)
  56.    {
  57.       for (int i = 0; i < (int)sIndex; ++i)
  58.       {
  59.          if (stricmp(pszFileName, apszIncludeList[i]) == 0)
  60.          {
  61.             sResult = 1;
  62.             break;
  63.          }
  64.       }
  65.  
  66.       if (sResult == 0)
  67.       {
  68.          //-- add the item to the list
  69.          apszIncludeList[sIndex] = new char[180];
  70.          strcpy(apszIncludeList[sIndex], pszFileName);
  71.          ++sIndex;
  72.       }
  73.    }
  74.    else
  75.    {
  76.       printf("Error: Include structure is to complex to process\n");
  77.       exit(-1);
  78.    }
  79.  
  80.    //-- only process include files not in the list
  81.    return (sResult == 0) ? TRUE : FALSE;
  82. }
  83.  
  84. //-------------------------------------------------------------------------
  85. //-- this will cause a GPF if the file is loaded from the output window
  86.  
  87. void IndentChange(void)
  88. {
  89.    if (fFormat)
  90.    {
  91.       sIndent = (sIndent < 5) ? sIndent : 5;
  92.    
  93.       switch (sIndent)
  94.       {
  95.          case 1:
  96.             strcpy(szIndent, " ");
  97.             break;
  98.    
  99.          case 2:
  100.             strcpy(szIndent, "  |- ");
  101.             break;
  102.    
  103.          case 3:
  104.             strcpy(szIndent, "  |  |- ");
  105.             break;
  106.    
  107.          case 4:
  108.             strcpy(szIndent, "  |  |  |- ");
  109.             break;
  110.    
  111.          default:
  112.             strcpy(szIndent, "  |  |  |... ");
  113.             break;
  114.       }
  115.    }
  116.    else
  117.    {
  118.         strcpy(szIndent, " ");
  119.    }
  120. }
  121.  
  122. //-------------------------------------------------------------------------
  123. //-- use this one eventually
  124. void IndentChange1(void)
  125. {
  126.    if (fFormat)
  127.    {
  128.       sIndent = (sIndent < 5) ? sIndent : 5;
  129.    
  130.       switch (sIndent)
  131.       {
  132.          case 1:
  133.             strcpy(szIndent, " ");
  134.             break;
  135.    
  136.          case 2:
  137.             strcpy(szIndent, "  |- ");
  138.             break;
  139.    
  140.          case 3:
  141.             strcpy(szIndent, "  |  |- ");
  142.             break;
  143.    
  144.          case 4:
  145.             strcpy(szIndent, "  |  |  |- ");
  146.             break;
  147.    
  148.          default:
  149.             strcpy(szIndent, "  |  |  |... ");
  150.             break;
  151.       }
  152.    }
  153.    else
  154.    {
  155.         strcpy(szIndent, " ");
  156.    }
  157. }
  158.  
  159. //-------------------------------------------------------------------------
  160.  
  161. void PrintLine(char *pszFile, char *pszLine, int sLine)
  162. {
  163.    char szFileName[150];
  164.  
  165.    //-- build up an file name with indent
  166.    strcpy(szFileName, szIndent);
  167.    strcat(szFileName, pszFile);
  168.  
  169.    if (fFormat)
  170.    {
  171.       printf("%-35.35s Line: %4.4d  |  %s", szFileName, sLine, pszLine);
  172.    }
  173.    else
  174.    {
  175.       printf("File:%-35.35s Line: %4.4d  |  %s", szFileName, sLine, pszLine);
  176.    }
  177. }
  178.  
  179. //-------------------------------------------------------------------------
  180.  
  181. void ParseFile(char *pszFileName, BOOL fLocal = 0)
  182. {
  183.    char szLine[250];
  184.  
  185.    FILE *pFile = 0;
  186.  
  187.    ++sIndent;
  188.  
  189.    //-- build up an indent string level
  190.    IndentChange();
  191.  
  192.    //-- should we try to find the file locally
  193.    if (fLocal == 0)
  194.    {
  195.       //-- try to open the file in the current directory
  196.       pFile = fopen(pszFileName, "r");
  197.    }
  198.  
  199.    //-- search the include path if we don't have a file
  200.    if (pFile == 0)
  201.    {
  202.       //-- look for the file in the INCLUDE path
  203.       _searchenv(pszFileName, "INCLUDE", szFullName);
  204.  
  205.       //-- try to open it
  206.       if (szFullName[0])
  207.       {
  208.          pFile = fopen(szFullName, "r");
  209.       }
  210.    }
  211.  
  212.    int sLine = 0;
  213.  
  214.    //-- process the file if we have one
  215.    if (pFile)
  216.    {
  217.       while (feof(pFile) == 0)
  218.       {
  219.          sLine++;
  220.  
  221.          fgets(szLine, sizeof(szLine), pFile);
  222.  
  223.          if (strncmp(szLine, "#include ", 9) == 0)
  224.          {
  225.             strcpy(szLineTemp, szLine);
  226.  
  227.             char *pszFile = strchr(szLine, '<');
  228.  
  229.             if (pszFile)
  230.             {
  231.                ++pszFile;
  232.  
  233.                //-- get the end of the file name
  234.                char *pszEnd = strchr(pszFile, '>');
  235.                
  236.                if (pszEnd)
  237.                {
  238.                   *pszEnd = '\0';
  239.  
  240.                   //-- make sure we should process syustem files
  241.                   if (fParseSystem)
  242.                   {
  243.                      //-- make sure the file has not already been processed
  244.                      if (CheckFile(pszFile))
  245.                      {
  246.                         PrintLine(pszFileName, szLineTemp, sLine);
  247.    
  248.                         ParseFile(pszFile, 0);
  249.                      }
  250.                   }
  251.                   else
  252.                   {
  253.                      // just print it out
  254.                      PrintLine(pszFileName, szLineTemp, sLine);
  255.                   }
  256.                }
  257.             }
  258.             else
  259.             {
  260.                pszFile = strchr(szLine, '\"');
  261.  
  262.                if (pszFile)
  263.                {
  264.                   ++pszFile;
  265.  
  266.                   //-- get the end of the file name
  267.                   char *pszEnd = strchr(pszFile, '\"');
  268.  
  269.                   if (pszEnd)
  270.                   {
  271.                      *pszEnd = '\0';
  272.  
  273.                      //-- make sure the file has not already been processed
  274.                      if (CheckFile(pszFile))
  275.                      {
  276.                         PrintLine(pszFileName, szLineTemp, sLine);
  277.    
  278.                         ParseFile(pszFile, 1);
  279.                      }
  280.                   }
  281.                }
  282.             }
  283.          }
  284.       }
  285.  
  286.       fclose(pFile);
  287.    }
  288.    else
  289.    {
  290.       printf("Error opening or locating the '%s' include file.\n", pszFileName);
  291.    }
  292.  
  293.    //-- keep track of the indent level
  294.    --sIndent;
  295.  
  296.    //-- put out a blank line to make the output readable
  297.    if ((fFormat) && (sIndent == 1))
  298.    {
  299.       printf("\n");
  300.    }
  301.  
  302.    //-- restore the indent string level
  303.    IndentChange();
  304. }
  305.  
  306. //-------------------------------------------------------------------------
  307.  
  308. int main(int argc, char *argv[])
  309. {
  310.    int sResult = 0;
  311.    char *pszFileName = 0;
  312.  
  313.    memset(apszIncludeList, 0, sizeof(apszIncludeList));
  314.  
  315.    if (argc == 2)
  316.    {
  317.       pszFileName = argv[1];
  318.    }
  319.    else if (argc == 3)
  320.    {
  321.       if (strlen(argv[1]) != 2)
  322.       {
  323.          printf("Unknown flag '%s'\n \n", argv[1]);
  324.          printf(TITLE);
  325.          printf(COPYRIGHT);
  326.          printf(SYNTAX);
  327.          printf(AUTHOR);
  328.          exit(-1);
  329.       }
  330.  
  331.       if ((argv[1][1] == 's') || (argv[1][1] == 'S'))
  332.       {
  333.          fParseSystem = TRUE;
  334.  
  335.          pszFileName = argv[2];
  336.       }
  337.       else if ((argv[1][1] == 'f') || (argv[1][1] == 'F'))
  338.       {
  339.          fFormat = TRUE;
  340.  
  341.          pszFileName = argv[2];
  342.       }
  343.       else
  344.       {
  345.          printf("Unknown flag '%s'\n \n", argv[1]);
  346.          printf(TITLE);
  347.          printf(COPYRIGHT);
  348.          printf(SYNTAX);
  349.          printf(AUTHOR);
  350.          exit(-1);
  351.       }
  352.    }
  353.    else if (argc == 4)
  354.    {
  355.       if (strlen(argv[1]) != 2)
  356.       {
  357.          printf("Unknown flag '%s'\n \n", argv[1]);
  358.          printf(TITLE);
  359.          printf(COPYRIGHT);
  360.          printf(SYNTAX);
  361.          printf(AUTHOR);
  362.          exit(-1);
  363.       }
  364.  
  365.       if (strlen(argv[2]) != 2)
  366.       {
  367.          printf("Unknown flag '%s'\n \n", argv[2]);
  368.          printf(TITLE);
  369.          printf(COPYRIGHT);
  370.          printf(SYNTAX);
  371.          printf(AUTHOR);
  372.          exit(-1);
  373.       }
  374.  
  375.       if ((argv[1][1] == 's') || (argv[1][1] == 'S'))
  376.       {
  377.          fParseSystem = TRUE;
  378.  
  379.          pszFileName = argv[3];
  380.       }
  381.       else if ((argv[1][1] == 'f') || (argv[1][1] == 'F'))
  382.       {
  383.          fFormat = TRUE;
  384.  
  385.          pszFileName = argv[3];
  386.       }
  387.       else
  388.       {
  389.          printf("Unknown flag '%s'\n \n", argv[1]);
  390.          printf(TITLE);
  391.          printf(COPYRIGHT);
  392.          printf(SYNTAX);
  393.          printf(AUTHOR);
  394.          exit(-1);
  395.       }
  396.  
  397.       if ((argv[2][1] == 's') || (argv[1][1] == 'S'))
  398.       {
  399.          fParseSystem = TRUE;
  400.  
  401.          pszFileName = argv[3];
  402.       }
  403.       else if ((argv[2][1] == 'f') || (argv[1][1] == 'F'))
  404.       {
  405.          fFormat = TRUE;
  406.  
  407.          pszFileName = argv[3];
  408.       }
  409.       else
  410.       {
  411.          printf("Unknown flag '%s'\n \n", argv[1]);
  412.          printf(TITLE);
  413.          printf(COPYRIGHT);
  414.          printf(SYNTAX);
  415.          printf(AUTHOR);
  416.          exit(-1);
  417.       }
  418.    }
  419.    else
  420.    {
  421.       printf(TITLE);
  422.       printf(COPYRIGHT);
  423.       printf(SYNTAX);
  424.       printf(AUTHOR);
  425.       exit(-1);
  426.    }
  427.  
  428.    char szExt[_MAX_EXT];
  429.    char szDir[_MAX_DIR];
  430.    char szDrive[_MAX_DRIVE];
  431.    char szFileName[_MAX_FNAME];
  432.  
  433.    char szPathCurrent[_MAX_DRIVE];
  434.  
  435.    //-- save the current details   
  436.    int sDriveCurrent = _getdrive();
  437.    getcwd(szPathCurrent, sizeof(szPathCurrent));
  438.  
  439.    //-- change to the document directory if possible
  440.    _splitpath(pszFileName, szDrive, szDir, szFileName, szExt);
  441.  
  442.    //-- change to the disk required
  443.    if (szDrive[0] != '\0')
  444.    {
  445.       char chDrive = szDrive[0];
  446.    
  447.       //-- make sure it is upper case
  448.       toupper(chDrive);
  449.       
  450.       _chdrive(chDrive);
  451.    }
  452.  
  453.    //-- change to the dir required
  454.    if (szDir[0] != '\0')
  455.    {
  456.       //-- remove the '\' character
  457.       int sLength = strlen(szDir);
  458.       if (sLength > 3) szDir[sLength - 1] = '\0';
  459.  
  460.       //-- new directory
  461.       chdir(szDir);
  462.    }
  463.  
  464.    char szTempFile[_MAX_FNAME + _MAX_EXT + 1];
  465.  
  466.    //-- display a title
  467.    printf(TITLE);
  468.    printf("\nC/C++ Include File List for '%s'\n\n", pszFileName);
  469.  
  470.    //-- only need the filename part
  471.    strcpy(szTempFile, szFileName);
  472.    strcat(szTempFile, szExt);
  473.    ParseFile(szTempFile);
  474.  
  475.    //-- release any memory we may have allocated
  476.    for (int i = 0; i < (int)sIndex; ++i)
  477.    {
  478.       delete apszIncludeList[i];
  479.    }
  480.  
  481.    //-- restore the old details
  482.    _chdrive(sDriveCurrent);
  483.    chdir(szPathCurrent);
  484.  
  485.    return sResult;
  486. }
  487.  
  488. //-------------------------------------------------------------------------
  489.  
  490.  
  491.