home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / Stdfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-31  |  5.8 KB  |  182 lines

  1. // StdFile.cpp: implementation of the CStdFile class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "StdFile.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CStdFile::CStdFile()
  41. {// begin CStdFile constructor
  42.     // initilize variables
  43.     fileHandle = NULL;
  44.     currentFilePos = 0;
  45. }// end CStdFile constructor
  46.  
  47. CStdFile::~CStdFile()
  48. {// begin CStdFile destructor
  49.     if(fileHandle != NULL)
  50.         Close();
  51. }// end CStdFile destructor
  52.  
  53. bool CStdFile::Open(const char *fileName, DWORD accessFlags, DWORD shareAccess, DWORD creationAttributes)
  54. {// begin Open
  55. // CreateFile("wndMainPos.dat",GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  56.     fileHandle = CreateFile(fileName,accessFlags,shareAccess,NULL,creationAttributes,FILE_ATTRIBUTE_NORMAL,NULL);
  57.     if(fileHandle == INVALID_HANDLE_VALUE)
  58.         return false;
  59.     char tempFileName[MAX_PATH] = {NULL};
  60.     GetName(fileName,tempFileName);
  61.     char filePath[MAX_PATH] = {NULL};
  62.     GetCurrentDirectory(MAX_PATH-1,filePath);
  63.     lstrcat(filePath,"\\");
  64.     lstrcat(filePath,tempFileName);
  65.     lstrcpy(path,filePath);
  66.     return true;
  67. }// end Open
  68.  
  69. DWORD CStdFile::WriteLine(const char *buffer)
  70. {// begin WriteLine
  71.     DWORD bytesWritten = Write(buffer,lstrlen(buffer));
  72.     if(bytesWritten)
  73.     {// begin write endline
  74.         DWORD temp = Write("\r\n",2);
  75.         if(!temp)
  76.         {
  77.             return bytesWritten;
  78.         }
  79.         return bytesWritten+temp;
  80.     }// end write endline
  81. //    currentFilePos += bytesWritten;
  82.     return bytesWritten;
  83. }// end WriteLine
  84.  
  85. DWORD CStdFile::ReadLine(char *bufferOut)
  86. {// begin ReadLine
  87. //    DWORD bytesRead = 0;
  88.     for(int i = 0;Read(&bufferOut[i], 1);i++)
  89.     {// begin read file
  90.         if(bufferOut[i] == '\r')// || bufferOut[i] == '\n')
  91.         {// begin end of line found
  92.             bufferOut[i] = NULL;
  93.             Seek(currentFilePos+1,FILE_BEGIN);
  94.             return i;
  95.         }// end end of line found        
  96.     }// end read file
  97.     return i;
  98. }// end ReadLine
  99.  
  100. bool CStdFile::Close()
  101. {// begin Close
  102.     if(CloseHandle(fileHandle))
  103.     {// begin file was closed
  104.         fileHandle = NULL;
  105.         return true;
  106.     }// end file was closed
  107.     return false;
  108. }// end Close
  109.  
  110. bool CStdFile::Delete(const char *fileName)
  111. {// begin Delete
  112.     if(!DeleteFile(fileName))
  113.     {// begin could be a folder
  114.         if(!RemoveDirectory(fileName))
  115.             return false;
  116.     }// end could be a folder
  117.     return true;
  118. }// end Delete
  119.  
  120. void CStdFile::GetName(const char *fileString, char *fileBufferOut)
  121. {// begin GetName
  122.     for(int i = lstrlen(fileString)-1;fileString[i] != '\\' && i > -1;i--);
  123.     lstrcpyn(fileBufferOut,&fileString[i+1],(lstrlen(fileString))-i);
  124. }// end GetName
  125. /*
  126. bool CStdFile::Rename(const char *source, const char *dest)
  127. {// begin Rename
  128.     SHFILEOPSTRUCT file = {NULL,FO_RENAME ,source,dest,FOF_NOERRORUI ,FALSE,NULL,NULL};
  129.     if(::SHFileOperation(&file) == 0)
  130.     {// begin rename successful
  131.         return true;
  132.     }// end rename successful
  133.     return false;
  134. }// end Rename
  135. */
  136. const char * CStdFile::GetFileName()
  137. {// begin GetFileName
  138.     return name;
  139. }// end GetFileName
  140.  
  141. const char * CStdFile::GetFilePath()
  142. {// begin GetFilePath
  143.     return path;
  144. }// end GetFilePath
  145.  
  146. DWORD CStdFile::Read(char *buffer, DWORD bytesToRead)
  147. {// begin Read
  148.     DWORD bytesRead = 0;
  149.     ReadFile(fileHandle,buffer,bytesToRead,&bytesRead,NULL);
  150.     currentFilePos += bytesRead;
  151.     return bytesRead;
  152. }// end Read
  153.  
  154. DWORD CStdFile::Write(const char *buffer, DWORD bytesToWrite)
  155. {// begin Write
  156.     DWORD bytesWritten = 0;
  157.     WriteFile(fileHandle,buffer,bytesToWrite,&bytesWritten,NULL);
  158.     currentFilePos += bytesWritten;
  159.     return bytesWritten;
  160. }// end Write
  161.  
  162. DWORD CStdFile::GetLength()
  163. {// begin GetLength
  164.     return GetFileSize(fileHandle,NULL);
  165. }// end GetLength
  166.  
  167. DWORD CStdFile::Seek(DWORD pos,DWORD start)
  168. {// begin Seek
  169.     currentFilePos = SetFilePointer(fileHandle,pos,NULL,start);
  170.     return currentFilePos;
  171. }// end Seek
  172.  
  173. bool CStdFile::Rename(const char *source, const char *dest)
  174. {// begin Rename
  175.     return (bool)MoveFile(source,dest);
  176. }// end Rename
  177.  
  178. unsigned long int CStdFile::GetPos()
  179. {// begin GetPos
  180.     return currentFilePos;
  181. }// end GetPos
  182.