From 08071b75ddc4e94a3b9e3c43174644c99ae12237 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 10 Dec 2016 13:02:26 +0100 Subject: [PATCH] Changed: NeL helpers to launch an external editor --- .../world_editor/external_editor.cpp | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/code/ryzom/tools/leveldesign/world_editor/world_editor/external_editor.cpp b/code/ryzom/tools/leveldesign/world_editor/world_editor/external_editor.cpp index e17165349..8e20608e6 100644 --- a/code/ryzom/tools/leveldesign/world_editor/world_editor/external_editor.cpp +++ b/code/ryzom/tools/leveldesign/world_editor/world_editor/external_editor.cpp @@ -34,19 +34,23 @@ bool EditExternalText (const std::string &editor, std::string &text, const std:: { bool status = false; // Create a temporary file - char dir[512]; - if (getcwd (dir, 512)) + std::string dir = CPath::getCurrentPath(); + + if (!dir.empty()) { // Build a temporary filename string tempFilename; uint i = 0; + do - tempFilename = string(dir)+"/~tmp"+toString (i++)+"."+ext; + { + tempFilename = string(dir) + "/~tmp" + toString(i++) + "." + ext; + } while (NLMISC::CFile::isExists(tempFilename)); // Fill the temp file bool saved = false; - FILE *file = fopen (tempFilename.c_str(), "w"); + FILE *file = nlfopen (tempFilename, "w"); if (file) { saved = fputs (text.c_str(), file) != EOF; @@ -54,41 +58,32 @@ bool EditExternalText (const std::string &editor, std::string &text, const std:: } // Hide the file - SetFileAttributes (tempFilename.c_str(), FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM); + SetFileAttributes (utf8ToTStr(tempFilename), FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM); // Open the temp file with a text editor if (saved) { - STARTUPINFO si; - PROCESS_INFORMATION pi; - memset(&si, 0, sizeof(si)); - memset(&pi, 0, sizeof(pi)); - si.cb = sizeof(si); - char cmdLine[1024]; - strncpy (cmdLine, ("\""+editor+"\" \""+tempFilename+"\"").c_str(), sizeof(cmdLine)-1); - if (CreateProcess(editor.c_str (), cmdLine, NULL, NULL, FALSE, 0, NULL, dir, &si, &pi)) + if (launchProgramAndWaitForResult(editor, tempFilename)) { - if (WaitForSingleObject (pi.hProcess, INFINITE) == WAIT_OBJECT_0) + // Open the file.. + std::string tempText; + FILE *file = nlfopen (tempFilename.c_str(), "r"); + if (file) { - // Open the file.. - std::string tempText; - FILE *file = fopen (tempFilename.c_str(), "r"); - if (file) + // Read the new file + char buffer[513]; + int red; + while (red=fread (buffer, 1, 512, file)) { - // Read the new file - char buffer[513]; - int red; - while (red=fread (buffer, 1, 512, file)) - { - buffer[red] = 0; - tempText += buffer; - } - fclose (file); - - // Return the text - text = tempText; - status = true; + buffer[red] = 0; + tempText += buffer; } + + fclose (file); + + // Return the text + text = tempText; + status = true; } } }