home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bcpp1611.zip / BCPP / CONFIG.CPP < prev    next >
C/C++ Source or Header  |  1996-12-18  |  18KB  |  612 lines

  1. #ifndef _CONFIG_CODE
  2. #define _CONFIG_CODE
  3.  
  4. // Program C(++) beautifier Written By Steven De Toni ACBC 11 11/94
  5. //
  6. // This program module contains routines to read data from a text file a
  7. // line at a time, and able to read parameters from a configuration file.
  8.  
  9. #include "config.h"           // Prototypes, struct, and enum declarations ...
  10. #include <stdlib.h>           // atol(),
  11. #include <string.h>           // strlen(), strstr(), strcpy(), strcmp(), strpbrk()
  12. #include <stdio.h>            // NULL constant, printf(), FILE, ftell(), fseek(), fprintf(), stderr
  13. #include "cmdline.h"          // StrUpr()
  14.  
  15. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  16. // Allocates memory for line in file, and places that the data in it.
  17. // pInFile = the file handle to use when reading the file !
  18. // EndOfFile variable is ued to test if the end of the file has been reached.
  19. //           When  this is true, the variable is changed to -1
  20. //
  21. // A string is returned with the contents the current line in the file,
  22. // memory is allocated via the ReadLine routine, and should be deleted
  23. // when not needed !
  24. char* ReadLine (FILE *pInFile, int& EndOfFile)
  25. {
  26.     long int startOfLine, endOfLine;
  27.  
  28.     ////////////////////////////////////////////////////////////////////////
  29.     // Start with 1 for linelen...just for the first case and to make sure :)
  30.     // Quite a few hacks in here...maybe someone will clean up later :>
  31.     // nitin@poboxes.com
  32.     ////////////////////////////////////////////////////////////////////////
  33.     int lineLen     = 1;
  34.     int testChar;
  35.  
  36.     startOfLine = ftell (pInFile);
  37.  
  38.     // find length
  39.     testChar = fgetc(pInFile);
  40.  
  41.     while ( (testChar == LF) || (testChar == CR) )
  42.         {
  43.         testChar = fgetc(pInFile);
  44.         startOfLine++;
  45.         }
  46.  
  47.     // while not at end of file, or line feed
  48.     while ( (testChar != LF) && (testChar != CR) && (testChar > 0) )
  49.         {
  50.         lineLen++;
  51.         testChar = fgetc (pInFile);
  52.         }
  53.  
  54.     // chack if at endoffile !
  55.     if (testChar < 0)
  56.         EndOfFile = testChar;
  57.  
  58.     // allocate buffer memory!
  59.     char* pLineBuffer = new char [lineLen+1];
  60.  
  61.     if (pLineBuffer == NULL)
  62.         return NULL;
  63.     else
  64.         pLineBuffer[lineLen] = NULLC;
  65.  
  66.     // reset position in file!
  67.     endOfLine = ftell (pInFile);
  68.     fseek (pInFile, startOfLine, 0);
  69.  
  70.     // place data into buffer!
  71.     int counter = lineLen;
  72.     lineLen = 0;
  73.  
  74.     testChar = fgetc(pInFile);
  75.     while (counter > 0)
  76.         {
  77.         pLineBuffer[lineLen] = testChar;
  78.         lineLen++;
  79.         counter--;
  80.         testChar = fgetc(pInFile);
  81.         }
  82.  
  83.     fseek (pInFile, endOfLine, 0);
  84.  
  85.     return pLineBuffer;
  86. }
  87.  
  88.  
  89. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\
  90. // Functions finds keywords within a line of data.
  91. //
  92. // Parameters:
  93. // Type :
  94. //        The parameter is used to define the type of keyword to find within
  95. //        a configuration line.
  96. //
  97. //        See ConfigWords enum for values, Use ANYT, or a value of 0 to search
  98. //        for any valid keywords within the line.
  99. //
  100. // pConfigLine :
  101. //        This parameter is a pointer to a string that contains the data that
  102. //        is going to be searched.
  103. //
  104. // Return Values:
  105. //
  106. // Type : Returns the keyword value expected, or keyword value found if
  107. //        searching for any.
  108. //
  109. // Char*: Returns a pointer in the string to the next starting location
  110. //        AFTER the keyword found. Or returns NULL if no keyword found!
  111. //
  112. char* FindConfigWords (char* pConfigLine, ConfigWords& type)
  113. {
  114.     char* pWordLoc = NULL;
  115.  
  116.     // check is there is a comment in the line, if so then any chars
  117.     // after a ";" will be ignored !
  118.     // search for a comment !
  119.     if ( pConfigLine[ 0 ] != pConfigWord[ 0 ][ 0 ] )
  120.     {
  121.     pWordLoc = strstr( pConfigLine, pConfigWord[0] );
  122.     if (pWordLoc != NULL)
  123.         {
  124.         *pWordLoc = NULLC;
  125.         }
  126.  
  127.     if (type > ANYT)
  128.         {
  129.         pWordLoc = strstr( pConfigLine, pConfigWord[type] );
  130. //        pWordLoc = strcmp( pConfigLine, pConfigWord[type] );
  131.  
  132.         if  (pWordLoc != NULL)// if word found
  133.             {
  134.             // advance to next word !
  135.             pWordLoc += strlen (pConfigWord[type]);
  136.             return pWordLoc;
  137.             }
  138.         }
  139.  
  140.     for (int typeCount = (int) FSPC; typeCount < ((int)OFF)+1; typeCount++)
  141.         {
  142.  
  143.         pWordLoc = strstr (pConfigLine, pConfigWord[typeCount]);
  144.  
  145.         if (pWordLoc != NULL)
  146.             {
  147.             type = (ConfigWords) typeCount;
  148.             // advance to next word !
  149.             pWordLoc += strlen (pConfigWord[typeCount]);
  150.             return pWordLoc;
  151.             }
  152.         }
  153.     }
  154.     type   = ANYT;            // not a keyword !
  155.     return NULL;
  156. }
  157.  
  158.  
  159. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  160. // This function is used to generate a generic error message.
  161. //
  162. // Parameters:
  163. // LineNo       : Line number where the error occured
  164. // errorCode    : Error type to output to the user
  165. // errorCount   : This variable is increment when this function is used
  166. // pMessage     : Use by programmer to add additional information about the error
  167. //
  168. //
  169. // Return Values:
  170. // errorCount   : This variable is used to show how many errors have occured!
  171. //
  172. void ErrorMessage (int lineNo, int errorCode, int& errorCount, const char* pMessage)
  173. {
  174.  
  175.     switch (errorCode)
  176.         {
  177.         case (1):
  178.             {
  179.             fprintf (stderr, "Syntax Error After Key Word ");
  180.             break;
  181.             }
  182.  
  183.         case (2):
  184.             {
  185.             fprintf (stderr, "Range Error  !");
  186.             break;
  187.             }
  188.  
  189.         case (3):
  190.             {
  191.             fprintf (stderr, "Expected Numeric Data !");
  192.             break;
  193.             }
  194.  
  195.         case (4):
  196.             {
  197.             fprintf (stderr, "Can't Decipher");
  198.             break;
  199.             }
  200.  
  201.         }
  202.  
  203.     if (pMessage != NULL)
  204.         fprintf (stderr, "%s", pMessage);
  205.  
  206.     fprintf (stderr, " At Line %d\n", lineNo);
  207.  
  208.     errorCount++;
  209. }
  210.  
  211.  
  212. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  213. // ConfigAssignment function is used to assigned Boolean, or unsigned integer
  214. // values from 0 - 500 to variables that are passed to it.
  215. //
  216. // Parameters:
  217. // type       : This variable defines the keyword next to expect within config line.
  218. // assignType : The variable is used to define what type of assignment to use,
  219. //              (i.e 1 = Boolean, 2 = Integer)
  220. // errorCount : Variable used to define how many error have occured. If any errors
  221. //              encounted within the function, then this var will be incremented.
  222. // PosInLine  : Defines a pointer to the starting location to read in data for
  223. //              assignment from config data line (string).
  224. // variable   : This defines the variables that's going to be altered, be boolean, or
  225. //              integer.
  226. //
  227. // Return Values:
  228. // errorCount : If any error occur within variable assignment, the a error
  229. //              message is displayed, and this vaiable is incremented.
  230. // variable   : If no errors have occured, then this variable will contain the value
  231. //              that was set by the user !
  232. //
  233. void ConfigAssignment (ConfigWords type, int assignType, int& errorCount, int& configError, char* pPosInLine, int& variable)
  234. {
  235.  
  236.     switch (assignType)
  237.         {
  238.         case (1):
  239.             {
  240.             // check if key words are there
  241.             pPosInLine = FindConfigWords (pPosInLine, type);
  242.  
  243.             switch (type)
  244.                 {
  245.                 case (YES):   // YES
  246.                 case (ON) :   // ON
  247.                     {
  248.                     variable = True;
  249.                     break;
  250.                     }
  251.  
  252.                 case (NO) :   // NO
  253.                 case (OFF):   // OFF
  254.                     {
  255.                     variable = False;
  256.                     break;
  257.                     }
  258.  
  259.                 default:
  260.                     ErrorMessage (errorCount, 1, configError, pConfigWord[EQUAL]);
  261.  
  262.                 }                 // switch
  263.  
  264.             break;
  265.             }
  266.  
  267.         case (2):
  268.             {
  269.             // covert whats left in the string to an INTEGER !
  270.             if (strpbrk(pPosInLine, "0123456789") != NULL)
  271.                 variable = atoi (pPosInLine);
  272.             else
  273.                 ErrorMessage (errorCount, 3, configError);
  274.  
  275.             // check range of lines numbers between functions!
  276.             if ( (variable < 0) || (variable > 500) )
  277.                 ErrorMessage (errorCount, 2, configError, " Valid Range = 0 - 500");
  278.  
  279.             break;
  280.             }
  281.  
  282.         }                         // switch (assignType)
  283.  
  284. }
  285.  
  286.  
  287. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  288. // This function is used to load the users configuration from a file.
  289. //
  290. // Parameters:       2
  291. // pConfigFile  : Pointer to a FILE structure/handle that contains the
  292. //                configuration data.
  293. // userSettings : Config structure that will contain the user settings.
  294. //
  295. // Return Values:
  296. // int          : Returns the number of errors encounted when reading the
  297. //                configuration file.
  298. // userSettings : This variable is altered to the user settings read from the
  299. //                config file.
  300. //
  301. int SetConfig (FILE* pConfigFile, Config& userSettings)
  302. {
  303.  
  304.     int         noMoreConfig  = 0   ;
  305.     char*       pLineOfConfig = NULL;
  306.     char*       pPosInLine    = NULL;
  307.     ConfigWords type                ;
  308.     int         lineCount     = 0   ;
  309.     int         configError   = 0   ;
  310.  
  311.     while ( !noMoreConfig )
  312.         {
  313.  
  314.         pLineOfConfig = ReadLine (pConfigFile, noMoreConfig);
  315.  
  316.         lineCount++;
  317.  
  318.         // upcase all characters in string !
  319.         StrUpr (pLineOfConfig);
  320.  
  321.         type = ANYT;
  322.         pPosInLine = FindConfigWords (pLineOfConfig, type);
  323.  
  324.         switch (type)
  325.             {
  326.  
  327.             // ############################################################
  328.             case (FSPC):      // Function_Spacing = (%d)
  329.                 {
  330.                 type =  EQUAL;
  331.                 pPosInLine = FindConfigWords (pPosInLine, type);
  332.  
  333.                 // expect a "="
  334.                 if (type == EQUAL)
  335.                     ConfigAssignment (ANYT, 2, lineCount, configError, pPosInLine, userSettings.numOfLineFunc);
  336.                 else
  337.                     ErrorMessage (lineCount, 1, configError, pConfigWord[FSPC]);
  338.  
  339.                 break;
  340.                 }                 // case (FSPC)
  341.  
  342.             // ############################################################
  343.             case (UTAB):      // use_tabs = {on, off, yes, no}
  344.                 {
  345.                 type =  EQUAL;
  346.                 pPosInLine = FindConfigWords (pPosInLine, type);
  347.  
  348.                 // expect a "="
  349.                 if (type == EQUAL)
  350.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.useTabs);
  351.                 else
  352.                     ErrorMessage (lineCount, 1, configError, pConfigWord[UTAB]);
  353.  
  354.                 break;
  355.                 }                 // case (UTAB)
  356.  
  357.             // ############################################################
  358.             case (ISPC):      // Indent_Spacing = (%d)
  359.                 {
  360.                 type =  EQUAL;
  361.                 pPosInLine = FindConfigWords (pPosInLine, type);
  362.  
  363.                 // expect a "="
  364.                 if (type == EQUAL)
  365.                     ConfigAssignment (ANYT, 2, lineCount, configError, pPosInLine, userSettings.tabSpaceSize);
  366.                 else
  367.                     ErrorMessage (lineCount, 1, configError, pConfigWord[ISPC]);
  368.  
  369.                 break;
  370.                 }                 // case (ISPC)
  371.  
  372.             // ############################################################
  373.             case (NAQTOOCT):  // NONASCII_QUOTES_TO_OCTAL = {on, off, yes, no}
  374.                 {
  375.                 type =  EQUAL;
  376.                 pPosInLine = FindConfigWords (pPosInLine, type);
  377.  
  378.                 // expect a "="
  379.                 if (type == EQUAL)
  380.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.quoteChars);
  381.                 else
  382.                     ErrorMessage (lineCount, 1, configError, pConfigWord[NAQTOOCT]);
  383.  
  384.                 break;
  385.                 }                 // case (NAQTOOCT)
  386.  
  387.             // ############################################################
  388.             case (COMWC):     // Comments_With_Code = (%d)
  389.                 {
  390.                 type =  EQUAL;
  391.                 pPosInLine = FindConfigWords (pPosInLine, type);
  392.  
  393.                 // expect a "="
  394.                 if (type == EQUAL)
  395.                     ConfigAssignment (ANYT, 2, lineCount, configError, pPosInLine, userSettings.posOfCommentsWC);
  396.                 else
  397.                     ErrorMessage (lineCount, 1, configError, pConfigWord[COMWC]);
  398.  
  399.                 break;
  400.                 }                 // case (COMWC)
  401.  
  402.             // ############################################################
  403.             case (COMNC):     // Comments_With_NoCode = (%d)
  404.                 {
  405.                 type = EQUAL;
  406.                 pPosInLine = FindConfigWords (pPosInLine, type);
  407.  
  408.                 // expect a "="
  409.                 if (type == EQUAL)
  410.                     ConfigAssignment (ANYT, 2, lineCount, configError, pPosInLine, userSettings.posOfCommentsNC);
  411.                 else
  412.                     ErrorMessage (lineCount, 1, configError, pConfigWord[COMNC]);
  413.  
  414.                 break;
  415.                 }                 // case (COMNC)
  416.  
  417.             //JZAS Start
  418.             // ############################################################
  419.             case (LCNC):      // leave_comments_nocode = {on, off, yes, no}
  420.                 {
  421.                 type = EQUAL;
  422.                 pPosInLine = FindConfigWords (pPosInLine, type);
  423.  
  424.                 // expect a "="
  425.                 if (type == EQUAL)
  426.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.leaveCommentsNC);
  427.                 else
  428.                     ErrorMessage (lineCount, 1, configError, pConfigWord[BUF]);
  429.  
  430.                 break;
  431.                 }                 // case (LCNC)
  432.             //JZAS End
  433.  
  434.  
  435.             // ############################################################
  436.             case (LGRAPHC):   // LEAVE_GRAPHIC_CHARS = {on, off, yes, no}
  437.                 {
  438.                 type =  EQUAL;
  439.                 pPosInLine = FindConfigWords (pPosInLine, type);
  440.  
  441.                 // expect a "="
  442.                 if (type == EQUAL)
  443.                     {
  444.                     int test = False;
  445.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, test);
  446.  
  447.                     if (test == True)
  448.                         //   set bit 0, 1
  449.                         userSettings.deleteHighChars = 3;
  450.                     else
  451.                         // unset bit 0, 1
  452.                         userSettings.deleteHighChars = 0;
  453.                     }
  454.                 else
  455.                     ErrorMessage (lineCount, 1, configError, pConfigWord[LGRAPHC]);
  456.  
  457.                 break;
  458.                 }                 // case (LGRAPHC)
  459.  
  460.             // ############################################################
  461.             case (ASCIIO):    // ASCII_CHARS_ONLY = {on, off, yes, no}
  462.                 {
  463.                 type =  EQUAL;
  464.                 pPosInLine = FindConfigWords (pPosInLine, type);
  465.  
  466.                 // expect a "="
  467.                 if (type == EQUAL)
  468.                     {
  469.                     int test = False;
  470.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, test);
  471.  
  472.                     if (test == True)
  473.                         //   set bit 0
  474.                         userSettings.deleteHighChars = 1;
  475.                     else
  476.                         // unset bit 0
  477.                         userSettings.deleteHighChars = 0;
  478.                     }
  479.                 else
  480.                     ErrorMessage (lineCount, 1, configError, pConfigWord[ASCIIO]);
  481.  
  482.                 break;
  483.                 }                 // case (ASCIIO)
  484.  
  485.             // ############################################################
  486.             case (PBNLINE):   // Braces On Newline = {on, off, yes, no}
  487.                 {
  488.                 type =  EQUAL;
  489.                 pPosInLine = FindConfigWords (pPosInLine, type);
  490.  
  491.                 // expect a "="
  492.                 if (type == EQUAL)
  493.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.braceLoc);
  494.                 else
  495.                     ErrorMessage (lineCount, 1, configError, pConfigWord[PBNLINE]);
  496.  
  497.                 break;
  498.                 }                 // case (PBNLINE)
  499.  
  500.             // ############################################################
  501.             case (PROGO):     // program_output = {on, off, yes, no}
  502.                 {
  503.                 type =  EQUAL;
  504.                 pPosInLine = FindConfigWords (pPosInLine, type);
  505.  
  506.                 // expect a "="
  507.                 if (type == EQUAL)
  508.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.output);
  509.                 else
  510.                     ErrorMessage (lineCount, 1, configError, pConfigWord[PROGO]);
  511.  
  512.                 break;
  513.                 }                 // case (PROGO)
  514.  
  515.             // ############################################################
  516.             case (QBUF):      // queue_buffer = (%d)
  517.                 {
  518.                 type =  EQUAL;
  519.                 pPosInLine = FindConfigWords (pPosInLine, type);
  520.  
  521.                 // expect a "="
  522.                 if (type == EQUAL)
  523.                     ConfigAssignment (ANYT, 2, lineCount, configError, pPosInLine, userSettings.queueBuffer);
  524.                 else
  525.                     ErrorMessage (lineCount, 1, configError, pConfigWord[QBUF]);
  526.  
  527.                 // minimum needed is a 2 line buffer !
  528.                 if (userSettings.queueBuffer < 2)
  529.                     userSettings.queueBuffer = 2;
  530.  
  531.                 break;
  532.                 }                 // case (QBUF)
  533.  
  534.             // ############################################################
  535.             case (BUF):       // backup_file = {on, off, yes, no}
  536.                 {
  537.                 type = EQUAL;
  538.                 pPosInLine = FindConfigWords (pPosInLine, type);
  539.  
  540.                 // expect a "="
  541.                 if (type == EQUAL)
  542.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.backUp);
  543.                 else
  544.                     ErrorMessage (lineCount, 1, configError, pConfigWord[BUF]);
  545.  
  546.                 break;
  547.                 }                 // case (BF)
  548.  
  549.             // ############################################################
  550.             case (FMTSTY):       // format_style = {on, off, yes, no}
  551.                 {
  552.                 type = EQUAL;
  553.                 pPosInLine = FindConfigWords (pPosInLine, type);
  554.  
  555.                 // expect a "="
  556.                 if (type == EQUAL)
  557.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&) userSettings.formatStyle);
  558.                 else
  559.                     ErrorMessage (lineCount, 1, configError, pConfigWord[BUF]);
  560.  
  561.                 break;
  562.                 }                 // case (BF)
  563.  
  564.             // ############################################################
  565.             case (IFUNC):       // indent_functions = {on, off, yes, no}
  566.                 {
  567.                 type = EQUAL;
  568.                 pPosInLine = FindConfigWords (pPosInLine, type);
  569.  
  570.                 // expect a "="
  571.                 if (type == EQUAL)
  572.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&)userSettings.functionIndent);
  573.                 else
  574.                     ErrorMessage (lineCount, 1, configError, pConfigWord[BUF]);
  575.  
  576.                 break;
  577.                 }                 // case (BF)
  578.  
  579.             // ############################################################
  580.             case (DOSF):       // DOS_File = {on, off, yes, no}
  581.                 {
  582.                 type = EQUAL;
  583.                 pPosInLine = FindConfigWords (pPosInLine, type);
  584.  
  585.                 // expect a "="
  586.                 if (type == EQUAL)
  587.                     ConfigAssignment (ANYT, 1, lineCount, configError, pPosInLine, (int&)userSettings.dosLines);
  588.                 else
  589.                     ErrorMessage (lineCount, 1, configError, pConfigWord[BUF]);
  590.  
  591.                 break;
  592.                 }                 // case (BF)
  593.  
  594.             case (EQUAL):
  595.             case (YES):
  596.             case (ON):
  597.             case (NO):
  598.             case (OFF):
  599.                 ErrorMessage (lineCount, 4, configError);
  600.             }                     // switch
  601.  
  602.         delete pLineOfConfig;
  603.         }                         // while
  604.  
  605.     return configError;
  606. }
  607.  
  608.  
  609. #endif
  610.  
  611.  
  612.