Back
1 //------------------------------------------------------------------------------
2 // Module Options.cpp //
3 // //
4 // Class TOptions which encapsulates several options. Options may be //
5 // saved/loaded to/from file. //
6 // //
7 // Copyright (c) 2000-2004 by Lars Haendel //
8 // Home: http://www.newty.de //
9 // //
10 // This program is free software and can be used under the terms of the //
11 // GNU licence. See header-file for further information and disclaimer. //
12 // //
13 //------------------------------------------------------------------------------
14
15
16 #include <iomanip> // setw()
17
18 #include "options.h"
19 #include "FileUtil.h" // skipws() ...
20 #include "defines.h" // defines like STS (string size)
21 #include "ErrText.h" // TErrText
22
23
24
25 //----------------------------------------------------------------------------------------------------------------------
26 // factory default options and their names for ini file
27
28
29 // section 'ExpertSettings'
30 #define SZ_EXPERT_SETTINGS "[ExpertSettings]" // section name
31 #define DEF_TAG_CLOSING_ON_ENDL true // close all tags when there are >= 'nEndl' linefeeds
32 #define DEF_N_ENDL 1 // see above
33 #define DEF_HTML_AND_BODY_TAGS true // enclose output in <html> and <body> tags
34 #define SZ_TAG_CLOSING_ON_ENDL "TagClosingOnEndl"
35 #define SZ_N_ENDL "nEndl"
36 #define SZ_HTML_AND_BODY_TAGS "HTMLandBodyTags"
37
38 // section 'Misc'
39 #define SZ_MISC "[Misc]" // section name
40 #define DEF_TABPOS 3 // default tab stop position
41 #define DEF_INDENT 0 // default left indent
42 #define DEF_ADD_BACK_LINK true // add back links to overview file
43 #define DEF_USER_DEFINED_BACK_LINK false // user defined back link text
44 #define DEF_ADD_TOP_LINKS true // add top links at bottom
45 #define DEF_ADD_LINE_NUMBERS false // add line numbers
46 #define DEF_ADD_LINE_ANCHORS false // add line anchors
47 #define SZ_TABPOS "Tab"
48 #define SZ_INDENT "Indent"
49 #define SZ_ADD_BACK_LINK "AddBackLink"
50 #define SZ_USER_DEFINED_BACK_LINK "UserDefinedBackLink"
51 #define SZ_ADD_TOP_LINKS "AddTopLinks"
52 #define SZ_ADD_LINE_NUMBERS "AddLineNumbers"
53 #define SZ_ADD_LINE_ANCHORS "AddLineAnchors"
54 #define SZ_BACK_LINK_TEXT "BackLinkText"
55
56
57 // section 'OverviewFile'
58 #define SZ_OVERVIEW_FILE "[OverviewFile]" // section name
59 #define DEF_GENERATE_OVERVIEW_FILE true // generate overview file
60 #define DEF_OVERVIEW_FILE_NAME "Overview.html" // default name that is used if none is specified
61 #define SZ_GENERATE_OVERVIEW_FILE "GenerateOverviewFile"
62 #define SZ_OVERVIEW_FILE_NAME "OverviewFileName" // name of generated overview file
63
64
65 #define WIDTH 22 // width used for option names on writing ini file
66 #define SZ_MODULE "TOptions"
67 #define SZ_MODULE_VERSION "v1.2b"
68
69
70 //----------------------------------------------------------------------------------------------------------------------
71 // constructor
72 TOptions::TOptions()
73 {
74 RestoreFactoryDefaults(); // initialize with defaults
75 }
76
77
78 //----------------------------------------------------------------------------------------------------------------------
79 // restore factory defaults settings
80 void TOptions::RestoreFactoryDefaults()
81 {
82 // section 'Expert'
83 f_TagClosingOnEndl = DEF_TAG_CLOSING_ON_ENDL; // close tabs if there are more than 'nEndl' linefeeds
84 nEndl = DEF_N_ENDL; // see above
85 f_HtmlAndBodyTags = DEF_HTML_AND_BODY_TAGS; // enclose output in <html> and <body> tags
86
87
88 // section 'Misc'
89 tabPos = DEF_TABPOS; // tab stop positions
90 indent = DEF_INDENT; // left indent
91 f_AddBackLink = DEF_ADD_BACK_LINK; // add back links
92 f_UserDefinedBackLink = DEF_USER_DEFINED_BACK_LINK; // user defined back link text
93 f_AddTopLinks = DEF_ADD_TOP_LINKS; // add top links at bottom
94 f_AddLineNumbers = DEF_ADD_LINE_NUMBERS; // add line numbers
95 f_AddLineAnchors = DEF_ADD_LINE_ANCHORS; // add line anchors
96
97 // section 'OverviewFile'
98 f_GenerateOverviewFile = DEF_GENERATE_OVERVIEW_FILE; // generate overview file
99 strcpy(szOverviewFileName, DEF_OVERVIEW_FILE_NAME); // overview filename
100 strcpy(szBackLinkText, DEF_OVERVIEW_FILE_NAME);
101 }
102
103
104 //----------------------------------------------------------------------------------------------------------------------
105 // restore defaults from given object
106 void TOptions::RestoreDefaults(const TOptions& defaults)
107 {
108 // section 'Expert'
109 f_TagClosingOnEndl = defaults.f_TagClosingOnEndl; // close all tags when there are >= 'endlNo' linefeeds
110 nEndl = defaults.nEndl; // see above
111 f_HtmlAndBodyTags = defaults.f_HtmlAndBodyTags; // enclose output in <html> and <body> tags
112
113
114 // section 'Misc'
115 tabPos = defaults.tabPos; // tab stop positions
116 indent = defaults.indent; // left indent
117 f_AddBackLink = defaults.f_AddBackLink; // add back links to overview file
118 f_UserDefinedBackLink = defaults.f_UserDefinedBackLink; // user defined back link text
119 f_AddTopLinks = defaults.f_AddTopLinks; // add top links at bottom
120 f_AddLineNumbers = defaults.f_AddLineNumbers; // add line numbers
121 f_AddLineAnchors = defaults.f_AddLineAnchors; // add line anchors
122
123 // section 'OverviewFile'
124 f_GenerateOverviewFile = defaults.f_GenerateOverviewFile; // generate overview file
125 strcpy(szOverviewFileName, defaults.szOverviewFileName); // overview filename
126 strcpy(szBackLinkText, defaults.szBackLinkText);
127 }
128
129
130 //----------------------------------------------------------------------------------------------------------------------
131 // Load options from file
132 void TOptions::Load(ifstream& file)
133 {
134 // read all sections of project file
135 char szSection[STS]; // name of section currently checked
136 try
137 {
138 //----------------------------------------------------------------------------------------------------------------
139 // a) read section '[ExpertSettings]'
140 streampos curPos = file.tellg(); // preserve actual stream position
141 strcpy(szSection, SZ_EXPERT_SETTINGS);
142 SearchKey(file, szSection); // position to section
143
144 f_TagClosingOnEndl = ReadKeyBool (file, SZ_TAG_CLOSING_ON_ENDL, DEF_TAG_CLOSING_ON_ENDL, SEARCH_LINES);
145 nEndl = ReadKeyValue(file, SZ_N_ENDL, DEF_N_ENDL, SEARCH_LINES);
146 f_HtmlAndBodyTags = ReadKeyBool (file, SZ_HTML_AND_BODY_TAGS, DEF_HTML_AND_BODY_TAGS, SEARCH_LINES);
147
148
149 //----------------------------------------------------------------------------------------------------------------
150 // b) read section '[Misc]'
151 file.seekg(curPos); // restore stream position
152 strcpy(szSection, SZ_MISC);
153 SearchKey(file, szSection); // position to section
154
155 tabPos = ReadKeyValue(file, SZ_TABPOS, DEF_TABPOS, SEARCH_LINES);
156 indent = ReadKeyValue(file, SZ_INDENT, DEF_INDENT, SEARCH_LINES);
157 f_AddBackLink = ReadKeyBool (file, SZ_ADD_BACK_LINK, DEF_ADD_BACK_LINK, SEARCH_LINES);
158 f_UserDefinedBackLink = ReadKeyBool (file, SZ_USER_DEFINED_BACK_LINK, DEF_USER_DEFINED_BACK_LINK, SEARCH_LINES);
159 f_AddTopLinks = ReadKeyBool (file, SZ_ADD_TOP_LINKS, DEF_ADD_TOP_LINKS, SEARCH_LINES);
160 f_AddLineNumbers = ReadKeyBool (file, SZ_ADD_LINE_NUMBERS, DEF_ADD_LINE_NUMBERS, SEARCH_LINES);
161 f_AddLineAnchors = ReadKeyBool (file, SZ_ADD_LINE_ANCHORS, DEF_ADD_LINE_ANCHORS, SEARCH_LINES);
162 char szBuffer[STS];
163 ReadKeyString(file, SZ_BACK_LINK_TEXT, szBuffer, STS, SEARCH_LINES); // read text to buffer
164 if(szBuffer[0]!='\0')
165 strcpy(szBackLinkText, szBuffer); // and copy if non-empty, else the default value will still be valid
166
167
168 //----------------------------------------------------------------------------------------------------------------
169 // c) read section '[OverviewFile]'
170 file.seekg(curPos); // restore stream position
171 strcpy(szSection, SZ_OVERVIEW_FILE);
172 SearchKey(file, szSection); // position to section
173
174 f_GenerateOverviewFile = ReadKeyBool (file, SZ_GENERATE_OVERVIEW_FILE, DEF_GENERATE_OVERVIEW_FILE, SEARCH_LINES);
175 ReadKeyString(file, SZ_OVERVIEW_FILE_NAME, szBuffer, STS, SEARCH_LINES); // read filename to buffer
176 if(szBuffer[0]!='\0')
177 strcpy(szOverviewFileName, szBuffer); // and copy if non-empty, else the default value will still be valid
178
179 file.seekg(curPos); // restore stream position
180 }
181 catch(int errNo) // exception handling
182 {
183 char szText[STS];
184
185 // compose error text
186 if(strcmp(szSection, GetLastKey())==0)
187 sprintf(szText, "Section '%s' not found!", szSection);
188 else
189 sprintf(szText, "Section '%s' reading key '%s': %s", szSection, GetLastKey(), GetLastError(errNo));
190
191 throw TErrText(szText); // propagate exception
192 }
193 }
194
195
196 //----------------------------------------------------------------------------------------------------------------------
197 // Save options to file
198 void TOptions::Save(ofstream& file)
199 {
200 // write header
201 file << "# Options/Project file for " << SZ_PRGNAME << " " << SZ_VERSION;
202 file << " written by " << SZ_MODULE << " " << SZ_MODULE_VERSION << endl << endl;
203
204 // left justified output
205 file << setiosflags(ios::left) << resetiosflags(ios::right);
206
207
208 // a) write section '[ExpertSettings]'
209 file << SZ_EXPERT_SETTINGS << endl;
210 file << setw(WIDTH) << SZ_TAG_CLOSING_ON_ENDL << " = " << FlagToString(f_TagClosingOnEndl, true);
211 file << " # close HTML tags if there are 'nEndl' or more successive linefeeds" << endl;
212
213 file << setw(WIDTH) << SZ_N_ENDL << " = " << setw(3) << nEndl << " # see above" << endl;
214
215 file << setw(WIDTH) << SZ_HTML_AND_BODY_TAGS << " = " << FlagToString(f_HtmlAndBodyTags, true);
216 file << " # enclose output in <html> and <body> tags" << endl;
217 file << endl << endl;
218
219
220 // b) write section '[Misc]'
221 file << SZ_MISC << endl;
222
223 // tab stop positions
224 file << setw(WIDTH) << SZ_TABPOS << " = " << setw(3) << tabPos << " # tab stop positions" << endl;
225
226 // indent
227 file << setw(WIDTH) << SZ_INDENT << " = " << setw(3) << indent;
228 file << " # number of spaces to be added at the begin of each none empty line" << endl;
229
230 // flag back links
231 file << setw(WIDTH) << SZ_ADD_BACK_LINK << " = " << FlagToString(f_AddBackLink, true);
232 file << " # add back link to overview file" << endl;
233
234 // flag user defined back link text
235 file << setw(WIDTH) << SZ_USER_DEFINED_BACK_LINK << " = " << FlagToString(f_UserDefinedBackLink, true);
236 file << " # user defined back link text" << endl;
237
238
239 // flag top links
240 file << setw(WIDTH) << SZ_ADD_TOP_LINKS << " = " << FlagToString(f_AddTopLinks, true);
241 file << " # add top link at bottom" << endl;
242
243 // flag line numbers
244 file << setw(WIDTH) << SZ_ADD_LINE_NUMBERS << " = " << FlagToString(f_AddLineNumbers, true);
245 file << " # add line numbers" << endl;
246
247 // flag line anchors
248 file << setw(WIDTH) << SZ_ADD_LINE_ANCHORS << " = " << FlagToString(f_AddLineAnchors, true);
249 file << " # add line numbers" << endl;
250
251
252 // back link text
253 file << setw(WIDTH) << SZ_BACK_LINK_TEXT << " = " << "\"" << szBackLinkText << "\"" << endl;
254 file << endl << endl;
255
256
257 // c) write section '[OverviewFile]'
258 file << SZ_OVERVIEW_FILE << endl;
259
260 file << setw(WIDTH) << SZ_GENERATE_OVERVIEW_FILE << " = " << FlagToString(f_GenerateOverviewFile, true);
261 file << " # generate overview file" << endl;
262
263 file << setw(WIDTH) << SZ_OVERVIEW_FILE_NAME << " = " << "\"" << szOverviewFileName << "\"" << endl;
264
265
266 // restore right justified output
267 file << resetiosflags(ios::left) << setiosflags(ios::right);
268 }
Top |