home *** CD-ROM | disk | FTP | other *** search
- // IO.C - Contains all file I/O stuff
-
- /*****************************************************************************
-
- Functions:
- ------------------------------------------------------------------------------
-
- DosCopy Copies a file given fully qualified path names,
- returns TRUE if succesful.
-
- CopyFile Copies a file given .INF file structures. This
- routine calls DosCopy.
-
- DosDelete Deletes a fully qualified filename.
-
- DosMakeDir Creates a directory, and any necessary parent
- directories. Reurns TRUE if directory was
- succesfully created, or if dir already existed.
- FALSE means total failure.
-
- InfFileOpen Opens the .INF file, setting a global variable
- to the handle of the .INF file. If the handle
- is not NULL, then this file is open.
-
- InfFileClose Closes the file, and sets the global handle to
- NULL.
-
- FileRead Performs an _lread(), and adds a null terminator
- to the block of memory read.
-
- CheckForCRLFandComment Causes memory pointer into cached .INF file
- to skip white space and comment lines.
-
- PositionInfPointer Given a tag, i.e. "apps", will cache into
- a globally alloced block ( hBuffer ) the
- entire section of the .INF file (for
- example "[apps]" section).
-
- *****************************************************************************/
-
- #include <windows.h>
- #include <dos.h>
- #include "bincode.h"
-
-
- unsigned char Buffer[16385]; // Dos Copy buffer
-
-
- //*******************************************************************
- //
- // Returns TRUE if no DOS error, FALSE if bad
- //
- //*******************************************************************
- BOOL DosCopy ( LPSTR szSrc, LPSTR szDest )
- {
- LONG FileSize;
- WORD NumChunks;
- WORD LastChunk;
- int i;
- BOOL err;
- HANDLE hSource, hDest;
- OFSTRUCT of1, of2;
-
- int NumBytes;
-
-
- err = FALSE;
- CopyError = FALSE;
-
- if (-1 == (hSource = OpenFile ( szSrc, &of1, OF_READ ))) return FALSE;
-
- if (-1 == (hDest = OpenFile ( szDest, &of2, OF_CREATE )))
- {
- _lclose ( hSource );
- CopyError = TRUE;
- return FALSE;
- }
-
- if (-1 == (FileSize = _llseek (hSource, 0L, 2))) // moves to EOF
- {
- _lclose ( hSource );
- _lclose ( hDest );
- return FALSE;
- }
-
- NumChunks = (WORD) ((LONG)FileSize / 16384L );
- LastChunk = (WORD) ((LONG)FileSize % 16384L );
-
- if (-1 == _llseek ( hSource, 0L, 0 ))
- {
- _lclose ( hSource );
- _lclose ( hDest );
- return FALSE;
- }
-
- for ( i = 0; (((WORD)i < NumChunks) && (!err)); i++ )
- {
- err |= ( (NumBytes = _lread ( hSource, Buffer, 16384 )) <= 0);
- if (!err)
- {
- err |= (-1 == (NumBytes =_lwrite ( hDest , Buffer, 16384 )));
- if (err) CopyError = TRUE;
- }
- }
-
- if ((LastChunk) && (!err))
- {
- err |= ( (NumBytes =_lread ( hSource, Buffer, LastChunk )) <= 0 );
- if (!err)
- {
- err |= (-1 == (NumBytes = _lwrite ( hDest , Buffer, LastChunk )));
- if (err) CopyError = TRUE;
- }
- }
-
- err |= (-1 == _lclose ( hSource ));
- err |= (-1 == _lclose ( hDest ));
-
- return !err;
- }
- //*******************************************************************
- BOOL CopyFile ( LPSTR Source, LPSTR Dest, LPSTR FileName, BOOL Compressed )
- {
- char szFullSrcPath[65];
- char szFullDestPath[65];
-
- wsprintf ( (LPSTR)szFullSrcPath, (LPSTR)"%s\\%s", Source, FileName );
- wsprintf ( (LPSTR)szFullDestPath, (LPSTR)"%s\\%s", Dest, FileName );
-
- if (Compressed)
- return (BOOL) DosExpandCopy ( (LPSTR)szFullSrcPath, (LPSTR)szFullDestPath );
- else
- return (BOOL) DosCopy ( (LPSTR)szFullSrcPath, (LPSTR)szFullDestPath );
- }
- //*******************************************************************
- void DosDelete ( char *szSrc )
- {
- HANDLE hSource;
- OFSTRUCT of1;
-
- hSource = OpenFile ( szSrc, &of1, OF_EXIST );
- if (hSource > 0) // If OK
- {
- hSource = OpenFile ( szSrc, &of1, OF_DELETE );
- _lclose ( hSource );
- }
- }
- /*******************************************************************
-
- Returns TRUE if the directory was make succesfully, FALSE if not
-
- *******************************************************************/
- int DosMakeDir ( LPSTR szPath )
- {
- int count; // number of backslashes
- int i; // Counter
- int err, err1; // Error return code
-
- count = 0;
- err = 0;
- err1 = 0;
-
- if (DosValidDir(szPath)) return TRUE; // no error
-
- for ( i = lstrlen (szPath)-1; i > 0 ; i-- )
- {
- if (szPath[i] == '\\')
- {
- count++;
- szPath[i] = 0;
- }
- }
-
- if (count)
- {
- for ( i = 0; i < count; i++ )
- {
- szPath[lstrlen(szPath)] = '\\';
-
- if (!DosValidDir(szPath))
- err = DosMkDir (szPath);
- else
- err = 0; // Directory alredy exists
-
- if (i == (count-1)) err1 = err; // check the last try
- }
- }
- else
- if (!DosValidDir(szPath))
- err1 = DosMkDir (szPath);
- else
- err1 = 0; // already there, dude!
-
- return (err1 == 0); // If err was zero, we must return TRUE!
-
- }
- //*******************************************************************
- void InfFileOpen ( void )
- {
- OFSTRUCT of;
-
- if (hInfFile)
- _llseek ( hInfFile, 0L, 0 ); // Move to BOF
- else
- {
- hBuffer1 = GlobalAlloc ( GMEM_MOVEABLE, CHUNKSIZE+2 );
- if (hBuffer1)
- hInfFile = OpenFile ( INFFILENAME, &of, OF_READ );
- }
- }
- //*******************************************************************
- void InfFileClose ( void )
- {
- if (hInfFile)
- {
- GlobalFree ( hBuffer1 );
- _lclose ( hInfFile );
- hBuffer1 = NULL;
- hInfFile = NULL;
- }
- }
- //*******************************************************************
- int FileRead ( HANDLE hFile, LPSTR szBuf, WORD wBytes )
- {
- int result;
-
- result = _lread ( hFile, szBuf, wBytes );
- szBuf [result] = 0;
-
- return result;
- }
- /*******************************************************************/
- void CheckForCRLFandComment ( WORD *i, WORD wMax, LPSTR szBuf )
- {
- BOOL bFlag = FALSE;
-
- // Skips over CR/LF's, and comments
-
- do
- {
- if (szBuf[*i] == 13) (*i)++;
- if (szBuf[*i] == 10) (*i)++;
- if (*i)
- {
- if ((szBuf[(*i)-1] == 10) && (szBuf[*i] == ';'))
- {
- while ( (*i < wMax) && (szBuf[(*i)-1] != 13)) (*i)++;
- bFlag = TRUE;
- }
- else
- {
- bFlag = (JunkCharacter(szBuf[*i])); // Skip white space
- if (bFlag) (*i)++;
- }
- }
- else
- bFlag = FALSE;
- }
- while (bFlag);
- }
- /*******************************************************************
-
- Positions the file pointer to the first entry in the section
- indicated. For example, if we pass "FOO" in, then the pointer
- would go to the first character after [FOO]\n\r in the .INF file.
-
- Not Case Sensitive
-
-
- ASSUMPTIONS: hBuffer must be valid!!!
-
- ------------------------------------------------------------------*/
-
-
- int PositionInfPointer ( LPSTR SectionHeader )
- {
- WORD wNumBytesRead;
- WORD wSectionPosition;
- int iNumChunksRead;
- WORD i, j, k;
- BOOL bFoundKey;
- BOOL bKeyMatch;
- char Key[80]; // Key found in the file
-
- InfFileOpen (); // Open and reset the file if not already open
- iNumChunksRead = 0;
-
- if (hInfFile)
- {
- szBuffer1 = GlobalLock ( hBuffer1 );
-
- j = 0; // Start of Key
-
- wNumBytesRead = FileRead ( hInfFile, szBuffer1, CHUNKSIZE );
-
- if (wNumBytesRead > 0)
- {
- // Time to look for the key, after the \r\n sequence
-
- bFoundKey = FALSE;
-
- for ( i = 0; ((i < (WORD)wNumBytesRead)) && (!bFoundKey); i++ )
- {
- CheckForCRLFandComment ( &i, wNumBytesRead, szBuffer1 );
- if (i >= wNumBytesRead)
- {
- i= 0;
- wNumBytesRead = FileRead ( hInfFile, szBuffer1, CHUNKSIZE );
- if (!wNumBytesRead) break;
- }
-
- if ('[' == szBuffer1[i])
- {
- i++; // Move to start of string;
-
- // Strip out the located key
-
- while (( szBuffer1[i] != ']' ) && (i < wNumBytesRead))
- {
- Key[j] = szBuffer1[i];
- j++;
- i++;
-
- if ( (WORD)i == CHUNKSIZE )
- {
- wNumBytesRead = FileRead ( hInfFile, szBuffer1, CHUNKSIZE );
- iNumChunksRead++;
- i = 0;
- } // if
- } // while
-
- Key[j] = 0; // End the string...
-
- if (i < wNumBytesRead ) // if successful read of key
- {
- bKeyMatch = TRUE;
- k = 0;
-
- // Quick and dirty uncase sensitive strcmp
-
- while (bKeyMatch && Key[k])
- {
- bKeyMatch &= (toupper(Key[k]) == toupper(SectionHeader[k]));
- k++;
- }
-
- if (bKeyMatch) // This is it!
- {
- bFoundKey = TRUE;
- szBuffer = GlobalLock ( hBuffer );
- i ++; // skip ]
- wSectionPosition = (iNumChunksRead*CHUNKSIZE)+i;
- _llseek ( hInfFile, (LONG)wSectionPosition, 0 ); // Move to BOF
- wNumBytesRead = FileRead ( hInfFile, szBuffer, SECTIONSIZE );
-
- // We can reuse the local variables, since we will break
- // out of the loop immediately.
-
- // Strip out any sections accidentally loaded afterwards
-
- bKeyMatch = FALSE;
- for ( i = 0; (i < wNumBytesRead) && (!bKeyMatch); i++ )
- {
- CheckForCRLFandComment ( &i, wNumBytesRead, szBuffer );
- if (i >= wNumBytesRead-2) break;
-
- if ('[' == szBuffer[i])
- {
- bKeyMatch = TRUE;
- szBuffer[i] = 0;
- }
- }
- if (!bKeyMatch) szBuffer[wNumBytesRead-1] = 0;
- GlobalUnlock ( hBuffer );
-
- } // if bKeymatch
- else
- {
- Key[0] = 0;
- j = 0; // Clear Key string
- }
-
- } // if (i < wnumbytes read)...
-
- } // if 13 == ...
-
- } // for
-
- } // if wNumbytes > 0
-
- GlobalUnlock ( hBuffer1 );
- InfFileClose ();
- } // if hInfile
- }
- /********************************************************************/