Back
1 //------------------------------------------------------------------------------
2 // Module: Languages.cpp //
3 // //
4 // Class which encapsulates a programming language's definition //
5 // //
6 // Copyright (c) 2000-2004 by Lars Haendel //
7 // Home: http://www.newty.de //
8 // //
9 // This program is free software and can be used under the terms of the //
10 // GNU licence. See header-file for further information and disclaimer. //
11 // //
12 //------------------------------------------------------------------------------
13
14 #include <iomanip> // due to: setw()
15 #include <limits> // INT_MAX
16
17
18 #include "Languages.h"
19 #include "ErrText.h" // TErrText
20 #include "FileUtil.h" // file utilities
21 #include "NameUtil.h" // GetFileName()
22
23 #define MODULE "TLanguages"
24 #define VERSION "v1.0b"
25
26
27
28 //----------------------------------------------------------------------------------------------------------------------
29 // release memory
30 void TLangs::Release()
31 {
32 DeleteEntries(&langs); // delete langauge list entries
33 langs.Clear(); // clear list
34 }
35
36
37 //----------------------------------------------------------------------------------------------------------------------
38 // load langauges definitions from file
39 void TLangs::Load(ifstream& file)
40 {
41 Release(); // release languages
42
43 char szSection[256];
44 try
45 {
46 // read language definitions
47 while(true)
48 {
49 sprintf(szSection, "[Language%d]",langs.Size()+1); // compose section key
50 if(SearchKey(file, szSection, INT_MAX, EOF, /*enable exceptions*/ false)<0) // if section does not exist -> break
51 break;
52 TLanguage*& lang = langs.Ins(); // insert new list element
53 lang = new TLanguage(); // new it
54 lang->Load(file); // and load definition
55 }
56 }
57 catch(TErrText err)
58 {
59 char szText[256]; // re-compose error text
60 sprintf(szText, "Section '%s': %s", szSection, err.szErrText);
61
62 langs.Del(); // delete last list element again
63
64 throw TErrText(szText);
65 }
66 }
67
68
69 //----------------------------------------------------------------------------------------------------------------------
70 // save language definitions to file
71 void TLangs::Save(ofstream& file) const
72 {
73 // write header
74 file << "# Language definitions for " << SZ_PRGNAME << " " << SZ_VERSION;
75 file << " written by " << MODULE << " " << VERSION << endl;
76
77
78 // write all language definitions
79 for(int i=0;i<langs.Size();i++)
80 {
81 file << endl << "#---------------------------------------------------------------------------------------------";
82 file << endl << "[Language" << i+1 << "]" << endl; // section name
83 langs.Get(i)->Save(file); // langauge definition
84 }
85 }
86
87
88 //----------------------------------------------------------------------------------------------------------------------
89 // get i'th language definition
90 const TLanguage* TLangs::Get(const int& i)
91 {
92 if(i<0 || i>=langs.Size())
93 return NULL;
94 else
95 return langs.Get(i);
96 }
97
98
99 //----------------------------------------------------------------------------------------------------------------------
100 // get language definition id by name
101 int TLangs::GetId(const char*const& szName)
102 {
103 for(int i=0;i<langs.Size();i++) // for all languages in list
104 if(strcmp(langs.Get(i)->Name(), szName)==0) // check if the name matches ...
105 return i; // ... and if yes return pointer to language
106
107 return -1; // name not found, return -1
108 }
Top |