xnim

Untitled

Sep 18th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include "HFile.h"
  2.  
  3. HFile::HFile()
  4. {
  5.     FileHandle = NULL;
  6. }
  7. HFile::~HFile()
  8. {
  9.     Close();
  10. }
  11.  
  12. bool HFile::Open(const char* FilePath,bool CreateIfNotExists /* = true */)
  13. {
  14.    
  15.      FileHandle = CreateFile(
  16.         FilePath,
  17.         GENERIC_READ || GENERIC_WRITE,
  18.         0,
  19.         NULL,
  20.         CreateIfNotExists? CREATE_NEW:OPEN_EXISTING,
  21.         0,
  22.         NULL);
  23.      if (FileHandle == INVALID_HANDLE_VALUE && CreateIfNotExists)
  24.      {
  25.          FileHandle = CreateFile(
  26.              FilePath,
  27.              GENERIC_READ || GENERIC_WRITE,
  28.              0,
  29.              NULL,
  30.              OPEN_EXISTING,
  31.              0,
  32.              NULL);
  33.      }
  34.     return FileHandle != INVALID_HANDLE_VALUE;
  35. }
  36.  
  37. bool HFile::Open(const string FilePath, bool CreateIfNotExists /* = true */)
  38. {
  39.     return Open(FilePath.c_str(),CreateIfNotExists);
  40. }
  41.  
  42. bool HFile::Create(const char* FilePath)
  43. {
  44.     FileHandle = CreateFile(
  45.         FilePath,
  46.         GENERIC_READ || GENERIC_WRITE,
  47.         0,
  48.         NULL,
  49.         CREATE_ALWAYS,
  50.         0,
  51.         NULL);
  52.     return FileHandle != INVALID_HANDLE_VALUE;
  53. }
  54.  
  55. bool HFile::Create(const string& FilePath)
  56. {
  57.     return Create(FilePath.c_str());
  58. }
  59.  
  60. void HFile::Write(const char* info,UINT infoLen)
  61. {
  62.     DWORD Temp;
  63.     WriteFile(FileHandle,info,infoLen, &Temp,NULL);
  64. }
  65.  
  66. void HFile::Write(const char* info)
  67. {
  68.     Write(info,strlen(info));
  69. }
  70.  
  71. void HFile::Write(const string& info)
  72. {
  73.     Write(info.c_str(),info.length());
  74. }
  75.  
  76. void HFile::Write(const int& n)
  77. {
  78.     char* info = new char[50];
  79.     sprintf(info,"%i",n);
  80.     Write(info);
  81.     delete[] info;
  82. }
  83.  
  84. void HFile::Write(const double& n)
  85. {
  86.     char* info = new char[50];
  87.     sprintf(info,"%f",n);
  88.     Write(info);
  89.     delete[] info;
  90. }
  91.  
  92. void HFile::WriteLine(const char* info)
  93. {
  94.     Write(info);
  95.     Write("\r\n");
  96. }
  97.  
  98. void HFile::WriteLine(const string& info)
  99. {
  100.     Write(info);
  101.     Write("\r\n");
  102. }
  103.  
  104. void HFile::WriteLine(const int&n)
  105. {
  106.     Write(n);
  107.     Write("\r\n");
  108. }
  109.  
  110. void HFile::WriteLine(const double&n)
  111. {
  112.     Write(n);
  113.     Write("\r\n");
  114. }
  115.  
  116. void HFile::WriteByte(byte n)
  117. {
  118.     Write((char*)&n,1);
  119. }
  120.  
  121. bool HFile::Read(char* output,UINT count)
  122. {
  123.     DWORD ReadedBytes;
  124.     return ReadFile(FileHandle,output,count,&ReadedBytes,NULL) && ReadedBytes;
  125. }
  126.  
  127. bool HFile::Read(string& output,UINT count)
  128. {
  129.     char*  GetStr;
  130.     bool Result = Read(GetStr,count);
  131.     output = GetStr;
  132.     delete[] GetStr;
  133.     return Result;
  134. }
  135.  
  136. bool HFile::ReadChar(char& output)
  137. {
  138.     return Read(&output,1);
  139. }
  140.  
  141. bool HFile::PeekChar(char& output)
  142. {
  143.     bool result = ReadChar(output);
  144.     DWORD CurrentPoint = SetFilePointer(FileHandle,0,NULL,FILE_CURRENT);
  145.     long Posit;
  146.     SetFilePointer(FileHandle,CurrentPoint-1,&Posit,FILE_BEGIN);
  147.     return result;
  148. }
  149.  
  150. bool HFile::ReadToSplit(string& output,const char* Split)
  151. {
  152.     char Symbol;
  153.     bool res = ReadChar(Symbol);
  154.     if (!res) return false;
  155.     output = "";
  156.     while (strspn(&Symbol,Split) == 0)
  157.     {
  158.         output += Symbol;
  159.         res = ReadChar(Symbol);
  160.         if (!res) return false;
  161.     }
  162.     return true;
  163. }
  164.  
  165. bool HFile::ReadToSplit(char* output,const char* Split)
  166. {
  167.     string Str;
  168.     bool res = ReadToSplit(Str,Split);
  169.     if (!Str.empty())
  170.         output = (char*)Str.c_str();
  171.     return res;
  172. }
  173.  
  174. void HFile::Close()
  175. {
  176.     CloseHandle(FileHandle);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment