Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <vector>
- std::string& string_replace (std::string& str, const std::string& from, const std::string& to)
- {
- std::string::size_type pos = 0;
- while (pos = str.find(from, pos), pos != std::string::npos) {
- str.replace(pos, from.length(), to);
- pos += to.length();
- }
- return str;
- }
- std::string csv_escape(const std::string &token)
- {
- std::string res(token);
- bool needsQuote = false;
- if (res.find("\"", 0) != std::string::npos) {
- needsQuote = true;
- string_replace(res, "\"", "\"\"");
- }
- if (res.find(",", 0) != std::string::npos ||
- res.find("\n", 0) != std::string::npos) {
- needsQuote = true;
- }
- if (needsQuote) {
- res.insert(0, "\"");
- res.append("\"");
- }
- return res;
- }
- std::string csv_unescape(const std::string &token)
- {
- std::string res(token);
- if (res.length() >= 2 && res[0] == '\"' && res[res.length() - 1] == '\"') {
- res.erase(res.length() - 1, 1);
- res.erase(0, 1);
- }
- string_replace(res, "\"\"", "\"");
- return res;
- }
- std::vector<std::string> csv_split(const std::string &str, bool *valid = NULL)
- {
- std::vector<std::string> res;
- if (valid != NULL) {
- *valid = true;
- }
- std::string::size_type start = 0;
- std::string::size_type current = 0;
- std::string::size_type found;
- bool insideQuote = false;
- while ((found = str.find_first_of(",\"", current)) != std::string::npos) {
- char c = str[found];
- if (c == '\"') {
- if (insideQuote) {
- if (str[found + 1] == '\"') {
- found++; // skip the next quote
- }
- else {
- if (valid != NULL && str[found + 1] != ',' && str[found + 1] != '\0' && str[found + 1] != '\r' && str[found + 1] != '\n') {
- *valid = false;
- }
- insideQuote = false;
- }
- }
- else {
- if (valid != NULL && found > 0 && str[found - 1] != ',' && str[found - 1] != '\r' && str[found - 1] != '\n') {
- *valid = false;
- }
- insideQuote = true;
- }
- }
- else if (c == ',') {
- if (!insideQuote) {
- res.push_back(std::string(str, start, found - start));
- start = found + 1;
- }
- }
- current = found + 1;
- }
- res.push_back(std::string(str, start, str.size() - start));
- if (valid != NULL && insideQuote) {
- *valid = false;
- }
- for (int i = 0; i < res.size(); i++) {
- res[i] = csv_unescape(res[i]);
- }
- return res;
- }
- std::string csv_join(std::vector<std::string> &v)
- {
- std::string res;
- std::vector<std::string>::iterator iter = v.begin();
- bool first = true;
- while(iter != v.end())
- {
- if (first) {
- first = false;
- }
- else {
- res.append(",");
- }
- res.append(csv_escape(*iter));
- iter++;
- }
- return res;
- }
- bool ParseCSV(const char *filename)
- {
- FILE *fs = fopen(filename, "r");
- char linebuf[1024];
- // note: this function do not support fields containing line breaks
- while (fgets(linebuf, 1024, fs) != NULL)
- {
- linebuf[strlen(linebuf) - 1] = '\0';
- bool valid = true;
- std::vector<std::string> param = csv_split(std::string(linebuf), &valid);
- printf("{ ");
- for (int i = 0; i < param.size(); i++)
- {
- if (i)
- printf(", ");
- printf("[%d]=%s", i, param[i].c_str());
- }
- printf(" }");
- if (!valid) {
- printf(" // illegal?");
- }
- printf("\n");
- }
- fclose(fs);
- return true;
- }
- int main(int argc, char *argv[])
- {
- if (argc < 2) {
- fprintf(stderr, "error: no filename\n");
- return 1;
- }
- printf("return %s;\n", ParseCSV(argv[1]) ? "true" : "false");
- //FILE *fs = fopen("saved.csv", "w");
- //if (!fs) return 0;
- //bool enabled = true;
- //std::string code("7E00BE:99+7E00BF:99");
- //std::string name("Money MAX");
- //std::vector<std::string> v;
- //v.push_back(enabled ? "enabled" : "disabled");
- //v.push_back(code);
- //v.push_back(name);
- //fprintf(fs, "%s\n", csv_join(v).c_str());
- //fclose(fs);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement