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
|
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <vector>
#include <fstream>
#include <sstream>
#include <regex>
#include <thread>
std::vector<std::string> get_input();
std::vector<std::string> process_record(std::string& r);
inline void print_record(const std::regex& r, std::vector<std::string> vs, std::fstream& ofh);
inline void replace_all(std::string& data, std::string to_search, std::string replace_str);
void task(const std::regex& number, const std::regex& leading_and_trailing, std::string str, std::vector<std::string>& record);
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]);
//here are my precompiled regex
const std::regex leading_and_trailing {R"(^ +| +$|( ) +)"};
const std::regex number { R"(\$?\d\d?\d?(,\d{3})*(\.\d*)?)" };
const std::regex trailing_comma {R"(,+$)"};
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;
}
std::vector<std::string> record;
std::thread t1(task, std::cref(number), std::cref(leading_and_trailing), str, std::ref(record));
t1.join();
print_record(trailing_comma, record, ofh);
}
return 0;
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
catch (...) {
std::cerr << "uncaught" << '\n';
return 1;
}
inline void print_record(const std::regex& r, std::vector<std::string> vs, std::fstream& ofh) {
std::string str;
for(auto s : vs) {
str += s;
str += ',';
}
ofh << std::regex_replace (str, r, "") << 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;
}
inline void replace_all(std::string& data, std::string to_search, std::string replace_str){
size_t pos = data.find(to_search);
while( pos != std::string::npos){
data.replace(pos, to_search.size(), replace_str);
pos =data.find(to_search, pos + replace_str.size());
}
}
void task(const std::regex& number, const std::regex& leading_and_trailing, std::string str, std::vector<std::string>& record){
replace_all(str, R"("")", R"('')"); //removing double quotes inside of quotes
record = process_record(str); //putting the individual fields in a vector.
for(auto& s : record) {
s = std::regex_replace(s , leading_and_trailing, "$1"); //remove leading and trailing spaces
std::smatch sm;
if (std::regex_match(s, sm, number)) {
const auto e1 {std::remove(s.begin(), s.end(), ',')}; //remove commas in numbers
s.erase(std::remove(s.begin(), e1, '$'), s.end()); //remove dollar sign
}
std::replace(s.begin(), s.end(), ',', ';'); //replacing commas with semi-colons
}
}
|