source: Sophya/trunk/SophyaLib/SUtils/strutilxx.cc@ 2706

Last change on this file since 2706 was 2706, checked in by cmv, 20 years ago

une routine + rapide pour splitter les chaine de caracteres cmv 28/04/05

File size: 2.4 KB
RevLine 
[2615]1#include "sopnamsp.h"
[1238]2#include "machdefs.h"
3#include <stdio.h>
4#include <stdlib.h>
[2322]5#include <iostream>
[1238]6
7#include "strutilxx.h"
8
9
[2706]10/* methode */
[2511]11void FillVStringFrString(string const & s,vector<string>& vs,char sep)
[1238]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// Tp 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{
22uint_4 ls = s.size();
23if(ls<=0 || sep=='\\') return;
24const char* str = s.c_str();
25ls = strlen(str); // str[ls-1]==sep cf ci-dessus
26string dum = "";
27for(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}
[2514]43// Ajout du dernier mot eventuellement - Pas de separateur a la fin -
44if(dum.size() > 0) vs.push_back(dum);
[1238]45}
[2706]46
47/* methode */
48void 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 altouch 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}
Note: See TracBrowser for help on using the repository browser.