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

Last change on this file since 3719 was 3619, checked in by cmv, 16 years ago

add various #include<> for g++ 4.3 (jaunty 9.04), cmv 05/05/2009

File size: 2.5 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <iostream>
7
8#include "strutilxx.h"
9
10
11/* methode */
12void FillVStringFrString(string const & s,vector<string>& vs,char sep)
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).
16// To write a "sep" char, use \'sep'
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{
23uint_4 ls = s.size();
24if(ls<=0 || sep=='\\') return;
25const char* str = s.c_str();
26ls = strlen(str); // str[ls-1]==sep cf ci-dessus
27string dum = "";
28for(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}
44// Ajout du dernier mot eventuellement - Pas de separateur a la fin -
45if(dum.size() > 0) vs.push_back(dum);
46}
47
48/* methode */
49void 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
58// That routine is much more rapid than FillVStringFrString although less general
59// (ex no '\' gestion is done)
60{
61 uint_4 ls = s.size();
62 if(ls<=0) return;
63 //bool sep_non_blanc = (separator==' ')? false: true;
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}
Note: See TracBrowser for help on using the repository browser.