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