Optimize source printing

This commit is contained in:
momo5502 2016-06-04 14:25:55 +02:00
parent 74033ad557
commit 843580f164
4 changed files with 33 additions and 47 deletions

View File

@ -422,7 +422,7 @@ namespace Components
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vsprintf_s(buffer, format, ap); _vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap); va_end(ap);
perror(buffer); perror(buffer);

View File

@ -78,7 +78,7 @@ namespace Components
va_list ap = reinterpret_cast<char*>(const_cast<char**>(&message[1])); va_list ap = reinterpret_cast<char*>(const_cast<char**>(&message[1]));
//va_start(ap, *message); //va_start(ap, *message);
vsprintf_s(buffer, *message, ap); _vsnprintf(buffer, sizeof(buffer), *message, ap);
va_end(ap); va_end(ap);
return buffer; return buffer;

View File

@ -18,7 +18,7 @@ namespace Components
Logger::Print(23, "Error: unknown function %s in %s\n", funcName.data(), Script::ScriptName.data()); Logger::Print(23, "Error: unknown function %s in %s\n", funcName.data(), Script::ScriptName.data());
Logger::Print(23, "************************************\n"); Logger::Print(23, "************************************\n");
Logger::Error(5, "script compile error\nunknown function %s\n%s\n", funcName.data(), Script::ScriptName.data()); Logger::Error(5, "script compile error\nunknown function %s\n%s\n\n", funcName.data(), Script::ScriptName.data());
} }
void __declspec(naked) Script::StoreFunctionNameStub() void __declspec(naked) Script::StoreFunctionNameStub()
@ -84,58 +84,49 @@ namespace Components
} }
} }
// TODO: Optimize that! void Script::PrintSourcePos(const char* filename, unsigned int offset)
void Script::PrintSourcePos(const char* filename, int offset)
{ {
int line = 0; FileSystem::File script(filename);
int inLineOffset = 0;
char* scriptFile = 0;
char* currentLine = 0;
bool freeScript = false;
if (Game::FS_ReadFile(filename, &scriptFile) > -1) if (script.Exists())
{ {
int globalOffset = 0; std::string buffer = script.GetBuffer();
Utils::Replace(buffer, "\t", " ");
freeScript = true; int line = 1;
int lineOffset = 0;
int inlineOffset = 0;
for (char* c = scriptFile; *c != '\0'; c++) for (unsigned int i = 0; i < buffer.size(); ++i)
{ {
if (!currentLine || *c == '\n') // Terminate line
if (i == offset)
{ {
line++; while (buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != '\0')
inLineOffset = 0;
currentLine = c;
}
if (globalOffset == offset)
{
while (*c != '\r' && *c != '\n' && c != '\0')
{ {
c++; ++i;
} }
*c = '\0'; buffer[i] = '\0';
break; break;
} }
if (*c == '\t') if (buffer[i] == '\n')
{ {
*c = ' '; ++line;
lineOffset = i; // Includes the line break!
inlineOffset = 0;
}
else
{
++inlineOffset;
} }
globalOffset++;
inLineOffset++;
} }
}
Logger::Print(23, "in file %s, line %d:", filename, line); Logger::Print(23, "in file %s, line %d:", filename, line);
Logger::Print(23, "%s\n", buffer.data() + lineOffset);
if (currentLine) for (int i = 0; i < (inlineOffset - 1); ++i)
{
Logger::Print(23, "%s\n", currentLine);
for (int i = 0; i < (inLineOffset - 1); i++)
{ {
Logger::Print(23, " "); Logger::Print(23, " ");
} }
@ -144,16 +135,11 @@ namespace Components
} }
else else
{ {
Logger::Print(23, "\n"); Logger::Print(23, "in file %s, offset %d\n", filename, offset);
}
if (freeScript)
{
Game::FS_FreeFile(scriptFile);
} }
} }
void Script::CompileError(int offset, const char* message, ...) void Script::CompileError(unsigned int offset, const char* message, ...)
{ {
char msgbuf[1024] = { 0 }; char msgbuf[1024] = { 0 };
va_list v; va_list v;
@ -167,7 +153,7 @@ namespace Components
Logger::Print(23, "******* script compile error *******\n"); Logger::Print(23, "******* script compile error *******\n");
Logger::Print(23, "Error: %s ", msgbuf); Logger::Print(23, "Error: %s ", msgbuf);
Script::PrintSourcePos(Script::ScriptName.data(), offset); Script::PrintSourcePos(Script::ScriptName.data(), offset);
Logger::Print(23, "************************************\n"); Logger::Print(23, "************************************\n\n");
Logger::Error(5, "script compile error\n%s\n%s\n(see console for actual details)\n", msgbuf, Script::ScriptName.data()); Logger::Error(5, "script compile error\n%s\n%s\n(see console for actual details)\n", msgbuf, Script::ScriptName.data());
} }

View File

@ -15,8 +15,8 @@ namespace Components
static std::vector<std::string> ScriptNameStack; static std::vector<std::string> ScriptNameStack;
static unsigned short FunctionName; static unsigned short FunctionName;
static void CompileError(int offset, const char* message, ...); static void CompileError(unsigned int offset, const char* message, ...);
static void PrintSourcePos(const char* filename, int offset); static void PrintSourcePos(const char* filename, unsigned int offset);
static void FunctionError(); static void FunctionError();
static void StoreFunctionNameStub(); static void StoreFunctionNameStub();