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
|
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
#include <fstream>
#include <sstream>
#include <regex>
std::vector<std::string> get_input();
std::vector<std::string> process_record(std::string& r);
void print_record(std::vector<std::string> vs, std::fstream& ofh);
int main() try {
auto vs = get_input();
std::fstream ifh;
ifh.open(vs[0], std::ios::in);
if (!ifh)
throw std::runtime_error("File not opened: " + vs[0]);
std::fstream ofh;
ofh.open(vs[1], std::ios::out);
if (!ofh)
throw std::runtime_error("File not opened: " + vs[1]);
//precompling regex
std::regex leading_trailing_spaces(R"(^ +| +$|( ) +)");
std::regex looks_like_number { R"(\$?\d\d?\d?(,\d{3})*(\.\d*)?)" };
for(std::string str; std::getline(ifh,str);) {
for (size_t n{}; (n = std::count(str.begin(), str.end(), '"') % 2);){ //ensuring all double quotes are closed
//If they're not closed I'm grabing the the next record and replacing
//the return character with a literal \n.
std::string next_line;
std::getline(ifh, next_line);
str += R"(\n)" + next_line;
}
auto record = process_record(str); //putting the individual fields in a vector.
for(auto& s : record) {
s = std::regex_replace(s , leading_trailing_spaces, "$1"); //remove leading and trailing spaces
std::smatch sm;
if (std::regex_match(s, sm, looks_like_number)) {
std::remove(s.begin(), s.end(), ','); //remove commas in numbers
std::remove(s.begin(), s.end(), '$'); //remove dollar sign
}
std::replace(s.begin(), s.end(), ',', ';'); //replacing commas with semi-colons
}
print_record(record, ofh);
}
return 0;
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
catch (...) {
std::cerr << "uncaught" << '\n';
return 1;
}
void print_record(std::vector<std::string> vs, std::fstream& ofh) {
std::string str;
for(auto s : vs) {
str += s;
str += ',';
}
str = std::regex_replace (str, std::regex(R"(,+$)"), ""); //removing trailing commas
ofh << str << std::endl;
}
std::vector<std::string> process_record(std::string& r) {
std::stringstream ss{r};
std::vector<std::string> rv;
char ch;
std::string field{};
while (ss.get(ch)) {
if(ch == '"') {
while (ss.get(ch)) {
if (ch != '"') {
field += ch;
}
else {
ss.get(ch);
break;
}
}
}
if(ch == ',') {
rv.push_back(field);
field.clear();
continue;
}
field += ch;
}
return rv;
}
std::vector<std::string> get_input() {
std::vector<std::string> return_vstr;
std::fstream ifh;
ifh.open(R"(input_info.xml)", std::ios::in);
if (!ifh)
throw std::runtime_error("File not opened--check to see if input_info.xml is correct");
std::string info;
char ch;
while (ifh.get(ch)) {
if (ch == '\n') ch = ' ';
info.push_back(ch);
}
std::smatch sm;
std::regex in(R"(.*?<input1>(.*?)</input1>.*)");
std::regex out(R"(.*?<output1>(.*?)</output1>.*)");
std::regex_match (info, sm, in);
return_vstr.push_back(sm[1]);
std::regex_match (info, sm, out);
return_vstr.push_back(sm[1]);
return return_vstr;
}
|