// Abort if the variable prefix is not found if (input.find("${") == string::npos) { return input;
}
string result = input;
// Note: rather than be fancy to detect recursive replacements, // we simply iterate till a given threshold is met.
int retries = 1000; bool did_replace;
do {
did_replace = false; for (map<string, string>::const_iterator it = variables.begin();
it != variables.end(); ++it) {
string::size_type pos = 0; while((pos = result.find(it->first, pos)) != string::npos) {
result = result.replace(pos, it->first.length(), it->second);
pos += it->second.length();
did_replace = true;
}
} if (did_replace && --retries == 0) {
*error = true;
fprintf(stderr, "Recursive replacement detected during variables " "substitution. Full list of variables is: ");
for (map<string, string>::const_iterator it = variables.begin();
it != variables.end(); ++it) {
fprintf(stderr, " %s=%s\n",
it->first.c_str(), it->second.c_str());
}
return result;
}
} while (did_replace);
return result;
}
int
read_list_file(const string& filename, const map<string, string>& variables,
vector<FileRecord>* files,
vector<string>* excludes)
{ int err = 0;
FILE* f = NULL; long size; char* buf = NULL; char *p, *q; int i, lineCount;
f = fopen(filename.c_str(), "r"); if (f == NULL) {
fprintf(stderr, "Could not open list file (%s): %s\n",
filename.c_str(), strerror(errno));
err = errno; goto cleanup;
}
err = fseek(f, 0, SEEK_END); if (err != 0) {
fprintf(stderr, "Could not seek to the end of file %s. (%s)\n",
filename.c_str(), strerror(errno));
err = errno; goto cleanup;
}
size = ftell(f);
err = fseek(f, 0, SEEK_SET); if (err != 0) {
fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n",
filename.c_str(), strerror(errno));
err = errno; goto cleanup;
}
// split on lines
p = buf;
q = buf+size;
lineCount = 0; while (p<q) { if (*p == '\r' || *p == '\n') {
*p = '\0';
lineCount++;
}
p++;
}
// read lines
p = buf; for (i=0; i<lineCount; i++) { int len = strlen(p);
q = p + len + 1; if (is_whitespace_line(p) || is_comment_line(p)) {
;
} elseif (is_exclude_line(p)) { while (*p != '-') p++;
p++;
excludes->push_back(string(p));
} else {
vector<string> words;
split_line(p, &words);
#if0
printf("[ "); for (size_t k=0; k<words.size(); k++) {
printf("'%s' ", words[k].c_str());
}
printf("]\n"); #endif
FileOpType op = FILE_OP_COPY;
string paths[2]; int pcount = 0;
string errstr; for (vector<string>::iterator it = words.begin(); it != words.end(); ++it) { const string& word = *it; if (word == "rm") { if (op != FILE_OP_COPY) {
errstr = "Error: you can only specifiy 'rm' or 'strip' once per line."; break;
}
op = FILE_OP_REMOVE;
} elseif (word == "strip") { if (op != FILE_OP_COPY) {
errstr = "Error: you can only specifiy 'rm' or 'strip' once per line."; break;
}
op = FILE_OP_STRIP;
} elseif (pcount < 2) { bool error = false;
paths[pcount++] = replace_variables(word, variables, &error); if (error) {
err = 1; goto cleanup;
}
} else {
errstr = "Error: More than 2 paths per line."; break;
}
}
if (pcount == 0 && !errstr.empty()) {
errstr = "Error: No path found on line.";
}
if (!errstr.empty()) {
fprintf(stderr, "%s:%d: bad format: %s\n%s\nExpected: [SRC] [rm|strip] DEST\n",
filename.c_str(), i+1, p, errstr.c_str());
err = 1;
} else { if (pcount == 1) { // pattern: [rm|strip] DEST
paths[1] = paths[0];
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.