Advertisement
Shiny_

SFile

Sep 30th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <string>
  4. #include <fstream>
  5. #include <iostream>
  6.  
  7. #define fwrite_ex(file, string_) \
  8.         fwrite(string_, sizeof(string_), sizeof(string_), file)
  9.  
  10. using namespace std;
  11.  
  12. #ifdef MAX_STRING
  13.     #define S_FILE_MAX_VALUE MAX_STRING
  14. #else
  15.     #define S_FILE_MAX_VALUE 128
  16. #endif
  17. #ifndef S_FILE_MAX_KEY
  18.     #define S_FILE_MAX_KEY 24
  19. #endif
  20. #ifndef S_FILE_MAX_LINE
  21.     #define S_FILE_MAX_LINE (S_FILE_MAX_KEY + S_FILE_MAX_VALUE + 3)
  22. #endif
  23. #ifndef S_FILE_MAX_FILENAME
  24.     #define S_FILE_MAX_FILENAME 256
  25. #endif
  26. #ifndef S_FILE_MAX_LINES
  27.     #define S_FILE_MAX_LINES 256
  28. #endif
  29.  
  30. struct E_CACHE {
  31.     char E_KEY[S_FILE_MAX_KEY];
  32.     char E_VALUE[S_FILE_MAX_VALUE];
  33. };
  34.  
  35. struct E_FILE {
  36.     char E_FILENAME[S_FILE_MAX_FILENAME];
  37.     bool E_OPEN;
  38. };
  39.  
  40. E_CACHE gCache[S_FILE_MAX_LINES];
  41. E_CACHE gEmptyCache;
  42. E_FILE gFile;
  43. E_FILE gNoFile;
  44.  
  45. bool S_FILE_EXISTS_C_NORMAL(const char * filename) {
  46.     FILE * fp = NULL;
  47.     fp = fopen(filename, "r");
  48.     if(fp != NULL) {
  49.         fclose(fp);
  50.         return true;
  51.     }
  52.     fclose(fp);
  53.     return false;
  54. }
  55.  
  56. bool S_FILE_EXISTS_CPP_NORMAL(const string & filename) {
  57.     fstream fin;
  58.     fin.open(filename.c_str(), ios::in);
  59.     if(fin.is_open()) {
  60.         fin.close();
  61.         return true;
  62.     }
  63.     fin.close();
  64.     return false;
  65. }
  66.  
  67. bool S_FILE_EXISTS_C(const char * filename)  {
  68.     struct stat info;
  69.     int ret = -1;
  70.     ret = stat(filename, &info);
  71.     return ret == 0? true: false;
  72. }
  73.  
  74. bool S_FILE_EXISTS_CPP(const string & filename) {
  75.     return S_FILE_EXISTS_C(filename.c_str());
  76. }
  77.  
  78. bool S_FILE_SAVE() {
  79.     if(!gFile.E_OPEN) return true;
  80.    
  81.     FILE * h;
  82.     h = fopen(gFile.E_FILENAME, "rw");
  83.     if(h) {
  84.         char line[S_FILE_MAX_LINE];
  85.         int ln = -1;
  86.         while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[0])) {
  87.             ln++;
  88.             if(gCache[ln].E_VALUE[0] == ';') {
  89.                 if(gCache[ln].E_VALUE[1]) {
  90.                     sprintf(line, "%s\r\n", gCache[ln].E_VALUE);
  91.                     fwrite_ex(h, line);
  92.                     //fwrite(line, sizeof(h), sizeof(line), h);
  93.                     continue;
  94.                 }
  95.                 fwrite_ex(h, "\r\n");
  96.                 //fwrite("\r\n", sizeof(h), sizeof(line), h);
  97.                 continue;
  98.             }
  99.             sprintf(line, "%s=%s\r\n", gCache[ln].E_KEY, gCache[ln].E_VALUE);
  100.             fwrite_ex(h, line);
  101.             //fwrite(line, sizeof(h), sizeof(line), h);
  102.         }
  103.         fclose(h);
  104.         return true;
  105.     }
  106.     return false;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement