Changed: NeL helpers to launch an external editor

This commit is contained in:
kervala 2016-12-10 13:02:26 +01:00
parent a184dd7d8f
commit 08071b75dd

View file

@ -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;
}
}
}