home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_syst / setup.arj / PARSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-25  |  13.0 KB  |  434 lines

  1. // PARSE.C  -- The code to parse .INF blocks into their respective
  2. //             data strcutures
  3.  
  4. /*****************************************************************************
  5.  
  6. Functions:
  7. ------------------------------------------------------------------------------
  8.  
  9. JunkCharacter                Returns TRUE if this is a white space char.
  10.  
  11. LoadLine                     Copies a single line from the cached .INF
  12.                              section into the Line array. Max 132 chars.
  13.  
  14. ParseLine                    Takes the next DoubleQuote delimited token
  15.                              from the Line array.
  16.  
  17. AddDiskInfo                  These routines fill the DiskInfo data structure
  18. FillDiskInfoStructures       [disks]
  19.  
  20. SetFlags                     These routines fill the FileInfo data structure
  21. AddFileInfo                  [apps]
  22. FillFileInfoStructures       
  23.  
  24. AddDirInfo                   These routines fill the DirInfo data structure
  25. FillDirInfoStructures        [directories]
  26.  
  27. AddProgmanInfo               These routines fill the PManInfo data strcutures
  28. FillProgmanInfoStructures    [progman]
  29.  
  30. TokenValue                   These routines fill the miscelleneous data
  31. FillGlobalInfoStructures     strucutres. [setup]
  32.  
  33. *****************************************************************************/
  34.  
  35.  
  36. #include <windows.h>
  37. #include <stdio.h>
  38. #include "bincode.h"
  39.  
  40. LPSTR   szCurrentBuffer;    // Points to the current line
  41. char    Line[132];          // Holds the current line
  42.  
  43. /********************************************************************/
  44. BOOL JunkCharacter ( char Letter )
  45. {
  46.   // Returns TRUE if character is whitespace.
  47.  
  48.  
  49.   switch ( Letter )
  50.     {
  51.     case ' ' :
  52.     case '\t':
  53.     case 10  :
  54.     case 13  : return TRUE;
  55.     default  : return FALSE;
  56.     }
  57. /********************************************************************/
  58. BOOL LoadLine (void)
  59. {
  60.   // returns TRUE if line loaded, FALSE if EOF
  61.  
  62.   BOOL ValidLine = TRUE;
  63.   int  LineIndex = 0;
  64.  
  65.   // This will load in the next non-comment line
  66.  
  67.   // First, skip the spaces and CR/LF's, and comments
  68.   
  69.   do
  70.     {
  71.     while (JunkCharacter(*szCurrentBuffer) && (*szCurrentBuffer)) szCurrentBuffer++;
  72.  
  73.     if (!(*szCurrentBuffer)) return FALSE;
  74.  
  75.     ValidLine = TRUE;
  76.     if (';' == *szCurrentBuffer)
  77.       {
  78.       ValidLine = FALSE;
  79.       while (*szCurrentBuffer != 13) szCurrentBuffer++;  // skip to EOLN
  80.       }
  81.     }
  82.   while (!ValidLine);
  83.  
  84.   do
  85.     {
  86.     Line[LineIndex] = *szCurrentBuffer;
  87.     szCurrentBuffer++;
  88.     LineIndex++;
  89.     }
  90.   while ((*szCurrentBuffer) && (*szCurrentBuffer != 13));
  91.  
  92.   // Even if this is EOF, nect call with return the false OK.
  93.  
  94.   Line[LineIndex] = 0;
  95.  
  96.   return TRUE;
  97. }  
  98. /********************************************************************/
  99. void ParseLine ( char *DestBuffer, int *Index )
  100. {
  101.   // This will grab the next QUOTED string
  102.   
  103.   *DestBuffer = 0;
  104.  
  105.   // Move to the first quote;
  106.  
  107.   while (( Line[*Index] ) && (Line[*Index] != '\"')) (*Index)++;
  108.  
  109.   if (!(Line[*Index])) return;  // NULL String, item doesn't exist
  110.  
  111.   (*Index)++;  // Start at next character
  112.  
  113.   while (( Line[*Index] ) && (Line[*Index] != '\"')) 
  114.     {
  115.     *DestBuffer = Line[*Index];
  116.     DestBuffer++;
  117.     (*Index)++;
  118.     }
  119.  
  120.   *DestBuffer = 0;  // Add a terminating char
  121.   if (*Index) (*Index)++;  // Advance over ending quote if possible
  122. }    
  123. /********************************************************************/
  124. void AddDiskInfo ( void )
  125. {
  126.   // This will parse out whatever is in Line, and add it to
  127.   // the DISKINFO structure...
  128.  
  129.   int   LineIndex;
  130.   char  Item[80];
  131.  
  132.   LineIndex = 0;
  133.  
  134.   ParseLine ( Item, &LineIndex );
  135.   DiskInfo[NumDisks].DiskNumber = *Item;
  136.   ParseLine ( DiskInfo[NumDisks].szSrcDir, &LineIndex );
  137.   ParseLine ( DiskInfo[NumDisks].szDiskTitle, &LineIndex );
  138.   NumDisks++;
  139. }
  140. /********************************************************************/
  141. /*                                                                  */
  142. /*   FillDiskInfoStructures (void)                                  */
  143. /*                                                                  */
  144. /*   This routine reads the [disks] section and fills the           */
  145. /*   structures appropriately.                                      */
  146. /*                                                                  */
  147. /********************************************************************/
  148. void FillDiskInfoStructures ( void )
  149. {
  150.  
  151.   NumDisks = 0;
  152.  
  153.   hBuffer = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT, SECTIONSIZE+1 );
  154.  
  155.   PositionInfPointer ( (LPSTR)"disks" );
  156.  
  157.   szCurrentBuffer = GlobalLock ( hBuffer );
  158.  
  159.   while ( LoadLine()) AddDiskInfo ();       // Fill the structrures
  160.  
  161.   GlobalUnlock ( hBuffer );
  162.   GlobalFree ( hBuffer );
  163. }
  164. /********************************************************************/
  165. void SetFlags ( char *Item, int *ListBoxIndex )
  166. {
  167.   // Defaults...
  168.  
  169. #ifdef COMPLEXSETUP
  170.   FileInfo[NumFiles].bSelected    = FALSE;
  171. #endif
  172. #ifdef SIMPLESETUP
  173.   FileInfo[NumFiles].bSelected    = TRUE;
  174. #endif
  175.   FileInfo[NumFiles].bInvisible   = TRUE;
  176.   FileInfo[NumFiles].bProgmanIcon = FALSE;
  177.   FileInfo[NumFiles].ExtraFiles   = 0;
  178.   FileInfo[NumFiles].bCompressed  = FALSE; 
  179.  
  180.   while (*Item)
  181.     {
  182.     switch (toupper(*Item))
  183.       {
  184.       case 'R': FileInfo[NumFiles].bSelected  = TRUE;  
  185.                 Item++; 
  186.                 break;
  187.  
  188.       case 'C': FileInfo[NumFiles].bCompressed = TRUE;  
  189.                 Item++; 
  190.                 break;
  191.  
  192.       case 'D': (*ListBoxIndex)++;
  193.                 FileInfo[NumFiles].bInvisible = FALSE; 
  194.                 Item++; 
  195.                 break;
  196.  
  197.       case 'I': FileInfo[NumFiles].bProgmanIcon = TRUE;
  198.                 Item++;
  199.                 FileInfo[NumFiles].ProgmanGroupNumber = *Item;
  200.                 Item++;
  201.                 break;
  202.  
  203.       case '0':
  204.       case '1':
  205.       case '2':
  206.       case '3':
  207.       case '4':
  208.       case '5':
  209.       case '6':
  210.       case '7':
  211.       case '8':
  212.       case '9': FileInfo[NumFiles].ExtraFiles = ((*Item)-'0') * 10;
  213.                 Item++;
  214.                 FileInfo[NumFiles].ExtraFiles = ((*Item)-'0');
  215.                 Item++;
  216.                 break;
  217.  
  218.       default : Item++; break;  // Skip over bogus flags
  219.  
  220.       }   // switch
  221.     }  // while
  222. }
  223. /********************************************************************/
  224. void AddFileInfo ( int *ListBoxIndex )
  225. {
  226.   // This will parse out whatever is in Line, and add it to
  227.   // the FILEINFO structure...
  228.  
  229.   int   LineIndex;
  230.   char  Item[80];
  231.  
  232.   LineIndex = 0;
  233.  
  234.   ParseLine ( Item, &LineIndex );
  235.   FileInfo[NumFiles].DiskNumber = *Item;
  236.  
  237.   ParseLine ( Item, &LineIndex );    // Get the Flags
  238.   SetFlags ( Item, ListBoxIndex );  // Go figure them out!
  239.  
  240.   FileInfo[NumFiles].Index = *ListBoxIndex;
  241.  
  242.   ParseLine ( Item, &LineIndex );  // Get the Flags
  243.   FileInfo[NumFiles].DirectoryNumber = *Item;
  244.  
  245.   ParseLine ( FileInfo[NumFiles].szMasterSrcDir, &LineIndex );
  246.   ParseLine ( FileInfo[NumFiles].szFileName, &LineIndex );
  247.   ParseLine ( FileInfo[NumFiles].szProgManName, &LineIndex );
  248.   ParseLine ( FileInfo[NumFiles].szDescription, &LineIndex );
  249.  
  250.   if ((!FileInfo[NumFiles].szDescription[0]) && (NumFiles))
  251.     sprintf (FileInfo[NumFiles].szDescription,FileInfo[NumFiles-1].szDescription);
  252.  
  253.   ParseLine ( FileInfo[NumFiles].szComment, &LineIndex );
  254.  
  255.   ParseLine ( Item, &LineIndex );  // Get the Flags
  256.   FileInfo[NumFiles].BytesRequired = (LONG)atoi(Item)*1024L;
  257.  
  258.   NumFiles++;
  259. }  
  260. /********************************************************************/
  261. /*                                                                  */
  262. /*   FillFileInfoStructures (void)                                  */
  263. /*                                                                  */
  264. /*   This routine reads the [apps] section and fills the FILEINFO   */
  265. /*   structures appropriately.                                      */
  266. /*                                                                  */
  267. /********************************************************************/
  268. void FillFileInfoStructures ( void )
  269. {
  270.   int  ListBoxIndex;
  271.  
  272.   NumFiles     = 0;
  273.   ListBoxIndex = -1;
  274.  
  275.   hBuffer = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT, SECTIONSIZE+1 );
  276.  
  277.   PositionInfPointer ( (LPSTR)"apps" );
  278.  
  279.   szCurrentBuffer = GlobalLock ( hBuffer );
  280.  
  281.   while ( LoadLine()) AddFileInfo ( &ListBoxIndex );       // Fill the structures
  282.  
  283.   GlobalUnlock ( hBuffer );
  284.   GlobalFree ( hBuffer );
  285. }
  286. /********************************************************************/
  287. void AddDirInfo ( void )
  288. {
  289.   // This will parse out whatever is in Line, and add it to
  290.   // the DIRINFO structure...
  291.  
  292.   // MEGA ASSUMTION!: We assume that the .INF file is set
  293.   //                  up right, i.e. no gaps in the sequence
  294.  
  295.   int   LineIndex;
  296.   char  Item[80];
  297.   int   DirNum;
  298.  
  299.   LineIndex = 0;
  300.  
  301.   ParseLine ( Item, &LineIndex );
  302.  
  303.   if ('0' == *Item) 
  304.     DirNum = 0;
  305.   else
  306.     DirNum = toupper(*Item) - 'A' + 1;
  307.  
  308.   NumDirs++;
  309.  
  310.   ParseLine ( DirInfo[DirNum].szDirectory,   &LineIndex );
  311.   ParseLine ( DirInfo[DirNum].szDescription, &LineIndex );
  312.  
  313. }  
  314. /********************************************************************/
  315. /*                                                                  */
  316. /*   FillDIrInfoStructures (void)                                   */
  317. /*                                                                  */
  318. /*   This routine reads the [directories] section and fills the     */
  319. /*   DIRINFO structures appropriately.                              */
  320. /*                                                                  */
  321. /********************************************************************/
  322. void FillDirInfoStructures ( void )
  323. {
  324.   NumDirs     = 0;
  325.  
  326.   hBuffer = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT, SECTIONSIZE+1 );
  327.  
  328.   PositionInfPointer ( (LPSTR)"directories" );
  329.  
  330.   szCurrentBuffer = GlobalLock ( hBuffer );
  331.  
  332.   while ( LoadLine()) AddDirInfo ( );       // Fill the structures
  333.  
  334.   GlobalUnlock ( hBuffer );
  335.   GlobalFree ( hBuffer );
  336. }
  337. /********************************************************************/
  338. void AddProgmanInfo ( void )
  339. {
  340.   // This will parse out whatever is in Line, and add it to
  341.   // the PMANINFO structure...
  342.  
  343.   // MEGA ASSUMTION!: We assume that the .INF file is set
  344.   //                  up right, i.e. no gaps in the sequence
  345.  
  346.   int   LineIndex;
  347.   char  Item[80];
  348.   int   GroupNum;
  349.  
  350.   LineIndex = 0;
  351.  
  352.   ParseLine ( Item, &LineIndex );
  353.   GroupNum = toupper(*Item) - 'A';
  354.  
  355.   NumPManGroups++;
  356.  
  357.   ParseLine ( PManInfo[GroupNum].szCaption,       &LineIndex );
  358.   ParseLine ( PManInfo[GroupNum].szGroupFilename, &LineIndex );
  359. }  
  360. /********************************************************************/
  361. /*                                                                  */
  362. /*   FillFileInfoStructures (void)                                  */
  363. /*                                                                  */
  364. /*   This routine reads the [apps] section and fills the FILEINFO   */
  365. /*   structures appropriately.                                      */
  366. /*                                                                  */
  367. /********************************************************************/
  368. void FillProgmanInfoStructures ( void )
  369. {
  370.   NumPManGroups     = 0;
  371.  
  372.   hBuffer = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT, SECTIONSIZE+1 );
  373.  
  374.   PositionInfPointer ( (LPSTR)"progman" );
  375.  
  376.   szCurrentBuffer = GlobalLock ( hBuffer );
  377.  
  378.   while ( LoadLine()) AddProgmanInfo ( );       // Fill the structures
  379.  
  380.   GlobalUnlock ( hBuffer );
  381.   GlobalFree ( hBuffer );
  382. }
  383. /********************************************************************/
  384. int TokenValue ( char *Item )
  385. {
  386.   if (!strcmp ( Item, "PACKAGENAME" )) return ID_PACKAGENAME;
  387.   if (!strcmp ( Item, "CAPTIONNAME" )) return ID_CAPTIONNAME;
  388.  
  389.   return 0;
  390. }
  391. /********************************************************************/
  392. /*                                                                  */
  393. /*   FillGlobalInfoStructures (void)                                */
  394. /*                                                                  */
  395. /*   This routine reads the [setup] section and fills the global    */
  396. /*   structures appropriately.                                      */
  397. /*                                                                  */
  398. /********************************************************************/
  399. void FillGlobalInfoStructures ( void )
  400. {
  401.   char Item[80];
  402.   int  LineIndex;
  403.  
  404.   NumPManGroups     = 0;
  405.  
  406.   hBuffer = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT, SECTIONSIZE+1 );
  407.  
  408.   PositionInfPointer ( (LPSTR)"setup" );
  409.  
  410.   szCurrentBuffer = GlobalLock ( hBuffer );
  411.  
  412.   while ( LoadLine())
  413.     {
  414.     LineIndex = 0;  
  415.  
  416.     ParseLine ( Item, &LineIndex );
  417.  
  418.     switch ( TokenValue ( Item ))
  419.       {
  420.       case ID_PACKAGENAME : ParseLine (szPackageName, &LineIndex); break;
  421.       case ID_CAPTIONNAME : ParseLine (szCaptionName, &LineIndex); break;
  422.       default          : break;
  423.       }
  424.     }
  425.  
  426.   GlobalUnlock ( hBuffer );
  427.   GlobalFree ( hBuffer );
  428. }
  429.  
  430.  
  431.  
  432.  
  433.