|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #ifndef CDLTKUTIL_H |
|
18 #define CDLTKUTIL_H |
|
19 #pragma warning (disable:4786) // disable "identifier was truncated to '255' characters in the browser information" warning |
|
20 |
|
21 #include <string> |
|
22 #include <iosfwd> |
|
23 #include <vector> |
|
24 #include <CdlDef.h> |
|
25 |
|
26 namespace CdlCompilerToolkit { |
|
27 |
|
28 class CCdlTkFileCleanup; |
|
29 |
|
30 /** |
|
31 * This is a collection of utility functions that the CDL compiler toolkit uses |
|
32 * and which may be of use to other tools based on this toolkit |
|
33 */ |
|
34 class CdlTkUtil |
|
35 { |
|
36 private: |
|
37 typedef std::pair<std::string, std::string> CReplaceTerm; |
|
38 |
|
39 public: |
|
40 /** |
|
41 * Gives the current working drive |
|
42 * @return the drive as a "X:" string. |
|
43 */ |
|
44 static std::string CurrentDrive(); |
|
45 /** |
|
46 * Gives the current working directory, without drive |
|
47 * @return the current working directory |
|
48 */ |
|
49 static std::string CurrentDir(); |
|
50 /** |
|
51 * Gives the current output path for generated files |
|
52 * @return the output path |
|
53 */ |
|
54 static std::string OutputPath(); |
|
55 /** |
|
56 * Sets the curreent output path for generated files |
|
57 * @param aPath the output path for generated files |
|
58 */ |
|
59 static void SetOutputPath(const std::string& aPath); |
|
60 |
|
61 /** |
|
62 * Gives the command line used to run the tool using this toolkit |
|
63 * @return the command line, will be empty if SetCommandLine() is not called |
|
64 */ |
|
65 static std::string CommandLine(); |
|
66 /** |
|
67 * Sets the command line for the tool using this toolkit. |
|
68 */ |
|
69 static void SetCommandLine(int argc, char* argv[]); |
|
70 |
|
71 /** |
|
72 * Extract an int from the string. This function automatically detects |
|
73 * bases 8, 10 and 16. |
|
74 * @param aInt a string containing an integer |
|
75 * @return the integer value parsed |
|
76 */ |
|
77 static int ParseInt(const std::string& aInt); |
|
78 /** |
|
79 * Formats an integer as a string |
|
80 * @param aInt the integer to format |
|
81 * @return a string representing the integer in base 10 |
|
82 */ |
|
83 static std::string IntToString(int aInt); |
|
84 /** |
|
85 * Formats an integer as a hex string |
|
86 * @param aInt the integer to format |
|
87 * @return a string representing the integer in base 16 |
|
88 */ |
|
89 static std::string IntToHexString(int aInt); |
|
90 /** |
|
91 * Formats a short integer as a hex string |
|
92 * @param aInt the integer to format |
|
93 * @return a string representing the integer in base 16 |
|
94 */ |
|
95 static std::string ShortToHexString(short aInt); |
|
96 /** |
|
97 * Formats an 8-bit integer as a hex string |
|
98 * @param aInt the integer to format |
|
99 * @return a string representing the integer in base 16 |
|
100 */ |
|
101 static std::string CharToHexString(char aInt); |
|
102 |
|
103 /** |
|
104 * Converts a string to lower case |
|
105 * @param aString the string to convert |
|
106 * @return a version of the string in lower case |
|
107 */ |
|
108 static std::string ToLower(const std::string& aString); |
|
109 /** |
|
110 * Converts a string to upper case |
|
111 * @param aString the string to convert |
|
112 * @return a version of the string in upper case |
|
113 */ |
|
114 static std::string ToUpper(const std::string& aString); |
|
115 /** |
|
116 * Converts a string to C++ identifier format. Characters which are not |
|
117 * valid C++ identifier characters are replaced with an underscore. If the |
|
118 * string starts with a number or is empty, an underscore is added to the start. |
|
119 * @param aString a string to convert to a C++ identifier |
|
120 * @return the converted string |
|
121 */ |
|
122 static std::string ToCpp(const std::string& aString); |
|
123 /** |
|
124 * Strips white-space from the start and end of the string |
|
125 * @param aStr the string from which white space should be removed |
|
126 */ |
|
127 static void StripLeadingAndTrailingWhitespace(std::string& aStr); |
|
128 |
|
129 /** |
|
130 * Is the character alphabetic? |
|
131 * @param aChar the character to test |
|
132 * @return true if the character is alphabetic |
|
133 */ |
|
134 static bool IsAlpha(char aChar); |
|
135 /** |
|
136 * Is the character numeric? |
|
137 * @param aChar the character to test |
|
138 * @return true if the character is numeric |
|
139 */ |
|
140 static bool IsNumeric(char aChar); |
|
141 /** |
|
142 * Is the character valid for a C++ identifier? |
|
143 * @param aChar the character to test |
|
144 * @return true if the character is valid for a C++ identifier |
|
145 */ |
|
146 static bool IsCpp(char aChar); |
|
147 |
|
148 /** |
|
149 * This class contains a set of terms to replace in a string. It is used |
|
150 * with CdlTkUtil::MultiReplace(). |
|
151 */ |
|
152 class CReplaceSet : public std::vector<CReplaceTerm> |
|
153 { |
|
154 public: |
|
155 void Add(const std::string& aTarget, const std::string& aWith); |
|
156 }; |
|
157 /** |
|
158 * Replace a set of targets with other text in a string and return the result. |
|
159 * @param aSet the set of pairs of targets and replacement text |
|
160 * @param aIn the string to replace text in |
|
161 * @return The result of the replacement |
|
162 */ |
|
163 static std::string MultiReplace(const CReplaceSet& aSet, const std::string& aIn); |
|
164 /** |
|
165 * Replace a target in a string with some text and return the result. |
|
166 * @param aTarget the text to replace |
|
167 * @param aWith the text that replaces aTarget |
|
168 * @param aIn the string in which to replace the text |
|
169 * @return the result of the replacement |
|
170 */ |
|
171 static std::string Replace(const std::string& aTarget, const std::string& aWith, const std::string& aIn); |
|
172 /** |
|
173 * Add a string to another. This function uses exponential growth of the target |
|
174 * string to reduce average runtime for long sequences of appends to O(n log(n)). |
|
175 * @param aTarget the string to which text is to be appended |
|
176 * @param aAppend the text to append |
|
177 */ |
|
178 static void AppendString(std::string& aTarget, const std::string& aAppend); |
|
179 |
|
180 /** |
|
181 * Remove and path components from a file name and path, and return the file name. |
|
182 * @param aPath the path and filename |
|
183 * @return the filename component of the path and filename passed in. |
|
184 */ |
|
185 static std::string StripPath(const std::string& aPath); |
|
186 /** |
|
187 * Resolves a filename relative to a path, and returns the resulting resolved path |
|
188 * @param aPath the path to which a relative filename is to be calculated |
|
189 * @param aFileName the filename, possibly including relative path components |
|
190 * such as . and .. which should be resolved. |
|
191 * @return the resolved path and filename |
|
192 */ |
|
193 static std::string ResolvePath(const std::string& aPath, const std::string& aFileName); |
|
194 /** |
|
195 * Converts a filename and path so that first letter of file name is capitalized |
|
196 * and the rest is lower case |
|
197 * @param aString the string to convert |
|
198 * @return the converted filename and path |
|
199 */ |
|
200 static std::string CapitalizeFilename(const std::string& aString); |
|
201 /** |
|
202 * Converts a filename and path to use the current standard for file name case |
|
203 * @param aString the string to convert |
|
204 * @return the converted filename and path |
|
205 */ |
|
206 static std::string CorrectFilenameCase(const std::string& aString); |
|
207 |
|
208 /** |
|
209 * Opens a temporary output file, with a temporary file name. |
|
210 * This function is normally used in conjunction with ExportFile() to ensure |
|
211 * that files are only modified if they are changed. |
|
212 * @param aStream the output stream to open |
|
213 * @param aFile a file cleanup object which will contain the temporary file name |
|
214 * and will remove the file on destruction |
|
215 */ |
|
216 static void OpenTempOutput(std::ofstream& aStream, CCdlTkFileCleanup& aFile, std::ios_base::openmode aOpenMode = std::ios_base::out); |
|
217 /** |
|
218 * Opens an output file, throwing a CdlTkFileOpenErr exception if it fails. |
|
219 * @param aStream the stream to open |
|
220 * @param aFileName the name of the file to open |
|
221 */ |
|
222 static void OpenOutput(std::ofstream& aStream, const std::string& aFileName, std::ios_base::openmode aOpenMode = std::ios_base::out); |
|
223 /** |
|
224 * Opens an input file, throwing a CdlTkFileOpenErr exception if it fails. |
|
225 * @param aStream the stream to open |
|
226 * @param aFileName the name of the file to open |
|
227 */ |
|
228 static void OpenInput(std::ifstream& aStream, const std::string& aFileName, std::ios_base::openmode aOpenMode = std::ios_base::in); |
|
229 |
|
230 /** |
|
231 * Compares the source file with the destination. If they differ, |
|
232 * it replaces the destination file with the source. The source file is removed. |
|
233 * This function will throw a CdlTkFileOpenErr if the destination file |
|
234 * is not writable. This function should be used when the destination file |
|
235 * should not be modified manually. |
|
236 * @param aSourceFile the temporary source file. |
|
237 * @param aDestinationFileName the name of the destination file. |
|
238 */ |
|
239 static void ExportFile(CCdlTkFileCleanup& aSourceFile, const std::string& aDestinationFileName, std::ios_base::openmode aOpenMode = 0); |
|
240 /** |
|
241 * Compares the source file with the destination. If they differ, |
|
242 * it replaces the destination file with the source. The source file is removed. |
|
243 * This function will report an error to the standard error stream if the |
|
244 * destination file is not writable. This function should be used when the |
|
245 * destination file may be modified manually, and may be under source control. |
|
246 * @param aSourceFile the temporary source file. |
|
247 * @param aDestinationFileName the name of the destination file. |
|
248 */ |
|
249 static void ExportFileIfWritable(CCdlTkFileCleanup& aSourceFile, const std::string& aDestinationFileName, std::ios_base::openmode aOpenMode = 0); |
|
250 /** |
|
251 * Delete a file |
|
252 * @param aFileName the name of the file to be deleted |
|
253 */ |
|
254 static void DeleteFile(const std::string& aFileName); |
|
255 /** |
|
256 * Copies a file |
|
257 * @param aSourceFileName the source file. |
|
258 * @param aDestinationFileName the destination file. |
|
259 */ |
|
260 static void CopyFile(const std::string& aSourceFileName, const std::string& aDestinationFileName, std::ios_base::openmode aOpenMode = 0); |
|
261 /** |
|
262 * Checks to see if two files are identical |
|
263 * @param aLeftFileName one of the files to check |
|
264 * @param aRightFileName the other file to check |
|
265 */ |
|
266 static bool FilesAreIdentical(const std::string& aLeftFileName, const std::string& aRightFileName, std::ios_base::openmode aOpenMode = std::ios_base::in); |
|
267 |
|
268 static void ReadFile(std::string& aContent, const std::string& aFileName); |
|
269 static void WriteFile(const std::string& aContent, const std::string& aFileName); |
|
270 |
|
271 template <class OutputIterator> |
|
272 inline static void Tokenize(const std::string& aString, OutputIterator pOut) |
|
273 { |
|
274 const std::string ws(" \t\r\n"); |
|
275 std::string::const_iterator pChar = aString.begin(); |
|
276 std::string::const_iterator pWord = pChar; |
|
277 for (; ; ++pChar) |
|
278 { |
|
279 if (pChar == aString.end() || ws.find_first_of(*pChar) != string::npos) |
|
280 { |
|
281 if (pWord != pChar) |
|
282 { |
|
283 *pOut = std::string(pWord, pChar); |
|
284 pOut++; |
|
285 } |
|
286 pWord = pChar+1; |
|
287 if (pChar == aString.end()) |
|
288 break; |
|
289 } |
|
290 } |
|
291 } |
|
292 }; |
|
293 |
|
294 |
|
295 /** |
|
296 * This is the base class for all CDL Compiler Toolkit exceptions |
|
297 */ |
|
298 class CdlCompilerToolkitErr |
|
299 { |
|
300 public: |
|
301 /** |
|
302 * destructor |
|
303 */ |
|
304 virtual ~CdlCompilerToolkitErr(); |
|
305 /** |
|
306 * virtual method for describing the exception on a stream |
|
307 * @param stream the output stream for the exceptions description |
|
308 */ |
|
309 virtual void Show(std::ostream& stream) const = 0; |
|
310 }; |
|
311 |
|
312 |
|
313 /** |
|
314 * This exception class is thrown for general assertions |
|
315 */ |
|
316 class CdlTkAssert : public CdlCompilerToolkitErr |
|
317 { |
|
318 public: |
|
319 /** |
|
320 * constructor for a general assertion exception |
|
321 * @param aText the assertion description |
|
322 */ |
|
323 CdlTkAssert(const std::string& aText); |
|
324 /** |
|
325 * virtual method for describing the exception on a stream |
|
326 * @param stream the output stream for the exceptions description |
|
327 */ |
|
328 void Show(std::ostream& stream) const; |
|
329 private: |
|
330 std::string iText; |
|
331 }; |
|
332 |
|
333 |
|
334 /** |
|
335 * This exception class is thrown for errors in opening a file |
|
336 */ |
|
337 class CdlTkFileOpenErr : public CdlCompilerToolkitErr |
|
338 { |
|
339 public: |
|
340 /** |
|
341 * constructor for a file open exception |
|
342 * @param aFileName the file that has failed to open |
|
343 */ |
|
344 CdlTkFileOpenErr(const std::string& aFileName); |
|
345 /** |
|
346 * virtual method for describing the exception on a stream |
|
347 * @param stream the output stream for the exceptions description |
|
348 */ |
|
349 void Show(std::ostream& aStream) const; |
|
350 private: |
|
351 std::string iFileName; |
|
352 }; |
|
353 |
|
354 |
|
355 /** |
|
356 * This class represents a temporary file. If an instance is destroyed, it will |
|
357 * delete the filename it has been given. |
|
358 */ |
|
359 class CCdlTkFileCleanup |
|
360 { |
|
361 public: |
|
362 /** |
|
363 * default constructor |
|
364 */ |
|
365 CCdlTkFileCleanup(); |
|
366 /** |
|
367 * constructor which sets the filename for the temporary file |
|
368 * @param aName the name for the file to be cleaned up |
|
369 */ |
|
370 CCdlTkFileCleanup(const std::string& aName); |
|
371 /** |
|
372 * destructor |
|
373 * This will delete the temporary file, if a filename has been set. |
|
374 */ |
|
375 ~CCdlTkFileCleanup(); |
|
376 |
|
377 /** |
|
378 * Set the filename of the temporary file. |
|
379 * @param aName the filename of the temporary file. |
|
380 */ |
|
381 void Set(const std::string& aName); |
|
382 /** |
|
383 * Gets the filename of the temporary file. |
|
384 * @return the filename. |
|
385 */ |
|
386 std::string Name() const; |
|
387 /** |
|
388 * Deletes the temporary file and resets the filename. |
|
389 */ |
|
390 void Cleanup(); |
|
391 /** |
|
392 * Resets the filename so that no file will be deleted. |
|
393 */ |
|
394 void Release(); |
|
395 private: |
|
396 std::string iName; |
|
397 }; |
|
398 |
|
399 |
|
400 } // end of namespace CdlCompilerToolkit |
|
401 |
|
402 #endif |