[2615] | 1 | #include "sopnamsp.h"
|
---|
[1238] | 2 | #include "machdefs.h"
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
[3619] | 5 | #include <string.h>
|
---|
[2322] | 6 | #include <iostream>
|
---|
[1238] | 7 |
|
---|
| 8 | #include "strutilxx.h"
|
---|
| 9 |
|
---|
| 10 |
|
---|
[2706] | 11 | /* methode */
|
---|
[2511] | 12 | void FillVStringFrString(string const & s,vector<string>& vs,char sep)
|
---|
[1238] | 13 | // Use string "s" to fill vector of strings "vs"
|
---|
| 14 | // considering char "sep" as a separator.
|
---|
| 15 | // Vector is filled from its end (no reset done).
|
---|
[3572] | 16 | // To write a "sep" char, use \'sep'
|
---|
[1238] | 17 | // Warning: separator "sep" could not be set to '\'
|
---|
| 18 | // Ex: sep=' ': s="aaa bbb cc d " -> vs=(aaa,bbb,cc,d)
|
---|
| 19 | // Ex: sep=';': s="aaa ;bbb; cc;d " -> vs=(aaa ,bbb, cc,d )
|
---|
| 20 | // Ex: sep=';': s=";aaa\;bbb;;;ccc;ddd" -> vs=(aaa;bbb,ccc,ddd)
|
---|
| 21 | // Ex: sep=';': s=";aaa\;bbb;;;ccc;ddd\" -> vs=(aaa;bbb,ccc,ddd\)
|
---|
| 22 | {
|
---|
| 23 | uint_4 ls = s.size();
|
---|
| 24 | if(ls<=0 || sep=='\\') return;
|
---|
| 25 | const char* str = s.c_str();
|
---|
| 26 | ls = strlen(str); // str[ls-1]==sep cf ci-dessus
|
---|
| 27 | string dum = "";
|
---|
| 28 | for(uint_4 i=0; i<ls; i++) {
|
---|
| 29 | if(i==0 && str[i]==sep) {
|
---|
| 30 | continue;
|
---|
| 31 | } else if(str[i]=='\\') {
|
---|
| 32 | if(str[i+1]!=sep || i==ls-2) dum += str[i];
|
---|
| 33 | } else if(str[i]!=sep) {
|
---|
| 34 | dum += str[i];
|
---|
| 35 | } else { // C'est un "sep" mais est-ce vraiment un separateur?
|
---|
| 36 | if(str[i-1]=='\\' && i!=ls-1) dum += str[i];
|
---|
| 37 | else { // C'est un separateur, ne delimite t-il pas d'autres separateurs?
|
---|
| 38 | if(dum.size()<=0) continue;
|
---|
| 39 | vs.push_back(dum);
|
---|
| 40 | dum = "";
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
[2514] | 44 | // Ajout du dernier mot eventuellement - Pas de separateur a la fin -
|
---|
| 45 | if(dum.size() > 0) vs.push_back(dum);
|
---|
[1238] | 46 | }
|
---|
[2706] | 47 |
|
---|
| 48 | /* methode */
|
---|
| 49 | void SplitStringToVString(string const & s,vector<string>& vs,char separator)
|
---|
| 50 | // Split the string "s" into a string vector "vs"
|
---|
| 51 | // - Separator is "separator"
|
---|
| 52 | // - If the separator is not a space (' '), the space is considered
|
---|
| 53 | // as a dummy character:
|
---|
| 54 | // - If ',' is the separator, "1, 2 ,3 , 4 " == "1,2,3,4"
|
---|
| 55 | // - If ',' is the separator, "1, 2 3 , 4 " == "1,23,4"
|
---|
| 56 | // - Fill "vs" at the end: NO RESET IS DONE
|
---|
| 57 | // - If ',' is the separator, ",,," is c
|
---|
[3572] | 58 | // That routine is much more rapid than FillVStringFrString although less general
|
---|
[2706] | 59 | // (ex no '\' gestion is done)
|
---|
| 60 | {
|
---|
| 61 | uint_4 ls = s.size();
|
---|
| 62 | if(ls<=0) return;
|
---|
[3572] | 63 | //bool sep_non_blanc = (separator==' ')? false: true;
|
---|
[2706] | 64 | char *str = new char[ls+2];
|
---|
| 65 |
|
---|
| 66 | uint_4 i=0, lstr=0;
|
---|
| 67 | while(i<ls) {
|
---|
| 68 | // le caractere n'est ni un blanc ni un separateur
|
---|
| 69 | if(s[i]!=separator && s[i]!=' ') {
|
---|
| 70 | str[lstr] = s[i]; lstr++;
|
---|
| 71 | }
|
---|
| 72 | // Condition de remplissage du vecteur
|
---|
| 73 | if(s[i]==separator || i==ls-1) {
|
---|
| 74 | if(lstr>0) {str[lstr]='\0'; vs.push_back(str); lstr=0;}
|
---|
| 75 | }
|
---|
| 76 | i++;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | delete [] str;
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|