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

  1. #ifndef _CMDLINE_CODE
  2. #define _CMDLINE_CODE
  3.  
  4. // Program C(++) Beautifier Written By Steven De Toni ACBC 11 12/94
  5. //
  6. // This module contains methods to parse the command line.
  7.  
  8. #include <stdlib.h>           // atoi()
  9. #include <stdio.h>            // printf(), getc(), fprintf(), stderr
  10. #include <string.h>           // strcmp()
  11.  
  12. #include "cmdline.h"          // prototypes
  13.  
  14. // Function converts a lower case string into upper case, any special
  15. // characters remain the same (i.e "$", "%" ...)
  16. void StrUpr (char* pUpCase)
  17. {
  18.     while (*pUpCase != '\0')
  19.         {
  20.         if ((*pUpCase >= 'a') && (*pUpCase <= 'z'))
  21.             *pUpCase = (*pUpCase & 223);
  22.         pUpCase++;
  23.         }
  24. }
  25.  
  26.  
  27. // This function displays brief command line help to the user.
  28. // Parameters:
  29. // char* argv[]     : Pointer to command line parameter pointer array
  30. //
  31. void PrintProgramHelp (char* argv[])
  32. {
  33.     int userKey;
  34.  
  35.     printf ("\nC(++) Beautifier     V1.61.1\n");
  36.     printf ("--------------------------\n");
  37.     printf ("Program Was Written By Steven De Toni, January 1995\n");
  38.     printf ("All Parts Of This Program Are Public Domain.\n\n");
  39.     printf ("Modified by Nitin Chandra, December 1996\n");
  40.     printf ("For my type of indenting and DOS CR/LF support\n\n");
  41.  
  42.     printf ("Usage : %s [Parameters] [Input File Name] [Output File Name] \n\n", argv[0]);
  43.     printf ("                    Available Command Line Parameters\n");
  44.     printf ("                    ---------------------------------\n\n");
  45.     printf ("[-bcl] [-bnl] [-cc <num>] [-f <num>] [-fi <string>] [-fnc <string>]\n");
  46.     printf ("[-fo <string>] [-h] [-i <num>] [-lg] [-na] [-nb] [-nc] [-nlcnc] [-no]\n");
  47.     printf ("[-nq] [-qb] [-s] [-t] [-ya] [-yb] [-ylcnc] [-yo] [-yq]\n");
  48.     printf ("[<string>] [<string>]\n");
  49.     printf ("\n[] = Optional <> = Parameter Requirement\n");
  50.     printf ("\n* Support For I/O Redirection Is Provided *\n");
  51.  
  52.                               // make sure this is displayed !
  53.     fprintf (stderr, "\n More Detail Y/N ? ");
  54.     userKey = getc(stdin);
  55.     userKey = (userKey & 223);// change key to upper case!
  56.  
  57.     if (userKey == 'Y')
  58.         {
  59.         printf ("-bcl          : -bcl         : Open Braces On Code Line\n");
  60.         printf ("-bnl          : -bnl         : Open Braces On New Line\n");
  61.         printf ("-cc  <num>    : -cc  50      : Comments With Code\n");
  62.         printf ("-f   <num>    : -f   2       : Function Line Spacing\n");
  63.         printf ("-fi <string>  : -fi  in.cpp  : Input File Name\n");
  64.         printf ("-fnc <string> : -fnc c.cfg   : Load Custom Configuration File\n");
  65.         printf ("-fo <string>  : -fo  out.cpp : Output File Name\n");
  66.         printf ("-h    or   -? : -h   or  -?  : This Help\n");
  67.         printf ("-i   <num>    : -i   4       : Indent Space Length\n");
  68.         printf ("-lg           : -lg          : Leave Graphic Chars\n");
  69.         printf ("-na           : -na          : Leave Non-Ascii Chars As Normal\n");
  70.         printf ("-nb           : -nb          : Don't Backup Input File\n");
  71.         printf ("-nc  <num>    : -nc  0       : Comments With No Code\n");
  72.         printf ("-nlcnc        : -nlcnc       : Turn Off Leave Comments With NoCode\n");
  73.         printf ("-no           : -no          : Turn Off Program Output (Unless Errors Occur)\n");
  74.         printf ("-nq           : -nq          : Turn Off Non-Ascii Chars In Quotes To Octal\n");
  75.         printf ("-qb  <num>    : -qb  10      : Define Internal Queue Buffer\n");
  76.         printf ("-s            : -s           : Use Spaces In Indenting\n");
  77.         printf ("-t            : -t           : Use Tabs In Indenting\n");
  78.         printf ("-ya           : -ya          : Remove Non-Ascii chars\n");
  79.         printf ("-yb           : -yb          : Backup Input File With .bac Extension\n");
  80.         printf ("-ylcnc        : -ylcnc       : Turn ON Leave Comments With NoCode\n");
  81.         printf ("-yo           : -yo          : Turn On Program Output\n");
  82.         printf ("-yq           : -yq          : Turn On Non-Ascii Chars In Quotes To Octal\n");
  83.         printf ("<string>      :      in.cpp  : Input File Name\n");
  84.         printf ("<string>      :      out.cpp : Output File Name\n");
  85.         }
  86. }
  87.  
  88.  
  89. // integer assignment
  90. int intAssign (int& cmdCount, int argc, char* argv[] )
  91. {
  92.     if (cmdCount+1 <= (argc-1))
  93.         {
  94.         cmdCount++;
  95.         return atoi (argv[cmdCount]);
  96.         }
  97.     else
  98.         {
  99.         fprintf (stderr, "Expected Another Integer Parameter For Command Directive %s\n", argv[cmdCount]);
  100.         PrintProgramHelp (argv);
  101.         cmdCount = -1;
  102.         return cmdCount;
  103.         }
  104. }
  105.  
  106.  
  107. // string assignment
  108. char* strAssign (int& cmdCount, int argc, char* argv[])
  109. {
  110.     if (cmdCount+1 <= (argc-1))
  111.         {
  112.         cmdCount++;
  113.         return argv[cmdCount];
  114.         }
  115.     else
  116.         {
  117.         fprintf (stderr, "Expected Another String Parameter For Command Directive %s\n", argv[cmdCount]);
  118.         PrintProgramHelp (argv);
  119.         cmdCount = -1;
  120.         return NULL;
  121.         }
  122. }
  123.  
  124.  
  125. // Function processes command line parameters
  126. int ProcessCommandLine (int argc, char* argv[], Config& settings,
  127. char*& pInFile, char*& pOutFile, char*& pConfig)
  128. {
  129.  
  130.     int   cmdCount = 0;
  131.     char* cmdRead;
  132.  
  133.     while (cmdCount < argc-1)
  134.         {
  135.         // command line error
  136.         if (cmdCount < 0)
  137.             return cmdCount;
  138.  
  139.         // next command to process!
  140.         cmdCount++;
  141.         cmdRead = argv[cmdCount];
  142.  
  143.         // this is a command directive
  144.         if (cmdRead[0] == '-')
  145.             {
  146.             // upcase the command parameter
  147.             StrUpr (cmdRead);
  148.  
  149.             cmdRead++;
  150.  
  151.             // Test for "No Output Wanted" = false
  152.             if (strcmp ("NO", cmdRead) == 0)
  153.                 {
  154.                 settings.output = False;
  155.                 continue;
  156.                 }
  157.  
  158.             // Test for "Comments With No Code Position"
  159.             if (strcmp ("NC", cmdRead) == 0)
  160.                 {
  161.                 settings.posOfCommentsNC = intAssign (cmdCount, argc, argv);
  162.                 continue;
  163.                 }
  164.  
  165.             // Test for "Leave Non-Ascii Chars As Normal"
  166.             if (strcmp ("NA", cmdRead) == 0)
  167.                 {
  168.                 settings.deleteHighChars = 0;
  169.                 continue;
  170.                 }
  171.  
  172.             // Test for "Backup File" = no !
  173.             if (strcmp ("NB", cmdRead) == 0)
  174.                 {
  175.                 settings.backUp = False;
  176.                 continue;
  177.                 }
  178.  
  179.             // Test for "Leave Comments No Code" = False
  180.             if (strcmp ("NLCNC", cmdRead) == 0)
  181.                 {
  182.                 settings.leaveCommentsNC = False;
  183.                 continue;
  184.                 }
  185.  
  186.             // Test for "Dont Convert Non-Ascii Chars In Quotes To Octal"
  187.             if (strcmp ("NQ", cmdRead) == 0)
  188.                 {
  189.                 settings.quoteChars = False;
  190.                 continue;
  191.                 }
  192.  
  193.             // Test for "Leave Graphic Chars"
  194.             if (strcmp ("LG", cmdRead) == 0)
  195.                 {
  196.                 settings.deleteHighChars = 3;
  197.                 continue;
  198.                 }
  199.  
  200.             // Test for "Comments With Code Position"
  201.             if (strcmp ("CC", cmdRead) == 0)
  202.                 {
  203.                 settings.posOfCommentsWC = intAssign (cmdCount, argc, argv);
  204.                 continue;
  205.                 }
  206.  
  207.             // Test for  "File Name To Read New Configuration File"
  208.             if (strcmp ("FNC", cmdRead) == 0)
  209.                 {
  210.                 pConfig = strAssign (cmdCount, argc, argv);
  211.                 continue;
  212.                 }
  213.  
  214.             // Test for "Input File Name"
  215.             if (strcmp ("FI", cmdRead) == 0)
  216.                 {
  217.                 pInFile = strAssign (cmdCount, argc, argv);
  218.                 continue;
  219.                 }
  220.  
  221.             // Test for "Output File Name"
  222.             if (strcmp ("FO", cmdRead) == 0)
  223.                 {
  224.                 pOutFile = strAssign (cmdCount, argc, argv);
  225.                 continue;
  226.                 }
  227.  
  228.             // Test for "Function Spacing"
  229.             if (strcmp ("F", cmdRead) == 0)
  230.                 {
  231.                 settings.numOfLineFunc = intAssign (cmdCount, argc, argv);
  232.                 continue;
  233.                 }
  234.  
  235.             // Test for "Use Tabs In Indenting"
  236.             if (strcmp ("T", cmdRead) == 0)
  237.                 {
  238.                 settings.useTabs = True;
  239.                 continue;
  240.                 }
  241.  
  242.             // Test for "Use Spaces In Indenting"
  243.             if (strcmp ("S", cmdRead) == 0)
  244.                 {
  245.                 settings.useTabs = False;
  246.                 continue;
  247.                 }
  248.  
  249.             // Test for "Indent Spacing Length"
  250.             if (strcmp ("I", cmdRead) == 0)
  251.                 {
  252.                 settings.tabSpaceSize = intAssign (cmdCount, argc, argv);
  253.                 continue;
  254.                 }
  255.  
  256.             // Test for "Open Brace On Code Line"
  257.             if (strcmp ("BCL", cmdRead) == 0)
  258.                 {
  259.                 settings.braceLoc = False;
  260.                 continue;
  261.                 }
  262.  
  263.             // Test for "Open Brace On New Line"
  264.             if (strcmp ("BNL", cmdRead) == 0)
  265.                 {
  266.                 settings.braceLoc = True;
  267.                 continue;
  268.                 }
  269.  
  270.             // Test for "Queue Buffer"
  271.             if (strcmp ("QB", cmdRead) == 0)
  272.                 {
  273.                 settings.queueBuffer = intAssign (cmdCount, argc, argv);
  274.  
  275.                 if (settings.queueBuffer < 2)
  276.                     settings.queueBuffer = 2;
  277.  
  278.                 continue;
  279.                 }
  280.  
  281.             // Test for "Remove Non-Ascii Chars"
  282.             if (strcmp ("YA", cmdRead) == 0)
  283.                 {
  284.                 settings.deleteHighChars = 1;
  285.                 continue;
  286.                 }
  287.  
  288.             // Test for "Backup File" = yes
  289.             if (strcmp ("YB", cmdRead) == 0)
  290.                 {
  291.                 settings.backUp = True;
  292.                 continue;
  293.                 }
  294.  
  295.             // Test for "Leave Comments No Code" = True
  296.             if (strcmp ("YLCNC", cmdRead) == 0)
  297.                 {
  298.                 settings.leaveCommentsNC = True;
  299.                 continue;
  300.                 }
  301.  
  302.             // Test for  "Output Wanted" = True
  303.             if (strcmp ("YO", cmdRead) == 0)
  304.                 {
  305.                 settings.output = True;
  306.                 continue;
  307.                 }
  308.  
  309.             // Test for "Convert Non-Ascii Chars In Quotes To Octal"
  310.             if (strcmp ("YQ", cmdRead) == 0)
  311.                 {
  312.                 settings.quoteChars = True;
  313.                 continue;
  314.                 }
  315.  
  316.             // ### display help ###
  317.             if( (strcmp ("?", cmdRead) == 0) ||
  318.                 (strcmp ("H", cmdRead) == 0) )
  319.                 {
  320.                 printf ("*** Displaying Brief Help ***\n");
  321.                 PrintProgramHelp (argv);
  322.                 return -1;
  323.                 }
  324.  
  325.             fprintf (stderr, "Unkown Command Directive %s \n", cmdRead);
  326.             PrintProgramHelp (argv);
  327.             return -1;
  328.             }
  329.                               // Test for "Input File Name"
  330.         else if (pInFile == NULL)
  331.             pInFile  = argv [cmdCount];
  332.                               // Test for "Output File Name"
  333.         else if (pOutFile == NULL)
  334.             pOutFile = argv [cmdCount];
  335.         else
  336.             {
  337.             fprintf (stderr, "Command Line Error : Expected Command Directive, Not %s\n", argv[cmdCount]);
  338.             PrintProgramHelp (argv);
  339.             return -1;
  340.             }
  341.         }
  342.  
  343.     return 0;
  344. }
  345.  
  346. #endif
  347. // _CMDLINE_CODE
  348.  
  349.