Author: h.mohri
Convert from CSV file to “JSON file” of “TFDMemTable format”.
This program uses C++Builder 10.2 Tokyo Release 1.
include
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><FireDAC.Comp.Client.hpp></span> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><FireDAC.Stan.StorageJSON.hpp></span> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"><FireDAC.Stan.Intf.hpp></span> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> <span class="synPreProc" style="color:#9355e6;">#include </span><span class="synConstant" style="color:#ff6666;"> |
code
|
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 |
<span class="synType" style="color:#3ec63e;">struct</span> _Tcsv_to_memtable { std::mutex mx_; std::vector<std::thread> vth_; std::unique_ptr std::unique_ptr <span class="synType" style="color:#3ec63e;">void</span> create_column(std::vector { ">//Field creation of "TFDMemTable" synComment" style="color:#4f80e5;">//The field name is the first line of CSV File synStatement" style="color:#d88a17;">for (synType" style="color:#3ec63e;">auto s: v) { synType" style="color:#3ec63e;">auto fld = mem_table_->FieldDefs->AddFieldDef(); synComment" style="color:#4f80e5;">//Discard the "BOM (Byte Order Mark)" if present. fld->Name = StringReplace(s, synSpecial" style="color:#c000c0;">L'x0feff', synConstant" style="color:#ff6666;">"", TReplaceFlags()); fld->DataType = TFieldType::ftWideString; } mem_table_->Active = synConstant" style="color:#ff6666;">true; } synType" style="color:#3ec63e;">void create_line(std::vector { synComment" style="color:#4f80e5;">//Add 1 line of "CSV File" to "TFDMemTable" synType" style="color:#3ec63e;">int i{synConstant" style="color:#ff6666;">0}; mem_table_->Append(); synStatement" style="color:#d88a17;">for (synType" style="color:#3ec63e;">auto s: v) { synComment" style="color:#4f80e5;">//One item for one field. mem_table_->Fields->synStatement" style="color:#d88a17;">operator[](i)->AsWideString = s; ++i; } mem_table_->Post(); } synType" style="color:#3ec63e;">void create_line(std::wstring ws, synType" style="color:#3ec63e;">int line_num) { std::lock_guard synStatement" style="color:#d88a17;">if (ws.length() > synConstant" style="color:#ff6666;">0) { synComment" style="color:#4f80e5;">//Inserted by dividing one line of CSV File to vector std::vector std::wstring column_data; std::wistringstream line_data(ws); synStatement" style="color:#d88a17;">while (std::getline(line_data, column_data, synConstant" style="color:#ff6666;">L',')) { v1.push_back(column_data.c_str()); } synStatement" style="color:#d88a17;">switch (line_num) { synStatement" style="color:#d88a17;">case synConstant" style="color:#ff6666;">0: create_column(v1); synStatement" style="color:#d88a17;">break; synStatement" style="color:#d88a17;">default: create_line(v1); } } } synType" style="color:#3ec63e;">void threads_join() { synComment" style="color:#4f80e5;">//Wait for the termination of all threads. synStatement" style="color:#d88a17;">for (synType" style="color:#3ec63e;">auto& th: vth_) { th.join(); } } }; synType" style="color:#3ec63e;">int _tmain(synType" style="color:#3ec63e;">int argc, _TCHAR* argv[]) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); _Tcsv_to_memtable memtable_; UnicodeString argv_ = argv[synConstant" style="color: #ff6666;">1]; synComment" style="color:#4f80e5;">//Specify CSV Fire for input. UnicodeString arg_o_ = argv[synConstant" style="color: #ff6666;">2]; synComment" style="color:#4f80e5;">//JSON output destination specification made with "TFDMemTable". synStatement" style="color:#d88a17;">if (FileExists(argv_)) { std::wfstream file_; file_.open(argv_.w_str(), std::ios::in); synComment" style="color:#4f80e5;">//The CSV file is utf8 format. file_.imbue(std::locale(std::locale::empty(), synStatement" style="color:#d88a17;">new std::codecvt_utf8<synType" style="color:#3ec63e;">wchar_t>)); std::wstring buff; synType" style="color:#3ec63e;">int line_num_{synConstant" style="color:#ff6666;">0}; synStatement" style="color:#d88a17;">while (std::getline(file_, buff)) { synComment" style="color:#4f80e5;">//The append processing to "TFDMemTable" threads line by line. memtable_.vth_.push_back(std::thread([&memtable_, &buff, line_num_](){ memtable_.create_line(buff, line_num_); })); synComment" style="color:#4f80e5;">//Since the command line is multibyte, AnsiString conversion is done. AnsiString as_ = buff.c_str(); std::cout << as_ << std::endl; ++line_num_; } memtable_.threads_join(); synComment" style="color:#4f80e5;">//Finally, save the TFDMemTable contents as a JSON file. memtable_.mem_table_->SaveToFile(arg_o_, TFDStorageFormat::sfJSON); } synType" style="color:#3ec63e;">auto keika = std::chrono::system_clock::now() - now; std::cout << std::chrono::duration_cast std::cout << synConstant" style="color:#ff6666;">" millisecond." << std::endl ; synStatement" style="color:#d88a17;">return synConstant" style="color:#ff6666;">0</span>; } |
There is a project file in github, you can try it right away.
GitHub – mojeld/csvfstream_to_TFDMemTable: Create json file of TFDMemTable format from CSV file.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Free Delphi Community Edition Free C++Builder Community Edition







