Advertisement
Shiny_

S_FILE

Sep 30th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define fread_ex(file, string_) \
  4.         fread(string_, sizeof(string_), sizeof(string_), file)
  5.  
  6. #define fwrite_ex(file, string_) \
  7.         fwrite(string_, sizeof(string_), sizeof(string_), file)
  8.  
  9. using namespace std;
  10.  
  11. #ifndef DOUBLE_NULL
  12.     #define DOUBLE_NULL 0.0
  13. #endif
  14. #ifdef MAX_STRING
  15.     #define S_FILE_MAX_VALUE MAX_STRING
  16. #else
  17.     #define S_FILE_MAX_VALUE 128
  18. #endif
  19. #ifndef S_FILE_MAX_KEY
  20.     #define S_FILE_MAX_KEY 24
  21. #endif
  22. #ifndef S_FILE_MAX_LINE
  23.     #define S_FILE_MAX_LINE (S_FILE_MAX_KEY + S_FILE_MAX_VALUE + 3)
  24. #endif
  25. #ifndef S_FILE_MAX_FILENAME
  26.     #define S_FILE_MAX_FILENAME 256
  27. #endif
  28. #ifndef S_FILE_MAX_LINES
  29.     #define S_FILE_MAX_LINES 256
  30. #endif
  31.  
  32. struct E_CACHE {
  33.     char E_KEY[S_FILE_MAX_KEY];
  34.     char E_VALUE[S_FILE_MAX_VALUE];
  35. };
  36.  
  37. struct E_FILE {
  38.     char E_FILENAME[S_FILE_MAX_FILENAME];
  39.     bool E_OPEN;
  40. };
  41.  
  42. E_CACHE gCache[S_FILE_MAX_LINES];
  43. E_CACHE gEmptyCache;
  44. E_FILE gFile;
  45. E_FILE gNoFile;
  46.  
  47. bool S_FILE_EXISTS(const char * filename) {
  48.     FILE * fp = NULL;
  49.     fp = fopen(filename, "r");
  50.  
  51.     if(fp != NULL) {
  52.         fclose(fp);
  53.         return true;
  54.     }
  55.     fclose(fp);
  56.     return false;
  57. }
  58.  
  59. bool S_FILE_SAVE() {
  60.     FILE * h;
  61.     h = fopen(gFile.E_FILENAME, "rw");
  62.  
  63.     if(!gFile.E_OPEN) return true;
  64.     if(h) {
  65.         char line[S_FILE_MAX_LINE];
  66.         int ln = -1;
  67.         while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
  68.             ln++;
  69.             if(gCache[ln].E_VALUE[NULL] == ';') {
  70.                 if(gCache[ln].E_VALUE[1]) {
  71.                     sprintf(line, "%s\r\n", gCache[ln].E_VALUE);
  72.                     fwrite_ex(h, line);
  73.                     continue;
  74.                 }
  75.                 fwrite_ex(h, "\r\n");
  76.                 continue;
  77.             }
  78.             sprintf(line, "%s=%s\r\n", gCache[ln].E_KEY, gCache[ln].E_VALUE);
  79.             fwrite_ex(h, line);
  80.         }
  81.         fclose(h);
  82.         return true;
  83.     }
  84.     return false;
  85. }
  86.  
  87. bool S_FILE_CLOSE() {
  88.     if(!gFile.E_OPEN) return true;
  89.    
  90.     for(int ln = 0; ln < S_FILE_MAX_LINES; ln++) {
  91.         gCache[ln] = gEmptyCache;
  92.     }
  93.     gFile = gNoFile;
  94.     return true;
  95. }
  96.  
  97. void S_FILE_STRING_COPY(char * dest, const char * source, int numcells, int maxlength) {
  98.     int i = NULL;
  99.    
  100.     numcells = sizeof(source);
  101.     maxlength = sizeof(dest);
  102.     while((source[i]) && (i < numcells) && (i < maxlength)) {
  103.         dest[i] = source[i];
  104.         i++;
  105.     }
  106.     dest[i == maxlength? i - 1: i] = '\0';
  107. }
  108.  
  109. void S_FILE_STRIP_LINE(char * string_) {
  110.     int l;
  111.    
  112.     l = strlen(string_);
  113.     if(string_[l - 2] == '\r') string_[l - 2] = '\0';
  114.     else if(string_[l - 1] == '\n') string_[l - 1] = '\0';
  115. }
  116.  
  117. bool S_FILE_OPEN(const char * filename) {
  118.     FILE * h;
  119.    
  120.     if(!filename[NULL]) return false;
  121.     else if(gFile.E_OPEN) {
  122.         if(!strcmp(gFile.E_FILENAME, filename)) return true;
  123.         S_FILE_CLOSE();
  124.     }
  125.     h = fopen(filename, "rw");
  126.     if(h) {
  127.         int ln = -1;
  128.         int separator;
  129.         char line[S_FILE_MAX_LINE];
  130.        
  131.         S_FILE_STRING_COPY(gFile.E_FILENAME, filename, S_FILE_MAX_FILENAME, S_FILE_MAX_FILENAME);
  132.         while(((ln + 1) < S_FILE_MAX_LINES) && (fread_ex(h, line))) {
  133.             ln++;
  134.             S_FILE_STRIP_LINE(line);
  135.             separator = strcmp(line, "=");
  136.             if((line[NULL] == ';') || (line[NULL] == '=') || (separator == 0) || (separator > S_FILE_MAX_KEY)) {
  137.                 line[NULL] == ';'? S_FILE_STRING_COPY(gCache[ln].E_VALUE, line, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE): S_FILE_STRING_COPY((char *) gCache[ln].E_VALUE[NULL], line, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
  138.                 continue;
  139.             }
  140.             S_FILE_STRING_COPY(gCache[ln].E_KEY, line, separator, S_FILE_MAX_KEY);
  141.             S_FILE_STRING_COPY(gCache[ln].E_VALUE, (char *) line[separator + 1], S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
  142.             if(!gCache[ln].E_VALUE[NULL]) gCache[ln].E_VALUE[NULL] = ' ';
  143.         }
  144.         fclose(h);
  145.         gFile.E_OPEN = true;
  146.         return true;
  147.     }
  148.     return false;
  149. }
  150.  
  151. int S_FILE_READ_STRING(char * dest, const char * key, int maxlength) {
  152.     int ln = -1;
  153.    
  154.     if((!gFile.E_OPEN) || (!key[NULL])) return false;
  155.     maxlength = sizeof(dest);
  156.     while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
  157.         ln++;
  158.  
  159.         if(gCache[ln].E_VALUE[NULL] == ';') continue;
  160.         if(!strcmp(gCache[ln].E_KEY, key)) {
  161.             S_FILE_STRING_COPY(dest, gCache[ln].E_VALUE, S_FILE_MAX_VALUE, maxlength);
  162.             return true;
  163.         }
  164.     }
  165.     return false;
  166. }
  167.  
  168. bool S_FILE_READ_INTEGER(const char * key) {
  169.     char dest[12];
  170.  
  171.     if(S_FILE_READ_STRING(dest, key, sizeof(dest))) {
  172.         atoi(dest);
  173.     }
  174.     return false;
  175. }
  176.  
  177. float S_FILE_READ_FLOAT(const char * key) {
  178.     char dest[12];
  179.  
  180.     if(S_FILE_READ_STRING(dest, key, sizeof(dest))) {
  181.         atof(dest);
  182.     }
  183.     return DOUBLE_NULL;
  184. }
  185.  
  186. bool S_FILE_WRITE_STRING(const char * key, const char * value) {
  187.     int ln = -1;
  188.    
  189.     if((!gFile.E_OPEN) || (!key[NULL]) || (key[NULL] == ';') || (strcmp(key, "=") != NULL)) return false;
  190.     while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
  191.         ln++;
  192.         if(gCache[ln].E_VALUE[NULL] == ';') continue;
  193.         if(!strcmp(gCache[ln].E_KEY, key)) {
  194.             S_FILE_STRING_COPY(gCache[ln].E_VALUE, value, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
  195.             return true;
  196.         }
  197.     }
  198.     ln++;
  199.     if(ln < S_FILE_MAX_LINES) {
  200.         S_FILE_STRING_COPY(gCache[ln].E_KEY, key, S_FILE_MAX_KEY, S_FILE_MAX_KEY);
  201.         S_FILE_STRING_COPY(gCache[ln].E_VALUE, value, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
  202.         return true;
  203.     }
  204.     return false;
  205. }
  206.  
  207. bool S_FILE_WRITE_INTEGER(const char * key, int value) {
  208.     char dest[12];
  209.  
  210.     sprintf(dest, "%i", value);
  211.     return S_FILE_WRITE_STRING(key, dest);
  212. }
  213.  
  214. bool S_FILE_WRITE_FLOAT(const char * key, float value) {
  215.     char dest[12];
  216.  
  217.     sprintf(dest, "%0.4f", value);
  218.     return S_FILE_WRITE_STRING(key, dest);
  219. }
  220.  
  221. bool S_FILE_REMOVE_ENTRY(const char * key) {
  222.     int ln = -1;
  223.    
  224.     if((!gFile.E_OPEN) || (!key[NULL]) || (strcmp(key, "=") != NULL)) return false;
  225.     while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
  226.         ln++;
  227.         if(gCache[ln].E_VALUE[NULL] == ';') continue;
  228.         if(!strcmp(gCache[ln].E_KEY, key)) {
  229.             for(; ln < (S_FILE_MAX_LINES - 1); ln++) {
  230.                 S_FILE_STRING_COPY(gCache[ln].E_KEY, gCache[ln + 1].E_KEY, S_FILE_MAX_KEY, S_FILE_MAX_KEY);
  231.                 S_FILE_STRING_COPY(gCache[ln].E_VALUE, gCache[ln + 1].E_VALUE, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
  232.             }
  233.             return true;
  234.         }
  235.     }
  236.     return false;
  237. }
  238.  
  239. int S_FILE_REMOVE(const char * filename) {
  240.     if(!filename[NULL]) return false;
  241.     if((gFile.E_OPEN) && (!strcmp(gFile.E_FILENAME, filename))) {
  242.         gFile = gNoFile;
  243.     }
  244.     return remove(filename);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement