Orbiter 2022
Combinatorial Objects
string_tools.cpp
Go to the documentation of this file.
1/*
2 * string_tools.cpp
3 *
4 * Created on: Apr 15, 2021
5 * Author: betten
6 */
7
8
9
10
11#include "foundations.h"
12
13using namespace std;
14
15
16namespace orbiter {
17namespace layer1_foundations {
18namespace data_structures {
19
20
21
23{
24
25}
26
28{
29
30}
31
32
33int string_tools::is_csv_file(const char *fname)
34{
35 char ext[1000];
36
37 get_extension_if_present(fname, ext);
38 if (strcmp(ext, ".csv") == 0) {
39 return TRUE;
40 }
41 else {
42 return FALSE;
43 }
44}
45
46int string_tools::is_inc_file(const char *fname)
47{
48 char ext[1000];
49
50 get_extension_if_present(fname, ext);
51 if (strcmp(ext, ".inc") == 0) {
52 return TRUE;
53 }
54 else {
55 return FALSE;
56 }
57}
58
59int string_tools::is_xml_file(const char *fname)
60{
61 char ext[1000];
62
63 get_extension_if_present(fname, ext);
64 if (strcmp(ext, ".xml") == 0) {
65 return TRUE;
66 }
67 else {
68 return FALSE;
69 }
70}
71
72int string_tools::s_scan_int(char **s, int *i)
73{
74 char str1[512];
75
76 if (!s_scan_token(s, str1)) {
77 return FALSE;
78 }
79 if (strcmp(str1, ",") == 0) {
80 if (!s_scan_token(s, str1)) {
81 return FALSE;
82 }
83 }
84 //*i = atoi(str1);
85 sscanf(str1, "%d", i);
86 return TRUE;
87}
88
89int string_tools::s_scan_lint(char **s, long int *i)
90{
91 char str1[512];
92
93 if (!s_scan_token(s, str1)) {
94 return FALSE;
95 }
96 if (strcmp(str1, ",") == 0) {
97 if (!s_scan_token(s, str1)) {
98 return FALSE;
99 }
100 }
101 //*i = atoi(str1);
102 sscanf(str1, "%ld", i);
103 return TRUE;
104}
105
106int string_tools::s_scan_double(char **s, double *d)
107{
108 char str1[512];
109 char c;
110 int len;
111
112 //cout << "s_scan_double input='" << *s << "'" << endl;
113 while (TRUE) {
114 c = **s;
115 if (c == 0) {
116 return(FALSE);
117 }
118 if (c == ' ' || c == '\t' ||
119 c == '\r' || c == 10 || c == 13) {
120 (*s)++;
121 continue;
122 }
123 break;
124 }
125 len = 0;
126 c = **s;
127 if (isdigit(c) || c == '-') {
128 //cout << "s_scan_double character '" << c << "'" << endl;
129 while (isdigit(c) || c == '.' || c == 'e' || c == '-') {
130 str1[len] = c;
131 len++;
132 (*s)++;
133 c = **s;
134 //cout << "character '" << c << "'" << endl;
135 //<< *s << "'" << endl;
136 }
137 str1[len] = 0;
138 }
139 //cout << "s_scan_double token = " << str1 << endl;
140 sscanf(str1, "%lf", d);
141 return TRUE;
142}
143
144int string_tools::s_scan_token(char **s, char *str)
145{
146 char c;
147 int len;
148
149 while (TRUE) {
150 c = **s;
151 if (c == 0) {
152 return(FALSE);
153 }
154 if (c == ' ' || c == '\t' ||
155 c == '\r' || c == 10 || c == 13) {
156 (*s)++;
157 continue;
158 }
159 break;
160 }
161 len = 0;
162 c = **s;
163 if (isalpha(c)) {
164 //cout << "character '" << c << "', remainder '"
165 //<< *s << "'" << endl;
166 while (isalnum(c) || c == '_') {
167 str[len] = c;
168 len++;
169 (*s)++;
170 c = **s;
171 //cout << "character '" << c << "', remainder '"
172 //<< *s << "'" << endl;
173 }
174 str[len] = 0;
175 }
176 else if (isdigit(c) || c == '-') {
177 str[len++] = c;
178 (*s)++;
179 //cout << "character '" << c << "', remainder '"
180 //<< *s << "'" << endl;
181 //printf("\"%s\"\n", *s);
182 c = **s;
183 while (isdigit(c)) {
184 str[len] = c;
185 len++;
186 (*s)++;
187 c = **s;
188 }
189 str[len] = 0;
190 }
191 else {
192 str[0] = c;
193 str[1] = 0;
194 (*s)++;
195 }
196 // printf("token = \"%s\"\n", str);
197 return TRUE;
198}
199
201{
202 char c;
203 int len;
204
205 while (TRUE) {
206 c = **s;
207 if (c == 0) {
208 return(FALSE);
209 }
210 if (c == ' ' || c == '\t' ||
211 c == '\r' || c == 10 || c == 13) {
212 (*s)++;
213 continue;
214 }
215 break;
216 }
217 len = 0;
218 c = **s;
219 while (c != 0 && c != ' ' && c != '\t' &&
220 c != '\r' && c != 10 && c != 13) {
221 //cout << "s_scan_token_arbitrary len=" << len
222 //<< " reading " << c << endl;
223 str[len] = c;
224 len++;
225 (*s)++;
226 c = **s;
227 }
228 str[len] = 0;
229 //printf("token = \"%s\"\n", str);
230 return TRUE;
231}
232
233
234int string_tools::s_scan_str(char **s, char *str)
235{
236 char c;
237 int len, f_break;
238
239 while (TRUE) {
240 c = **s;
241 if (c == 0) {
242 return(FALSE);
243 }
244 if (c == ' ' || c == '\t' ||
245 c == '\r' || c == 10 || c == 13) {
246 (*s)++;
247 continue;
248 }
249 break;
250 }
251 if (c != '\"') {
252 cout << "s_scan_str() error: c != '\"'" << endl;
253 return(FALSE);
254 }
255 (*s)++;
256 len = 0;
257 f_break = FALSE;
258 while (TRUE) {
259 c = **s;
260 if (c == 0) {
261 break;
262 }
263 if (c == '\\') {
264 (*s)++;
265 c = **s;
266 str[len] = c;
267 len++;
268 }
269 else if (c == '\"') {
270 f_break = TRUE;
271 }
272 else {
273 str[len] = c;
274 len++;
275 }
276 (*s)++;
277 if (f_break) {
278 break;
279 }
280 }
281 str[len] = 0;
282 return TRUE;
283}
284
285int string_tools::s_scan_token_comma_separated(const char **s, char *str)
286{
287 char c;
288 int len;
289
290 len = 0;
291 c = **s;
292 if (c == 0) {
293 return false;
294 }
295#if 0
296 if (c == 10 || c == 13) {
297 (*s)++;
298 sprintf(str, "END_OF_LINE");
299 return FALSE;
300 }
301#endif
302 if (c == ',') {
303 (*s)++;
304 str[0] = 0;
305 //sprintf(str, "");
306 return TRUE;
307 }
308 while (c != 13 && c != ',') {
309 if (c == 0) {
310 break;
311 }
312 if (c == '"') {
313 str[len] = c;
314 len++;
315 (*s)++;
316 c = **s;
317 while (TRUE) {
318 //cout << "read '" << c << "'" << endl;
319 if (c == 0) {
320 str[len] = 0;
321 cout << "s_scan_token_comma_separated: "
322 "end of line inside string" << endl;
323 cout << "while scanning '" << str << "'" << endl;
324 exit(1);
325 break;
326 }
327 str[len] = c;
328 len++;
329 if (c == '"') {
330 //cout << "end of string" << endl;
331 (*s)++;
332 c = **s;
333 break;
334 }
335 (*s)++;
336 c = **s;
337 }
338 }
339 else {
340 str[len] = c;
341 len++;
342 (*s)++;
343 c = **s;
344 }
345 }
346 str[len] = 0;
347 if (c == ',') {
348 (*s)++;
349 }
350 // printf("token = \"%s\"\n", str);
351 return TRUE;
352}
353
355 int *&perm, int &degree, int verbose_level)
356{
357 int f_v = (verbose_level >= 1);
358
359 if (f_v) {
360 cout << "string_tools::scan_permutation_from_string s = " << s << endl;
361 }
362 istringstream ins(s);
363 if (f_v) {
364 cout << "string_tools::scan_permutation_from_string before scan_permutation_from_stream" << endl;
365 }
366 scan_permutation_from_stream(ins, perm, degree, verbose_level);
367 if (f_v) {
368 cout << "string_tools::scan_permutation_from_string done" << endl;
369 }
370}
371
373 int *&perm, int &degree, int verbose_level)
374// Scans a permutation from a stream.
375{
376 int f_v = (verbose_level >= 1);
377
378 if (f_v) {
379 cout << "string_tools::scan_permutation_from_string" << endl;
380 }
381
382 int l = 20;
383 int *cycle; // [l]
384 //int *perm; // [l]
385 int i, a_last, a, dig, ci;
386 char s[1000], c;
387 int si, largest_point = 0;
389
390 cycle = NEW_int(l);
391 perm = NEW_int(l);
392 degree = l;
393 //l = s_l();
394 //perm.m_l(l);
395 //cycle.m_l_n(l);
396 Combi.perm_identity(perm, l);
397 //perm.one();
398 while (TRUE) {
399 c = get_character(is, verbose_level - 2);
400 while (c == ' ' || c == '\t') {
401 c = get_character(is, verbose_level - 2);
402 }
403 ci = 0;
404 if (c != '(') {
405 break;
406 }
407 if (f_v) {
408 cout << "opening parenthesis" << endl;
409 }
410 c = get_character(is, verbose_level - 2);
411 while (TRUE) {
412 while (c == ' ' || c == '\t') {
413 c = get_character(is, verbose_level - 2);
414 }
415
416 si = 0;
417 // read digits:
418 while (c >= '0' && c <= '9') {
419 s[si++] = c;
420 c = get_character(is, verbose_level - 2);
421 }
422 while (c == ' ' || c == '\t') {
423 c = get_character(is, verbose_level - 2);
424 }
425 if (c == ',') {
426 c = get_character(is, verbose_level - 2);
427 }
428 s[si] = 0;
429 dig = atoi(s);
430 if (dig > largest_point) {
431 largest_point = dig;
432 }
433 if (f_v) {
434 cout << "digit as string: " << s << ", numeric: " << dig << endl;
435 }
436 if (dig < 0) {
437 cout << "string_tools::scan_permutation_from_stream digit < 0" << endl;
438 exit(1);
439 }
440 if (dig >= l) {
441 int *perm1;
442 int *cycle1;
443 //permutation perm1;
444 //vector cycle1;
445 int l1, i;
446
447 l1 = MAXIMUM(l + (l >> 1), largest_point + 1);
448 if (f_v) {
449 cout << "string_tools::scan_permutation_from_stream digit = "
450 << dig << " >= " << l
451 << ", extending permutation degree to "
452 << l1 << endl;
453 }
454 perm1 = NEW_int(l1);
455 cycle1 = NEW_int(l1);
456
457 //perm1.m_l(l1);
458 for (i = 0; i < l; i++) {
459 //perm1.m_ii(i, perm.s_i(i));
460 perm1[i] = perm[i];
461 }
462 for (i = l; i < l1; i++) {
463 perm1[i] = i;
464 }
465 FREE_int(perm);
466 perm = perm1;
467 degree = l1;
468 //perm.swap(perm1);
469
470 //cycle1.m_l_n(l1);
471 for (i = 0; i < l; i++) {
472 //cycle1.m_ii(i, cycle.s_ii(i));
473 cycle1[i] = cycle[i];
474 }
475 FREE_int(cycle);
476 cycle = cycle1;
477 //cycle.swap(cycle1);
478 l = l1;
479 }
480 si = 0;
481 //cycle.m_ii(ci, dig + 1);
482 cycle[ci] = dig;
483 ci++;
484 if (c == ')') {
485 if (f_v) {
486 cout << "closing parenthesis, cycle = ";
487 for (i = 0; i < ci; i++)
488 cout << cycle[i] << " ";
489 cout << endl;
490 }
491 for (i = 1; i < ci; i++) {
492 a_last = cycle[i - 1];
493 a = cycle[i];
494 perm[a_last] = a;
495 }
496 if (ci > 1) {
497 a_last = cycle[ci - 1];
498 a = cycle[0];
499 perm[a_last] = a;
500 }
501 ci = 0;
502 if (!is) {
503 break;
504 }
505 //c = get_character(is, verbose_level - 2);
506 break;
507 }
508 } // loop for one cycle
509 if (!is) {
510 break;
511 }
512 while (c == ' ' || c == '\t') {
513 c = get_character(is, verbose_level - 2);
514 }
515 ci = 0;
516 } // end of loop over all cycles
517#if 0
518 {
519 permutation perm1;
520 int i;
521
522 perm1.m_l(largest_point + 1);
523 for (i = 0; i <= largest_point; i++) {
524 perm1.m_ii(i, perm.s_i(i));
525 }
526 perm.swap(perm1);
527 }
528#endif
529 degree = largest_point + 1;
530 if (f_v) {
531 cout << "read permutation: ";
532 Combi.perm_print(cout, perm, degree);
533 cout << endl;
534 }
535 FREE_int(cycle);
536}
537
538void string_tools::chop_string(const char *str, int &argc, char **&argv)
539{
540 int l, i, len;
541 char *s;
542 char *buf;
543 char *p_buf;
544
545 l = strlen(str);
546 s = NEW_char(l + 1);
547 buf = NEW_char(l + 1);
548
549 strcpy(s, str);
550 p_buf = s;
551 i = 0;
552 while (TRUE) {
553 if (*p_buf == 0) {
554 break;
555 }
556 s_scan_token_arbitrary(&p_buf, buf);
557
558 if (FALSE) {
559 cout << "Token " << setw(6) << i << " is '"
560 << buf << "'" << endl;
561 }
562 i++;
563 }
564 argc = i;
565 argv = NEW_pchar(argc);
566 i = 0;
567 p_buf = s;
568 while (TRUE) {
569 if (*p_buf == 0) {
570 break;
571 }
572 s_scan_token_arbitrary(&p_buf, buf);
573
574 if (FALSE) {
575 cout << "Token " << setw(6) << i << " is '"
576 << buf << "'" << endl;
577 }
578 len = strlen(buf);
579 argv[i] = NEW_char(len + 1);
580 strcpy(argv[i], buf);
581 i++;
582 }
583
584#if 0
585 cout << "argv:" << endl;
586 for (i = 0; i < argc; i++) {
587 cout << i << " : " << argv[i] << endl;
588 }
589#endif
590
591
592 FREE_char(s);
593 FREE_char(buf);
594#if 0
595 for (i = 0; i < argc; i++) {
596 FREE_char(argv[i]);
597 }
598 FREE_pchar(argv);
599#endif
600}
601
602
603void string_tools::chop_string_comma_separated(const char *str, int &argc, char **&argv)
604{
605 int l, i, len;
606 char *s;
607 char *buf;
608 const char *p_buf;
609
610 l = strlen(str);
611 s = NEW_char(l + 1);
612 buf = NEW_char(l + 1);
613
614 strcpy(s, str);
615 p_buf = s;
616 i = 0;
617 while (TRUE) {
618 if (*p_buf == 0) {
619 break;
620 }
621 s_scan_token_comma_separated(&p_buf, buf);
622
623 if (FALSE) {
624 cout << "Token " << setw(6) << i << " is '"
625 << buf << "'" << endl;
626 }
627 i++;
628 }
629 argc = i;
630 argv = NEW_pchar(argc);
631 i = 0;
632 p_buf = s;
633 while (TRUE) {
634 if (*p_buf == 0) {
635 break;
636 }
637 s_scan_token_comma_separated(&p_buf, buf);
638
639 if (FALSE) {
640 cout << "Token " << setw(6) << i << " is '"
641 << buf << "'" << endl;
642 }
643 len = strlen(buf);
644 argv[i] = NEW_char(len + 1);
645 strcpy(argv[i], buf);
646 i++;
647 }
648
649#if 0
650 cout << "argv:" << endl;
651 for (i = 0; i < argc; i++) {
652 cout << i << " : " << argv[i] << endl;
653 }
654#endif
655
656
657 FREE_char(s);
658 FREE_char(buf);
659#if 0
660 for (i = 0; i < argc; i++) {
661 FREE_char(argv[i]);
662 }
663 FREE_pchar(argv);
664#endif
665}
666
667
668
669void string_tools::convert_arguments(int &argc, const char **argv, std::string *&Argv)
670{
671 int i;
672 vector<string> Arg_vec;
673
674 for (i = 0; i < argc; i++) {
675
676 if (FALSE /*strcmp(argv[i], "-repeat") == 0*/) {
677 string variable_name;
678 int loop_from;
679 int loop_upper_bound;
680 int loop_increment;
681 int index_of_repeat_start;
682 int index_of_repeat_end;
683
684 variable_name.assign(argv[++i]);
685 loop_from = atoi(argv[++i]);
686 loop_upper_bound = atoi(argv[++i]);
687 loop_increment = atoi(argv[++i]);
688 i++;
689 index_of_repeat_start = i;
690 while (i < argc) {
691 if (strcmp(argv[i], "-repeat_end") == 0) {
692 index_of_repeat_end = i;
693 break;
694 }
695 i++;
696 }
697 int loop_var;
698 int h;
699 string variable;
700
701 variable.assign("%");
702 variable.append(variable_name);
703
704 for (loop_var = loop_from; loop_var < loop_upper_bound; loop_var += loop_increment) {
705 for (h = index_of_repeat_start; h < index_of_repeat_end; h++) {
706 string arg;
707 string value_L;
708 char str[1000];
709
710 sprintf(str, "%d", loop_var);
711 value_L.assign(str);
712
713 arg.assign(argv[h]);
714
715 while (arg.find(variable) != std::string::npos) {
716 arg.replace(arg.find(variable), variable.length(), value_L);
717 }
718
719
720 Arg_vec.push_back(arg);
721 }
722 }
723 }
724 else {
725 string str;
726
727 str.assign(argv[i]);
728 Arg_vec.push_back(str);
729 }
730 }
731 argc = Arg_vec.size();
732 Argv = new string[argc];
733 for (i = 0; i < argc; i++) {
734 Argv[i].assign(Arg_vec[i]);
735 }
736
737}
738
739char string_tools::get_character(istream & is, int verbose_level)
740{
741 int f_v = (verbose_level >= 1);
742 char c;
743
744 if (!is) {
745 cout << "string_tools::get_character at end" << endl;
746 exit(1);
747 }
748 is >> c;
749 if (f_v) {
750 cout << "string_tools::get_character: \"" << c
751 << "\", ascii=" << (int)c << endl;
752 }
753 return c;
754}
755
756void string_tools::replace_extension_with(char *p, const char *new_ext)
757{
758 int i, l;
759
760 l = strlen(p);
761 for (i = l - 1; i >= 0; i--) {
762 if (p[i] == '.') {
763 p[i] = 0;
764 break;
765 }
766 else if (p[i] == '/') {
767 break;
768 }
769 }
770 strcat(p, new_ext);
771}
772
773void string_tools::replace_extension_with(std::string &p, const char *new_ext)
774{
775 int i, l;
776 string q;
777
778 l = p.length();
779 for (i = l - 1; i >= 0; i--) {
780 if (p[i] == '.') {
781 q = p.substr(0, i);
782 //p[i] = 0;
783 break;
784 }
785 else if (p[i] == '/') {
786 q = p.substr(0, i);
787 break;
788 }
789 }
790 if (i == -1) {
791 q = p;
792 }
793 q.append(new_ext);
794 p = q;
795}
796
798{
799 int len = strlen(p);
800 int i;
801
802 for (i = len - 1; i >= 0; i--) {
803 if (p[i] == '/') {
804 break;
805 }
806 if (p[i] == '.') {
807 p[i] = 0;
808 break;
809 }
810 }
811}
812
814{
815 chop_off_path(p);
817}
818
820{
821#if 0
822 int l;
823 int i;
824 string q;
825
826 l = p.length();
827 for (i = l - 1; i >= 0; i--) {
828 if (p[i] == '/') {
829 q = p.substr(0, i);
830 break;
831 }
832 if (p[i] == '.') {
833 q = p.substr(0, i);
834 break;
835 }
836 }
837 if (i == -1) {
838 q = p;
839 }
840 p = q;
841#else
842
843 std::string ext;
844 std::string q;
845
846 get_extension(p, ext);
847 q = p.substr(0, p.length() - ext.length());
848 p = q;
849#endif
850}
851
852void string_tools::chop_off_path(std::string &p)
853{
854 int l;
855 int i;
856 string q;
857
858 l = p.length();
859 for (i = l - 1; i >= 0; i--) {
860 if (p[i] == '/') {
861 q = p.substr(i + 1, l - i - 1);
862 break;
863 }
864 }
865 if (i == -1) {
866 q = p;
867 }
868 p = q;
869}
870
871
872void string_tools::chop_off_extension_if_present(std::string &p, const char *ext)
873{
874 int l1 = p.length();
875 int l2 = strlen(ext);
876
877 if (l1 > l2) {
878 string q;
879 q = p.substr(l1 - l2, l2);
880 if (strcmp(p.c_str(), ext) == 0) {
881 string r;
882 r = q.substr(0, l1 - l2);
883 p = r;
884 }
885 }
886}
887
888
890{
891 int l1 = strlen(p);
892 int l2 = strlen(ext);
893
894 if (l1 > l2 && strcmp(p + l1 - l2, ext) == 0) {
895 p[l1 - l2] = 0;
896 }
897}
898
899void string_tools::get_fname_base(const char *p, char *fname_base)
900{
901 int i, l = strlen(p);
902
903 strcpy(fname_base, p);
904 for (i = l - 1; i >= 0; i--) {
905 if (fname_base[i] == '.') {
906 //cout << "p[" << i << "] is dot" << endl;
907 fname_base[i] = 0;
908 return;
909 }
910 }
911}
912
913void string_tools::get_extension(std::string &p, std::string &ext)
914{
915 int i, l = p.length();
916
917 //cout << "string_tools::get_extension " << p << " l=" << l << endl;
918 for (i = l - 1; i >= 0; i--) {
919 if (p[i] == '.') {
920 //cout << "p[" << i << "] is dot" << endl;
921 ext = p.substr(i, l - i);
922 return;
923 }
924 if (p[i] == '/') {
925 break;
926 }
927 }
928 ext.assign("");
929}
930
931
932void string_tools::get_extension_if_present(const char *p, char *ext)
933{
934 int i, l = strlen(p);
935
936 //cout << "string_tools::get_extension_if_present " << p << " l=" << l << endl;
937 ext[0] = 0;
938 for (i = l - 1; i >= 0; i--) {
939 if (p[i] == '.') {
940 //cout << "p[" << i << "] is dot" << endl;
941 strcpy(ext, p + i);
942 return;
943 }
944 }
945}
946
948{
949 int i, l = strlen(p);
950
951 //cout << "string_tools::get_extension_if_present " << p << " l=" << l << endl;
952 ext[0] = 0;
953 for (i = l - 1; i >= 0; i--) {
954 if (p[i] == '.') {
955 //cout << "p[" << i << "] is dot" << endl;
956 strcpy(ext, p + i);
957 p[i] = 0;
958 return;
959 }
960 }
961}
962
964{
965 string str_t("\\t");
966 string str_D("\\D");
967 string str_B("\\B");
968 string str_n("\\n");
969
970
971 while (str.find(str_t) != std::string::npos) {
972 str.replace(str.find(str_t),str_t.length(),"\t");
973 }
974
975 while (str.find(str_D) != std::string::npos) {
976 str.replace(str.find(str_D),str_D.length(),"$");
977 }
978
979 while (str.find(str_B) != std::string::npos) {
980 str.replace(str.find(str_B),str_B.length(),"\\");
981 }
982
983 while (str.find(str_n) != std::string::npos) {
984 str.replace(str.find(str_n),str_n.length(),"\n");
985 }
986
987}
988
989void string_tools::remove_specific_character(std::string &str, char c)
990{
991 char st[1000];
992
993 st[0] = c;
994 st[1] = 0;
995 string str_t(st);
996
997
998 while (str.find(str_t) != std::string::npos) {
999 str.replace(str.find(str_t),str_t.length(),"");
1000 }
1001
1002
1003}
1004
1005void string_tools::create_comma_separated_list(std::string &output, long int *input, int input_sz)
1006{
1007 char str[1000];
1008 int i;
1009
1010 sprintf(str, "%ld", input[0]);
1011 output.assign(str);
1012 for (i = 1; i < input_sz; i++) {
1013 output.append(",");
1014 sprintf(str, "%ld", input[i]);
1015 output.append(str);
1016 }
1017
1018}
1019
1020
1022{
1023 int i, l;
1024
1025 l = strlen(str);
1026 for (i = 0; i < l; i++) {
1027 if (str[i] == ' ') {
1028 continue;
1029 }
1030 if (str[i] == '\\') {
1031 i++;
1032 if (str[i] == 0) {
1033 return TRUE;
1034 }
1035 if (str[i] == 'n') {
1036 continue;
1037 }
1038 }
1039 return FALSE;
1040 }
1041 return TRUE;
1042}
1043
1044void string_tools::text_to_three_double(std::string &text, double *d)
1045{
1046 double *data;
1047 int data_sz;
1048 numerics Num;
1049
1050 Num.vec_scan(text.c_str(), data, data_sz);
1051 if (data_sz != 3) {
1052 cout << "string_tools::text_to_three_double "
1053 "data_sz != 3, data_sz = " << data_sz << endl;
1054 exit(1);
1055 }
1056 d[0] = data[0];
1057 d[1] = data[1];
1058 d[2] = data[2];
1059 delete [] data;
1060
1061}
1062
1064{
1065 char *str1;
1066 char *str2;
1067 int ret;
1068
1069 if (p[0] == '"') {
1070 str1 = NEW_char(strlen(p) + 1);
1071 strcpy(str1, p);
1072 }
1073 else {
1074 str1 = NEW_char(strlen(p) + 3);
1075 strcpy(str1, "\"");
1076 strcpy(str1 + strlen(str1), p);
1077 strcpy(str1 + strlen(str1), "\"");
1078 }
1079 if (q[0] == '"') {
1080 str2 = NEW_char(strlen(q) + 1);
1081 strcpy(str2, q);
1082 }
1083 else {
1084 str2 = NEW_char(strlen(q) + 3);
1085 strcpy(str2, "\"");
1086 strcpy(str2 + strlen(str2), q);
1087 strcpy(str2 + strlen(str2), "\"");
1088 }
1089 ret = strcmp(str1, str2);
1090 FREE_char(str1);
1091 FREE_char(str2);
1092 return ret;
1093}
1094
1096{
1097 char c;
1098
1099 c = str.c_str()[0];
1100 if (c >= '0' && c <= '9') {
1101 return TRUE;
1102 }
1103 else {
1104 return FALSE;
1105 }
1106}
1107
1108int string_tools::stringcmp(std::string &str, const char *p)
1109{
1110 return strcmp(str.c_str(), p);
1111}
1112
1113int string_tools::strtoi(std::string &str)
1114{
1115 int i;
1116
1117 i = atoi(str.c_str());
1118 return i;
1119}
1120
1121int string_tools::str2int(std::string &str)
1122{
1123 int i, res, l;
1124
1125 l = (int) str.length();
1126 res = 0;
1127 for (i = 0; i < l; i++) {
1128 res = (res * 10) + (str[i] - 48);
1129 }
1130 return res;
1131}
1132
1133long int string_tools::strtolint(std::string &str)
1134{
1135 long int i;
1136
1137 i = atol(str.c_str());
1138 return i;
1139}
1140
1141double string_tools::strtof(std::string &str)
1142{
1143 double f;
1144
1145 f = atof(str.c_str());
1146 return f;
1147}
1148
1149int string_tools_compare_strings(void *a, void *b, void *data)
1150{
1151 char *A = (char *) a;
1152 char *B = (char *) b;
1153 return strcmp(A, B);
1154}
1155
1156
1157}}}
void get_fname_base(const char *p, char *fname_base)
void create_comma_separated_list(std::string &output, long int *input, int input_sz)
void scan_permutation_from_stream(std::istream &is, int *&perm, int &degree, int verbose_level)
void chop_string(const char *str, int &argc, char **&argv)
void scan_permutation_from_string(const char *s, int *&perm, int &degree, int verbose_level)
void get_extension(std::string &p, std::string &ext)
void chop_string_comma_separated(const char *str, int &argc, char **&argv)
void convert_arguments(int &argc, const char **argv, std::string *&Argv)
void replace_extension_with(char *p, const char *new_ext)
void chop_off_extension_if_present(std::string &p, const char *ext)
char get_character(std::istream &is, int verbose_level)
numerical functions, mostly concerned with double
Definition: globals.h:129
void vec_scan(const char *s, double *&v, int &len)
Definition: numerics.cpp:2218
#define NEW_pchar(n)
Definition: foundations.h:635
#define FREE_pchar(p)
Definition: foundations.h:648
#define FREE_int(p)
Definition: foundations.h:640
#define NEW_char(n)
Definition: foundations.h:632
#define NEW_int(n)
Definition: foundations.h:625
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define FREE_char(p)
Definition: foundations.h:646
#define MAXIMUM(x, y)
Definition: foundations.h:217
int string_tools_compare_strings(void *a, void *b, void *data)
the orbiter library for the classification of combinatorial objects