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

  1. // IO.C - Contains all file I/O stuff
  2.  
  3. /*****************************************************************************
  4.  
  5. Functions:
  6. ------------------------------------------------------------------------------
  7.  
  8. DosCopy                 Copies a file given fully qualified path names,
  9.                         returns TRUE if succesful.
  10.  
  11. CopyFile                Copies a file given .INF file structures. This
  12.                         routine calls DosCopy.
  13.  
  14. DosDelete               Deletes a fully qualified filename.
  15.  
  16. DosMakeDir              Creates a directory, and any necessary parent
  17.                         directories. Reurns TRUE if directory was
  18.                         succesfully created, or if dir already existed.
  19.                         FALSE means total failure.
  20.  
  21. InfFileOpen             Opens the .INF file, setting a global variable
  22.                         to the handle of the .INF file.  If the handle
  23.                         is not NULL, then this file is open.
  24.  
  25. InfFileClose            Closes the file, and sets the global handle to
  26.                         NULL.
  27.  
  28. FileRead                Performs an _lread(), and adds a null terminator
  29.                         to the block of memory read.
  30.  
  31. CheckForCRLFandComment  Causes memory pointer into cached .INF file
  32.                         to skip white space and comment lines.
  33.  
  34. PositionInfPointer      Given a tag, i.e. "apps", will cache into
  35.                         a globally alloced block ( hBuffer ) the
  36.                         entire section of the .INF file (for
  37.                         example "[apps]" section).
  38.  
  39. *****************************************************************************/
  40.  
  41. #include <windows.h>
  42. #include <dos.h>
  43. #include "bincode.h"
  44.  
  45.  
  46. unsigned char Buffer[16385];     // Dos Copy buffer
  47.  
  48.  
  49. //*******************************************************************
  50. //
  51. //  Returns TRUE if no DOS error, FALSE if bad
  52. //
  53. //*******************************************************************
  54. BOOL DosCopy ( LPSTR szSrc, LPSTR szDest )
  55. {
  56.   LONG     FileSize;
  57.   WORD     NumChunks;
  58.   WORD     LastChunk;
  59.   int      i;
  60.   BOOL     err;
  61.   HANDLE   hSource, hDest;
  62.   OFSTRUCT of1, of2;
  63.  
  64.   int      NumBytes;
  65.  
  66.  
  67.   err       = FALSE;
  68.   CopyError = FALSE;
  69.  
  70.   if (-1 == (hSource = OpenFile ( szSrc,  &of1, OF_READ   ))) return FALSE;
  71.  
  72.   if (-1 == (hDest   = OpenFile ( szDest, &of2, OF_CREATE )))
  73.     {
  74.     _lclose ( hSource );
  75.     CopyError = TRUE;
  76.     return FALSE;
  77.     }
  78.  
  79.   if (-1 == (FileSize = _llseek (hSource, 0L, 2)))  // moves to EOF
  80.     {
  81.     _lclose ( hSource );
  82.     _lclose ( hDest );
  83.     return FALSE;  
  84.     }
  85.  
  86.   NumChunks = (WORD)  ((LONG)FileSize / 16384L );
  87.   LastChunk = (WORD)  ((LONG)FileSize % 16384L );
  88.  
  89.   if (-1 == _llseek ( hSource, 0L, 0 ))
  90.     {
  91.     _lclose ( hSource );
  92.     _lclose ( hDest );
  93.     return FALSE;  
  94.     }
  95.  
  96.   for ( i = 0; (((WORD)i < NumChunks) && (!err)); i++ )
  97.     {
  98.     err |= ( (NumBytes = _lread  ( hSource, Buffer, 16384 )) <= 0);
  99.     if (!err) 
  100.       {
  101.       err |= (-1 == (NumBytes =_lwrite ( hDest  , Buffer, 16384 )));
  102.       if (err) CopyError = TRUE;
  103.       }
  104.     }
  105.   
  106.   if ((LastChunk) && (!err))
  107.     {
  108.     err |= ( (NumBytes =_lread  ( hSource, Buffer, LastChunk )) <= 0 );
  109.     if (!err) 
  110.       {
  111.       err |= (-1 == (NumBytes = _lwrite ( hDest  , Buffer, LastChunk )));
  112.       if (err) CopyError = TRUE;
  113.       }
  114.     }
  115.  
  116.   err |= (-1 == _lclose ( hSource ));
  117.   err |= (-1 == _lclose ( hDest   ));
  118.  
  119.   return !err;
  120. }
  121. //*******************************************************************
  122. BOOL CopyFile ( LPSTR Source, LPSTR Dest, LPSTR FileName, BOOL Compressed )
  123. {
  124.   char szFullSrcPath[65];
  125.   char szFullDestPath[65];
  126.  
  127.   wsprintf ( (LPSTR)szFullSrcPath, (LPSTR)"%s\\%s", Source, FileName );
  128.   wsprintf ( (LPSTR)szFullDestPath, (LPSTR)"%s\\%s", Dest, FileName );
  129.  
  130.   if (Compressed)
  131.     return (BOOL) DosExpandCopy ( (LPSTR)szFullSrcPath, (LPSTR)szFullDestPath );
  132.   else
  133.     return (BOOL) DosCopy ( (LPSTR)szFullSrcPath, (LPSTR)szFullDestPath );
  134. }
  135. //*******************************************************************
  136. void DosDelete ( char *szSrc )
  137. {
  138.   HANDLE   hSource;
  139.   OFSTRUCT of1;
  140.  
  141.   hSource = OpenFile ( szSrc,  &of1, OF_EXIST );
  142.   if (hSource > 0)  // If OK
  143.     {
  144.     hSource = OpenFile ( szSrc,  &of1, OF_DELETE );
  145.     _lclose ( hSource );
  146.     }
  147. }
  148. /*******************************************************************
  149.  
  150. Returns TRUE if the directory was make succesfully, FALSE if not
  151.  
  152. *******************************************************************/
  153. int DosMakeDir ( LPSTR szPath )
  154. {
  155.   int count;      // number of backslashes
  156.   int i;          // Counter
  157.   int err, err1;  // Error return code
  158.      
  159.   count = 0;
  160.   err   = 0;
  161.   err1  = 0;
  162.  
  163.   if (DosValidDir(szPath)) return TRUE;   // no error
  164.       
  165.   for ( i = lstrlen (szPath)-1; i > 0 ; i-- )
  166.     {
  167.     if (szPath[i] == '\\')
  168.       {
  169.       count++;
  170.       szPath[i] = 0;
  171.       }
  172.     }
  173.  
  174.   if (count)
  175.     {
  176.     for ( i = 0; i < count; i++ )
  177.       {
  178.       szPath[lstrlen(szPath)] = '\\';
  179.  
  180.       if (!DosValidDir(szPath)) 
  181.         err = DosMkDir (szPath);
  182.       else
  183.         err = 0;  // Directory alredy exists
  184.  
  185.       if (i == (count-1)) err1 = err;    // check the last try
  186.       }
  187.     }
  188.   else
  189.     if (!DosValidDir(szPath)) 
  190.       err1 = DosMkDir (szPath);
  191.     else
  192.       err1 = 0;   // already there, dude!
  193.  
  194.   return (err1 == 0);   // If err was zero, we must return TRUE!
  195.  
  196. }
  197. //*******************************************************************
  198. void InfFileOpen ( void )
  199. {
  200.   OFSTRUCT of;
  201.  
  202.   if (hInfFile) 
  203.     _llseek ( hInfFile, 0L, 0 );    // Move to BOF
  204.   else
  205.     {
  206.     hBuffer1 = GlobalAlloc ( GMEM_MOVEABLE, CHUNKSIZE+2 );
  207.     if (hBuffer1)
  208.       hInfFile = OpenFile ( INFFILENAME, &of, OF_READ );
  209.     }
  210. }
  211. //*******************************************************************
  212. void InfFileClose ( void )
  213. {
  214.   if (hInfFile) 
  215.     {
  216.     GlobalFree ( hBuffer1 );
  217.     _lclose ( hInfFile );
  218.     hBuffer1  = NULL;
  219.     hInfFile  = NULL;
  220.     }
  221. }
  222. //*******************************************************************
  223. int FileRead ( HANDLE hFile, LPSTR szBuf, WORD wBytes )
  224. {
  225.  int result;
  226.  
  227.  result = _lread ( hFile, szBuf, wBytes );
  228.  szBuf [result] = 0;
  229.  
  230.  return result;
  231. }
  232. /*******************************************************************/
  233. void CheckForCRLFandComment ( WORD *i, WORD wMax, LPSTR szBuf )
  234. {
  235.   BOOL bFlag = FALSE;
  236.  
  237.   // Skips over CR/LF's, and comments
  238.  
  239.   do
  240.     {
  241.     if (szBuf[*i] == 13) (*i)++;
  242.     if (szBuf[*i] == 10) (*i)++;
  243.     if (*i)
  244.       {
  245.       if ((szBuf[(*i)-1] == 10) && (szBuf[*i] == ';'))
  246.         {
  247.         while ( (*i < wMax) && (szBuf[(*i)-1] != 13)) (*i)++;
  248.         bFlag = TRUE;
  249.         }
  250.       else
  251.         {
  252.         bFlag = (JunkCharacter(szBuf[*i]));  // Skip white space
  253.         if (bFlag) (*i)++;
  254.         }
  255.       }
  256.     else
  257.       bFlag = FALSE;
  258.     }
  259.   while (bFlag);
  260. }
  261. /*******************************************************************
  262.  
  263.   Positions the file pointer to the first entry in the section
  264.   indicated.  For example, if we pass "FOO" in, then the pointer
  265.   would go to the first character after [FOO]\n\r in the .INF file.
  266.   
  267.   Not Case Sensitive
  268.  
  269.  
  270.   ASSUMPTIONS: hBuffer must be valid!!!
  271.  
  272. ------------------------------------------------------------------*/
  273.  
  274.  
  275. int PositionInfPointer ( LPSTR SectionHeader )
  276. {
  277.   WORD wNumBytesRead;
  278.   WORD wSectionPosition;
  279.   int  iNumChunksRead;
  280.   WORD i, j, k;
  281.   BOOL bFoundKey;
  282.   BOOL bKeyMatch;
  283.   char Key[80];        // Key found in the file
  284.  
  285.   InfFileOpen ();      // Open and reset the file if not already open
  286.   iNumChunksRead = 0;
  287.  
  288.   if (hInfFile)
  289.     {
  290.     szBuffer1 = GlobalLock ( hBuffer1 );
  291.  
  292.     j  = 0;  // Start of Key
  293.   
  294.     wNumBytesRead = FileRead ( hInfFile, szBuffer1, CHUNKSIZE );
  295.  
  296.     if (wNumBytesRead > 0)
  297.       {
  298.       // Time to look for the key, after the \r\n sequence
  299.  
  300.       bFoundKey = FALSE;
  301.  
  302.       for ( i = 0; ((i < (WORD)wNumBytesRead)) && (!bFoundKey); i++ )
  303.         {
  304.         CheckForCRLFandComment ( &i, wNumBytesRead, szBuffer1 );
  305.         if (i >= wNumBytesRead) 
  306.           {
  307.           i= 0;
  308.           wNumBytesRead = FileRead ( hInfFile, szBuffer1, CHUNKSIZE );
  309.           if (!wNumBytesRead) break;
  310.           }
  311.             
  312.         if ('['  == szBuffer1[i])
  313.           {
  314.           i++;  // Move to start of string;
  315.           
  316.           // Strip out the located key
  317.  
  318.           while (( szBuffer1[i] != ']' ) && (i < wNumBytesRead))
  319.             {
  320.             Key[j] = szBuffer1[i];
  321.             j++;
  322.             i++;
  323.  
  324.             if ( (WORD)i == CHUNKSIZE )
  325.               {
  326.               wNumBytesRead = FileRead ( hInfFile, szBuffer1, CHUNKSIZE );
  327.               iNumChunksRead++;
  328.               i = 0;
  329.               }    // if
  330.             }   // while
  331.  
  332.           Key[j] = 0;  // End the string...
  333.           
  334.           if (i < wNumBytesRead )   // if successful read of key
  335.             {
  336.             bKeyMatch = TRUE;
  337.             k = 0;
  338.  
  339.             // Quick and dirty uncase sensitive strcmp
  340.    
  341.             while (bKeyMatch && Key[k])
  342.               {
  343.               bKeyMatch &= (toupper(Key[k]) == toupper(SectionHeader[k]));
  344.               k++;
  345.               }
  346.            
  347.             if (bKeyMatch)   // This is it!
  348.               {
  349.               bFoundKey = TRUE;
  350.               szBuffer = GlobalLock ( hBuffer );
  351.               i ++;  // skip ]
  352.               wSectionPosition = (iNumChunksRead*CHUNKSIZE)+i;
  353.               _llseek ( hInfFile, (LONG)wSectionPosition, 0 );    // Move to BOF
  354.               wNumBytesRead = FileRead ( hInfFile, szBuffer, SECTIONSIZE );
  355.          
  356.               // We can reuse the local variables, since we will break
  357.               // out of the loop immediately.
  358.  
  359.               // Strip out any sections accidentally loaded afterwards
  360.  
  361.               bKeyMatch = FALSE;
  362.               for ( i = 0; (i < wNumBytesRead) && (!bKeyMatch); i++ )
  363.                 {
  364.                 CheckForCRLFandComment ( &i, wNumBytesRead, szBuffer );
  365.                 if (i >= wNumBytesRead-2) break;
  366.  
  367.                 if ('['  == szBuffer[i])
  368.                   {
  369.                   bKeyMatch = TRUE;
  370.                   szBuffer[i] = 0;
  371.                   }
  372.                 }
  373.               if (!bKeyMatch) szBuffer[wNumBytesRead-1] = 0;
  374.               GlobalUnlock ( hBuffer );
  375.  
  376.               } // if bKeymatch
  377.             else
  378.               {
  379.               Key[0] = 0;
  380.               j = 0;   // Clear Key string
  381.               }
  382.  
  383.             } // if (i < wnumbytes read)...
  384.  
  385.           } // if 13 == ...
  386.  
  387.         } // for
  388.  
  389.       } // if wNumbytes > 0
  390.  
  391.     GlobalUnlock ( hBuffer1 );
  392.     InfFileClose ();
  393.     } // if hInfile
  394. }
  395. /********************************************************************/
  396.