00001 #include "ChapiStringUtilities.h"
00002 #include <sys/types.h>
00003 #include <string.h>
00004 using namespace std;
00005 typedef vector<string>::const_iterator VCI;
00006
00007 namespace chapi_string_utilities
00008 {
00009
00010 vector<string> slice(string A, string sep)
00011 {
00012 vector<string> v;
00013 string tmp = "";
00014
00015 u_int N = A.size();
00016 for (u_int i=0; i<N; i++)
00017 {
00018 string comp = A.substr(i,1);
00019 if (comp != sep)
00020 {
00021 tmp += comp;
00022 }
00023 else
00024 {
00025 if (tmp!="") {v.push_back(tmp);}
00026 tmp = "";
00027 }
00028 }
00029 v.push_back(tmp);
00030
00031 return v;
00032 }
00034 map<string,string> associate_pieces(vector<string> v, string sep)
00035 {
00036 map<string,string> m;
00037 VCI b = v.begin();
00038 VCI e = v.end();
00039 for (VCI i = b; i!=e; ++i)
00040 {
00041 vector<string> ss = slice(*i,sep);
00042 if (ss[0] !="")
00043 {
00044 m[ss[0]] = ss[1];
00045 }
00046 }
00047 return m;
00048 }
00049
00051 void cut_string_after_sub(string& input, const string& sub)
00052 {
00053
00054
00055 string::size_type I;
00056 string::iterator b;
00057 string::iterator e;
00058 I = input.find_last_of(sub);
00059 if (I==string::npos) return;
00060 b = input.begin() + I;
00061 e = input.end();
00062 input.erase(b,e);
00063 }
00065 bool good_character(char* src)
00066 {
00067 if (strncmp(src," ",1)==0) return false;
00068 if (strncmp(src,"\n",1)==0) return false;
00069 if (strncmp(src,"\t",1)==0) return false;
00070 if (strncmp(src,"\r",1)==0) return false;
00071 return true;
00072 }
00074 string filter_string(char* src)
00075 {
00076 int n=strlen(src);
00077 string rtrn;
00078
00079 for (int i=0; i<n; i++)
00080 {
00081 if (good_character(&src[i]))
00082 {
00083 rtrn += src[i];
00084 }
00085 }
00086 return rtrn;
00087 }
00089
00090 }