Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A string utility class and a new Exception (used to throw out of band exception)
[simgrid.git] / src / cxx / StringHelper.cxx
1 #include <StringHelper.hpp>\r
2 \r
3 #include <BadAllocException.hpp>\r
4 #include <NullPointerException.hpp>\r
5 #include <InvalidArgumentException.hpp>\r
6 #include <OutOfBoundsException.hpp>\r
7 \r
8 #ifndef BUFF_MAX\r
9 #define BUFF_MAX ((size_t)260)\r
10 #endif // BUFF_MAX\r
11 \r
12 \r
13 // namespace SimGrid::Msg\r
14 namespace SimGrid\r
15 {\r
16         namespace Msg\r
17         {\r
18 \r
19                 #define DEFAULT_STRING_HELPER_CAPACITY ((int)128)\r
20                 \r
21 \r
22                 void StringHelper::init(void)\r
23                 {\r
24                         capacity = DEFAULT_STRING_HELPER_CAPACITY;\r
25 \r
26                         if(!(content = (char*) calloc(capacity + 1, sizeof(char))))\r
27                                 throw BadAllocException();\r
28 \r
29                         len = 0;\r
30                 }\r
31                                 \r
32         // Default constructor\r
33                 StringHelper::StringHelper()\r
34                 {\r
35                         init();\r
36                 }\r
37 \r
38                 StringHelper::StringHelper(char c)\r
39                 {\r
40                         init();\r
41                         append(c);\r
42 \r
43                 }\r
44                 \r
45                 StringHelper::StringHelper(char c, int n)\r
46                 {\r
47                         init();\r
48                         append(c, n);\r
49                 }\r
50                 \r
51                 StringHelper::StringHelper(const char* cstr)\r
52                 {\r
53                         init();\r
54                         append(cstr);\r
55                 }\r
56                 \r
57                 StringHelper::StringHelper(const char* cstr, int n)\r
58                 {\r
59                         init();\r
60                         append(cstr, n);\r
61                 }\r
62                 \r
63                 StringHelper::StringHelper(const char* cstr, int pos, int n)\r
64                 {\r
65                         init();\r
66                         append(cstr, pos, n);\r
67                 }\r
68                 \r
69                 StringHelper::StringHelper(const string& rstr)\r
70                 {\r
71                         init();\r
72                         append(rstr);\r
73                 }\r
74                 \r
75                 StringHelper::StringHelper(const string& rstr, int n)\r
76                 {\r
77                         init();\r
78                         append(rstr, n);\r
79                 }\r
80                 \r
81                 StringHelper::StringHelper(const string& rstr, int pos, int n)\r
82                 {\r
83                         init();\r
84                         append(rstr, pos, n);\r
85                 }\r
86                 \r
87                 StringHelper::StringHelper(short si)\r
88                 {\r
89                         init();\r
90                         append(si);\r
91                 }\r
92                 \r
93                 StringHelper::StringHelper(int i)\r
94                 {\r
95                         init();\r
96                         append(i);\r
97                 }\r
98                 \r
99                 StringHelper::StringHelper(long l)\r
100                 {\r
101                         init();\r
102                         append(l);\r
103                 }\r
104                 \r
105                 StringHelper::StringHelper(float f)\r
106                 {\r
107                         init();\r
108                         append(f);\r
109                 }\r
110                 \r
111                 StringHelper::StringHelper(double d)\r
112                 {\r
113                         init();\r
114                         append(d);\r
115                 }\r
116 \r
117                 StringHelper::StringHelper(double d, const char* format)\r
118                 {\r
119                         char toAppend[BUFF_MAX + 1] = {0};\r
120                         \r
121                         sprintf(toAppend,format,d);\r
122 \r
123                         init();\r
124 \r
125                         append(toAppend);\r
126                 }\r
127                 \r
128                 StringHelper::StringHelper(unsigned short usi)\r
129                 {\r
130                         init();\r
131                         append(usi);\r
132                 }\r
133                 \r
134                 StringHelper::StringHelper(unsigned int ui)\r
135                 {\r
136                         init();\r
137                         append(ui);\r
138                 }\r
139                 \r
140                 StringHelper::StringHelper(unsigned long ul)\r
141                 {\r
142                         init();\r
143                         append(ul);\r
144                 }\r
145         \r
146         // Copy constructor\r
147                 StringHelper::StringHelper(const StringHelper& rStringHelper)\r
148                 {\r
149                         if(this != &rStringHelper && rStringHelper.size())\r
150                         {\r
151                                 clear();\r
152                                 append(rStringHelper.cstr());\r
153                         }\r
154                 }\r
155 \r
156         // Destructor\r
157                 StringHelper::~StringHelper()\r
158                 {\r
159                         if(content)\r
160                                 free(content);\r
161                 }\r
162 \r
163         // Operations\r
164 \r
165                 void StringHelper::clear(void)\r
166                 {\r
167                         if(len)\r
168                                 memset(content, 0, len);\r
169 \r
170                         len = 0;\r
171                 }\r
172 \r
173                 bool StringHelper::empty(void)\r
174                 {\r
175                         return len == 0;\r
176                 }\r
177         \r
178                 StringHelper& StringHelper::append(unsigned char c)\r
179                 {\r
180                         if(capacity < len + 1)\r
181                         {\r
182                                 int new_capacity = (capacity << 1) ;\r
183 \r
184                                 if(!(content = (char*) realloc(content, new_capacity)))\r
185                                         throw BadAllocException();\r
186 \r
187                                 capacity = new_capacity;\r
188                         }\r
189                         \r
190 \r
191                         content[len] = c;\r
192                         len++;\r
193 \r
194                         content[len] = '\0';\r
195 \r
196                         return *this;\r
197                 }\r
198 \r
199                 StringHelper& StringHelper::append(unsigned char c, int n)\r
200                 {\r
201                         if(n <=0)\r
202                                 throw InvalidArgumentException("n");\r
203 \r
204                         char* toAppend = (char*) calloc(n + 1, sizeof(char));\r
205 \r
206                         if(!toAppend)\r
207                                 throw BadAllocException();\r
208 \r
209                         memset(toAppend, c, n); \r
210 \r
211                         append(toAppend);\r
212 \r
213                         free(toAppend);\r
214 \r
215                         return *this;\r
216                 }\r
217 \r
218         \r
219                 StringHelper& StringHelper::append(char c)\r
220                 {\r
221                         if(capacity < len + 1)\r
222                         {\r
223                                 int new_capacity = (capacity << 1) ;\r
224 \r
225                                 if(!(content = (char*) realloc(content, new_capacity)))\r
226                                         throw BadAllocException();\r
227 \r
228                                 capacity = new_capacity;\r
229                         }\r
230                         \r
231 \r
232                         content[len] = c;\r
233                         len++;\r
234 \r
235                         content[len] = '\0';\r
236 \r
237                         return *this;\r
238                 }\r
239                 \r
240                 StringHelper& StringHelper::append(char c, int n)\r
241                 {\r
242                         if(n <=0)\r
243                                 throw InvalidArgumentException("n");\r
244 \r
245                         char* toAppend = (char*) calloc(n + 1, sizeof(char));\r
246 \r
247                         if(!toAppend)\r
248                                 throw BadAllocException();\r
249 \r
250                         memset(toAppend, c, n); \r
251 \r
252                         append(toAppend);\r
253 \r
254                         free(toAppend);\r
255 \r
256                         return *this;\r
257                 }\r
258                 \r
259                 StringHelper& StringHelper::append(const char* cstr)\r
260                 {\r
261                         if(!cstr)\r
262                                 throw NullPointerException("cstr");\r
263                         \r
264                         int l =  (int) strlen(cstr);\r
265 \r
266                         if(capacity < len + l)\r
267                         {\r
268                                 int new_capacity = (capacity << 1) < (len + l) ? (len + l) << 1 : capacity << 1;\r
269 \r
270                                 if(!(content = (char*) realloc(content, new_capacity)))\r
271                                         throw BadAllocException();\r
272 \r
273                                 strcat(content, cstr);\r
274 \r
275                                 capacity = new_capacity;\r
276                         }\r
277                         else\r
278                         {\r
279                                 strcat(content, cstr);\r
280                         }\r
281 \r
282                         len += l;\r
283                         content[len] = '\0';\r
284 \r
285                         return *this;\r
286                         \r
287                 }\r
288                 \r
289                 StringHelper& StringHelper::append(const char* cstr, int n)\r
290                 {\r
291                         if(!cstr)\r
292                                 throw NullPointerException("cstr");\r
293 \r
294                         if(n <= 0)\r
295                                 throw InvalidArgumentException("n");\r
296 \r
297                         \r
298                         int l =  ((int) strlen(cstr)) * n;\r
299 \r
300                         if(capacity < len + l)\r
301                         {\r
302                                 int new_capacity = (capacity << 1) < (len + l) ? (len + l) << 1 : capacity << 1;\r
303 \r
304                                 if(!(content = (char*) realloc(content, new_capacity)))\r
305                                         throw BadAllocException();\r
306                                 \r
307                                 for(int i = 0; i < n; i++)\r
308                                         strcat(content, cstr);\r
309 \r
310                                 capacity = new_capacity;\r
311                         }\r
312                         else\r
313                         {\r
314                                 for(int i = 0; i < n; i++)\r
315                                         strcat(content, cstr);\r
316                         }\r
317 \r
318                         len += l;\r
319                         content[len] = '\0';\r
320 \r
321                         return *this;\r
322 \r
323                 }\r
324                 \r
325                 StringHelper& StringHelper::append(const char* cstr, int pos, int n)\r
326                 {\r
327                         if(!cstr)\r
328                                 throw NullPointerException("cstr");\r
329 \r
330                         if(n <= 0)\r
331                                 throw InvalidArgumentException("n");\r
332 \r
333                         if(pos < 0 || pos >= (int)strlen(cstr) )\r
334                                 throw OutOfBoundsException(pos);\r
335 \r
336                         if(pos + n >= (int)strlen(cstr))\r
337                                 throw OutOfBoundsException(pos, n);\r
338 \r
339 \r
340                         char* toAppend = (char*) calloc(n + 1, sizeof(char));\r
341                         \r
342                         strncpy(toAppend, cstr + pos, n);\r
343 \r
344                         append(toAppend);\r
345 \r
346                         free(toAppend);\r
347 \r
348                         return *this;\r
349                 }\r
350                 \r
351                 StringHelper& StringHelper::append(const string& rstr)\r
352                 {\r
353                         append(rstr.c_str());\r
354                         return *this;\r
355                 }\r
356                 \r
357                 StringHelper& StringHelper::append(const string& rstr, int n)\r
358                 {\r
359                         if(rstr.empty())\r
360                                 throw NullPointerException("rstr");\r
361 \r
362                         if(n <= 0)\r
363                                 throw InvalidArgumentException("n");\r
364 \r
365                         \r
366                         int l =  ((int) rstr.size()) * n;\r
367 \r
368                         if(capacity < len + l)\r
369                         {\r
370                                 int new_capacity = (capacity << 1) < (len + l) ? (len + l) << 1 : capacity << 1;\r
371 \r
372                                 if(!(content = (char*) realloc(content, new_capacity)))\r
373                                         throw BadAllocException();\r
374                                 \r
375                                 for(int i = 0; i < n; i++)\r
376                                         strcat(content, rstr.c_str());\r
377 \r
378                                 capacity = new_capacity;\r
379                         }\r
380                         else\r
381                         {\r
382                                 for(int i = 0; i < n; i++)\r
383                                         strcat(content, rstr.c_str());\r
384                         }\r
385 \r
386                         len += l;\r
387                         content[len] = '\0';\r
388 \r
389                         return *this;\r
390                 }\r
391                 \r
392                 StringHelper& StringHelper::append(const string& rstr, int pos, int n)\r
393                 {\r
394                         if(rstr.empty())\r
395                                 throw InvalidArgumentException("rstr");\r
396 \r
397                         if(n <= 0)\r
398                                 throw InvalidArgumentException("n");\r
399 \r
400                         if(pos < 0 || pos >= (int) rstr.size() )\r
401                                 throw OutOfBoundsException(pos);\r
402 \r
403                         if(pos + n >=  (int) rstr.size())\r
404                                 throw OutOfBoundsException(pos, n);\r
405 \r
406 \r
407                         char* toAppend = (char*) calloc(n + 1, sizeof(char));\r
408                         \r
409                         strncpy(toAppend, rstr.c_str() + pos, n);\r
410 \r
411                         append(toAppend);\r
412 \r
413                         free(toAppend);\r
414 \r
415                         return *this;\r
416                 }\r
417                 \r
418                 StringHelper& StringHelper::append(short si)\r
419                 {\r
420                         char toAppend[26] = {0};\r
421 \r
422                         sprintf(toAppend, "%hd",si);\r
423 \r
424                         append(toAppend);\r
425 \r
426                         return *this;\r
427                 }\r
428                 \r
429                 StringHelper& StringHelper::append(int i)\r
430                 {\r
431                         char toAppend[26] = {0};\r
432 \r
433                         sprintf(toAppend, "%d",i);\r
434 \r
435                         append(toAppend);\r
436 \r
437                         return *this;\r
438                 }\r
439                 \r
440                 StringHelper& StringHelper::append(long l)\r
441                 {\r
442                         char toAppend[26] = {0};\r
443 \r
444                         sprintf(toAppend, "%ld",l);\r
445 \r
446                         append(toAppend);\r
447 \r
448                         return *this;\r
449                 }\r
450                 \r
451                 StringHelper& StringHelper::append(float f)\r
452                 {\r
453                         char toAppend[26] = {0};\r
454 \r
455                         sprintf(toAppend, "%f",f);\r
456 \r
457                         append(toAppend);\r
458 \r
459                         return *this;\r
460                 }\r
461                 \r
462                 StringHelper& StringHelper::append(double d)\r
463                 {\r
464                         char toAppend[26] = {0};\r
465 \r
466                         sprintf(toAppend, "%lf",d);\r
467 \r
468                         append(toAppend);\r
469 \r
470                         return *this;\r
471                 }\r
472 \r
473                 StringHelper& StringHelper::append(double d, const char* format)\r
474                 {\r
475                         char toAppend[BUFF_MAX + 1] = {0};\r
476 \r
477                         sprintf(toAppend, format, d);\r
478 \r
479                         append(toAppend);\r
480 \r
481                         return *this;\r
482                 }\r
483                 \r
484                 StringHelper& StringHelper::append(unsigned short usi)\r
485                 {\r
486                         char toAppend[26] = {0};\r
487 \r
488                         sprintf(toAppend, "%hu",usi);\r
489 \r
490                         append(toAppend);\r
491 \r
492                         return *this;\r
493                 }\r
494                 \r
495                 StringHelper& StringHelper::append(unsigned int ui)\r
496                 {\r
497                         char toAppend[26] = {0};\r
498 \r
499                         sprintf(toAppend, "%u",ui);\r
500 \r
501                         append(toAppend);\r
502 \r
503                         return *this;\r
504                 }\r
505                 \r
506                 StringHelper& StringHelper::append(unsigned long ul)\r
507                 {\r
508                         char toAppend[26] = {0};\r
509 \r
510                         sprintf(toAppend, "%lu",ul);\r
511 \r
512                         append(toAppend);\r
513 \r
514                         return *this;\r
515                 }\r
516                         \r
517                 const char& StringHelper::at(int pos) const\r
518                 {\r
519                         if(pos < 0 || pos >= len)\r
520                                 throw OutOfBoundsException(pos);\r
521                         \r
522                         return content[pos];\r
523                 }\r
524                 \r
525                 char& StringHelper::at(int pos)\r
526                 {\r
527                         if(pos < 0 || pos >= len)\r
528                                 throw OutOfBoundsException(pos);\r
529                         \r
530                         return content[pos];\r
531                 }\r
532                 \r
533                 const char* StringHelper::cstr(void) const\r
534                 {\r
535                         return (const char*)content;\r
536                 }\r
537                 \r
538                 string& StringHelper::toString(void)\r
539                 {\r
540                         string* s = new string();\r
541                         s->append(content);\r
542                         return *s;\r
543                 }\r
544                 \r
545                 int StringHelper::size(void) const\r
546                 {\r
547                         return len;\r
548                 }\r
549                 \r
550         //  Operators\r
551                 \r
552                 // Assignement\r
553                 StringHelper& StringHelper::operator = (const StringHelper& rStringHelper)\r
554                 {\r
555 \r
556                         if(this !=&rStringHelper && rStringHelper.size())\r
557                         {\r
558                                 clear();\r
559                                 append(rStringHelper.cstr());\r
560                         }\r
561 \r
562                         return *this;\r
563                 }\r
564                 \r
565                 StringHelper& StringHelper::operator = (const char* cstr)\r
566                 {\r
567                         clear();\r
568                         append(cstr);\r
569                         return *this;\r
570                 }\r
571                 \r
572                 StringHelper& StringHelper::operator = (const string& str)\r
573                 {\r
574                         clear();\r
575                         append(str);\r
576                         return *this;\r
577                 }\r
578                 \r
579                 StringHelper& StringHelper::operator = (short n)\r
580                 {\r
581                         clear();\r
582                         append(n);\r
583                         return *this;\r
584                 }\r
585                 \r
586                 StringHelper& StringHelper::operator = (int n)\r
587                 {\r
588                         clear();\r
589                         append(n);\r
590                         return *this;\r
591                 }\r
592                 \r
593                 StringHelper& StringHelper::operator = (long n)\r
594                 {\r
595                         clear();\r
596                         append(n);\r
597                         return *this;\r
598                 }\r
599                 \r
600                 StringHelper& StringHelper::operator = (float n)\r
601                 {\r
602                         clear();\r
603                         append(n);\r
604                         return *this;\r
605                 }\r
606                 \r
607                 StringHelper& StringHelper::operator = (double n)\r
608                 {\r
609                         clear();\r
610                         append(n);\r
611                         return *this;\r
612                 }\r
613                 \r
614                 StringHelper& StringHelper::operator = (unsigned short n)\r
615                 {\r
616                         clear();\r
617                         append(n);\r
618                         return *this;\r
619                 }\r
620                 \r
621                 StringHelper& StringHelper::operator = (unsigned int n)\r
622                 {\r
623                         clear();\r
624                         append(n);\r
625                         return *this;\r
626                 }\r
627                 \r
628                 StringHelper& StringHelper::operator = (unsigned long n)\r
629                 {\r
630                         clear();\r
631                         append(n);\r
632                         return *this;\r
633                 }\r
634                 \r
635                 char& StringHelper::operator[](int pos)\r
636                 {\r
637                         if(pos < 0 || pos >= len)\r
638                                 throw OutOfBoundsException(pos);\r
639                         \r
640                         return content[pos];    \r
641                 }\r
642                 \r
643                 char StringHelper::operator[](int pos) const\r
644                 {\r
645                         if(pos < 0 || pos >= len)\r
646                                 throw OutOfBoundsException(pos);\r
647                         \r
648                         return content[pos];\r
649                 }\r
650                 \r
651                 StringHelper& StringHelper::operator += (short n)\r
652                 {\r
653                         append(n);\r
654                         return *this;\r
655                 }\r
656                 \r
657                 StringHelper& StringHelper::operator += (int n)\r
658                 {\r
659                         append(n);\r
660                         return *this;\r
661                 }\r
662                 \r
663                 StringHelper& StringHelper::operator += (long n)\r
664                 {\r
665                         append(n);\r
666                         return *this;\r
667                 }\r
668                 \r
669                 StringHelper& StringHelper::operator += (float n)\r
670                 {\r
671                         append(n);\r
672                         return *this;\r
673                 }\r
674                 \r
675                 StringHelper& StringHelper::operator += (double n)\r
676                 {\r
677                         append(n);\r
678                         return *this;\r
679                 }\r
680                 \r
681                 StringHelper& StringHelper::operator += (unsigned short n)\r
682                 {\r
683                         append(n);\r
684                         return *this;\r
685                 }\r
686                 \r
687                 StringHelper& StringHelper::operator += (unsigned int n)\r
688                 {\r
689                         append(n);\r
690                         return *this;\r
691                 }\r
692                 \r
693                 StringHelper& StringHelper::operator += (unsigned long n)\r
694                 {\r
695                         append(n);\r
696                         return *this;\r
697                 }\r
698                 \r
699                 StringHelper& StringHelper::operator += (const StringHelper& rStringHelper)\r
700                 {\r
701                         append(rStringHelper.content);\r
702                         return *this;\r
703                 }\r
704 \r
705                 StringHelper& StringHelper::operator += (const string& rstr)\r
706                 {\r
707                         append(rstr.c_str());\r
708                         return *this;\r
709                 }\r
710                 \r
711                 StringHelper& StringHelper::operator += (const char* cstr)\r
712                 {\r
713                         append(cstr);\r
714                         return *this;\r
715                 }\r
716                 \r
717                 StringHelper& StringHelper::operator += (char c)\r
718                 {\r
719                         append(c);\r
720                         return *this;\r
721                 }\r
722                 \r
723                 StringHelper& StringHelper::operator + (short n)\r
724                 {\r
725                         append(n);\r
726                         return *this;\r
727                 }\r
728                 \r
729                 StringHelper& StringHelper::operator + (int n)\r
730                 {\r
731                         append(n);\r
732                         return *this;\r
733                 }\r
734                 \r
735                 StringHelper& StringHelper::operator + (long n)\r
736                 {\r
737                         append(n);\r
738                         return *this;\r
739                 }\r
740                 \r
741                 StringHelper& StringHelper::operator + (float n)\r
742                 {\r
743                         append(n);\r
744                         return *this;\r
745                 }\r
746                 \r
747                 StringHelper& StringHelper::operator + (double n)\r
748                 {\r
749                         append(n);\r
750                         return *this;\r
751                 }\r
752                 \r
753                 StringHelper& StringHelper::operator + (unsigned short n)\r
754                 {\r
755                         append(n);\r
756                         return *this;\r
757                 }\r
758                 \r
759                 StringHelper& StringHelper::operator + (unsigned int n)\r
760                 {\r
761                         append(n);\r
762                         return *this;\r
763                 }\r
764                 \r
765                 StringHelper& StringHelper::operator + (unsigned long n)\r
766                 {\r
767                         append(n);\r
768                         return *this;\r
769                 }\r
770                 \r
771                 \r
772                 StringHelper& StringHelper::operator + (const StringHelper& rStringHelper)\r
773                 {\r
774                         append(rStringHelper.content);\r
775                         return *this;\r
776                 }\r
777                 \r
778                 StringHelper& StringHelper::operator + (const string& rstr)\r
779                 {\r
780                         append(rstr.c_str());\r
781                         return *this;\r
782                 }\r
783                 \r
784                 StringHelper& StringHelper::operator + (const char* cstr)\r
785                 {\r
786                         append(cstr);\r
787                         return *this;\r
788                 }\r
789                 \r
790                 StringHelper& StringHelper::operator + (char c)\r
791                 {\r
792                         append(c);\r
793                         return *this;\r
794                 }\r
795 \r
796                 StringHelper::operator char *()\r
797                 {\r
798                         return content;\r
799                 }\r
800 \r
801                 StringHelper::operator const char *()\r
802                 {\r
803                         return content;\r
804                 }\r
805 \r
806                 ostream& operator<<(ostream& stream, const StringHelper& s)\r
807                 {\r
808                         stream << s.cstr();\r
809                         return stream;\r
810                 }\r
811 \r
812                 istream& operator<<(istream& stream, StringHelper& s)\r
813                 {\r
814                         char buff[256] = {0};\r
815 \r
816                         stream >> buff;\r
817 \r
818                         s.append(buff);\r
819 \r
820                         return stream;\r
821                 }\r
822                                 \r
823         } // namespace Msg\r
824 } // namespace SimGrid