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 <string>
#include <fstream>
#include <vector>
#include <cctype>
#include <iomanip>
constexpr size_t defchunk1 {5'000'000};
// fn - file name
// buff - where to read to file to
// returns - true if read succeeded, false otherwise
bool WOreadFile(const std::string& fn, std::string& buff, size_t chunk = defchunk1) {
bool ret {}; // Problems
if (const auto fh {CreateFile(fn.c_str(), FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL)}; fh != INVALID_HANDLE_VALUE) {
if (const auto sz {GetFileSize(fh, NULL)}; sz != INVALID_FILE_SIZE) {
constexpr size_t evtsz {50};
const size_t nochk {sz / chunk + 1};
const auto thid {GetThreadId(GetCurrentThread())}; // Sort of unique event name
std::vector<OVERLAPPED> vo(nochk);
std::vector<HANDLE> vh(nochk);
char evt[evtsz] {};
buff.resize(sz);
for (size_t i = 0, done = 0; i < nochk; ++i, done += chunk) {
snprintf(evt, evtsz, "oevt%u-%zu", thid, i);
vo[i].hEvent = vh[i] = CreateEvent(NULL, FALSE, FALSE, evt);
vo[i].OffsetHigh = 0;
vo[i].Offset = done;
ReadFile(fh, buff.data() + done, std::min(chunk, (size_t)(sz - done)), NULL, &vo[i]);
}
WaitForMultipleObjects(vh.size(), (HANDLE*)vh.data(), TRUE, INFINITE);
ret = true;
for (const auto& e : vo) {
ret &= ((DWORD)e.Internal == ERROR_SUCCESS); // Have all reads succeeded
CloseHandle(e.hEvent); // Close events
}
}
CloseHandle(fh);
}
return ret;
}
// Remove spaces before/after , if not in quotes
// Remove $
// Replace "" with ''
// Remove , if in a number inside quotes
// Replace , with ; if in quotes
// Ignore /n /r inside quotes
std::string process(const std::string& str) {
std::string outstr(str.size(), 0);
bool inelem {};
bool inquote {};
const char* ch {str.c_str()};
char* outch {outstr.data()};
for (; *ch; ++ch) {
// Remove $
if (*ch == '$')
continue;
// Ignore space if not in an element
if (*ch == ' ') {
if (inelem) {
if (inquote && (*ch == ' '))
if ((*(ch - 1) == '\"') || (*(ch - 1) == ' '))
continue;
*outch++ = *ch;
}
continue;
}
// Remove previous spaces before a , or \n if not in quote
if (!inquote && (*ch == ',' || *ch == '\n' || *ch == '\r')) {
while ((outch > outstr.data()) && *(outch - 1) == ' ')
--outch;
*outch++ = *ch;
inelem = false;
continue;
}
// Ignore \n or \r if in quote
if (inquote && (*ch == '\n' || *ch == '\r'))
continue;
// Replace "" with ''
if (*ch == '\"' && *(ch + 1) == '\"') {
*outch++ = '\"';
++ch;
continue;
}
// Deal with quote
if (*ch == '"') {
inquote = !inquote;
inelem = true;
continue;
}
// Is , in a number in quotes
if (*ch == ',') {
// Ignore if part of a number otherwise replace with ;
if (!std::isdigit(static_cast<unsigned char>(*(ch + 1))))
*outch++ = ';';
continue;
}
inelem = true;
*outch++ = *ch;
}
outstr.resize(outch - outstr.data());
return outstr;
}
int main() {
//std::string str{ R"!(Item exp, 2"" B/M 90deg widget (As Per Quote AABBITU26.056))!" };
//const std::string str {R"!( qwe , pou , "4,5 qw,po "" (8)", 987)!"};
std::string buff;
if (!WOreadFile("junk_in.csv", buff))
return (std::cout << "Cannot read input file\n"), 1;
std::ofstream ofs("csvout2.csv", std::ios::binary);
if (!ofs)
return (std::cout << "Cannot open files\n"), 2;
ofs << process(buff);
}
| |