Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace XYZ_FileIO
- {
- template <int STACK_CHUNK_SIZE=32> class FileIO
- {
- private:
- FILE* FileHandle;
- std::string name;
- std::string IOmode;
- bool error;
- char StackChunk[STACK_CHUNK_SIZE];
- char *HeapChunk;
- signed long int HeapSize;
- signed long long position;
- void overwriteChunk(char *chunk, signed long int size)
- {
- for(int i=0; i <= size; i++)
- {
- chunk[i]=0;
- }
- }
- bool setFileSize()
- {
- if (fseeko(this->FileHandle, 0L, SEEK_END) != 0)
- {
- perror("ABC");
- return false;
- }
- this->FileSize = ftello(this->FileHandle);
- if (this->FileSize == -1)
- {
- return false;
- }
- rewind(this->FileHandle);
- return true;
- }
- bool fopenHelper()
- {
- this->FileHandle=fopen(this->name.c_str(), this->IOmode.c_str());
- if(ferror(this->FileHandle))
- perror("Error! Couldn't open file. ");
- this->position=0;
- return (!ferror(this->FileHandle));
- }
- public:
- signed long long FileSize;
- FileIO(std::string Filename, char *mode):name(Filename), IOmode(mode)
- {
- this->error=this->fopenHelper();
- this->HeapSize=0;
- if(!this->setFileSize())
- perror("Couldn't set file size Stack!");
- }
- FileIO(std::string Filename, char *mode, signed long HeapS)
- {
- this->error=this->fopenHelper();
- this->HeapSize=HeapS;
- this->HeapChunk=new char[this->HeapSize];
- if(!this->setFileSize())
- perror("Couldn't set file size! Heap");
- }
- char *HeapRead()
- {
- this->overwriteChunk(this->HeapChunk, this->HeapSize);
- if(feof(this->FileHandle))
- return this->HeapChunk;
- fgets(this->HeapChunk, this->HeapSize, this->FileHandle);
- this->position=this->HeapSize;
- return this->HeapChunk;
- }
- char *StackRead()
- {
- this->overwriteChunk(this->StackChunk, STACK_CHUNK_SIZE);
- if(feof(this->FileHandle))
- return this->StackChunk;
- fgets(this->StackChunk, STACK_CHUNK_SIZE, this->FileHandle);
- this->position+=STACK_CHUNK_SIZE;
- return this->StackChunk;
- }
- bool isEOF(long long in)
- {
- if( (this->position+in) >= this->FileSize)
- return true;
- else
- return false;
- }
- bool hasError()
- {
- return (!this->error);
- }
- ~FileIO()
- {
- if(fclose(this->FileHandle))
- perror("ERRORERROR!!! FILE CLOSE DIDNT HAPPEN !!!");
- if(this->HeapSize!=0)
- delete[] HeapChunk;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement