diff --git a/src/drivers/win/cdlogger.cpp b/src/drivers/win/cdlogger.cpp index 481a400a..a2d3b881 100644 --- a/src/drivers/win/cdlogger.cpp +++ b/src/drivers/win/cdlogger.cpp @@ -219,7 +219,7 @@ void LoadCDLog (const char* nameo) } void LoadCDLogFile(){ - const char filter[]="Code Data Log File(*.CDL)\0*.cdl\0"; + const char filter[]="Code Data Log File(*.CDL)\0*.cdl\0\0"; char nameo[2048]; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); @@ -237,7 +237,7 @@ void LoadCDLogFile(){ } void SaveCDLogFileAs(){ - const char filter[]="Code Data Log File(*.CDL)\0*.cdl\0"; + const char filter[]="Code Data Log File(*.CDL)\0*.cdl\0All Files (*.*)\0*.*\0\0"; char nameo[2048]; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); @@ -245,13 +245,16 @@ void SaveCDLogFileAs(){ ofn.hInstance=fceu_hInstance; ofn.lpstrTitle="Save Code Data Log File As..."; ofn.lpstrFilter=filter; - ofn.lpstrDefExt = "cdl"; + //ofn.lpstrDefExt = "cdl"; strcpy(nameo,GetRomName()); ofn.lpstrFile=nameo; ofn.nMaxFile=256; ofn.Flags=OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY; ofn.hwndOwner = hCDLogger; if(!GetSaveFileName(&ofn))return; + if (ofn.nFilterIndex == 1) + AddExtensionIfMissing(nameo, sizeof(nameo), ".cdl"); + strcpy(loadedcdfile,nameo); SaveCDLogFile(); return; @@ -307,8 +310,8 @@ void UpdateCDLogger(){ void SaveStrippedRom(){ //this is based off of iNesSave() //todo: make this support nsfs - const char NESfilter[]="Stripped iNes Rom file(*.NES)\0*.nes\0"; - const char NSFfilter[]="Stripped NSF file(*.NSF)\0*.nsf\0"; + const char NESfilter[]="Stripped iNes Rom file(*.NES)\0*.nes\0All Files (*.*)\0*.*\0\0"; + const char NSFfilter[]="Stripped NSF file(*.NSF)\0*.nsf\0All Files (*.*)\0*.*\0\0"; char sromfilename[MAX_PATH]; FILE *fp; OPENFILENAME ofn; @@ -329,11 +332,10 @@ void SaveStrippedRom(){ //this is based off of iNesSave() ofn.lpstrTitle="Save Stripped File As..."; if (GameInfo->type==GIT_NSF) { ofn.lpstrFilter=NSFfilter; - ofn.lpstrDefExt = "nsf"; - } - else { + //ofn.lpstrDefExt = "nsf"; + } else { ofn.lpstrFilter=NESfilter; - ofn.lpstrDefExt = "nes"; + //ofn.lpstrDefExt = "nes"; } strcpy(sromfilename,GetRomName()); ofn.lpstrFile=sromfilename; @@ -341,6 +343,14 @@ void SaveStrippedRom(){ //this is based off of iNesSave() ofn.Flags=OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY; ofn.hwndOwner = hCDLogger; if(!GetSaveFileName(&ofn))return; + + if (ofn.nFilterIndex == 1) { + if (GameInfo->type==GIT_NSF) { + AddExtensionIfMissing(sromfilename, sizeof(sromfilename), ".nsf"); + } else { + AddExtensionIfMissing(sromfilename, sizeof(sromfilename), ".nes"); + } + } fp = fopen(sromfilename,"wb"); diff --git a/src/drivers/win/common.cpp b/src/drivers/win/common.cpp index 7930edaa..7782af27 100644 --- a/src/drivers/win/common.cpp +++ b/src/drivers/win/common.cpp @@ -25,3 +25,33 @@ void WindowBoundsCheckNoResize(int &windowPosX, int &windowPosY, long windowRigh windowPosY = -18; } } +// Check if a filename/path has an extension. Extensions can be up to 4 characters. +// Extension strings must begin with a . or they won't work right. +void AddExtensionIfMissing(char * name,unsigned int maxsize,const char * extension) { + //if user did not add an extension, add it for them + + std::string tempName = name; + + //Non-null terminated lengths of both strings, +1 for null termination + if ((strlen(name) + strlen(extension) + 1) <= maxsize) { + unsigned int x = tempName.find_last_of("."); + + //If the extension(".????") is longer then 5 characters, it's probably part of the filename. If x == -1, wasn't found + if ((x < (tempName.size() - 6)) || (x == -1)) { + //If everything above passed, append the extension, and update the string + tempName.append(extension); + strcpy(name, tempName.c_str()); + } + } +} + +// Overloaded operator of above, which deals with native std::string variants. +void AddExtensionIfMissing(std::string &name,const char * extension) { + //if user did not add an extension, add it for them + + unsigned int x = name.find_last_of("."); + + //If the extension(".????") is longer then 5 characters, it's probably part of the filename. If x == -1, wasn't found + if ((x < (name.size() - 6)) || (x == -1)) + name.append(extension); +} \ No newline at end of file diff --git a/src/drivers/win/common.h b/src/drivers/win/common.h index c645048e..edd42f22 100644 --- a/src/drivers/win/common.h +++ b/src/drivers/win/common.h @@ -52,4 +52,6 @@ extern int eoptions; bool directoryExists(const char* dirname); void WindowBoundsCheckResize(int &windowPosX, int &windowPosY, int windowSizeX, long windowRight); void WindowBoundsCheckNoResize(int &windowPosX, int &windowPosY, long windowRight); +void AddExtensionIfMissing(char * name,unsigned int maxsize,const char * extension); +void AddExtensionIfMissing(std::string &name,const char * extension); #endif diff --git a/src/drivers/win/input.cpp b/src/drivers/win/input.cpp index ef97c21c..70423847 100644 --- a/src/drivers/win/input.cpp +++ b/src/drivers/win/input.cpp @@ -1556,7 +1556,7 @@ void FCEUI_UseInputPreset(int preset) static void PresetExport(int preset) { - const char filter[]="Input Preset File(*.pre)\0*.pre\0"; + const char filter[]="Input Preset File(*.pre)\0*.pre\0All Files (*.*)\0*.*\0\0"; char nameo[2048]; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); @@ -1572,7 +1572,6 @@ static void PresetExport(int preset) ofn.lpstrInitialDir=initdir.c_str(); if(GetSaveFileName(&ofn)) { - int i; //Save the directory if(ofn.nFileOffset < 1024) { @@ -1582,24 +1581,8 @@ static void PresetExport(int preset) InputPresetDir[ofn.nFileOffset]=0; } - //quick get length of nameo - for(i=0;i<2048;i++) - { - if(nameo[i] == 0) - { - break; - } - } - - //add .pre if nameo doesn't have it - if((i < 4 || nameo[i-4] != '.') && i < 2040) - { - nameo[i] = '.'; - nameo[i+1] = 'p'; - nameo[i+2] = 'r'; - nameo[i+3] = 'e'; - nameo[i+4] = 0; - } + if (ofn.nFilterIndex == 1) + AddExtensionIfMissing(nameo, sizeof(nameo), ".pre"); FILE *fp=FCEUD_UTF8fopen(nameo,"w"); switch(preset) @@ -1614,7 +1597,7 @@ static void PresetExport(int preset) static void PresetImport(int preset) { - const char filter[]="Input Preset File(*.pre)\0*.pre\0"; + const char filter[]="Input Preset File(*.pre)\0*.pre\0\0"; char nameo[2048]; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); diff --git a/src/drivers/win/memview.cpp b/src/drivers/win/memview.cpp index a0be42d2..086d6d58 100644 --- a/src/drivers/win/memview.cpp +++ b/src/drivers/win/memview.cpp @@ -281,13 +281,10 @@ static int GetRomFileSize(){ //todo: fix or remove this? return 0; } - void SaveRomAs() { - const char filter[]="NES ROM file (*.nes)\0*.nes\0"; + const char filter[]="NES ROM file (*.nes)\0*.nes\0All Files (*.*)\0*.*\0\0"; char nameo[2048]; - std::string tempName; - int x; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); @@ -295,7 +292,7 @@ void SaveRomAs() ofn.hInstance=fceu_hInstance; ofn.lpstrTitle="Save Nes ROM as..."; ofn.lpstrFilter=filter; - ofn.lpstrDefExt="nes"; + //ofn.lpstrDefExt="nes"; strcpy(nameo,GetRomName()); ofn.lpstrFile=nameo; ofn.nMaxFile=256; @@ -303,12 +300,9 @@ void SaveRomAs() ofn.hwndOwner = hMemView; if (GetSaveFileName(&ofn)) { - //if user did not add .nes, add it for them - tempName = nameo; - x = tempName.find_last_of(".nes"); - if (x < 0) - tempName.append(".nes"); - strcpy(nameo, tempName.c_str()); + if (ofn.nFilterIndex == 1) + AddExtensionIfMissing(nameo, sizeof(nameo), ".nes"); + iNesSaveAs(nameo); } } @@ -371,7 +365,7 @@ int LoadTable(const char* nameo) //should return -1, otherwise returns the line number it had the error on int LoadTableFile(){ - const char filter[]="Table Files (*.TBL)\0*.tbl\0All files (*.*)\0*.*\0"; + const char filter[]="Table Files (*.TBL)\0*.tbl\0All files (*.*)\0*.*\0\0"; char nameo[2048]; //todo: possibly no need for this? can lpstrfilter point to loadedcdfile instead? OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); @@ -645,16 +639,19 @@ void dumpToFile(const char* buffer, unsigned int size) ofn.lStructSize=sizeof(ofn); ofn.hInstance=fceu_hInstance; ofn.lpstrTitle="Save to file ..."; - ofn.lpstrFilter="Binary File (*.BIN)\0*.bin\0"; - ofn.lpstrDefExt="bin"; + ofn.lpstrFilter="Binary File (*.BIN)\0*.bin\0All Files (*.*)\0*.*\0\0"; + //ofn.lpstrDefExt="bin"; strcpy(name,GetRomName()); ofn.lpstrFile=name; ofn.nMaxFile=256; ofn.Flags=OFN_EXPLORER|OFN_HIDEREADONLY; - if (GetOpenFileName(&ofn)) + if (GetSaveFileName(&ofn)) { - FILE* memfile = fopen(ofn.lpstrFile, "wb"); + if (ofn.nFilterIndex == 1) + AddExtensionIfMissing(name, sizeof(name), ".bin"); + + FILE* memfile = fopen(name, "wb"); if (!memfile || fwrite(buffer, 1, size, memfile) != size) { diff --git a/src/drivers/win/memwatch.cpp b/src/drivers/win/memwatch.cpp index e3601c9b..8804e6e6 100644 --- a/src/drivers/win/memwatch.cpp +++ b/src/drivers/win/memwatch.cpp @@ -398,7 +398,7 @@ bool iftextchanged() //Save as... static void SaveMemWatch() { - const char filter[]="Memory address list(*.txt)\0*.txt\0"; + const char filter[]="Memory address list(*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0"; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); @@ -406,7 +406,7 @@ static void SaveMemWatch() ofn.hInstance=fceu_hInstance; ofn.lpstrTitle="Save Memory Watch As..."; ofn.lpstrFilter=filter; - ofn.lpstrDefExt="txt"; + //ofn.lpstrDefExt="txt"; char nameo[2048]; if (!memwLastFilename[0]) strcpy(nameo,GetRomName()); @@ -430,26 +430,11 @@ static void SaveMemWatch() MemWatchDir[ofn.nFileOffset]=0; } - //quick get length of memwLastFilename - strcpy(memwLastFilename,nameo); - for(i=0;i<2048;i++) - { - if(memwLastFilename[i] == 0) - { - break; - } - } + if (ofn.nFilterIndex == 1) + AddExtensionIfMissing(nameo, sizeof(nameo), ".txt"); + + strcpy(memwLastFilename,nameo); - //add .txt if memwLastFilename doesn't have it - if((i < 4 || memwLastFilename[i-4] != '.') && i < 2040) - { - memwLastFilename[i] = '.'; - memwLastFilename[i+1] = 't'; - memwLastFilename[i+2] = 'x'; - memwLastFilename[i+3] = 't'; - memwLastFilename[i+4] = 0; - } - SaveStrings(); FILE *fp=FCEUD_UTF8fopen(memwLastFilename,"w"); for(i=0;i