Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define fread_ex(file, string_) \
- fread(string_, sizeof(string_), sizeof(string_), file)
- #define fwrite_ex(file, string_) \
- fwrite(string_, sizeof(string_), sizeof(string_), file)
- using namespace std;
- #ifndef DOUBLE_NULL
- #define DOUBLE_NULL 0.0
- #endif
- #ifdef MAX_STRING
- #define S_FILE_MAX_VALUE MAX_STRING
- #else
- #define S_FILE_MAX_VALUE 128
- #endif
- #ifndef S_FILE_MAX_KEY
- #define S_FILE_MAX_KEY 24
- #endif
- #ifndef S_FILE_MAX_LINE
- #define S_FILE_MAX_LINE (S_FILE_MAX_KEY + S_FILE_MAX_VALUE + 3)
- #endif
- #ifndef S_FILE_MAX_FILENAME
- #define S_FILE_MAX_FILENAME 256
- #endif
- #ifndef S_FILE_MAX_LINES
- #define S_FILE_MAX_LINES 256
- #endif
- struct E_CACHE {
- char E_KEY[S_FILE_MAX_KEY];
- char E_VALUE[S_FILE_MAX_VALUE];
- };
- struct E_FILE {
- char E_FILENAME[S_FILE_MAX_FILENAME];
- bool E_OPEN;
- };
- E_CACHE gCache[S_FILE_MAX_LINES];
- E_CACHE gEmptyCache;
- E_FILE gFile;
- E_FILE gNoFile;
- bool S_FILE_EXISTS(const char * filename) {
- FILE * fp = NULL;
- fp = fopen(filename, "r");
- if(fp != NULL) {
- fclose(fp);
- return true;
- }
- fclose(fp);
- return false;
- }
- bool S_FILE_SAVE() {
- FILE * h;
- h = fopen(gFile.E_FILENAME, "rw");
- if(!gFile.E_OPEN) return true;
- if(h) {
- char line[S_FILE_MAX_LINE];
- int ln = -1;
- while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
- ln++;
- if(gCache[ln].E_VALUE[NULL] == ';') {
- if(gCache[ln].E_VALUE[1]) {
- sprintf(line, "%s\r\n", gCache[ln].E_VALUE);
- fwrite_ex(h, line);
- continue;
- }
- fwrite_ex(h, "\r\n");
- continue;
- }
- sprintf(line, "%s=%s\r\n", gCache[ln].E_KEY, gCache[ln].E_VALUE);
- fwrite_ex(h, line);
- }
- fclose(h);
- return true;
- }
- return false;
- }
- bool S_FILE_CLOSE() {
- if(!gFile.E_OPEN) return true;
- for(int ln = 0; ln < S_FILE_MAX_LINES; ln++) {
- gCache[ln] = gEmptyCache;
- }
- gFile = gNoFile;
- return true;
- }
- void S_FILE_STRING_COPY(char * dest, const char * source, int numcells, int maxlength) {
- int i = NULL;
- numcells = sizeof(source);
- maxlength = sizeof(dest);
- while((source[i]) && (i < numcells) && (i < maxlength)) {
- dest[i] = source[i];
- i++;
- }
- dest[i == maxlength? i - 1: i] = '\0';
- }
- void S_FILE_STRIP_LINE(char * string_) {
- int l;
- l = strlen(string_);
- if(string_[l - 2] == '\r') string_[l - 2] = '\0';
- else if(string_[l - 1] == '\n') string_[l - 1] = '\0';
- }
- bool S_FILE_OPEN(const char * filename) {
- FILE * h;
- if(!filename[NULL]) return false;
- else if(gFile.E_OPEN) {
- if(!strcmp(gFile.E_FILENAME, filename)) return true;
- S_FILE_CLOSE();
- }
- h = fopen(filename, "rw");
- if(h) {
- int ln = -1;
- int separator;
- char line[S_FILE_MAX_LINE];
- S_FILE_STRING_COPY(gFile.E_FILENAME, filename, S_FILE_MAX_FILENAME, S_FILE_MAX_FILENAME);
- while(((ln + 1) < S_FILE_MAX_LINES) && (fread_ex(h, line))) {
- ln++;
- S_FILE_STRIP_LINE(line);
- separator = strcmp(line, "=");
- if((line[NULL] == ';') || (line[NULL] == '=') || (separator == 0) || (separator > S_FILE_MAX_KEY)) {
- 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);
- continue;
- }
- S_FILE_STRING_COPY(gCache[ln].E_KEY, line, separator, S_FILE_MAX_KEY);
- S_FILE_STRING_COPY(gCache[ln].E_VALUE, (char *) line[separator + 1], S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
- if(!gCache[ln].E_VALUE[NULL]) gCache[ln].E_VALUE[NULL] = ' ';
- }
- fclose(h);
- gFile.E_OPEN = true;
- return true;
- }
- return false;
- }
- int S_FILE_READ_STRING(char * dest, const char * key, int maxlength) {
- int ln = -1;
- if((!gFile.E_OPEN) || (!key[NULL])) return false;
- maxlength = sizeof(dest);
- while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
- ln++;
- if(gCache[ln].E_VALUE[NULL] == ';') continue;
- if(!strcmp(gCache[ln].E_KEY, key)) {
- S_FILE_STRING_COPY(dest, gCache[ln].E_VALUE, S_FILE_MAX_VALUE, maxlength);
- return true;
- }
- }
- return false;
- }
- bool S_FILE_READ_INTEGER(const char * key) {
- char dest[12];
- if(S_FILE_READ_STRING(dest, key, sizeof(dest))) {
- atoi(dest);
- }
- return false;
- }
- float S_FILE_READ_FLOAT(const char * key) {
- char dest[12];
- if(S_FILE_READ_STRING(dest, key, sizeof(dest))) {
- atof(dest);
- }
- return DOUBLE_NULL;
- }
- bool S_FILE_WRITE_STRING(const char * key, const char * value) {
- int ln = -1;
- if((!gFile.E_OPEN) || (!key[NULL]) || (key[NULL] == ';') || (strcmp(key, "=") != NULL)) return false;
- while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
- ln++;
- if(gCache[ln].E_VALUE[NULL] == ';') continue;
- if(!strcmp(gCache[ln].E_KEY, key)) {
- S_FILE_STRING_COPY(gCache[ln].E_VALUE, value, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
- return true;
- }
- }
- ln++;
- if(ln < S_FILE_MAX_LINES) {
- S_FILE_STRING_COPY(gCache[ln].E_KEY, key, S_FILE_MAX_KEY, S_FILE_MAX_KEY);
- S_FILE_STRING_COPY(gCache[ln].E_VALUE, value, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
- return true;
- }
- return false;
- }
- bool S_FILE_WRITE_INTEGER(const char * key, int value) {
- char dest[12];
- sprintf(dest, "%i", value);
- return S_FILE_WRITE_STRING(key, dest);
- }
- bool S_FILE_WRITE_FLOAT(const char * key, float value) {
- char dest[12];
- sprintf(dest, "%0.4f", value);
- return S_FILE_WRITE_STRING(key, dest);
- }
- bool S_FILE_REMOVE_ENTRY(const char * key) {
- int ln = -1;
- if((!gFile.E_OPEN) || (!key[NULL]) || (strcmp(key, "=") != NULL)) return false;
- while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[NULL])) {
- ln++;
- if(gCache[ln].E_VALUE[NULL] == ';') continue;
- if(!strcmp(gCache[ln].E_KEY, key)) {
- for(; ln < (S_FILE_MAX_LINES - 1); ln++) {
- S_FILE_STRING_COPY(gCache[ln].E_KEY, gCache[ln + 1].E_KEY, S_FILE_MAX_KEY, S_FILE_MAX_KEY);
- S_FILE_STRING_COPY(gCache[ln].E_VALUE, gCache[ln + 1].E_VALUE, S_FILE_MAX_VALUE, S_FILE_MAX_VALUE);
- }
- return true;
- }
- }
- return false;
- }
- int S_FILE_REMOVE(const char * filename) {
- if(!filename[NULL]) return false;
- if((gFile.E_OPEN) && (!strcmp(gFile.E_FILENAME, filename))) {
- gFile = gNoFile;
- }
- return remove(filename);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement