Logo AND Algorithmique Numérique Distribuée

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