diff --git a/Source/nlf.cpp b/Source/nlf.cpp new file mode 100644 index 00000000..ced2da99 --- /dev/null +++ b/Source/nlf.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include "nlf.h" + +char SkipComments(FILE *f) { + char c; + while (c = fgetc(f)) + if (c == '#' || c == ';') { + while (c = fgetc(f)) + if (c == '\n') break; + } + else break; + return c; +} + +// NSIS Language File parser +NLF::NLF(char *filename) { + FILE *f = fopen(filename, "r"); + if (!f) throw runtime_error("Can't open language file!"); + + char buf[1024]; + buf[0] = SkipComments(f); + fgets(buf+1, 1024, f); + + // Check header + if (strncmp(buf, "NLF v", 5)) throw runtime_error("Invalid language file!"); + if (atoi(buf+5) != NLF_VERSION) throw runtime_error("Language file version doesn't match NSIS version!"); + + // Get language ID + buf[0] = SkipComments(f); + fgets(buf+1, 1024, f); + m_wLangId = atoi(buf); + + // Read strings + for (int i = 0; i < NLF_STRINGS; i++) { + buf[0] = SkipComments(f); + fgets(buf+1, 1024, f); + if (lstrlen(buf) == 1023) { + wsprintf(buf, "String too long (string #%d)!", i); + throw runtime_error(buf); + } + m_szStrings[i] = new char[lstrlen(buf)+1]; + lstrcpy(m_szStrings[i], buf); + } + + fclose(f); +} + +NLF::~NLF() { + MessageBox(0, "destructor", "info", MB_OK); + for (int i = 0; i < NLF_STRINGS; i++) { + delete [] m_szStrings[i]; + } +} + +WORD NLF::GetLang() { + return m_wLangId; +} + +char* NLF::GetString(int idx) { + if (idx < 0 || idx >= NLF_STRINGS) return 0; + return m_szStrings[idx]; +} \ No newline at end of file diff --git a/Source/nlf.h b/Source/nlf.h index cee93f35..52753ae7 100644 --- a/Source/nlf.h +++ b/Source/nlf.h @@ -1,7 +1,8 @@ #ifndef ___NLF___H_____ #define ___NLF___H_____ -#include +#include +using namespace std; #define NLF_VERSION 1 #define NLF_STRINGS 57 @@ -64,62 +65,14 @@ #define NLF_RENAME 55 #define NLF_SKIPPED 56 -char SkipComments(FILE *f) { - char c; - while (c = fgetc(f)) - if (c == '#' || c == ';') { - while (c = fgetc(f)) - if (c == '\n') break; - } - else break; - return c; -} - -extern FILE *g_output; -#define s(x) fprintf(g_output, x"\n"); fflush(g_output); -#define s2(x,y) fprintf(g_output, x"\n", y); fflush(g_output); - // NSIS Language File parser class NLF { public: - NLF(char *filename) { - FILE *f = fopen(filename, "r"); - if (!f) throw runtime_error("Can't open language file!"); + NLF(char *filename); + ~NLF(); - char buf[1024]; - buf[0] = SkipComments(f); - fgets(buf+1, 1024, f); - - // Check header - if (strncmp(buf, "NLF v", 5)) throw runtime_error("Invalid language file!"); - if (atoi(buf+5) != NLF_VERSION) throw runtime_error("Language file version doesn't match NSIS version!"); - - // Get language ID - buf[0] = SkipComments(f); - fgets(buf+1, 1024, f); - m_wLangId = atoi(buf); - - // Read entries - int i = 0; - while (i < NLF_STRINGS) { - buf[0] = SkipComments(f); - fgets(buf+1, 1024, f); - if (lstrlen(buf) == 1023) { - wsprintf(buf, "String too long (string #%d)!", i); - throw runtime_error(buf); - } - //m_szStrings[i] = new char[lstrlen(buf)]; - m_szStrings[i] = (char*)GlobalAlloc(GPTR, lstrlen(buf)); - lstrcpy(m_szStrings[i], buf); - i++; - } - - for (i = 0; i < NLF_STRINGS; i++) { - GlobalFree(m_szStrings[i]); - } - - fclose(f); - } + WORD GetLang(); + char* GetString(int idx); private: WORD m_wLangId; diff --git a/Source/script.cpp b/Source/script.cpp index ae104821..90395ba0 100644 --- a/Source/script.cpp +++ b/Source/script.cpp @@ -1215,7 +1215,19 @@ int CEXEBuild::doCommand(int which_token, LineParser &line, FILE *fp, const char case TOK_LOADNLF: { SCRIPT_MSG("LoadLanguageFile: %s\n", line.gettoken_str(1)); - NLF *lang = new NLF(line.gettoken_str(1)); + try { + NLF *newLang = new NLF(line.gettoken_str(1)); + for (int i = 0; i < build_langs.size(); i++) + if (build_langs[i]->GetLang() == newLang->GetLang()) { + ERROR_MSG("Error: Can't add same language twice!\n"); + return PS_ERROR; + } + build_langs.push_back(newLang); + } + catch (exception &err) { + ERROR_MSG("Error while adding language file: %s", err.what()); + return PS_ERROR; + } } return make_sure_not_in_secorfunc(line.gettoken_str(0));