From 843580f1642d5f418101f74fd22b4dd81cd737b5 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 4 Jun 2016 14:25:55 +0200 Subject: [PATCH] Optimize source printing --- src/Components/Modules/Console.cpp | 2 +- src/Components/Modules/Logger.cpp | 2 +- src/Components/Modules/Script.cpp | 72 ++++++++++++------------------ src/Components/Modules/Script.hpp | 4 +- 4 files changed, 33 insertions(+), 47 deletions(-) diff --git a/src/Components/Modules/Console.cpp b/src/Components/Modules/Console.cpp index 79144283..1d22fbfb 100644 --- a/src/Components/Modules/Console.cpp +++ b/src/Components/Modules/Console.cpp @@ -422,7 +422,7 @@ namespace Components va_list ap; va_start(ap, format); - vsprintf_s(buffer, format, ap); + _vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); perror(buffer); diff --git a/src/Components/Modules/Logger.cpp b/src/Components/Modules/Logger.cpp index bff8bd84..070a4c46 100644 --- a/src/Components/Modules/Logger.cpp +++ b/src/Components/Modules/Logger.cpp @@ -78,7 +78,7 @@ namespace Components va_list ap = reinterpret_cast(const_cast(&message[1])); //va_start(ap, *message); - vsprintf_s(buffer, *message, ap); + _vsnprintf(buffer, sizeof(buffer), *message, ap); va_end(ap); return buffer; diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index 410fb0fc..2a736065 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -18,7 +18,7 @@ namespace Components Logger::Print(23, "Error: unknown function %s in %s\n", funcName.data(), Script::ScriptName.data()); 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() @@ -84,58 +84,49 @@ namespace Components } } - // TODO: Optimize that! - void Script::PrintSourcePos(const char* filename, int offset) + void Script::PrintSourcePos(const char* filename, unsigned int offset) { - int line = 0; - int inLineOffset = 0; - char* scriptFile = 0; - char* currentLine = 0; - bool freeScript = false; + FileSystem::File script(filename); - 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++; - inLineOffset = 0; - currentLine = c; - } - - if (globalOffset == offset) - { - while (*c != '\r' && *c != '\n' && c != '\0') + while (buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != '\0') { - c++; + ++i; } - *c = '\0'; + buffer[i] = '\0'; 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) - { - Logger::Print(23, "%s\n", currentLine); - - for (int i = 0; i < (inLineOffset - 1); i++) + for (int i = 0; i < (inlineOffset - 1); ++i) { Logger::Print(23, " "); } @@ -144,16 +135,11 @@ namespace Components } else { - Logger::Print(23, "\n"); - } - - if (freeScript) - { - Game::FS_FreeFile(scriptFile); + Logger::Print(23, "in file %s, offset %d\n", filename, offset); } } - void Script::CompileError(int offset, const char* message, ...) + void Script::CompileError(unsigned int offset, const char* message, ...) { char msgbuf[1024] = { 0 }; va_list v; @@ -167,7 +153,7 @@ namespace Components Logger::Print(23, "******* script compile error *******\n"); Logger::Print(23, "Error: %s ", msgbuf); 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()); } diff --git a/src/Components/Modules/Script.hpp b/src/Components/Modules/Script.hpp index 9efcfd4c..523ea200 100644 --- a/src/Components/Modules/Script.hpp +++ b/src/Components/Modules/Script.hpp @@ -15,8 +15,8 @@ namespace Components static std::vector ScriptNameStack; static unsigned short FunctionName; - static void CompileError(int offset, const char* message, ...); - static void PrintSourcePos(const char* filename, int offset); + static void CompileError(unsigned int offset, const char* message, ...); + static void PrintSourcePos(const char* filename, unsigned int offset); static void FunctionError(); static void StoreFunctionNameStub();