-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupport.cc
201 lines (174 loc) · 4.37 KB
/
support.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// support.cc-- miscellaneous support functions.
#include <string>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#include "support.h"
using std::string;
using std::vector;
//----------
//
// parse_comma_list--
// Parse a string for the list of comma-separated fields it contains.
//
//----------
//
// Arguments:
// const string& s: The string to parse.
//
// Returns:
// A vector of the fields in the string.
//
//----------
vector<string> parse_comma_list
(const string& s)
{
vector<string> fields;
std::istringstream ss(s);
while (ss)
{
string field;
if (!getline (ss, field, ',' )) break;
fields.emplace_back (field);
}
return fields;
}
//----------
//
// tokenize, quoted_tokenize--
// Break a string into its whitespace-separated fields.
//
//----------
//
// Arguments:
// const string& s: The string to reverse complement.
//
// Returns:
// A vector of the tokens in the string.
//
//----------
//
// Notes:
// (1) quoted_tokenize() recognizes quoted strings, and is more suitable for
// command-line parsing.
// (2) tokenize() is based on one of the solutions posted at
// http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c
//
//----------
vector<string> tokenize
(const string& s)
{
vector<string> tokens;
string::size_type startIx, endIx;
startIx = s.find_first_not_of (" \t\n");
endIx = startIx;
while (startIx != std::string::npos)
{
endIx = s.find_first_of (" \t\n", startIx);
if (endIx == std::string::npos) endIx = s.length();
tokens.emplace_back (s.substr (startIx, endIx-startIx));
startIx = s.find_first_not_of (" \t\n", endIx);
}
return tokens;
}
vector<string> quoted_tokenize
(const string& s)
{
enum { whitespace = 0, darkspace, quoted } state;
vector<string> tokens;
char token[s.length()+1];
string::size_type tokenLen = 0;
state = whitespace;
for (string::size_type ix=0 ; ix<s.length() ; ix++)
{
char ch = s[ix];
bool finishToken = false;
switch (state)
{
default:
case whitespace:
if (ch == ' ') break;
if (ch == '\t') break;
if (ch == '\"') { state = quoted; break; }
if (ch != '\\') { state = darkspace; token[tokenLen++] = ch; break; }
if (ix+1 >= s.length()) break; // escape at end of string is ignored
ch = s[ix++];
state = darkspace; token[tokenLen++] = ch;
break;
case darkspace:
if (ch == ' ') { finishToken = true; break; }
if (ch == '\t') { finishToken = true; break; }
if (ch == '\"') { state = quoted; break; }
if (ch != '\\') { token[tokenLen++] = ch; break; }
if (ix+1 >= s.length()) break; // escape at end of string is ignored
ch = s[ix++];
token[tokenLen++] = ch;
break;
case quoted:
if (ch == '\"') { state = darkspace; break; }
if (ch != '\\') { token[tokenLen++] = ch; break; }
if (ix+1 >= s.length()) break; // escape at end of string is ignored
ch = s[ix++];
token[tokenLen++] = ch;
break;
}
if (finishToken)
{
if (tokenLen > 0)
{
token[tokenLen] = 0;
tokens.emplace_back (string(token));
}
state = whitespace; // nota bene: andy time we switch to whitespace,
tokenLen = 0; // .. we have to set tokenLen to zero
}
}
if (tokenLen > 0)
{
token[tokenLen] = 0;
tokens.emplace_back (string(token));
}
return tokens;
}
//----------
//
// expand_filenames--
// Copy a list of filenames. Names containing {number} are copied multiple
// times, replacing {number} with a different number for each copy (from 1 to
// fileCount).
//
//----------
//
// Arguments:
// const vector<string> filenames: The filenames to copy.
// int fileCount: How many copies to make of any
// .. filenames containing {number}.
// vector<string>& filenames: List to copy filenames to.
//
// Returns:
// (nothing)
//
//----------
void expand_filenames
(const vector<string> filenames,
int fileCount,
vector<string>& newFilenames)
{
for (const auto& filename : filenames)
{
string field = "{number}";
std::size_t fieldIx = filename.find (field);
if (fieldIx == string::npos)
newFilenames.emplace_back(filename);
else
{
for (int fileNum=1 ; fileNum<=fileCount ; fileNum++)
{
string name = filename;
name.replace (fieldIx, field.length(), std::to_string(fileNum));
newFilenames.emplace_back(name);
}
}
}
}