home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / source / gs_file.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  8.1 KB  |  381 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCFile
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCObject
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "gamesystem.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. char gsCFile::m_directory_name[_MAX_PATH] = { 0 };
  20.  
  21. //-------------------------------------------------------------
  22.  
  23. gsCFile::gsCFile()
  24. {
  25.     m_handle = INVALID_HANDLE_VALUE;
  26.     m_mode = gsFILE_READ;
  27. }
  28.  
  29. //-------------------------------------------------------------
  30.  
  31. gsCFile::~gsCFile()
  32. {
  33.     close();
  34. }
  35.  
  36. //-------------------------------------------------------------
  37.  
  38. bool gsCFile::setDirectory(const char *directory_name)
  39. {
  40.     strcpy(m_directory_name,directory_name);
  41.  
  42.     return true;
  43. }
  44.  
  45. //-------------------------------------------------------------
  46.  
  47. bool gsCFile::getFullName(const char *filename,char *fullname)
  48. {
  49.     strcpy(fullname,m_directory_name);
  50.     strcat(fullname,filename);
  51.  
  52.     return true;
  53. }
  54.  
  55. //-------------------------------------------------------------
  56.  
  57. bool gsCFile::open(const char *filename,gsFileMode mode)
  58. {
  59.     close();
  60.  
  61.     if (!filename)
  62.         return false;
  63.  
  64.     char fullname[_MAX_PATH];
  65.  
  66.     getFullName(filename,fullname);
  67.  
  68.     switch (mode) {
  69.         case gsFILE_READ:
  70.             m_handle = CreateFile(fullname,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  71.             break;
  72.         case gsFILE_WRITE:
  73.             m_handle = CreateFile(fullname,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  74.             break;
  75.         default:
  76.             return false;
  77.         }
  78.  
  79.     if (m_handle != INVALID_HANDLE_VALUE) {
  80.         m_mode = mode;
  81.         return true;
  82.         }
  83.     else
  84.         return false;
  85. }
  86.  
  87. //-------------------------------------------------------------
  88.  
  89. void gsCFile::close()
  90. {
  91.     if (m_handle != INVALID_HANDLE_VALUE)
  92.         CloseHandle(m_handle);
  93.  
  94.     m_handle = INVALID_HANDLE_VALUE;
  95. }
  96.  
  97. //-------------------------------------------------------------
  98.  
  99. bool gsCFile::isOpen()
  100. {
  101.     return m_handle != INVALID_HANDLE_VALUE;
  102. }
  103.  
  104. //-------------------------------------------------------------
  105.  
  106. gsFileMode gsCFile::getMode()
  107. {
  108.     return m_mode;
  109. }
  110.  
  111. //-------------------------------------------------------------
  112.  
  113. bool gsCFile::isEndOfFile()
  114. {
  115.     if (m_handle == INVALID_HANDLE_VALUE) {
  116.         gsREPORT("gsCFile::isEndOfFile called with invalid handle");
  117.         return true;
  118.         }
  119.  
  120.     return getPosition() == getLength();
  121. }
  122.  
  123. //-------------------------------------------------------------
  124.  
  125. gsUDWORD gsCFile::getLength()
  126. {
  127.     if (m_handle == INVALID_HANDLE_VALUE) {
  128.         gsREPORT("gsCFile::getLength called with invalid handle");
  129.         return 0;
  130.         }
  131.  
  132.     gsUDWORD old_pos = SetFilePointer(m_handle,0,NULL,FILE_CURRENT);
  133.  
  134.     if (old_pos == 0xFFFFFFFF)
  135.         return 0;
  136.  
  137.     gsUDWORD length = SetFilePointer(m_handle,0,NULL,FILE_END);
  138.  
  139.     if (length == 0xFFFFFFFF)
  140.         return 0;
  141.  
  142.     SetFilePointer(m_handle,old_pos,NULL,FILE_BEGIN);
  143.  
  144.     return length;
  145. }
  146.  
  147. //-------------------------------------------------------------
  148.  
  149. gsUDWORD gsCFile::getPosition()
  150. {
  151.     if (m_handle == INVALID_HANDLE_VALUE) {
  152.         gsREPORT("gsCFile::getPosition called with invalid handle");
  153.         return 0;
  154.         }
  155.  
  156.     return SetFilePointer(m_handle,0,NULL,FILE_CURRENT);
  157. }
  158.  
  159. //-------------------------------------------------------------
  160.  
  161. bool gsCFile::setPosition(gsUDWORD position)
  162. {
  163.     if (m_handle == INVALID_HANDLE_VALUE) {
  164.         gsREPORT("gsCFile::setPosition with invalid handle");
  165.         return false;
  166.         }
  167.  
  168.     return SetFilePointer(m_handle,position,NULL,FILE_BEGIN) != 0xFFFFFFFF;
  169. }
  170.  
  171. //-------------------------------------------------------------
  172.  
  173. gsUDWORD gsCFile::getByte()
  174. {
  175.     if (m_handle == INVALID_HANDLE_VALUE) {
  176.         gsREPORT("gsCFile::getByte called with invalid handle");
  177.         return gsFILE_READ_FAILED;
  178.         }
  179.  
  180.        char c;
  181.  
  182.     gsUDWORD bytes_read;
  183.  
  184.     if (ReadFile(m_handle,&c,1,&bytes_read,NULL))
  185.         if (bytes_read == 1)
  186.             return (gsUDWORD) c;
  187.         else
  188.             return gsFILE_READ_FAILED;
  189.     else
  190.         return gsFILE_READ_FAILED;
  191. }
  192.  
  193. //-------------------------------------------------------------
  194.  
  195. bool gsCFile::putByte(gsUBYTE byte)
  196. {
  197.     if (m_handle == INVALID_HANDLE_VALUE) {
  198.         gsREPORT("gsCFile::putByte called with invalid handle");
  199.         return false;
  200.         }
  201.  
  202.     gsUDWORD bytes_written;
  203.  
  204.     if (WriteFile(m_handle,&byte,1,&bytes_written,NULL))
  205.         return bytes_written == 1;
  206.     else
  207.         return false;
  208. }
  209.  
  210. //-------------------------------------------------------------
  211.  
  212. gsUDWORD gsCFile::read(void *buffer,gsUDWORD byte_count)
  213. {
  214.     if (m_handle == INVALID_HANDLE_VALUE) {
  215.         gsREPORT("gsCFile::read called with invalid handle");
  216.         return gsFILE_READ_FAILED;
  217.         }
  218.  
  219.     gsUDWORD bytes_read;
  220.         
  221.     if (buffer == 0 ||
  222.         byte_count <= 0)
  223.         return 0;
  224.  
  225.     if (ReadFile(m_handle,buffer,byte_count,&bytes_read,NULL))
  226.         return bytes_read;
  227.     else
  228.         return gsFILE_READ_FAILED;
  229. }
  230.  
  231. //-------------------------------------------------------------
  232.  
  233. gsUDWORD gsCFile::write(void *buffer,gsUDWORD byte_count)
  234. {
  235.     if (m_handle == INVALID_HANDLE_VALUE) {
  236.         gsREPORT("gsCFile::write called with invalid handle");
  237.         return gsFILE_WRITE_FAILED;
  238.         }
  239.  
  240.     gsUDWORD bytes_written;
  241.     
  242.     if (buffer == 0 ||
  243.         byte_count == 0)
  244.         return 0 ;
  245.  
  246.     if (WriteFile(m_handle,buffer,byte_count,&bytes_written,NULL))
  247.         return bytes_written;
  248.     else
  249.         return gsFILE_WRITE_FAILED;
  250. }
  251.  
  252. //-------------------------------------------------------------
  253.  
  254. static int find_path_num = 0;
  255. static WIN32_FIND_DATA find_data;
  256. static char find_search_string[_MAX_PATH];
  257. static HANDLE find_handle = INVALID_HANDLE_VALUE;
  258.  
  259. //-------------------------------------------------------------
  260. // Find file in current directory
  261.  
  262. const char *gsCFile::findFirst(const char *search_string)
  263. {
  264.     findClose();
  265.  
  266.     find_path_num = 0;
  267.     strcpy(find_search_string,search_string);
  268.     find_handle = INVALID_HANDLE_VALUE;
  269.  
  270.     return findNext();
  271. }
  272.  
  273. //-------------------------------------------------------------
  274.  
  275. const char *gsCFile::findNext()
  276. {
  277.     if (find_handle == INVALID_HANDLE_VALUE) {
  278.         char fullname[_MAX_PATH];
  279.         strcpy(fullname,m_directory_name);
  280.         strcat(fullname,find_search_string);
  281.         find_handle = FindFirstFile(fullname,&find_data);
  282.         if (find_handle != INVALID_HANDLE_VALUE)
  283.             return find_data.cFileName;
  284.         }
  285.     else {
  286.         if (FindNextFile(find_handle,&find_data))
  287.             return find_data.cFileName;
  288.         }
  289.  
  290.     findClose();
  291.  
  292.     return 0;
  293. }
  294.  
  295. //-------------------------------------------------------------
  296.  
  297. void gsCFile::findClose()
  298. {
  299.     if (find_handle != INVALID_HANDLE_VALUE)
  300.         FindClose(find_handle);
  301.  
  302.     find_handle = INVALID_HANDLE_VALUE;
  303. }
  304.  
  305. //-------------------------------------------------------------
  306.  
  307. gsUDWORD gsCFile::readString(char *buffer,int max_length)
  308. {
  309.     if (m_handle == INVALID_HANDLE_VALUE) {
  310.         gsREPORT("gsCFile::readString called with invalid handle");
  311.         return gsFILE_READ_FAILED;
  312.         }
  313.  
  314.     int i = 0;
  315.        char c;
  316.  
  317.     gsUDWORD bytes_read;
  318.  
  319.     while (i < max_length) {
  320.         if (ReadFile(m_handle,&c,1,&bytes_read,NULL)) {
  321.             if (bytes_read == 1) {
  322.                 buffer[i] = ((c == 10) ? 0 : c);
  323.                 i++;
  324.                 if (c == 10)
  325.                     return strlen(buffer);
  326.                 if (i >= max_length)
  327.                     return gsFILE_READ_FAILED;
  328.                 }
  329.             else
  330.                 return gsFILE_READ_FAILED;
  331.             }
  332.         }
  333.  
  334.     return gsFILE_READ_FAILED;
  335. }
  336.  
  337. //-------------------------------------------------------------
  338.  
  339. gsUDWORD __cdecl gsCFile::writeString(const char *format,...)
  340. {
  341.     if (m_handle == INVALID_HANDLE_VALUE) {
  342.         gsREPORT("gsCFile::writeString called with invalid handle");
  343.         return gsFILE_WRITE_FAILED;
  344.         }
  345.  
  346.     va_list arglist;
  347.     static char string[1000];
  348.  
  349.     va_start(arglist,format);
  350.     vsprintf(string,format,arglist);
  351.     va_end(arglist);
  352.  
  353.     gsUDWORD length = strlen(string);
  354.     gsUDWORD bytes_written;
  355.     
  356.     if (string == 0 ||
  357.         length == 0)
  358.         return 0 ;
  359.  
  360.     if (WriteFile(m_handle,string,length,&bytes_written,NULL))
  361.         return bytes_written;
  362.     else
  363.         return gsFILE_WRITE_FAILED;
  364. }
  365.  
  366. //-------------------------------------------------------------
  367.  
  368. bool gsCFile::exists(const char *filename)
  369. {
  370.     gsCFile temp;
  371.  
  372.     if (!temp.open(filename))
  373.         return false;
  374.  
  375.     temp.close();
  376.  
  377.     return true;
  378. }
  379.  
  380. //-------------------------------------------------------------
  381.