Advertisement
spinsquad

cFile - a revolutionary file read/write system coded by Fami

Mar 23rd, 2015
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. /*
  2.     cFile - a revolutionary file read/write system coded by Fami Harukaze!
  3.     04.08.2014
  4. */
  5.  
  6. #ifndef __cFile
  7. #define __cFile
  8.  
  9. #include <sys/stat.h>
  10. #include <stdio.h>
  11. #include <io.h>
  12. #include <minwindef.h>
  13. #include <fileapi.h>
  14. #include <stdarg.h>
  15.  
  16. class cFile
  17. {
  18.     public:
  19.         static bool __stdcall Exists(char*);
  20.         static void __stdcall Write(char*, char*, ...);
  21.         static void __stdcall Append(char*, char*, ...);
  22.         static char* __stdcall Read(char*);
  23.  
  24.     public:
  25.         static _iobuf* __stdcall cfopen(const char*, const char*);
  26. };
  27.  
  28. _iobuf* __stdcall cFile::cfopen(const char* fn, const char* m)
  29. {
  30.     if (_access(fn, 0) == -1)
  31.     {
  32.         char p[MAX_PATH];
  33.         strcpy(p, fn);
  34.  
  35.         for (unsigned int i = 0; i < strlen(p); i++)
  36.         if (p[i] == '/')
  37.         {
  38.             char oldch = p[i];
  39.             p[i] = 0;
  40.  
  41.             CreateDirectoryA(p, NULL);
  42.  
  43.             p[i] = oldch;
  44.         }
  45.     }
  46.  
  47.     return fopen(fn, m);
  48. } // Credits: NanoCat
  49.  
  50. bool __stdcall cFile::Exists(char* name)
  51. {
  52.     struct stat buffer;
  53.     return (stat(name, &buffer) == 0);
  54. }
  55.  
  56. void __stdcall cFile::Append(char* strFilename, char* strContent, ...)
  57. {
  58.     char szBuffer[16864] = { 0 };
  59.  
  60.     va_list va;
  61.  
  62.     va_start(va, strContent);
  63.     _vsnprintf(szBuffer, sizeof(szBuffer), strContent, va);
  64.     va_end(va);
  65.  
  66.     _iobuf* filelog = cfopen(strFilename, "a");
  67.     fprintf(filelog, "%s", szBuffer);
  68.     fclose(filelog);
  69. }
  70.  
  71. void __stdcall cFile::Write(char* strFilename, char* strContent, ...)
  72. {
  73.     char szBuffer[16864] = { 0 };
  74.  
  75.     va_list va;
  76.  
  77.     va_start(va, strContent);
  78.     _vsnprintf(szBuffer, sizeof(szBuffer), strContent, va);
  79.     va_end(va);
  80.  
  81.     _iobuf* filelog = cfopen(strFilename, "wb");
  82.     fprintf(filelog, "%s", szBuffer);
  83.     fclose(filelog);
  84. }
  85.  
  86. char* __stdcall cFile::Read(char* strFilename)
  87. {
  88.     char* strFilecontent;
  89.     strFilecontent = "";
  90.  
  91.     if (Exists(strFilename))
  92.     {
  93.  
  94.         FILE* fileStream;
  95.         fileStream = cfopen(strFilename, "r");
  96.  
  97.         if (!fileStream) {
  98.             return "";
  99.         }
  100.  
  101.         // Determine file size
  102.         fseek(fileStream, 0, SEEK_END);
  103.         size_t size = ftell(fileStream);
  104.  
  105.         char* where = new char[size];
  106.  
  107.         rewind(fileStream);
  108.         fread(where, sizeof(char), size, fileStream);
  109.  
  110.         strFilecontent = where;
  111.  
  112.         delete[] where;
  113.  
  114.     }
  115.  
  116.     return strFilecontent;
  117. }
  118.  
  119. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement