33 #ifdef USE_BOOST_LEXICAL_CAST 34 # include <boost/lexical_cast.hpp> 41 inline std::string
const &
replace(std::string & src, std::string
const & to_find, std::string
const & to_replace)
44 while (std::string::npos != pos)
46 pos = src.find(to_find, pos);
48 if (std::string::npos != pos)
50 src.erase(pos, to_find.size());
51 src.insert(pos, to_replace);
52 pos += to_replace.size();
59 inline std::string
trim_right(
const std::string& str,
const std::string& trimChars)
61 std::string result =
"";
62 size_t endpos = str.find_last_not_of(trimChars);
63 if (std::string::npos != endpos)
65 result = str.substr(0, endpos + 1);
73 inline std::string
trim_left(
const std::string& str,
const std::string& trimChars)
75 std::string result =
"";
77 size_t startpos = str.find_first_not_of(trimChars);
78 if (std::string::npos != startpos)
80 result = str.substr(startpos);
88 inline std::string
trim(
const std::string& str,
const std::string& trimChars)
96 ifstream() : str(
""), pos(0), delimiter(
","), unescape_str(
"##"), trim_quote_on_str(
false), trim_quote(
'\"'), terminate_on_blank_line(
true)
103 void open(
const char * file)
106 istm.open(file, std::ios_base::in);
114 trim_quote_on_str =
false;
116 terminate_on_blank_line =
true;
124 return istm.is_open();
126 void enable_trim_quote_on_str(
bool enable,
char quote)
128 trim_quote_on_str = enable;
133 void set_delimiter(
char delimiter_, std::string
const & unescape_str_)
135 delimiter = delimiter_;
136 unescape_str = unescape_str_;
138 std::string
const & get_delimiter()
const 142 std::string
const & get_unescape_str()
const 150 std::getline(istm, str);
159 std::getline(istm, this->str);
162 if (this->str.empty())
164 if (terminate_on_blank_line)
174 std::string get_delimited_str()
176 std::string str =
"";
178 bool within_quote =
false;
181 if(pos>=this->str.size())
185 return unescape(str);
189 if (trim_quote_on_str)
191 if (within_quote ==
false && ch == trim_quote && ((pos > 0 && this->str[pos - 1] == delimiter[0]) || pos == 0))
193 else if (within_quote && ch == trim_quote)
194 within_quote =
false;
199 if (ch == delimiter[0] && within_quote ==
false)
201 if (ch ==
'\r' || ch ==
'\n')
208 return unescape(str);
210 std::string unescape(std::string & src)
212 src = unescape_str.empty() ? src :
replace(src, unescape_str, delimiter);
213 return trim_quote_on_str ?
trim(src, std::string(1, trim_quote)) : src;
215 size_t num_of_delimiter()
const 217 if (delimiter.size() == 0)
221 for (
size_t i = 0; i < str.size(); ++i)
223 if (str[i] == delimiter[0])
228 std::string get_rest_of_line()
const 230 return str.substr(pos);
232 const std::string& get_line()
const 236 void enable_terminate_on_blank_line(
bool enable)
238 terminate_on_blank_line = enable;
240 bool is_terminate_on_blank_line()
const 242 return terminate_on_blank_line;
249 std::string delimiter;
250 std::string unescape_str;
251 bool trim_quote_on_str;
253 bool terminate_on_blank_line;
260 ofstream() : after_newline(
true), delimiter(
","), escape_str(
"##"), surround_quote_on_str(
false), surround_quote(
'\"')
267 void open(
const char * file)
270 ostm.open(file, std::ios_base::out);
274 after_newline =
true;
277 surround_quote_on_str =
false;
278 surround_quote =
'\"';
290 return ostm.is_open();
292 void enable_surround_quote_on_str(
bool enable,
char quote)
294 surround_quote_on_str = enable;
295 surround_quote = quote;
297 void set_delimiter(
char delimiter_, std::string
const & escape_str_)
299 delimiter = delimiter_;
300 escape_str = escape_str_;
302 std::string
const & get_delimiter()
const 306 std::string
const & get_escape_str()
const 310 void set_after_newline(
bool after_newline_)
312 after_newline = after_newline_;
314 bool get_after_newline()
const 316 return after_newline;
318 std::ofstream& get_ofstream()
322 void escape_and_output(std::string src)
324 ostm << ((escape_str.empty()) ? src :
replace(src, delimiter, escape_str));
326 void escape_str_and_output(std::string src)
328 src = ((escape_str.empty()) ? src :
replace(src, delimiter, escape_str));
329 if (surround_quote_on_str)
331 ostm << surround_quote << src << surround_quote;
341 std::string delimiter;
342 std::string escape_str;
343 bool surround_quote_on_str;
353 std::string str = istm.get_delimited_str();
355 #ifdef USE_BOOST_LEXICAL_CAST 356 val = boost::lexical_cast<
T>(str);
358 std::istringstream is(str);
367 val = istm.get_delimited_str();
375 if(!ostm.get_after_newline())
376 ostm.get_ofstream() << ostm.get_delimiter();
378 std::ostringstream os_temp;
382 ostm.escape_and_output(os_temp.str());
384 ostm.set_after_newline(
false);
392 if (!ostm.get_after_newline())
393 ostm.get_ofstream() << ostm.get_delimiter();
395 std::ostringstream os_temp;
399 ostm.escape_and_output(os_temp.str());
401 ostm.set_after_newline(
false);
409 if (!ostm.get_after_newline())
410 ostm.get_ofstream() << ostm.get_delimiter();
412 std::string temp = val;
413 ostm.escape_str_and_output(temp);
415 ostm.set_after_newline(
false);
424 ostm.get_ofstream() <<
NEWLINE;
426 ostm.set_after_newline(
true);
430 std::ostringstream os_temp;
434 ostm.escape_and_output(os_temp.str());
442 const std::string temp = val;
460 , trim_quote_on_str(
false)
462 , terminate_on_blank_line(
true)
466 void enable_trim_quote_on_str(
bool enable,
char quote)
468 trim_quote_on_str = enable;
471 void set_delimiter(
char delimiter_, std::string
const & unescape_str_)
473 delimiter = delimiter_;
474 unescape_str = unescape_str_;
476 std::string
const & get_delimiter()
const 480 std::string
const & get_unescape_str()
const 486 std::getline(istm, str);
494 std::getline(istm, this->str);
497 if (this->str.empty())
499 if (terminate_on_blank_line)
510 std::string get_delimited_str()
512 std::string str =
"";
514 bool within_quote =
false;
517 if (pos >= this->str.size())
521 return unescape(str);
525 if (trim_quote_on_str)
527 if (within_quote ==
false && ch == trim_quote && ((pos > 0 && this->str[pos - 1] == delimiter[0]) || pos == 0))
529 else if (within_quote && ch == trim_quote)
530 within_quote =
false;
535 if (ch == delimiter[0] && within_quote ==
false)
537 if (ch ==
'\r' || ch ==
'\n')
543 return unescape(str);
546 std::string unescape(std::string & src)
548 src = unescape_str.empty() ? src :
replace(src, unescape_str, delimiter);
549 return trim_quote_on_str ?
trim(src, std::string(1, trim_quote)) : src;
552 size_t num_of_delimiter()
const 554 if (delimiter.size() == 0)
558 for (
size_t i = 0; i < str.size(); ++i)
560 if (str[i] == delimiter[0])
565 std::string get_rest_of_line()
const 567 return str.substr(pos);
569 const std::string& get_line()
const 573 void enable_terminate_on_blank_line(
bool enable)
575 terminate_on_blank_line = enable;
577 bool is_terminate_on_blank_line()
const 579 return terminate_on_blank_line;
583 std::istringstream istm;
586 std::string delimiter;
587 std::string unescape_str;
588 bool trim_quote_on_str;
590 bool terminate_on_blank_line;
597 ostringstream() : after_newline(
true), delimiter(
","), escape_str(
"##"), surround_quote_on_str(
false), surround_quote(
'\"')
600 void enable_surround_quote_on_str(
bool enable,
char quote)
602 surround_quote_on_str = enable;
603 surround_quote = quote;
605 void set_delimiter(
char delimiter_, std::string
const & escape_str_)
607 delimiter = delimiter_;
608 escape_str = escape_str_;
610 std::string
const & get_delimiter()
const 614 std::string
const & get_escape_str()
const 618 void set_after_newline(
bool after_newline_)
620 after_newline = after_newline_;
622 bool get_after_newline()
const 624 return after_newline;
626 std::ostringstream& get_ostringstream()
630 std::string get_text()
634 void escape_and_output(std::string src)
636 ostm << ((escape_str.empty()) ? src :
replace(src, delimiter, escape_str));
638 void escape_str_and_output(std::string src)
640 src = ((escape_str.empty()) ? src :
replace(src, delimiter, escape_str));
641 if (surround_quote_on_str)
643 ostm << surround_quote << src << surround_quote;
652 std::ostringstream ostm;
654 std::string delimiter;
655 std::string escape_str;
656 bool surround_quote_on_str;
666 std::string str = istm.get_delimited_str();
668 #ifdef USE_BOOST_LEXICAL_CAST 669 val = boost::lexical_cast<
T>(str);
671 std::istringstream is(str);
680 val = istm.get_delimited_str();
688 if (!ostm.get_after_newline())
689 ostm.get_ostringstream() << ostm.get_delimiter();
691 std::ostringstream os_temp;
695 ostm.escape_and_output(os_temp.str());
697 ostm.set_after_newline(
false);
704 if (!ostm.get_after_newline())
705 ostm.get_ostringstream() << ostm.get_delimiter();
707 std::ostringstream os_temp;
711 ostm.escape_and_output(os_temp.str());
713 ostm.set_after_newline(
false);
720 if (!ostm.get_after_newline())
721 ostm.get_ostringstream() << ostm.get_delimiter();
723 std::string temp = val;
724 ostm.escape_str_and_output(temp);
726 ostm.set_after_newline(
false);
736 ostm.get_ostringstream() <<
NEWLINE;
738 ostm.set_after_newline(
true);
742 std::ostringstream os_temp;
746 ostm.escape_and_output(os_temp.str());
754 const std::string temp = val;
Definition: minicsv.h:256
const uint32_t T[512]
Definition: groestl_tables.h:34
Definition: minicsv.h:593
std::string trim(const std::string &str, const std::string &trimChars)
Definition: minicsv.h:88
#define NEWLINE
Definition: minicsv.h:37
std::string trim_left(const std::string &str, const std::string &trimChars)
Definition: minicsv.h:73
csv::ifstream & operator>>(csv::ifstream &istm, T &val)
Definition: minicsv.h:351
Definition: minicsv.h:452
std::string const & replace(std::string &src, std::string const &to_find, std::string const &to_replace)
Definition: minicsv.h:41
std::string trim_right(const std::string &str, const std::string &trimChars)
Definition: minicsv.h:59
csv::ofstream & operator<<(csv::ofstream &ostm, const T &val)
Definition: minicsv.h:373