root/trunk/StringFunctions.cpp

Revision 5, 7.1 kB (checked in by qbert, 6 years ago)

Initial ( and last :( ) commit

Line 
1 #include "stdafx.h"
2 #include "stringfunctions.h"
3
4 bool StartsWithBackward(const string& match, const string& backwardText ) {
5   string local = match;
6
7   //std::reverse(local.begin() , local.end() );
8
9   if ( backwardText.length() >= local.length () )   
10     {
11       //      AfxMessageBox(backwardText.substr(backwardText.length() - local.length() ).c_str() );
12       if ( backwardText.substr(backwardText.length() - local.length() ) == local ) {
13     return true;
14       }
15     }
16    
17   return false;
18
19 }
20
21 bool NextCharIsBackward(char ch , const string& backwardText, int skipNo  ) {
22
23   int pos = backwardText.length() - 1;
24   pos -= skipNo;
25   for ( pos; pos >= 0;pos-- )
26     {
27       if( isspace(backwardText[pos] ) ) continue;
28       else
29     {
30       if ( backwardText[pos] == ch ) return true;
31       else
32         {
33           // CString c;
34 //        c.Format("%c",backwardText[pos] );
35 //        AfxMessageBox (c);
36           return false;
37         }
38     }
39
40     }
41  
42  
43
44   return false;
45
46 }
47 bool NextCharIs(char ch , const string& backwardText,int skipNo  ) { return false; }
48
49
50 bool MatchesFunctionDefinition (const string& text) {
51   return false;
52 }
53
54 void AdvanceToWhiteSpaceOrChar(const string& text, int & pos, char c ) {
55
56
57     for ( int i = 0;i < text.size();i ++ ) {
58         if ( isspace(text[i] ) ) {
59             pos = i;
60             return;
61         }   
62         else if ( text[i] == c ) {
63             pos = i;
64             return;
65         }
66         else {
67           continue;
68         }
69
70     }
71 }
72
73 void AdvanceToWhiteSpaceOrCharBackward(const string& text, int & pos, char c ) {
74
75
76     for ( int i = text.length() - 1;i > 0 ;i-- ) {
77         if ( isspace(text[i] ) ) {
78             pos = i;
79             return;
80         }   
81         else if ( text[i] == c ) {
82             pos = i;
83             return;
84         }
85         else {
86             continue;
87         }
88
89     }
90 }
91 void AdvanceToWhiteSpaceBackward(const string& text, int & pos ) {
92
93
94     for ( int i = text.length() - 1;i > 0 ;i ++ ) {
95
96         if ( isspace(text[i] ) ) {
97             pos = i;
98             return;
99         }   
100         else {
101             continue;
102         }
103
104     }
105
106
107 }
108
109
110 Strtok::Strtok (string *_str, string delim,
111                         bool returnTokens)
112 {
113     currentPosition = 0;
114     str = _str;
115     maxPosition = str->length ();
116     delimiters = delim;
117     retTokens = returnTokens;
118 }
119
120
121 Strtok::Strtok (string *_str, string delim)
122 {
123     currentPosition = 0;
124     str = _str;
125     maxPosition = str->length ();
126     delimiters = delim;
127     retTokens = false;
128 }
129
130
131 Strtok::Strtok (string *_str)
132 {
133     currentPosition = 0;
134     str = _str;
135     maxPosition = str->length ();
136     delimiters = " \t\r\n";
137     retTokens = false;
138 }
139
140 Strtok::Strtok ()
141 {
142     currentPosition = 0;
143     maxPosition = 0;
144     delimiters = " \t\r\n";
145     retTokens = false;
146 }
147
148 Strtok::~Strtok ()
149 {
150 }
151
152 void Strtok::SkipDelimiters ()
153 {
154     while (!retTokens && (currentPosition < maxPosition) &&
155            (delimiters.find((*str)[currentPosition]) != delimiters.npos)) {
156         currentPosition++;
157     }
158 }
159
160 bool Strtok::HasMoreTokens ()
161 {
162     SkipDelimiters ();
163     return (currentPosition < maxPosition);
164 }
165
166 string Strtok::NextToken (void)
167 {
168     int start = GetStart ();
169     string ret;
170     ret.assign (&(*str)[start], &(*str)[currentPosition]);
171     return ret;
172 }
173
174
175 bool Strtok::NextToken(string &_str)
176 {
177     int start = GetStart ();
178     _str.assign (&(*str)[start], &(*str)[currentPosition]);
179     return true;
180 }
181
182 int Strtok::GetStart (void)
183 {
184     SkipDelimiters();
185
186     //assert (currentPosition < maxPosition);
187
188     int start = currentPosition;
189     while ((currentPosition < maxPosition) &&
190            (delimiters.find((*str)[currentPosition]) == delimiters.npos)) {
191         currentPosition++;
192     }
193     if (retTokens && (start == currentPosition) &&
194            (delimiters.find((*str)[currentPosition]) != delimiters.npos)) {
195         currentPosition++;
196     }
197
198     return start;
199 }
200
201
202 int Strtok::CountTokens()
203 {
204     int count = 0;
205     int currpos = currentPosition;
206
207     while (currpos < maxPosition) {
208         /*
209          * This is just skipDelimiters(); but it does not affect
210          * currentPosition.
211          */
212         while (!retTokens &&
213            (currpos < maxPosition) &&
214            (delimiters.find((*str)[currpos]) != delimiters.npos)) {
215         currpos++;
216         }
217
218         if (currpos >= maxPosition) {
219         break;
220         }
221
222         int start = currpos;
223         while ((currpos < maxPosition) &&
224            (delimiters.find((*str)[currpos]) == delimiters.npos)) {
225         currpos++;
226         }
227         if (retTokens && (start == currpos) &&
228            (delimiters.find((*str)[currpos]) != delimiters.npos)) {
229         currpos++;
230         }
231         count++;
232
233     }
234     return count;
235 }
236
237
238 void Strtok::Reset (string *_str)
239 {
240     currentPosition = 0;
241     str = _str;
242     maxPosition = str->length ();
243 }
244
245
246 void Strtok::Reset (string *_str, bool returnTokens)
247 {
248     retTokens = returnTokens;
249     currentPosition = 0;
250     str = _str;
251     maxPosition = str->length ();
252 }
253
254
255 void Strtok::Reset (string *_str, string delim, bool returnTokens)
256 {
257     retTokens = returnTokens;
258     delimiters = delim;
259     currentPosition = 0;
260     str = _str;
261     maxPosition = str->length ();
262 }
263
264
265
266 void Split(const string& s, const string& delim, vector<string>& items )
267 {
268     string str = s;
269     Strtok token(&str,delim);
270
271     while ( token.HasMoreTokens() )
272     {
273         items.push_back(token.NextToken() );
274     }
275
276
277 }
278
279 void Split(const string& s, string& result1, string& result2 )
280 {
281     string str = s;
282     Strtok token(&str," ");
283
284
285     result1 = token.NextToken();
286     result2 = token.NextToken();
287
288 }
289
290 string Join(const vector<string> &items, const string & delim)
291 {
292     string ret = "";
293     for ( int i = 0;i < items.size();i++){
294        
295         ret += items[i];
296         ret += delim;
297
298     }
299     return ret.substr(0,ret.length() - delim.length() );
300
301 }
302
303 void GetKeysForMap(map<string,vector<string> > & myMap, vector<string>  & vec)
304
305 {
306     std::map<string,vector<string> >::iterator p = myMap.begin();
307     for( p;p != myMap.end() ;p++)
308     {
309         vec.push_back( (*p).first );
310     }
311
312 }
313
314
315 void GetKeysForMap(map<string, string > & myMap, vector<string>  & vec)
316
317 {
318     std::map<string,string >::iterator p = myMap.begin();
319     for( p;p != myMap.end() ;p++)
320     {
321         vec.push_back( (*p).first );
322     }
323
324 }
325
326
327 void RemoveFileFromMap(map<string,vector<string> > & myMap,const string& mySet, const string& myFile ) {
328     std::map<string,vector<string> >::iterator p = myMap.begin();
329     vector<string> *files;
330     for( p;p != myMap.end() ;p++)
331     {
332         files = &(*p).second ;
333         for ( int i = 0;i < files->size();i++ ) {
334             if ( (*files)[i] == myFile ) {
335                 files->erase(files->begin() + i );
336                 break;
337             }
338
339         }
340     }
341    
342
343
344 };
345
346
347 void GetAbbExpansion(const string& exp, string& expands, int & cursor, bool& tabs ){
348     expands = exp;
349                        
350     unsigned long mtPos = expands.find_last_of("||~");
351     if ( mtPos == string::npos ) return;
352     tabs = expands.substr(mtPos+1) == "1";
353     expands[mtPos-2] = '\0'; // cut off of
354     unsigned long curPos = expands.find("||~");
355     if ( curPos == string::npos ) return;
356     cursor = atoi(expands.substr(curPos+3).c_str() );
357     expands[curPos] = '\0';
358                        
359 }
360
361 string SetAbbExpansion(CString& exp,CString & cursor, CString& tabs )
362 {
363
364     CString xp = exp + "||~" + cursor + "||~" ;
365     xp += tabs == "true" ? "1" : "0";
366 //  ElephantMessageBox(xp.GetBuffer(0) );
367     return xp.GetBuffer(0);
368 }
369
370
371 string shortPathName(const string& path ) {
372
373    
374
375     char buffer[MAX_PATH+1];   
376     GetShortPathName(path.c_str(),buffer,MAX_PATH+1) ; 
377     return buffer;
378
379 }
Note: See TracBrowser for help on using the browser.