1 |
#include "StringTools.h"
|
2 |
#include "../System/SystemDefine.h"
|
3 |
#include <iostream.h>
|
4 |
|
5 |
|
6 |
StringTools::StringTools()
|
7 |
{
|
8 |
}
|
9 |
|
10 |
StringTools::~StringTools()
|
11 |
{
|
12 |
}
|
13 |
|
14 |
//////////////////////////////////////////////////////////////////////////////////////
|
15 |
//compare two Strings against each other, without the null in the end of the Strings
|
16 |
//////////////////////////////////////////////////////////////////////////////////////
|
17 |
|
18 |
bool StringTools::compareString(char *source, char *source2)
|
19 |
{
|
20 |
bool statement=true;
|
21 |
|
22 |
if ( source != null && source2 != null )
|
23 |
{
|
24 |
int sizeSource=sizeOfString(source);
|
25 |
int sizeSource2=sizeOfString(source2);
|
26 |
|
27 |
if( sizeSource == sizeSource2 )
|
28 |
{
|
29 |
for ( int s=0; s<sizeSource ; s++ )
|
30 |
{
|
31 |
if ( source[s] != source2[s] )
|
32 |
{
|
33 |
statement=false;
|
34 |
break;
|
35 |
}
|
36 |
}
|
37 |
}
|
38 |
else
|
39 |
{
|
40 |
statement=false;
|
41 |
}
|
42 |
}
|
43 |
else
|
44 |
{
|
45 |
statement=false;
|
46 |
}
|
47 |
|
48 |
return statement;
|
49 |
}
|
50 |
|
51 |
//////////////////////////////////////////////////////////////////////////////////////
|
52 |
//compare two Strings against each other, without the null in the end of the Strings
|
53 |
//////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
55 |
bool StringTools::compareString(char *source, int sizeSource, char *source2, int sizeSource2)
|
56 |
{
|
57 |
bool statement=true;
|
58 |
|
59 |
if ( source != null && source2 != null )
|
60 |
{
|
61 |
if( sizeSource == sizeSource2 )
|
62 |
{
|
63 |
for ( int s=0; s<sizeSource ; s++ )
|
64 |
{
|
65 |
if ( source[s] != source2[s] )
|
66 |
{
|
67 |
statement=false;
|
68 |
break;
|
69 |
}
|
70 |
}
|
71 |
}
|
72 |
else
|
73 |
{
|
74 |
statement=false;
|
75 |
}
|
76 |
}
|
77 |
else
|
78 |
{
|
79 |
statement=false;
|
80 |
}
|
81 |
|
82 |
return statement;
|
83 |
}
|
84 |
|
85 |
//////////////////////////////////////////////////////////////////////////////////////
|
86 |
//compare two Strings against each other, without the null in the end of the Strings
|
87 |
//////////////////////////////////////////////////////////////////////////////////////
|
88 |
|
89 |
bool StringTools::compareStringAPart(char *source, int sizeSource, char *source2, int sizeSource2)
|
90 |
{
|
91 |
bool statement=true;
|
92 |
|
93 |
if ( source != null && source2 != null )
|
94 |
{
|
95 |
if ( sizeSource != 0 )
|
96 |
{
|
97 |
--sizeSource;
|
98 |
}
|
99 |
|
100 |
if( sizeSource <= sizeSource2 )
|
101 |
{
|
102 |
for ( int s=0; s<sizeSource ; s++ )
|
103 |
{
|
104 |
if ( source[s] != source2[s] )
|
105 |
{
|
106 |
statement=false;
|
107 |
break;
|
108 |
}
|
109 |
}
|
110 |
}
|
111 |
else
|
112 |
{
|
113 |
statement=false;
|
114 |
}
|
115 |
}
|
116 |
else
|
117 |
{
|
118 |
statement=false;
|
119 |
}
|
120 |
|
121 |
return statement;
|
122 |
}
|
123 |
|
124 |
//////////////////////////////////////////////////////////////////////////////////////
|
125 |
//takes the size of a String include the null in the end
|
126 |
//////////////////////////////////////////////////////////////////////////////////////
|
127 |
|
128 |
int StringTools::sizeOfString(char *source)
|
129 |
{
|
130 |
int size=0;
|
131 |
|
132 |
while ( source[size] != 0 )
|
133 |
{
|
134 |
size++;
|
135 |
}
|
136 |
|
137 |
size++;
|
138 |
|
139 |
return size;
|
140 |
}
|
141 |
|
142 |
//////////////////////////////////////////////////////////////////////////////////////
|
143 |
//takes the size of a String includes not the null in the end
|
144 |
//////////////////////////////////////////////////////////////////////////////////////
|
145 |
|
146 |
int StringTools::sizeOfStringWithOutEndNull(char *source)
|
147 |
{
|
148 |
int size=0;
|
149 |
|
150 |
while ( source[size] != 0 )
|
151 |
{
|
152 |
size++;
|
153 |
}
|
154 |
|
155 |
return size;
|
156 |
}
|
157 |
|
158 |
|
159 |
//////////////////////////////////////////////////////////////////////////////////////
|
160 |
//takes the size of a String include the null in the end
|
161 |
//////////////////////////////////////////////////////////////////////////////////////
|
162 |
|
163 |
int StringTools::sizeOfString(char *source, char *endSource)
|
164 |
{
|
165 |
int size=0;
|
166 |
|
167 |
bool statement=true;
|
168 |
|
169 |
while (statement)
|
170 |
{
|
171 |
if (source < endSource)
|
172 |
{
|
173 |
if (source[0] != 0)
|
174 |
{
|
175 |
source++;
|
176 |
size++;
|
177 |
}
|
178 |
else
|
179 |
{
|
180 |
statement=false;
|
181 |
}
|
182 |
}
|
183 |
else
|
184 |
{
|
185 |
statement=false;
|
186 |
}
|
187 |
}
|
188 |
|
189 |
size++;
|
190 |
|
191 |
return size;
|
192 |
}
|
193 |
|
194 |
|
195 |
//////////////////////////////////////////////////////////////////////////////////////
|
196 |
//copy a String to another destination includes the null in the of the String
|
197 |
//////////////////////////////////////////////////////////////////////////////////////
|
198 |
|
199 |
void StringTools::copyString(char *source, char *dest)
|
200 |
{
|
201 |
int size=sizeOfString(source);
|
202 |
|
203 |
int s;
|
204 |
|
205 |
for ( s=0; s<(size-1) ; s++ )
|
206 |
{
|
207 |
dest[s]=source[s];
|
208 |
}
|
209 |
|
210 |
dest[s]=0;
|
211 |
}
|
212 |
|
213 |
//////////////////////////////////////////////////////////////////////////////////////
|
214 |
//copy a String to another destination includes the null in the of the String
|
215 |
//////////////////////////////////////////////////////////////////////////////////////
|
216 |
|
217 |
void StringTools::copyString(char *source, int sourceSize, char *dest)
|
218 |
{
|
219 |
|
220 |
/* int size=sourceSize/4;
|
221 |
int rest=sourceSize%4;
|
222 |
|
223 |
int *intSource=(int*)source;
|
224 |
int *intDest=(int*)dest;
|
225 |
|
226 |
for ( int i=0; i<size ; i++ )
|
227 |
{
|
228 |
intDest[0]=intSource[0];
|
229 |
intDest++;
|
230 |
intSource++;
|
231 |
}
|
232 |
|
233 |
source=(char*)intSource;
|
234 |
dest=(char*)intDest;
|
235 |
|
236 |
for (int s=0; s<rest ; s++ )
|
237 |
{
|
238 |
dest[0]=source[0];
|
239 |
dest++;
|
240 |
source++;
|
241 |
}
|
242 |
*/
|
243 |
|
244 |
for (int s=0; s<sourceSize ; s++ )
|
245 |
{
|
246 |
dest[0]=source[0];
|
247 |
dest++;
|
248 |
source++;
|
249 |
}
|
250 |
|
251 |
dest[0]=0;
|
252 |
}
|
253 |
|
254 |
|
255 |
|
256 |
//////////////////////////////////////////////////////////////////////////////////////
|
257 |
//add two Strings together and returns a new String
|
258 |
//////////////////////////////////////////////////////////////////////////////////////
|
259 |
|
260 |
char *StringTools::addStringTwoString(char *source, char *source2)
|
261 |
{
|
262 |
int forward=0;
|
263 |
|
264 |
int sourceSize=sizeOfString(source)-1;
|
265 |
int source2Size=sizeOfString(source2);
|
266 |
|
267 |
char *buffer=(char*)malloc(sourceSize+source2Size);
|
268 |
|
269 |
for ( int s=0 ; s<sourceSize ; s++ )
|
270 |
{
|
271 |
buffer[forward]=source[s];
|
272 |
forward++;
|
273 |
}
|
274 |
|
275 |
for ( int s2=0 ; s2<source2Size ; s2++ )
|
276 |
{
|
277 |
buffer[forward]=source2[s2];
|
278 |
forward++;
|
279 |
}
|
280 |
|
281 |
return buffer;
|
282 |
}
|
283 |
|
284 |
//////////////////////////////////////////////////////////////////////////////////////
|
285 |
//add two Strings together and returns a new String
|
286 |
//////////////////////////////////////////////////////////////////////////////////////
|
287 |
|
288 |
char *StringTools::addStringTwoString(char *source, int sourceSize, char *source2, int sourceSize2)
|
289 |
{
|
290 |
int forward=0;
|
291 |
|
292 |
sourceSize2++;
|
293 |
|
294 |
char *buffer=(char*)malloc(sourceSize+sourceSize2);
|
295 |
|
296 |
for ( int s=0 ; s<sourceSize ; s++ )
|
297 |
{
|
298 |
buffer[forward]=source[s];
|
299 |
forward++;
|
300 |
}
|
301 |
|
302 |
for ( int s2=0 ; s2<(sourceSize2) ; s2++ )
|
303 |
{
|
304 |
buffer[forward]=source2[s2];
|
305 |
forward++;
|
306 |
}
|
307 |
|
308 |
return buffer;
|
309 |
}
|
310 |
|
311 |
|
312 |
//////////////////////////////////////////////////////////////////////////////////////
|
313 |
//compare two Strings against each other, without the null in the end of the Strings
|
314 |
//////////////////////////////////////////////////////////////////////////////////////
|
315 |
|
316 |
bool StringTools::isStringSmaller(char *source, int sourceSize, char *source2, int sourceSize2)
|
317 |
{
|
318 |
bool statement=true;
|
319 |
bool equalStatement=true;
|
320 |
int size=0;
|
321 |
|
322 |
if (sourceSize<sourceSize2)
|
323 |
{
|
324 |
size=sourceSize2;
|
325 |
}
|
326 |
else
|
327 |
{
|
328 |
size=sourceSize;
|
329 |
}
|
330 |
|
331 |
// --size;
|
332 |
|
333 |
for ( int r=0; r<size ; r++ )
|
334 |
{
|
335 |
if ( source[r] < source2[r] )
|
336 |
{
|
337 |
statement=false;
|
338 |
|
339 |
if ( source[r] != source2[r] )
|
340 |
{
|
341 |
equalStatement=false;
|
342 |
}
|
343 |
|
344 |
break;
|
345 |
}
|
346 |
else
|
347 |
{
|
348 |
if ( source[r] != source2[r] )
|
349 |
{
|
350 |
equalStatement=false;
|
351 |
break;
|
352 |
}
|
353 |
}
|
354 |
|
355 |
if ( source[r] != source2[r] )
|
356 |
{
|
357 |
equalStatement=false;
|
358 |
}
|
359 |
|
360 |
}
|
361 |
|
362 |
if (equalStatement == true)
|
363 |
{
|
364 |
if (sourceSize > sourceSize2 )
|
365 |
{
|
366 |
statement=false;
|
367 |
}
|
368 |
}
|
369 |
|
370 |
return statement;
|
371 |
}
|
372 |
|
373 |
bool StringTools::isStringLarger(char *source, int sourceSize, char *source2, int sourceSize2)
|
374 |
{
|
375 |
return isStringSmaller(source2, sourceSize2, source, sourceSize);
|
376 |
}
|
377 |
|
378 |
|
379 |
//////////////////////////////////////////////////////////////////////////////////////
|
380 |
//compare two Strings against each other, without the null in the end of the Strings
|
381 |
//////////////////////////////////////////////////////////////////////////////////////
|
382 |
|
383 |
bool StringTools::isStringValueSmaller(char *source, int sourceSize, char *source2, int sourceSize2)
|
384 |
{
|
385 |
bool statement=true;
|
386 |
bool equalStatement=true;
|
387 |
|
388 |
if (sourceSize<sourceSize2)
|
389 |
{
|
390 |
return true;
|
391 |
}
|
392 |
|
393 |
if (sourceSize>sourceSize2)
|
394 |
{
|
395 |
return false;
|
396 |
}
|
397 |
|
398 |
for ( int r=0; r<sourceSize ; r++ )
|
399 |
{
|
400 |
if ( source[r] < source2[r] )
|
401 |
{
|
402 |
return true;
|
403 |
}
|
404 |
}
|
405 |
|
406 |
return false;
|
407 |
}
|
408 |
|
409 |
bool StringTools::isStringValueLarger(char *source, int sourceSize, char *source2, int sourceSize2)
|
410 |
{
|
411 |
return isStringValueSmaller(source2, sourceSize2, source, sourceSize);
|
412 |
}
|
413 |
|
414 |
//////////////////////////////////////////////////////////////////////////////////////
|
415 |
//find a chareter , seeking back, returning the position on it
|
416 |
//////////////////////////////////////////////////////////////////////////////////////
|
417 |
|
418 |
int StringTools::backFind(char *source, char target)
|
419 |
{
|
420 |
int size=sizeOfString(source)-1;
|
421 |
|
422 |
int position=size;
|
423 |
|
424 |
while ( position >= 0 && source[position] != target )
|
425 |
{
|
426 |
--position;
|
427 |
}
|
428 |
|
429 |
return position;
|
430 |
}
|
431 |
|
432 |
|
433 |
//////////////////////////////////////////////////////////////////////////////////////
|
434 |
//it seeks back and when it find the character it is looking for it takes the data that is forward and returns it
|
435 |
//////////////////////////////////////////////////////////////////////////////////////
|
436 |
|
437 |
char *StringTools::cutTheBack(char * word, int *length, char target)
|
438 |
{
|
439 |
if (word != null)
|
440 |
{
|
441 |
int position=backFind(word,target);
|
442 |
|
443 |
//cout << position << endl;
|
444 |
|
445 |
if (position >= 0)
|
446 |
{
|
447 |
char *tempWord=word;
|
448 |
word+=position+1;
|
449 |
|
450 |
int size=sizeOfStringWithOutEndNull(word);
|
451 |
length[0]=size;
|
452 |
if (size>0)
|
453 |
{
|
454 |
char *tempWord2=(char*)malloc(size*sizeof(char)+sizeof(char));
|
455 |
copyString(word,tempWord2);
|
456 |
word=tempWord2;
|
457 |
free(tempWord);
|
458 |
}
|
459 |
else
|
460 |
{
|
461 |
free(tempWord);
|
462 |
word=null;
|
463 |
length[0]=0;
|
464 |
}
|
465 |
}
|
466 |
else
|
467 |
{
|
468 |
free(word);
|
469 |
word=null;
|
470 |
length[0]=0;
|
471 |
}
|
472 |
}
|
473 |
|
474 |
return word;
|
475 |
}
|
476 |
|
477 |
char *StringTools::cutTheBegining(char * word, int *length, char target)
|
478 |
{
|
479 |
if ( word != null )
|
480 |
{
|
481 |
int position=backFind(word,target);
|
482 |
|
483 |
//cout << position << endl;
|
484 |
|
485 |
if (position >= 0)
|
486 |
{
|
487 |
word[position]=0;
|
488 |
length[0]=sizeOfStringWithOutEndNull(word);
|
489 |
}
|
490 |
else
|
491 |
{
|
492 |
if(word!=null)
|
493 |
{
|
494 |
free(word);
|
495 |
word=null;
|
496 |
length[0]=0;
|
497 |
}
|
498 |
}
|
499 |
}
|
500 |
|
501 |
return word;
|
502 |
}
|
503 |
|
504 |
bool StringTools::checkBack(char *file, int fileSize, char *fileFormat, int fileFormatSize)
|
505 |
{
|
506 |
if ( fileFormatSize <= fileSize)
|
507 |
{
|
508 |
fileSize-=fileFormatSize;
|
509 |
return compareString((file+fileSize), fileFormatSize, fileFormat, fileFormatSize);
|
510 |
}
|
511 |
|
512 |
return false;
|
513 |
}
|
514 |
|
515 |
|
516 |
bool StringTools::checkFront(char *file, int fileSize, char *fileFormat, int fileFormatSize)
|
517 |
{
|
518 |
if ( fileFormatSize <= fileSize)
|
519 |
{
|
520 |
return compareString(file, fileFormatSize, fileFormat, fileFormatSize);
|
521 |
}
|
522 |
|
523 |
return false;
|
524 |
}
|
525 |
|
526 |
bool StringTools::theFilePathIsNotUnder(char *buffer, int length, char *filePath, int filePathLength)
|
527 |
{
|
528 |
length++;
|
529 |
filePathLength++;
|
530 |
|
531 |
char *tempBuffer=(char*)malloc(length*sizeof(char));
|
532 |
copyString(buffer,tempBuffer);
|
533 |
|
534 |
char *backPath="/../";
|
535 |
int backPathSize=5;
|
536 |
|
537 |
int i=0;
|
538 |
|
539 |
while(tempBuffer[i]!=0)
|
540 |
{
|
541 |
if( tempBuffer[i] == backPath[0] || (tempBuffer[i] == backPath[1] && i == 0))
|
542 |
{
|
543 |
int stepForward=0;
|
544 |
|
545 |
if ((tempBuffer[i] == backPath[1] && i == 0))
|
546 |
{
|
547 |
stepForward=1;
|
548 |
}
|
549 |
|
550 |
bool correct=true;
|
551 |
|
552 |
if ( (i+backPathSize) <= length)
|
553 |
{
|
554 |
for ( int b=0; b<(backPathSize-1-stepForward) ; b++ )
|
555 |
{
|
556 |
if (backPath[b+stepForward] != tempBuffer[i+b])
|
557 |
{
|
558 |
correct=false;
|
559 |
break;
|
560 |
}
|
561 |
}
|
562 |
}
|
563 |
else
|
564 |
{
|
565 |
correct=false;
|
566 |
}
|
567 |
|
568 |
if(correct)
|
569 |
{
|
570 |
int lastPosition=0;
|
571 |
int position=i-1;
|
572 |
|
573 |
if ( position >= 0 )
|
574 |
{
|
575 |
for ( int k=0; k<(i-1) ; k++ )
|
576 |
{
|
577 |
if ( tempBuffer[position] == backPath[0] )
|
578 |
{
|
579 |
lastPosition=position;
|
580 |
break;
|
581 |
}
|
582 |
--position;
|
583 |
}
|
584 |
}
|
585 |
else
|
586 |
{
|
587 |
lastPosition=0;
|
588 |
}
|
589 |
|
590 |
|
591 |
moveBack(tempBuffer, lastPosition, i+(backPathSize-3-stepForward), length);
|
592 |
i=lastPosition-1;
|
593 |
|
594 |
if ( i < -1 )
|
595 |
{
|
596 |
i=-1;
|
597 |
}
|
598 |
}
|
599 |
|
600 |
}
|
601 |
i++;
|
602 |
}
|
603 |
|
604 |
|
605 |
char *memTemp=tempBuffer;
|
606 |
|
607 |
if ( tempBuffer[0] == '/' )
|
608 |
{
|
609 |
tempBuffer++;
|
610 |
}
|
611 |
|
612 |
cout << tempBuffer << endl;
|
613 |
|
614 |
int tempBufferLength=sizeOfString(tempBuffer);
|
615 |
|
616 |
bool status=compareStringAPart(filePath, filePathLength,tempBuffer, tempBufferLength);
|
617 |
|
618 |
free(memTemp);
|
619 |
|
620 |
return status;
|
621 |
}
|
622 |
|
623 |
void StringTools::moveBack(char *tempBuffer, int forward, int howLong, int length)
|
624 |
{
|
625 |
howLong++;
|
626 |
int kSize=0;
|
627 |
|
628 |
if ( howLong < length )
|
629 |
{
|
630 |
int k=howLong;
|
631 |
|
632 |
while ( tempBuffer[k] != 0 )
|
633 |
{
|
634 |
kSize++;
|
635 |
k++;
|
636 |
}
|
637 |
|
638 |
kSize++;
|
639 |
|
640 |
int l;
|
641 |
|
642 |
for ( l=0; l<kSize ; l++ )
|
643 |
{
|
644 |
tempBuffer[forward+l]=tempBuffer[l+howLong];
|
645 |
}
|
646 |
|
647 |
|
648 |
kSize+=forward;
|
649 |
tempBuffer[kSize]=0;
|
650 |
}
|
651 |
}
|