Advertisement
Darksider3

Untitled

Oct 5th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. namespace XYZ_FileIO
  2. {
  3.   template <int STACK_CHUNK_SIZE=32> class FileIO
  4.   {
  5.   private:
  6.     FILE* FileHandle;
  7.     std::string name;
  8.     std::string IOmode;
  9.     bool error;
  10.     char StackChunk[STACK_CHUNK_SIZE];
  11.     char *HeapChunk;
  12.     signed long int HeapSize;
  13.     signed long long position;
  14.     void overwriteChunk(char *chunk, signed long int size)
  15.     {
  16.       for(int i=0; i <= size; i++)
  17.       {
  18.         chunk[i]=0;
  19.       }
  20.     }
  21.     bool setFileSize()
  22.     {
  23.       if (fseeko(this->FileHandle,  0L, SEEK_END) != 0)
  24.       {
  25.         perror("ABC");
  26.         return false;
  27.       }
  28.    
  29.       this->FileSize = ftello(this->FileHandle);
  30.       if (this->FileSize == -1)
  31.       {
  32.         return false;
  33.       }
  34.       rewind(this->FileHandle);
  35.       return true;
  36.     }
  37.    
  38.     bool fopenHelper()
  39.     {
  40.       this->FileHandle=fopen(this->name.c_str(), this->IOmode.c_str());
  41.       if(ferror(this->FileHandle))
  42.         perror("Error! Couldn't open file. ");
  43.       this->position=0;
  44.       return (!ferror(this->FileHandle));
  45.     }
  46.    
  47.   public:
  48.     signed long long FileSize;
  49.    
  50.     FileIO(std::string Filename, char *mode):name(Filename), IOmode(mode)
  51.     {
  52.       this->error=this->fopenHelper();
  53.       this->HeapSize=0;
  54.       if(!this->setFileSize())
  55.         perror("Couldn't set file size Stack!");
  56.     }
  57.    
  58.     FileIO(std::string Filename, char *mode, signed long HeapS)
  59.     {
  60.       this->error=this->fopenHelper();
  61.       this->HeapSize=HeapS;
  62.       this->HeapChunk=new char[this->HeapSize];
  63.       if(!this->setFileSize())
  64.         perror("Couldn't set file size! Heap");
  65.     }
  66.    
  67.     char *HeapRead()
  68.     {
  69.       this->overwriteChunk(this->HeapChunk, this->HeapSize);
  70.       if(feof(this->FileHandle))
  71.         return this->HeapChunk;
  72.       fgets(this->HeapChunk, this->HeapSize, this->FileHandle);
  73.       this->position=this->HeapSize;
  74.       return this->HeapChunk;
  75.     }
  76.    
  77.     char *StackRead()
  78.     {
  79.       this->overwriteChunk(this->StackChunk, STACK_CHUNK_SIZE);
  80.       if(feof(this->FileHandle))
  81.         return this->StackChunk;
  82.       fgets(this->StackChunk, STACK_CHUNK_SIZE, this->FileHandle);
  83.       this->position+=STACK_CHUNK_SIZE;
  84.       return this->StackChunk;
  85.     }
  86.    
  87.     bool isEOF(long long in)
  88.     {
  89.       if( (this->position+in) >= this->FileSize)
  90.         return true;
  91.       else
  92.         return false;
  93.     }
  94.    
  95.     bool hasError()
  96.     {
  97.       return (!this->error);
  98.     }
  99.    
  100.     ~FileIO()
  101.     {
  102.       if(fclose(this->FileHandle))
  103.         perror("ERRORERROR!!! FILE CLOSE DIDNT HAPPEN !!!");
  104.       if(this->HeapSize!=0)
  105.         delete[] HeapChunk;
  106.     }
  107.   };
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement