Back
1 //------------------------------------------------------------------------------
2 // Module Project.cpp //
3 // //
4 // Class TProject is derived from TOptions. Additionally some project //
5 // data like input/output file lists is added. //
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 #include <iomanip> // setw()
16
17 #include "project.h"
18 #include "FileUtil.h" // file utilities
19 #include "NameUtil.h" // file name utilities
20 #include "ErrText.h" // TErrText
21
22
23 //----------------------------------------------------------------------------------------------------------------------
24 // defaults and names used in project file
25 #define SZ_PROJECT_OPTIONS "[ProjectOptions]"
26 #define SZ_LANGUAGE "Language"
27 #define SZ_STYLE_NAME "Style"
28 #define SZ_OVERWRITE_EXISTING_FILES "OverwriteExistingFiles"
29 #define SZ_ENFORCE_OUTPUT_DIR "EnforceOutputDirectory"
30 #define SZ_OUTPUT_DIR "OutputDirectory"
31 #define SZ_SHOW_PATH "ShowPath"
32 #define SZ_FILENAMES "[FileNames]"
33 #define SZ_SRC_FILE "Source"
34 #define SZ_DES_FILE "Destination"
35
36 #define DEF_LANGUAGE "C and C++" // default language
37 #define DEF_OVERWRITE_EXISTING_FILES false
38 #define DEF_SHOW_PATH false
39 #define DEF_ENFORCE_OUTPUT_DIR false
40
41 #define WIDTH 22 // width used for option names on writing ini file
42
43 //----------------------------------------------------------------------------------------------------------------------
44 // constructor
45 // note: style name is set by GUI for new empty project on start in TFMain::InitializeStyle()
46 TProject::TProject() : TOptions()
47 {
48 szStyle[0] = '\0'; // clear highlighting style
49 strcpy(szLanguage, DEF_LANGUAGE); // ini language
50 }
51
52
53 //----------------------------------------------------------------------------------------------------------------------
54 // restore defaults settings
55 // note: style and language are not reset
56 void TProject::RestoreDefaults(const TOptions& defaults)
57 {
58 TOptions::RestoreDefaults(defaults); // call base class version
59
60 szOutputDir[0] = '\0'; // clear output directory
61
62 szSrcFiles.Clear(); // clear source and destination filename lists
63 szDesFiles.Clear();
64
65 f_OverwriteExistingFiles = DEF_OVERWRITE_EXISTING_FILES; // reset flags
66 f_ShowPath = DEF_SHOW_PATH;
67 f_EnforceOutputDir = DEF_ENFORCE_OUTPUT_DIR;
68 }
69
70
71 //----------------------------------------------------------------------------------------------------------------------
72 // get i'th destination filename
73 const char* TProject::GetDestinationFileName(const int& i)
74 {
75 if(i<0 && i>=szDesFiles.Size()) // check if <i> is valid
76 throw TErrText("Index out of bounds!");
77
78
79 const char* szName = szDesFiles.Get(i); // get i'th destination filename
80
81 if(f_EnforceOutputDir && szOutputDir[0]!='\0') // if enabled and specified ...
82 return GetPrefixedPath(GetFileName(szName), szOutputDir); // return filename with specified output directory
83 else
84 return szName; // else return filename as it is
85 }
86
87
88 //----------------------------------------------------------------------------------------------------------------------
89 // Load options from file
90 void TProject::Load(ifstream& file)
91 {
92 TOptions::Load(file); // call base class version
93
94 try
95 {
96 //----------------------------------------------------------------------------------------------------------------
97 // 1. read section '[ProjectOptions]'
98
99 // a) position to section
100 SearchKey(file, SZ_PROJECT_OPTIONS);
101
102
103 // b) read style name and language
104 ReadKeyString(file, SZ_STYLE_NAME, szStyle, STS, SEARCH_LINES);
105 ReadKeyString(file, SZ_LANGUAGE, szLanguage, STS, SEARCH_LINES); // read language string
106
107 // c) read flags
108 f_ShowPath = ReadKeyBool(file, SZ_SHOW_PATH, DEF_SHOW_PATH, SEARCH_LINES);
109 f_OverwriteExistingFiles = ReadKeyBool(file, SZ_OVERWRITE_EXISTING_FILES
110 , DEF_OVERWRITE_EXISTING_FILES, SEARCH_LINES);
111 f_EnforceOutputDir = ReadKeyBool(file, SZ_ENFORCE_OUTPUT_DIR, DEF_ENFORCE_OUTPUT_DIR, SEARCH_LINES);
112
113 ReadKeyString(file, SZ_OUTPUT_DIR, szOutputDir, STS, SEARCH_LINES);
114
115
116
117 //----------------------------------------------------------------------------------------------------------------
118 // 2. read section '[FileNames]'
119
120 // a) position to section
121 SearchKey(file, SZ_FILENAMES);
122
123 // b) read source filenames
124 char szTmp[STS];
125 int i=0; // filename counter
126 do
127 {
128 char szKey[256]; // compose key
129 sprintf(szKey, "%s%d", SZ_SRC_FILE, i+1);
130 ReadKeyString(file, szKey, szTmp, STS, SEARCH_LINES); // try to find key and read string
131
132 if(szTmp[0]!='\0')
133 strcpy(szSrcFiles.Ins(), szTmp); // insert filename in list
134
135 i++; // increment filename counter
136 } while(szTmp[0]!='\0'); // break loop if string is empty, i.e. key was not found
137
138
139 // c) read destination filenames
140 i=0; // reset
141 do
142 {
143 char szKey[256]; // compose key
144 sprintf(szKey, "%s%d", SZ_DES_FILE, i+1);
145 ReadKeyString(file, szKey, szTmp, STS, SEARCH_LINES); // try to find key and read string
146
147 if(szTmp[0]!='\0')
148 strcpy(szDesFiles.Ins(), szTmp); // insert filename in list
149
150 i++; // increment filename counter
151 } while(szTmp[0]!='\0'); // break loop if string is empty, i.e. key was not found
152
153
154 // d) check if number of source and destination filenames is equal
155 if(szSrcFiles.Size()!=szDesFiles.Size())
156 throw TErrText("Number of source and destination filenames does not match!");
157 }
158 catch(int errNo) // exception handling
159 {
160 char szText[STS];
161 sprintf(szText, "Section '%s' reading key '%s': %s", SZ_PROJECT_OPTIONS, GetLastKey(), GetLastError(errNo));
162
163 throw TErrText(szText); // propagate exception
164 }
165 }
166
167
168 //----------------------------------------------------------------------------------------------------------------------
169 // Save options to file
170 void TProject::Save(ofstream& file)
171 {
172 // 1. call base class version
173 TOptions::Save(file);
174
175 // 2. set left justified output
176 file << setiosflags(ios::left) << resetiosflags(ios::right);
177
178
179 //-------------------------------------------------------------------------------------------------------------------
180 // 3. write section '[ProjectOptions]'
181
182 // a) section name
183 file << endl << endl << SZ_PROJECT_OPTIONS << endl;
184
185 // b) highlighting style name and language
186 if(szStyle[0]!='\0') // if not empty
187 file << setw(WIDTH) << SZ_STYLE_NAME << " = \"" << szStyle << "\"" << endl;
188 file << setw(WIDTH) << SZ_LANGUAGE << " = \"" << szLanguage << "\"" << endl;
189
190 // c) flags
191 file << setw(WIDTH) << SZ_OVERWRITE_EXISTING_FILES << " = " << FlagToString(f_OverwriteExistingFiles, true) << endl;
192 file << setw(WIDTH) << SZ_SHOW_PATH << " = " << FlagToString(f_ShowPath, true) << endl;
193 file << setw(WIDTH) << SZ_ENFORCE_OUTPUT_DIR << " = " << FlagToString(f_EnforceOutputDir, true) << endl;
194
195
196 // d) write, if specified, output directory
197 if(szOutputDir[0]!='\0')
198 file << setw(WIDTH) << SZ_OUTPUT_DIR << " = \"" << szOutputDir << "\"" << endl;
199
200
201
202
203 //-------------------------------------------------------------------------------------------------------------------
204 // 4. write section '[FileNames]'
205
206 // a) section name
207 file << endl << endl << SZ_FILENAMES << endl;
208
209 // b) source filenames
210 for(int i=0;i<szSrcFiles.Size();i++)
211 file << SZ_SRC_FILE << setw(WIDTH-SizeOfString(SZ_SRC_FILE)) << (i+1) << " = \"" << szSrcFiles.Get(i) << "\"" << endl;
212
213 // c) destination filenames
214 for(int i=0;i<szDesFiles.Size();i++)
215 file << SZ_DES_FILE << setw(WIDTH-SizeOfString(SZ_DES_FILE)) << (i+1) << " = \"" << szDesFiles.Get(i) << "\"" << endl;
216
217
218 // 5. restore right justified output
219 file << resetiosflags(ios::left) << setiosflags(ios::right);
220 }
Top |