Back
1 //------------------------------------------------------------------------------
2 // Module NameUtil.cpp //
3 // //
4 // Filename utilities //
5 // //
6 // copyright (c) 1998-2004 by Lars Haendel //
7 // home: www.newty.de //
8 // //
9 // This program is free software; you can redistribute it and/or modify //
10 // it under the terms of the GNU General Public License as published by //
11 // the Free Software Foundation as version 2 of the License. //
12 // //
13 // This program is distributed in the hope that it will be useful, //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
16 // GNU General Public License for more details. //
17 // //
18 // You should have received a copy of the GNU General Public License //
19 // along with this program; if not, write to the Free Software //
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
21 // //
22 //------------------------------------------------------------------------------
23
24
25 #ifndef NameUtilH
26 #define NameUtilH
27
28
29 #include <ctype> // due to: isspace()
30 #include "defines.h" // general defines
31
32
33
34 //----------------------------------------------------------------------------------------------------------------------
35 // path delimiter in filenames (windows '\\' (backslash), Linux '/' (slash))
36 #ifdef LINUX
37 #define PATH_DELIMITER '/'
38 #define COMPL_PATH_DELIMITER '\\'
39 #else
40 #define PATH_DELIMITER '\\'
41 #define COMPL_PATH_DELIMITER '/'
42 #endif
43
44 //----------------------------------------------------------------------------------------------------------------------
45 // misc.
46
47 // returns true if passed char is a number '0..9'
48 inline bool IsDigit(const char& cChar) { return (cChar>=48 && cChar<=57); }
49
50 // returns true if passed char is an alpha
51 inline bool IsAlpha(const char& cChar) { return ((cChar >= 97 && cChar <= 122) || (cChar >= 65 && cChar <= 90)); }
52
53 // returns true if passed char is an alpha that may be within hex numbers, i.e. 'a..f' or 'A..F'
54 inline bool IsHexAlpha(const char& cChar) { return ((cChar >= 65 && cChar <= 70) || (cChar >= 97 && cChar <= 102)); }
55
56 // returns true if character is whitespace
57 inline bool IsSpace(const char& cChar) { return isspace(cChar); }
58
59
60 //----------------------------------------------------------------------------------------------------------------------
61 // string utilities
62
63 // gets the number of characters of first word within passed string
64 int GetLengthOfFirstWord(const char*const& szString);
65
66 // count number of words within string
67 int CountWords(const char*const& szString);
68
69 // size of a string excluding terminating '\0', i.e. szString[size] is '\0'
70 int SizeOfString(const char*const& szString);
71
72
73 //----------------------------------------------------------------------------------------------------------------------
74 // filename utilities
75
76 // exchange a filename's extension
77 void ExchangeExt(char*const& szFileName, const char*const& szExtension);
78
79 // a path delimiter is appended if passed string is not empty and if it is not already there
80 void EnsurePathDelimiter(char*const& szPath);
81
82 // replace wrong path delimiter by correct one
83 void CorrectPathDelimiter(char*const& szFileName);
84
85 // copy path without filename to <szPath>
86 void ExtractPath(char*const& szDir, const char*const& szFileName);
87
88 // return path without filename (using static memory)
89 const char* ExtractPath(const char*const& szFileName);
90
91 // prepend path to filename if necessary, i.e. if filename is not empty and relative (does not contain a ':')
92 void PrefixPath(char*const& szFileName, const char*const& szDir);
93
94 // get pointer to full qualified filename using PrefixPath()
95 const char* GetPrefixedPath (const char*const& szFileName, const char*const& szDir);
96
97 // check if passed string is a valid filename without(!) path and extension
98 bool CheckName(const char*const& szName);
99
100 // return pointer to filename without preceding path
101 const char* GetFileName(const char*const& szPath);
102
103 // try to remove/find string <_szKill> at the beginning of string <_szString>
104 const char* TryToRemove(const char* _szString, const char* _szKill);
105 #endif
Top |