| 1 | #include "machdefs.h" | 
|---|
| 2 | #include <stdio.h> | 
|---|
| 3 | #include <stdlib.h> | 
|---|
| 4 | #include <iostream.h> | 
|---|
| 5 |  | 
|---|
| 6 | #include "strutilxx.h" | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | void FillVStringFrString(string s,vector<string>& vs,char sep) | 
|---|
| 10 | // Use string "s" to fill vector of strings "vs" | 
|---|
| 11 | // considering char "sep" as a separator. | 
|---|
| 12 | // Vector is filled from its end (no reset done). | 
|---|
| 13 | // Tp write a "sep" char, use \'sep' | 
|---|
| 14 | // Warning: separator "sep" could not be set to '\' | 
|---|
| 15 | // Ex: sep=' ': s="aaa   bbb cc d " -> vs=(aaa,bbb,cc,d) | 
|---|
| 16 | // Ex: sep=';': s="aaa   ;bbb; cc;d " -> vs=(aaa   ,bbb, cc,d ) | 
|---|
| 17 | // Ex: sep=';': s=";aaa\;bbb;;;ccc;ddd" -> vs=(aaa;bbb,ccc,ddd) | 
|---|
| 18 | // Ex: sep=';': s=";aaa\;bbb;;;ccc;ddd\" -> vs=(aaa;bbb,ccc,ddd\) | 
|---|
| 19 | { | 
|---|
| 20 | uint_4 ls = s.size(); | 
|---|
| 21 | if(ls<=0 || sep=='\\') return; | 
|---|
| 22 | s += sep; // add a separator at the end | 
|---|
| 23 | const char* str = s.c_str(); | 
|---|
| 24 | ls = strlen(str); // str[ls-1]==sep cf ci-dessus | 
|---|
| 25 | string dum = ""; | 
|---|
| 26 | for(uint_4 i=0; i<ls; i++) { | 
|---|
| 27 | if(i==0 && str[i]==sep) { | 
|---|
| 28 | continue; | 
|---|
| 29 | } else if(str[i]=='\\') { | 
|---|
| 30 | if(str[i+1]!=sep || i==ls-2) dum += str[i]; | 
|---|
| 31 | } else if(str[i]!=sep) { | 
|---|
| 32 | dum += str[i]; | 
|---|
| 33 | } else {  // C'est un "sep" mais est-ce vraiment un separateur? | 
|---|
| 34 | if(str[i-1]=='\\' && i!=ls-1) dum += str[i]; | 
|---|
| 35 | else {  // C'est un separateur, ne delimite t-il pas d'autres separateurs? | 
|---|
| 36 | if(dum.size()<=0) continue; | 
|---|
| 37 | vs.push_back(dum); | 
|---|
| 38 | dum = ""; | 
|---|
| 39 | } | 
|---|
| 40 | } | 
|---|
| 41 | } | 
|---|
| 42 | } | 
|---|