implemented CreateServiceA
[reactos.git] / irc / ArchBlackmann / SplitJoin.cpp
1 // SplitJoin.cpp
2 //
3 // This code is copyright 2003-2004 Royce Mitchell III
4 // and released under the BSD & LGPL licenses
5
6 #ifdef _MSC_VER
7 #pragma warning ( disable : 4786 ) // MSVC6 can't handle too-long template names
8 #endif//_MSC_VER
9
10 //#include <sstream>
11
12 #include "SplitJoin.h"
13
14 #include <string.h>
15
16 using std::string;
17 using std::vector;
18 //using std::stringstream;
19
20 static const char* quotes = "\"\'";
21
22 bool Split ( vector<string>& vec, const char* csv, char sep, bool merge )
23 {
24 string scsv ( csv );
25 char* col = &scsv[0];
26 vec.resize ( 0 );
27 for ( ;; )
28 {
29 char* p = col;
30 while ( isspace(*p) && *p != sep )
31 p++;
32 char quote = 0;
33 if ( strchr ( quotes, *p ) )
34 quote = *p++;
35 while ( *p && (*p != sep || quote) )
36 {
37 if ( *p++ == quote )
38 break;
39 }
40
41 while ( isspace(*p) && *p != sep )
42 p++;
43
44 if ( *p && *p != sep )
45 return false;
46
47 string scol ( col, p-col );
48
49 //quote = scol[0];
50 if ( quote )
51 {
52 if ( scol[scol.size()-1] == quote )
53 scol = string ( &scol[1], scol.size()-2 );
54 }
55
56 if ( scol.length() || !merge )
57 vec.push_back ( scol );
58
59 if ( !*p )
60 break;
61
62 col = p + 1;
63 }
64 return true;
65 }
66
67 bool Join ( string& csv, vector<string>& vec, char sep )
68 {
69 csv.resize(0);
70 for ( int i = 0; i < vec.size(); i++ )
71 {
72 if ( i )
73 csv += sep;
74 string& s = vec[i];
75 if ( strchr ( s.c_str(), sep ) )
76 {
77 if ( strchr ( s.c_str(), '\"' ) )
78 {
79 if ( strchr ( s.c_str(), '\'' ) )
80 return false; // the sep, " and ' are all in the string, can't build valid output
81 csv += '\'';
82 csv += s;
83 csv += '\'';
84 }
85 else
86 {
87 csv += '\"';
88 csv += s;
89 csv += '\"';
90 }
91 }
92 else
93 csv += s;
94 }
95 return true;
96 }