diff --git a/desmume/src/cheatSystem.cpp b/desmume/src/cheatSystem.cpp index b400226e4..14531c4cd 100644 --- a/desmume/src/cheatSystem.cpp +++ b/desmume/src/cheatSystem.cpp @@ -804,52 +804,49 @@ BOOL CHEATS::save() { const char *types[] = {"DS", "AR", "CB"}; std::string cheatLineStr = ""; - FILE *flist = fopen((char *)filename, "w"); - if (flist) + EMUFILE_FILE flist((char *)filename, "w"); + if(flist.fail()) + return FALSE; + + flist.fprintf("; DeSmuME cheats file. VERSION %i.%03i\n", CHEAT_VERSION_MAJOR, CHEAT_VERSION_MINOR); + flist.fprintf("Name=%s\n", gameInfo.ROMname); + flist.fprintf("Serial=%s\n", gameInfo.ROMserial); + flist.fprintf("\n; cheats list\n"); + for (size_t i = 0; i < list.size(); i++) { - fprintf(flist, "; DeSmuME cheats file. VERSION %i.%03i\n", CHEAT_VERSION_MAJOR, CHEAT_VERSION_MINOR); - fprintf(flist, "Name=%s\n", gameInfo.ROMname); - fprintf(flist, "Serial=%s\n", gameInfo.ROMserial); - fputs("\n; cheats list\n", flist); - for (size_t i = 0; i < list.size(); i++) + if (list[i].num == 0) continue; + + char buf1[8] = {0}; + sprintf(buf1, "%s %c ", types[list[i].type], list[i].enabled?'1':'0'); + cheatLineStr = buf1; + + for (int t = 0; t < list[i].num; t++) { - if (list[i].num == 0) continue; - - char buf1[8] = {0}; - sprintf(buf1, "%s %c ", types[list[i].type], list[i].enabled?'1':'0'); - cheatLineStr = buf1; - - for (int t = 0; t < list[i].num; t++) + char buf2[10] = { 0 }; + + u32 adr = list[i].code[t][0]; + if (list[i].type == 0) { - char buf2[10] = { 0 }; - - u32 adr = list[i].code[t][0]; - if (list[i].type == 0) - { - //size of the cheat is written out as adr highest nybble - adr &= 0x0FFFFFFF; - adr |= (list[i].size << 28); - } - sprintf(buf2, "%08X", adr); - cheatLineStr += buf2; - - sprintf(buf2, "%08X", list[i].code[t][1]); - cheatLineStr += buf2; - if (t < (list[i].num - 1)) - cheatLineStr += ","; + //size of the cheat is written out as adr highest nybble + adr &= 0x0FFFFFFF; + adr |= (list[i].size << 28); } - - cheatLineStr += " ;"; - cheatLineStr += trim(list[i].description); - fprintf(flist, "%s\n", cheatLineStr.c_str()); + sprintf(buf2, "%08X", adr); + cheatLineStr += buf2; + + sprintf(buf2, "%08X", list[i].code[t][1]); + cheatLineStr += buf2; + if (t < (list[i].num - 1)) + cheatLineStr += ","; } - fputs("\n", flist); - fclose(flist); - return TRUE; + + cheatLineStr += " ;"; + cheatLineStr += trim(list[i].description); + flist.fprintf("%s\n", cheatLineStr.c_str()); } - - return FALSE; + flist.fprintf("\n"); + return TRUE; } char *CHEATS::clearCode(char *s) @@ -873,11 +870,9 @@ char *CHEATS::clearCode(char *s) BOOL CHEATS::load() { - FILE *flist = fopen((char *)filename, "r"); - if (flist == NULL) - { + EMUFILE_FILE flist((char *)filename, "r"); + if(flist.fail()) return FALSE; - } size_t readSize = (MAX_XX_CODE * 17) + sizeof(list[0].description) + 7; if (readSize < CHEAT_FILE_MIN_FGETS_BUFFER) @@ -886,11 +881,6 @@ BOOL CHEATS::load() } char *buf = (char *)malloc(readSize); - if (buf == NULL) - { - fclose(flist); - return FALSE; - } readSize *= sizeof(*buf); @@ -901,12 +891,12 @@ BOOL CHEATS::load() INFO("Load cheats: %s\n", filename); clear(); last = 0; line = 0; - while (!feof(flist)) + while (!flist.eof()) { CHEATS_LIST tmp_cht; line++; // only for debug memset(buf, 0, readSize); - if (fgets(buf, readSize, flist) == NULL) { + if (flist.fgets(buf, readSize) == NULL) { //INFO("Cheats: Failed to read from flist at line %i\n", line); continue; } @@ -980,7 +970,6 @@ BOOL CHEATS::load() free(buf); buf = NULL; - fclose(flist); INFO("Added %i cheat codes\n", list.size()); return TRUE; diff --git a/desmume/src/emufile.h b/desmume/src/emufile.h index d9bdbf775..bce46d9cf 100644 --- a/desmume/src/emufile.h +++ b/desmume/src/emufile.h @@ -83,6 +83,8 @@ public: virtual int fgetc() = 0; virtual int fputc(int c) = 0; + virtual char* fgets(char* str, int num) = 0; + virtual size_t _fread(const void *ptr, size_t bytes) = 0; virtual size_t fwrite(const void *ptr, size_t bytes) = 0; @@ -234,6 +236,11 @@ public: return 0; } + virtual char* fgets(char* str, int num) + { + throw "Not tested: emufile memory fgets"; + } + virtual size_t _fread(const void *ptr, size_t bytes); virtual size_t fwrite(const void *ptr, size_t bytes){ reserve(pos+(s32)bytes); @@ -335,6 +342,10 @@ public: return ::fputc(c, fp); } + virtual char* fgets(char* str, int num) { + return ::fgets(str, num, fp); + } + virtual size_t _fread(const void *ptr, size_t bytes); virtual size_t fwrite(const void *ptr, size_t bytes); diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index 6aec47a60..a04ba4c19 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -1044,13 +1044,12 @@ bool savestate_save (const char *file_name) if (!savestate_save(ms)) return false; - FILE* file = fopen(file_name,"wb"); - if(file) - { - elems_written = fwrite(ms.buf(),1,ms.size(),file); - fclose(file); - return (elems_written == ms.size()); - } else return false; + EMUFILE_FILE file(file_name, "wb"); + if(file.fail()) + return false; + + file.fwrite(ms.buf(), ms.size()); + return true; } static void wifi_savestate(EMUFILE &os)