From d6bbc99c90ef586b041651d373524b30fe6c8676 Mon Sep 17 00:00:00 2001 From: erihel Date: Sun, 17 Mar 2013 19:01:32 +0100 Subject: * Changed file loading to fix issue #73 * Moved few functions from misc.cpp to profile.cpp (used to set/get user dir) * Removed some warnings * More work to change const char* to std::string * Some work on file path to fix issue #60 with bad slashes on POSIX platform --- src/common/misc.cpp | 124 ++---------------------------------------- src/common/misc.h | 3 -- src/common/profile.cpp | 45 ++++++++++++++++ src/common/profile.h | 28 +++++++++- src/object/robotmain.cpp | 7 +-- src/script/script.cpp | 2 +- src/ui/displayinfo.cpp | 2 +- src/ui/edit.cpp | 136 ++++++++++++++++++++--------------------------- src/ui/edit.h | 29 +++++----- src/ui/maindialog.cpp | 25 +++++---- src/ui/maindialog.h | 6 +++ src/ui/studio.cpp | 36 ++++++------- src/ui/studio.h | 5 +- 13 files changed, 196 insertions(+), 252 deletions(-) (limited to 'src') diff --git a/src/common/misc.cpp b/src/common/misc.cpp index 2bce3b8..b96abca 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp @@ -25,10 +25,6 @@ #include -static bool g_bUserDir = false; -static char g_userDir[100] = ""; - - // Returns a non-accented letter. char GetNoAccent(char letter) @@ -234,72 +230,11 @@ void TimeToAscii(time_t time, char *buffer) #endif*/ } - -// Makes a copy of a file. - -bool Xfer(char* src, char* dst) -{ - FILE *fs, *fd; - char *buffer; - int len; - - fs = fopen(src, "rb"); - if ( fs == 0 ) - { - return false; - } - - fd = fopen(dst, "wb"); - if ( fd == 0 ) - { - fclose(fs); - return false; - } - - buffer = static_cast(malloc(10000)); - - while ( true ) - { - len = fread(buffer, 1, 10000, fs); - if ( len == 0 ) break; - fwrite(buffer, 1, len, fd); - } - - free(buffer); - fclose(fs); - fclose(fd); - return true; -} - -// Copy a file into the temporary folder. - -bool CopyFileToTemp(char* filename) -{ - char src[100]; - char dst[100]; - char save[100]; - - UserDir(src, filename, "textures"); - - strcpy(save, g_userDir); - strcpy(g_userDir, "temp"); - UserDir(dst, filename, "textures"); - strcpy(g_userDir, save); - - //_mkdir("temp"); - system("mkdir temp"); - - if ( !Xfer(src, dst) ) return false; - - strcpy(filename, dst); - return true; -} - // Copy a list of numbered files into the temporary folder. bool CopyFileListToTemp(char* filename, int* list, int total) { - char name[100]; + /*char name[100]; char ext[10]; char file[100]; char save[100]; @@ -329,8 +264,8 @@ bool CopyFileListToTemp(char* filename, int* list, int total) UserDir(file, filename, "textures"); strcpy(filename, file); strcpy(g_userDir, save); - - return true; +*/ + return false; } @@ -342,56 +277,3 @@ void AddExt(char* filename, const char* ext) strcat(filename, ext); } - -// Specifies the user folder. - -void UserDir(bool bUser, const char* dir) -{ - g_bUserDir = bUser; - strcpy(g_userDir, dir); -} - -// Replaces the string %user% by the user folder. -// in: dir = "%user%toto.txt" -// def = "abc\" -// out: buffer = "abc\toto.txt" - -void UserDir(char* buffer, const char* dir, const char* def) -{ - char ddir[100]; - const char* add; - - if ( strstr(dir, "\\") == 0 && def[0] != 0 ) - { - sprintf(ddir, "%s\\%s", def, dir); - } - else - { - strcpy(ddir, dir); - } - dir = ddir; - - while ( *dir != 0 ) - { - if ( dir[0] == '%' && - dir[1] == 'u' && - dir[2] == 's' && - dir[3] == 'e' && - dir[4] == 'r' && - dir[5] == '%' ) // %user% ? - { - if ( g_bUserDir ) add = g_userDir; - else add = def; - - while ( *add != 0 ) - { - *buffer++ = *add++; - } - dir += 6; // jumps to %user% - continue; - } - - *buffer++ = *dir++; - } - *buffer = 0; -} diff --git a/src/common/misc.h b/src/common/misc.h index f210706..e2ddc44 100644 --- a/src/common/misc.h +++ b/src/common/misc.h @@ -29,8 +29,5 @@ extern char GetToLower(char letter); extern void TimeToAscii(time_t time, char *buffer); -extern bool CopyFileToTemp(char* filename); extern bool CopyFileListToTemp(char* filename, int* list, int total); extern void AddExt(char* filename, const char* ext); -extern void UserDir(bool bUser, const char* dir); -extern void UserDir(char* buffer, const char* dir, const char* def); diff --git a/src/common/profile.cpp b/src/common/profile.cpp index 18cc187..2d11217 100644 --- a/src/common/profile.cpp +++ b/src/common/profile.cpp @@ -182,3 +182,48 @@ std::vector< std::string > CProfile::GetLocalProfileSection(std::string section, return ret_list; } + + +void CProfile::SetUserDir(std::string dir) +{ + m_userDirectory = dir; +} + + +std::string CProfile::GetUserBasedPath(std::string dir, std::string default_dir) +{ + std::string path = dir; + boost::replace_all(path, "\\", "/"); + if (dir.find("/") == std::string::npos) { + path = default_dir + "/" + dir; + } + + if (m_userDirectory.length() > 0) { + boost::replace_all(path, "%user%", m_userDirectory); + } else { + boost::replace_all(path, "%user%", default_dir); + } + + return fs::path(path).make_preferred().string(); +} + + +bool CProfile::CopyFileToTemp(std::string filename) +{ + std::string src, dst; + std::string tmp_user_dir = m_userDirectory; + + src = GetUserBasedPath(filename, "textures"); + SetUserDir("temp"); + dst = GetUserBasedPath(filename, "textures"); + SetUserDir(tmp_user_dir); + + fs::create_directory(fs::path(dst).parent_path().make_preferred().string()); + fs::copy_file(src, dst, fs::copy_option::overwrite_if_exists); + if (fs::exists(dst)) { + return true; + } + + return false; +} + diff --git a/src/common/profile.h b/src/common/profile.h index 9bc6c37..bcd76c3 100644 --- a/src/common/profile.h +++ b/src/common/profile.h @@ -21,14 +21,20 @@ #pragma once - #include "common/singleton.h" +// this is just to fix problem with undefined reference when compiling with c++11 support +#define BOOST_NO_SCOPED_ENUMS + #include +#include +#include #include #include +namespace fs = boost::filesystem; + /** * \class CProfile @@ -101,10 +107,30 @@ class CProfile : public CSingleton * \return vector of values */ std::vector< std::string > GetLocalProfileSection(std::string section, std::string key); + + /** Sets current user directory + * \param dir + */ + void SetUserDir(std::string dir); + + /** Returns path based on current user. Replaces %user% in path with current user dir or + * uses default_dir param if no user dir is specified + * \param dir + * \param default_dir + * \return path + */ + std::string GetUserBasedPath(std::string dir, std::string default_dir); + + /** opy a file into the temporary folder. + * \param filename + * \return true on success + */ + bool CopyFileToTemp(std::string filename); private: boost::property_tree::ptree m_propertyTree; bool m_profileNeedSave; + std::string m_userDirectory; }; //! Global function to get profile instance diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index da6ac42..635fc4e 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -1187,7 +1187,7 @@ void CRobotMain::ChangePhase(Phase phase) pe->SetFontType(Gfx::FONT_COLOBOT); pe->SetEditCap(false); pe->SetHiliteCap(false); - pe->ReadText("help/win.txt"); + pe->ReadText(std::string("help/win.txt")); } else { @@ -4094,8 +4094,9 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) { OpString(line, "image", name); AddExt(name, ".png"); - if (strstr(name, "%user%") != 0) - CopyFileToTemp(name); + if (strstr(name, "%user%") != 0) { + GetProfile().CopyFileToTemp(std::string(name)); + } m_terrain->AddMaterial(OpInt(line, "id", 0), name, diff --git a/src/script/script.cpp b/src/script/script.cpp index e8926d2..a62866d 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -3900,7 +3900,7 @@ bool CScript::WriteScript(const char* filename) edit->SetMaxChar(Ui::EDITSTUDIOMAX); edit->SetAutoIndent(m_engine->GetEditIndentMode()); edit->SetText(m_script); - edit->WriteText(name.c_str()); + edit->WriteText(name); m_interface->DeleteControl(EVENT_EDIT9); return true; } diff --git a/src/ui/displayinfo.cpp b/src/ui/displayinfo.cpp index 9a31956..5c41f65 100644 --- a/src/ui/displayinfo.cpp +++ b/src/ui/displayinfo.cpp @@ -671,7 +671,7 @@ void CDisplayInfo::ChangeIndexButton(int index) { filename = m_main->GetDisplayInfoName(m_index); edit->ReadText(filename); - edit->HyperHome(filename); + edit->HyperHome(std::string(filename)); SetPosition(m_main->GetDisplayInfoPosition(m_index)); } diff --git a/src/ui/edit.cpp b/src/ui/edit.cpp index c0b6446..a96d231 100644 --- a/src/ui/edit.cpp +++ b/src/ui/edit.cpp @@ -560,7 +560,7 @@ bool CEdit::IsLinkPos(Math::Point pos) if ( i == -1 ) return false; if ( i >= m_len ) return false; - if ( m_format.size() > i && ((m_format[i] & Gfx::FONT_MASK_HIGHLIGHT) == Gfx::FONT_HIGHLIGHT_LINK)) return true; // TODO + if ( m_format.size() > static_cast(i) && ((m_format[i] & Gfx::FONT_MASK_HIGHLIGHT) == Gfx::FONT_HIGHLIGHT_LINK)) return true; // TODO return false; } @@ -760,7 +760,7 @@ void CEdit::HyperFlush() // Indicates which is the home page. -void CEdit::HyperHome(const char *filename) +void CEdit::HyperHome(std::string filename) { HyperFlush(); HyperAdd(filename, 0); @@ -768,10 +768,10 @@ void CEdit::HyperHome(const char *filename) // Performs a hyper jump through a link. -void CEdit::HyperJump(const char *name, const char *marker) +void CEdit::HyperJump(std::string name, std::string marker) { - char filename[100]; - char sMarker[100]; + std::string filename; + std:: string sMarker; int i, line, pos; if ( m_historyCurrent >= 0 ) @@ -779,18 +779,17 @@ void CEdit::HyperJump(const char *name, const char *marker) m_history[m_historyCurrent].firstLine = m_lineFirst; } - strcpy(sMarker, marker); + sMarker = marker; //? sprintf(filename, "help\\%s.txt", name); - if ( name[0] == '%' ) - { - UserDir(filename, name, ""); - strcat(filename, ".txt"); - } - else - { - sprintf(filename, "help\\%s.txt", name); + + if ( name[0] == '%' ) { + filename = GetProfile().GetUserBasedPath(name, "") + ".txt"; + } else { + std::string path = CApplication::GetInstancePointer()->GetDataDirPath() + "/help/" + name + ".txt"; + filename = fs::path(path).generic_string(); } + if ( ReadText(filename) ) { Justif(); @@ -798,7 +797,7 @@ void CEdit::HyperJump(const char *name, const char *marker) line = 0; for ( i=0 ; i= EDITHISTORYMAX-1 ) return false; m_historyCurrent ++; - strcpy(m_history[m_historyCurrent].filename, filename); + m_history[m_historyCurrent].filename = filename; m_history[m_historyCurrent].firstLine = firstLine; m_historyTotal = m_historyCurrent+1; @@ -1136,16 +1135,14 @@ void CEdit::Draw() // Draw an image part. -void CEdit::DrawImage(Math::Point pos, const char *name, float width, +void CEdit::DrawImage(Math::Point pos, std::string name, float width, float offset, float height, int nbLine) { - Math::Point uv1, uv2, dim; - float dp; - char filename[100]; + Math::Point uv1, uv2, dim; + float dp; + std::string filename; -//? sprintf(filename, "diagram\\%s.png", name); - UserDir(filename, name, "diagram"); - strcat(filename, ".png"); + filename = GetProfile().GetUserBasedPath(name, "diagram") + ".png"; m_engine->SetTexture(filename); m_engine->SetState(Gfx::ENG_RSTATE_NORMAL); @@ -1389,77 +1386,64 @@ int CEdit::GetTextLength() // Returns a name in a command. // \x nom1 nom2 nom3; -void GetNameParam(const char *cmd, int rank, char *buffer) +std::string GetNameParam(std::string cmd, int rank) { - int i; - - for ( i=0 ; i results; + boost::split(results, cmd, boost::is_any_of(" ;")); + + if (results.size() > static_cast(rank)) { + return results.at(rank); } - while ( *cmd != ' ' && *cmd != ';' ) - { - *buffer++ = *cmd++; - } - *buffer = 0; + return ""; } // Returns a number of a command. // \x nom n1 n2; -int GetValueParam(const char *cmd, int rank) +int GetValueParam(std::string cmd, int rank) { - int n, i; - - for ( i=0 ; i results; + boost::split(results, cmd, boost::is_any_of(" ;")); + int return_value = 0; + + if (results.size() > static_cast(rank)) { + try { + return_value = std::stoi(results.at(rank)); + } catch (std::invalid_argument &e) { + GetLogger()->Error("Exception std::invalid_argument caught in GetValueParam function"); + } catch (std::out_of_range &e) { + GetLogger()->Error("Exception std::out_of_range caught in GetValueParam function"); } - if ( *cmd != ';' ) cmd ++; } - sscanf(cmd, "%d", &n); - return n; + return return_value; } // Frees all images. void CEdit::FreeImage() { - char filename[100]; - int i; + std::string filename; - for ( i=0 ; iDeleteTexture(filename); } } // Reads the texture of an image. -void CEdit::LoadImage(const char *name) +void CEdit::LoadImage(std::string name) { - char filename[100]; - -//? sprintf(filename, "diagram\\%s.png", name); - UserDir(filename, name, "diagram"); - strcat(filename, ".png"); + std::string filename; + filename = GetProfile().GetUserBasedPath(name, "diagram") + ".png"; m_engine->LoadTexture(filename); } // Read from a text file. -bool CEdit::ReadText(const char *filename, int addSize) +bool CEdit::ReadText(std::string filename, int addSize) { FILE *file = NULL; char *buffer; @@ -1471,7 +1455,7 @@ bool CEdit::ReadText(const char *filename, int addSize) bool bInSoluce, bBOL; if ( filename[0] == 0 ) return false; - file = fopen(filename, "rb"); + file = fopen(filename.c_str(), "rb"); if ( file == NULL ) return false; fseek(file, 0, SEEK_END); @@ -1605,8 +1589,8 @@ bool CEdit::ReadText(const char *filename, int addSize) { if ( iLink < EDITLINKMAX ) { - GetNameParam(buffer+i+3, 0, m_link[iLink].name); - GetNameParam(buffer+i+3, 1, m_link[iLink].marker); + m_link[iLink].name = GetNameParam(buffer+i+3, 0); + m_link[iLink].marker = GetNameParam(buffer+i+3, 1); iLink ++; } font &= ~Gfx::FONT_MASK_HIGHLIGHT; @@ -1622,7 +1606,7 @@ bool CEdit::ReadText(const char *filename, int addSize) { if ( m_markerTotal < EDITLINKMAX ) { - GetNameParam(buffer+i+3, 0, m_marker[m_markerTotal].name); + m_marker[m_markerTotal].name = GetNameParam(buffer+i+3, 0); m_marker[m_markerTotal].pos = j; m_markerTotal ++; } @@ -1640,21 +1624,19 @@ bool CEdit::ReadText(const char *filename, int addSize) { if ( m_bSoluce || !bInSoluce ) { -#if _DEMO - strcpy(iName, "demo"); -#else - GetNameParam(buffer+i+7, 0, iName); -#endif + + strcpy(iName, GetNameParam(buffer+i+7, 0).c_str()); + //? iWidth = m_lineHeight*RetValueParam(buffer+i+7, 1); iWidth = static_cast(GetValueParam(buffer+i+7, 1)); iWidth *= m_engine->GetText()->GetHeight(Gfx::FONT_COLOBOT, Gfx::FONT_SIZE_SMALL); iLines = GetValueParam(buffer+i+7, 2); - LoadImage(iName); + LoadImage(std::string(iName)); // A part of image per line of text. for ( iCount=0 ; iCount(iCount/iLines); m_image[iIndex].height = 1.0f/iLines; m_image[iIndex].width = iWidth*0.75f; @@ -1888,7 +1870,7 @@ bool CEdit::ReadText(const char *filename, int addSize) // Writes all the text in a file. -bool CEdit::WriteText(const char *filename) +bool CEdit::WriteText(std::string filename) { FILE* file; char buffer[1000+20]; @@ -1896,7 +1878,7 @@ bool CEdit::WriteText(const char *filename) float iDim; if ( filename[0] == 0 ) return false; - file = fopen(filename, "wb"); + file = fopen(filename.c_str(), "wb"); if ( file == NULL ) return false; if ( m_bAutoIndent ) diff --git a/src/ui/edit.h b/src/ui/edit.h index 8f46445..72423fa 100644 --- a/src/ui/edit.h +++ b/src/ui/edit.h @@ -35,7 +35,12 @@ #include "common/restext.h" #include +#include +#include +#include + +namespace fs = boost::filesystem; namespace Ui { @@ -84,7 +89,7 @@ enum OperUndo struct ImageLine { //! name of the image (without diagram \) - char name[40]; + std::string name; //! vertical offset (v texture) float offset; //! height of the part (dv texture) @@ -96,15 +101,15 @@ struct ImageLine struct HyperLink { //! text file name (without help \) - char name[40]; + std::string name; //! name of the marker - char marker[20]; + std::string marker; }; struct HyperMarker { //! name of the marker - char name[20]; + std::string name; //! position in the text int pos; }; @@ -112,7 +117,7 @@ struct HyperMarker struct HyperHistory { //! full file name text - char filename[50]; + std::string filename; //! rank of the first displayed line int firstLine; }; @@ -140,8 +145,8 @@ public: char* GetText(); int GetTextLength(); - bool ReadText(const char *filename, int addSize=0); - bool WriteText(const char *filename); + bool ReadText(std::string filename, int addSize=0); + bool WriteText(std::string filename); void SetMaxChar(int max); int GetMaxChar(); @@ -183,7 +188,7 @@ public: bool Undo(); void HyperFlush(); - void HyperHome(const char *filename); + void HyperHome(std::string filename); bool HyperTest(EventType event); bool HyperGo(EventType event); @@ -202,15 +207,15 @@ protected: int MouseDetect(Math::Point mouse); void MoveAdjust(); - void HyperJump(const char *name, const char *marker); - bool HyperAdd(const char *filename, int firstLine); + void HyperJump(std::string name, std::string marker); + bool HyperAdd(std::string filename, int firstLine); - void DrawImage(Math::Point pos, const char *name, float width, float offset, float height, int nbLine); + void DrawImage(Math::Point pos, std::string name, float width, float offset, float height, int nbLine); void DrawBack(Math::Point pos, Math::Point dim); void DrawPart(Math::Point pos, Math::Point dim, int icon); void FreeImage(); - void LoadImage(const char *name); + void LoadImage(std::string name); void Scroll(int pos, bool bAdjustCursor); void Scroll(); void MoveChar(int move, bool bWord, bool bSelect); diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp index a0b0e06..51d2dda 100644 --- a/src/ui/maindialog.cpp +++ b/src/ui/maindialog.cpp @@ -3559,11 +3559,11 @@ void CMainDialog::SetUserDir(char *base, int rank) if ( strcmp(base, "user") == 0 && rank >= 100 ) { dir = m_userDir + "/" + m_userList.at(rank/100-1); - UserDir(true, dir.c_str()); + GetProfile().SetUserDir(dir); } else { - UserDir(false, ""); + GetProfile().SetUserDir(""); } } @@ -4258,7 +4258,6 @@ bool CMainDialog::IsIOReadScene() FILE* file; std::string filename; - //TODO: Change this to point user dir acocrding to operating system filename = m_savegameDir + "/" + m_main->GetGamerName() + "/" + "save" + m_sceneName[0] + "000/data.sav"; file = fopen(filename.c_str(), "r"); if ( file == NULL ) return false; @@ -4354,7 +4353,7 @@ void CMainDialog::IOReadList() filename = m_savegameDir + "/" + m_main->GetGamerName() + "/save" + m_sceneName[0] + rankStream.str()+ "/data.sav"; // sprintf(filename, "%s\\%s\\save%c%.3d\\data.sav", m_savegameDir, m_main->GetGamerName(), m_sceneName[0], j); - file = fopen(filename.c_str(), "r"); + file = fopen(fs::path(filename).native().c_str(), "r"); if ( file == NULL ) break; while ( fgets(line, 500, file) != NULL ) @@ -5046,8 +5045,8 @@ void CMainDialog::UpdateDisplayDevice() CWindow* pw; CList* pl; char bufDevices[1000]; - char bufModes[5000]; - int i, j, totalDevices, selectDevices, totalModes, selectModes; + //char bufModes[5000]; + int i, j; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); if ( pw == 0 ) return; @@ -5055,7 +5054,7 @@ void CMainDialog::UpdateDisplayDevice() if ( pl == 0 ) return; pl->Flush(); - bufModes[0] = 0; + //bufModes[0] = 0; /* TODO: remove device choice m_engine->EnumDevices(bufDevices, 1000, bufModes, 5000, @@ -5070,10 +5069,10 @@ void CMainDialog::UpdateDisplayDevice() while ( bufDevices[i++] != 0 ); } - pl->SetSelect(selectDevices); + pl->SetSelect(0); pl->ShowSelect(false); - m_setupSelDevice = selectDevices; + m_setupSelDevice = 0; } // Updates the list of modes. @@ -5110,8 +5109,8 @@ void CMainDialog::ChangeDisplay() CWindow* pw; CList* pl; CCheck* pc; - char* device; - char* mode; + //char* device; + //char* mode; bool bFull; pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); @@ -5120,12 +5119,12 @@ void CMainDialog::ChangeDisplay() pl = static_cast(pw->SearchControl(EVENT_LIST1)); if ( pl == 0 ) return; m_setupSelDevice = pl->GetSelect(); - device = pl->GetName(m_setupSelDevice); + //device = pl->GetName(m_setupSelDevice); pl = static_cast(pw->SearchControl(EVENT_LIST2)); if ( pl == 0 ) return; m_setupSelMode = pl->GetSelect(); - mode = pl->GetName(m_setupSelMode); + //mode = pl->GetName(m_setupSelMode); pc = static_cast(pw->SearchControl(EVENT_INTERFACE_FULL)); if ( pc == 0 ) return; diff --git a/src/ui/maindialog.h b/src/ui/maindialog.h index be61299..a79b95e 100644 --- a/src/ui/maindialog.h +++ b/src/ui/maindialog.h @@ -23,6 +23,12 @@ #include "object/robotmain.h" +#include +#include + +namespace fs = boost::filesystem; + + class CEventQueue; class CSoundInterface; diff --git a/src/ui/studio.cpp b/src/ui/studio.cpp index bf2ff33..a056026 100644 --- a/src/ui/studio.cpp +++ b/src/ui/studio.cpp @@ -1477,8 +1477,7 @@ void CStudio::UpdateDialogPublic() CCheck* pc; CLabel* pl; char name[100]; - char dir[MAX_FNAME]; - char text[MAX_FNAME+100]; + //char text[MAX_FNAME+100]; pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW9)); if ( pw == nullptr ) return; @@ -1499,9 +1498,7 @@ void CStudio::UpdateDialogPublic() if ( pl != 0 ) { GetResource(RES_TEXT, RT_IO_LIST, name); - SearchDirectory(dir, false); - sprintf(text, name, dir); - pl->SetName(text, false); + pl->SetName(SearchDirectory(false).c_str(), false); } } @@ -1572,21 +1569,22 @@ void CStudio::UpdateDialogList() // Constructs the name of the folder or open/save. // If the folder does not exist, it will be created. -void CStudio::SearchDirectory(char *dir, bool bCreate) +std::string CStudio::SearchDirectory(bool bCreate) { - if ( m_main->GetIOPublic() ) - { - sprintf(dir, "%s\\", m_main->GetPublicDir()); + char dir[MAX_FNAME]; + if ( m_main->GetIOPublic() ) { + sprintf(dir, "%s/", m_main->GetPublicDir()); + } else { + sprintf(dir, "%s/%s/Program/", m_main->GetSavegameDir(), m_main->GetGamerName()); } - else - { - sprintf(dir, "%s\\%s\\Program\\", m_main->GetSavegameDir(), m_main->GetGamerName()); + + fs::path path = fs::path(dir); + + if ( bCreate ) { + fs::create_directory(path); } - if ( bCreate ) - {// TODO -// mkdir(dir,0777); // if does not exist yet! - } + return path.make_preferred().string(); } // Reads a new program. @@ -1612,7 +1610,7 @@ bool CStudio::ReadProgram() { strcat(filename, ".txt"); } - SearchDirectory(dir, true); + strcpy(dir, SearchDirectory(true).c_str()); strcat(dir, filename); pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); @@ -1650,7 +1648,7 @@ bool CStudio::WriteProgram() { strcat(filename, ".txt"); } - SearchDirectory(dir, true); + strcpy(dir, SearchDirectory(true).c_str()); strcat(dir, filename); pw = static_cast< CWindow* >(m_interface->SearchControl(EVENT_WINDOW3)); @@ -1658,7 +1656,7 @@ bool CStudio::WriteProgram() pe = static_cast< CEdit* >(pw->SearchControl(EVENT_STUDIO_EDIT)); if ( pe == nullptr ) return false; - if ( !pe->WriteText(dir) ) return false; + if ( !pe->WriteText(std::string(dir)) ) return false; m_script->SetFilename(filename); return true; diff --git a/src/ui/studio.h b/src/ui/studio.h index 7c2f652..1c14124 100644 --- a/src/ui/studio.h +++ b/src/ui/studio.h @@ -22,6 +22,9 @@ #include "graphics/engine/camera.h" +#include + +namespace fs = boost::filesystem; #include @@ -81,7 +84,7 @@ protected: void UpdateDialogAction(); void UpdateDialogPublic(); void UpdateDialogList(); - void SearchDirectory(char* dir, bool bCreate); + std::string SearchDirectory(bool bCreate); bool ReadProgram(); bool WriteProgram(); -- cgit v1.2.3-1-g7c22