Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/stat.h>
- #include <string>
- #include <fstream>
- #include <iostream>
- #define fwrite_ex(file, string_) \
- fwrite(string_, sizeof(string_), sizeof(string_), file)
- using namespace std;
- #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_C_NORMAL(const char * filename) {
- FILE * fp = NULL;
- fp = fopen(filename, "r");
- if(fp != NULL) {
- fclose(fp);
- return true;
- }
- fclose(fp);
- return false;
- }
- bool S_FILE_EXISTS_CPP_NORMAL(const string & filename) {
- fstream fin;
- fin.open(filename.c_str(), ios::in);
- if(fin.is_open()) {
- fin.close();
- return true;
- }
- fin.close();
- return false;
- }
- bool S_FILE_EXISTS_C(const char * filename) {
- struct stat info;
- int ret = -1;
- ret = stat(filename, &info);
- return ret == 0? true: false;
- }
- bool S_FILE_EXISTS_CPP(const string & filename) {
- return S_FILE_EXISTS_C(filename.c_str());
- }
- bool S_FILE_SAVE() {
- if(!gFile.E_OPEN) return true;
- FILE * h;
- h = fopen(gFile.E_FILENAME, "rw");
- if(h) {
- char line[S_FILE_MAX_LINE];
- int ln = -1;
- while(((ln + 1) < S_FILE_MAX_LINES) && (gCache[ln + 1].E_VALUE[0])) {
- ln++;
- if(gCache[ln].E_VALUE[0] == ';') {
- if(gCache[ln].E_VALUE[1]) {
- sprintf(line, "%s\r\n", gCache[ln].E_VALUE);
- fwrite_ex(h, line);
- //fwrite(line, sizeof(h), sizeof(line), h);
- continue;
- }
- fwrite_ex(h, "\r\n");
- //fwrite("\r\n", sizeof(h), sizeof(line), h);
- continue;
- }
- sprintf(line, "%s=%s\r\n", gCache[ln].E_KEY, gCache[ln].E_VALUE);
- fwrite_ex(h, line);
- //fwrite(line, sizeof(h), sizeof(line), h);
- }
- fclose(h);
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement