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,41 +58,32 @@ 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..
std::string tempText;
FILE *file = nlfopen (tempFilename.c_str(), "r");
if (file)
{ {
// Open the file.. // Read the new file
std::string tempText; char buffer[513];
FILE *file = fopen (tempFilename.c_str(), "r"); int red;
if (file) while (red=fread (buffer, 1, 512, file))
{ {
// Read the new file buffer[red] = 0;
char buffer[513]; tempText += buffer;
int red;
while (red=fread (buffer, 1, 512, file))
{
buffer[red] = 0;
tempText += buffer;
}
fclose (file);
// Return the text
text = tempText;
status = true;
} }
fclose (file);
// Return the text
text = tempText;
status = true;
} }
} }
} }