Changed: NeL helpers to launch an external editor

--HG--
branch : develop
This commit is contained in:
kervala 2016-12-10 13:02:26 +01:00
parent 5658cad02e
commit 5dfa19518a

View file

@ -34,19 +34,23 @@ bool EditExternalText (const std::string &editor, std::string &text, const std::
{ {
bool status = false; bool status = false;
// Create a temporary file // Create a temporary file
char dir[512]; std::string dir = CPath::getCurrentPath();
if (getcwd (dir, 512))
if (!dir.empty())
{ {
// Build a temporary filename // Build a temporary filename
string tempFilename; string tempFilename;
uint i = 0; uint i = 0;
do do
{
tempFilename = string(dir) + "/~tmp" + toString(i++) + "." + ext; tempFilename = string(dir) + "/~tmp" + toString(i++) + "." + ext;
}
while (NLMISC::CFile::isExists(tempFilename)); while (NLMISC::CFile::isExists(tempFilename));
// Fill the temp file // Fill the temp file
bool saved = false; bool saved = false;
FILE *file = fopen (tempFilename.c_str(), "w"); FILE *file = nlfopen (tempFilename, "w");
if (file) if (file)
{ {
saved = fputs (text.c_str(), file) != EOF; saved = fputs (text.c_str(), file) != EOF;
@ -54,25 +58,16 @@ bool EditExternalText (const std::string &editor, std::string &text, const std::
} }
// Hide the file // 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 // Open the temp file with a text editor
if (saved) if (saved)
{ {
STARTUPINFO si; if (launchProgramAndWaitForResult(editor, tempFilename))
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 (WaitForSingleObject (pi.hProcess, INFINITE) == WAIT_OBJECT_0)
{ {
// Open the file.. // Open the file..
std::string tempText; std::string tempText;
FILE *file = fopen (tempFilename.c_str(), "r"); FILE *file = nlfopen (tempFilename.c_str(), "r");
if (file) if (file)
{ {
// Read the new file // Read the new file
@ -83,6 +78,7 @@ bool EditExternalText (const std::string &editor, std::string &text, const std::
buffer[red] = 0; buffer[red] = 0;
tempText += buffer; tempText += buffer;
} }
fclose (file); fclose (file);
// Return the text // Return the text
@ -91,7 +87,6 @@ bool EditExternalText (const std::string &editor, std::string &text, const std::
} }
} }
} }
}
// Delete the file // Delete the file
NLMISC::CFile::deleteFile (tempFilename); NLMISC::CFile::deleteFile (tempFilename);