3 // This code is copyright 2003-2004 Royce Mitchell III
4 // and released under the BSD & LGPL licenses
7 #pragma warning ( disable : 4786 ) // MSVC6 can't handle too-long template names
12 #include "SplitJoin.h"
18 //using std::stringstream;
20 static const char* quotes
= "\"\'";
22 bool Split ( vector
<string
>& vec
, const char* csv
, char sep
, bool merge
)
30 while ( isspace(*p
) && *p
!= sep
)
33 if ( strchr ( quotes
, *p
) )
35 while ( *p
&& (*p
!= sep
|| quote
) )
41 while ( isspace(*p
) && *p
!= sep
)
44 if ( *p
&& *p
!= sep
)
47 string
scol ( col
, p
-col
);
52 if ( scol
[scol
.size()-1] == quote
)
53 scol
= string ( &scol
[1], scol
.size()-2 );
56 if ( scol
.length() || !merge
)
57 vec
.push_back ( scol
);
67 bool Join ( string
& csv
, vector
<string
>& vec
, char sep
)
70 for ( int i
= 0; i
< vec
.size(); i
++ )
75 if ( strchr ( s
.c_str(), sep
) )
77 if ( strchr ( s
.c_str(), '\"' ) )
79 if ( strchr ( s
.c_str(), '\'' ) )
80 return false; // the sep, " and ' are all in the string, can't build valid output