#include "machdefs.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>

#include "strutilxx.h"


void FillVStringFrString(string s,vector<string>& vs,char sep)
// Use string "s" to fill vector of strings "vs"
// considering char "sep" as a separator.
// Vector is filled from its end (no reset done).
// Tp write a "sep" char, use \'sep'
// Warning: separator "sep" could not be set to '\'
// Ex: sep=' ': s="aaa   bbb cc d " -> vs=(aaa,bbb,cc,d)
// Ex: sep=';': s="aaa   ;bbb; cc;d " -> vs=(aaa   ,bbb, cc,d )
// Ex: sep=';': s=";aaa\;bbb;;;ccc;ddd" -> vs=(aaa;bbb,ccc,ddd)
// Ex: sep=';': s=";aaa\;bbb;;;ccc;ddd\" -> vs=(aaa;bbb,ccc,ddd\)
{
uint_4 ls = s.size();
if(ls<=0 || sep=='\\') return;
s += sep; // add a separator at the end
const char* str = s.c_str();
ls = strlen(str); // str[ls-1]==sep cf ci-dessus
string dum = "";
for(uint_4 i=0; i<ls; i++) {
  if(i==0 && str[i]==sep) {
    continue;
  } else if(str[i]=='\\') {
    if(str[i+1]!=sep || i==ls-2) dum += str[i];
  } else if(str[i]!=sep) {
    dum += str[i];
  } else {  // C'est un "sep" mais est-ce vraiment un separateur?
    if(str[i-1]=='\\' && i!=ls-1) dum += str[i];
    else {  // C'est un separateur, ne delimite t-il pas d'autres separateurs?
      if(dum.size()<=0) continue;
      vs.push_back(dum);
      dum = "";
    }
  }
}
}
