xnim

FileWINAPI

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