Back

1     //------------------------------------------------------------------------------
2     // Module NameUtil.cpp                                                        //
3     //                                                                            //
4     //    Filename and string utilities                                           //
5     //                                                                            //
6     //    Utility functions to read values/strings from a stream. You can         //
7     //    realize format-sensitive reading without the "always read a whoole      //
8     //    line" concept.                                                          //
9     //                                                                            //
10    //    copyright (c) 1998-2004 by Lars Haendel                                 //
11    //    home: www.newty.de                                                      //
12    //                                                                            //
13    //    This program is free software and can be used under the terms of the    //
14    //    GNU licence. See header file for further information and disclaimer.    //
15    //                                                                            //
16    //------------------------------------------------------------------------------
17
18
19    #include <stdio>                 // due to:  sprintf()
20    #include <ctype>                 //          isspace()
21    #include <string>
22
23    #include "NameUtil.h"            //
24
25
26    //----------------------------------------------------------------------------------------------------------------------
27    // count number of words within string
28    int CountWords(const char*const& szString)
29    {
30       // count number of words within string
31       int i=0, nWords=0; // ini
32       while(szString[i]!='\0')
33       {
34          if(!isspace(szString[i]))
35          {
36             nWords++;                                             // increment word counter
37             while(!isspace(szString[i]) && szString[i]!='\0')     // position to next whitespace or '\0'
38                i++;
39          }
40          else
41             while(isspace(szString[i]))
42                i++; // skip all whitespaces
43       }
44
45       return nWords;
46    }
47
48
49    //----------------------------------------------------------------------------------------------------------------------
50    // gets the number of characters of next word within passed string
51    int GetLengthOfFirstWord(const char*const& szString)
52    {
53       int i=0;                                              // ini
54       while(!isspace(szString[i]) && szString[i]!='\0')     // position to next whitespace or '\0'
55          i++;
56
57       return i;
58    }
59
60
61    //----------------------------------------------------------------------------------------------------------------------
62    // size of a string excluding terminating '\0', i.e. szString[size] is '\0'
63    int SizeOfString(const char*const& szString)
64    {
65       int size = 0;
66       while(szString[size]!='\0')
67          size++;
68       return size;
69    }
70
71
72    //----------------------------------------------------------------------------------------------------------------------
73    // remove a filename's extension
74    void RemoveExt(char*const& szFileName)
75    {
76       int len = SizeOfString(szFileName);          // determine length
77
78       while(len>=0)                                // position back to last '.'
79       {
80          if(szFileName[len]=='.')                  // if point is found
81          {
82             szFileName[len] = '\0';                // 'delete' rest of string
83             break;                                 // and finish
84          }
85          len--;
86       }
87    }
88
89
90    //----------------------------------------------------------------------------------------------------------------------
91    // check if passed string is a valid filename without(!) path and extension
92    bool CheckName(const char*const& szName)
93    {
94       int i=0;
95       while(szName[i]!='\0')
96       {
97          if(szName[i]=='\\'|| szName[i]=='/' || szName[i]==':' || szName[i]=='.') // no backslash, slash, colon or dot
98             return false;
99          i++;
100      }
101      return true; // all ok
102   }
103
104
105   //----------------------------------------------------------------------------------------------------------------------
106   // exchange a filename's extension
107   void ExchangeExt(char*const& szFileName, const char*const& szExtension)
108   {
109      RemoveExt(szFileName);                       // remove extension
110      strcat(szFileName, ".");                     // append dot
111      strcat(szFileName, szExtension);             // append extension
112   }
113
114
115   //----------------------------------------------------------------------------------------------------------------------
116   // a path delimiter is appended if passed string is not empty and if it is not already there
117   void EnsurePathDelimiter(char*const& szPath)
118   {
119      int len = SizeOfString(szPath);              // determine length
120
121      if(len>0)                                    // if string is not empty
122         if(szPath[len-1]!=PATH_DELIMITER)         // append path delimiter if necessary
123         {
124            szPath[len] = PATH_DELIMITER;          // append and ...
125            szPath[len+1]  = '\0';                 // ... terminate string
126         }
127   }
128
129
130   //----------------------------------------------------------------------------------------------------------------------
131   // copy path without filename to <szPath>
132   void ExtractPath(char*const& szPath, const char*const& szFileName)
133   {
134      szPath[0]='\0';                              // 'delete' path
135
136      int i=0, pos=0;
137      while(szFileName[i++]!='\0')                 // find last path delimiter, set <pos> to point to next character
138         if(szFileName[i-1]==PATH_DELIMITER)       // path delimiter found
139            pos=i;                                 // store position
140
141      for(int i=0;i<pos;i++)                       // copy path (<pos> will be 0 if no delimiter was found)
142         szPath[i]=szFileName[i];
143      szPath[pos]='\0';
144   }
145
146
147   //----------------------------------------------------------------------------------------------------------------------
148   // return path without filename (using static memory)
149   const char* ExtractPath(const char*const& szFileName)
150   {
151      static char szPath[STS];
152      ExtractPath(szPath, szFileName);
153      return (const char*) szPath;
154   }
155
156
157   //----------------------------------------------------------------------------------------------------------------------
158   // replaces each wrong path delimiter (COMPL_PATH_DELIMITER) by the correct one (PATH_DELIMITER)
159   void CorrectPathDelimiter(char*const& szFileName)
160   {
161      int i=0;
162      while(szFileName[i]!='\0')                    // parse whole string
163      {
164         if(szFileName[i]==COMPL_PATH_DELIMITER)
165            szFileName[i]=PATH_DELIMITER;           // replace
166         i++;
167      }
168   }
169
170
171   //----------------------------------------------------------------------------------------------------------------------
172   // prepend path to filename if necessary, i.e. if filename is not empty and relative (does not contain a ':')
173   void PrefixPath(char*const& szFileName, const char*const& szDir)
174   {
175      if(szFileName[0]=='\0')          // return if filename is empty
176         return;
177
178      int i=0;
179      while(szFileName[i++]!='\0')     // return if filename contains ':' which indicates that it is already a full ...
180         if(szFileName[i-1]==':')      // ... qualified name
181            return;
182
183      char szText[STS];                // prefix filename
184      strcpy(szText, szDir);
185      EnsurePathDelimiter(szText);     // ensure path delimiter
186      strcat(szText, szFileName);
187      strcpy(szFileName, szText);
188   }
189
190
191   //----------------------------------------------------------------------------------------------------------------------
192   // get pointer to full qualified filename using PrefixPath()
193   const char* GetPrefixedPath(const char*const& szFileName, const char*const& szDir)
194   {
195      static char szText[STS];
196      strcpy(szText, szFileName);         // copy
197      PrefixPath(szText, szDir);          // prepend path if necessary
198
199      return szText;
200   }
201
202
203   //----------------------------------------------------------------------------------------------------------------------
204   // return pointer to filename without preceding path
205   const char* GetFileName(const char*const& szPath)
206   {
207      int len = SizeOfString(szPath);                                      // get string length
208      while(szPath[len]!=PATH_DELIMITER && szPath[len]!=':' && len >=0)    // position back from the end to the first ...
209         len--;                                                            // ... path delimiter or ':' (path on floppy)
210
211      return &szPath[len+1];
212   }
213
214
215   //----------------------------------------------------------------------------------------------------------------------
216   // function tries to find the substring <_szKill> at the begin of the passed string <_szString>. if found, a pointer to
217   // the first non-matching position is returned. Comparision is NOT case sensitive!
218   const char* TryToRemove(const char* szString, const char* szKill)
219   {
220      int nString = SizeOfString(szString);                    // determine string length
221      int nKill   = SizeOfString(szKill);                      // determine substring length
222
223      if (nKill>=nString)                                      // substring bigger than string, match impossible -> return
224         return szString;
225
226
227      // check match
228      for(int i=0; i<nKill;i++)                                // substring[i] must match string[i] for all i<len2
229      {
230         char a = szString[i];
231         char b = szKill[i];
232
233         // eventually change to lower case
234         if(a>=97 && a<=122)
235            a = (char) (a-32);
236         if(b>=97 && b <=122)
237            b = (char) (b-32);
238
239         // compare
240         if(a!=b)
241            return szString;                                   // non matching _-> return
242      }
243      return &szString[nKill];                                 // matching -> return
244   }

Top