41 #ifdef USE_BOOST_LEXICAL_CAST 42 # include <boost/lexical_cast.hpp> 52 inline std::string
const &
replace(std::string & src, std::string
const & to_find, std::string
const & to_replace)
55 while (std::string::npos != pos)
57 pos = src.find(to_find, pos);
59 if (std::string::npos != pos)
61 src.erase(pos, to_find.size());
62 src.insert(pos, to_replace);
63 pos += to_replace.size();
70 inline std::string
trim_right(
const std::string& str,
const std::string& trimChars)
72 std::string result =
"";
73 size_t endpos = str.find_last_not_of(trimChars);
74 if (std::string::npos != endpos)
76 result = str.substr(0, endpos + 1);
84 inline std::string
trim_left(
const std::string& str,
const std::string& trimChars)
86 std::string result =
"";
88 size_t startpos = str.find_first_not_of(trimChars);
89 if (std::string::npos != startpos)
91 result = str.substr(startpos);
99 inline std::string
trim(
const std::string& str,
const std::string& trimChars)
124 , trim_quote_on_str(
false)
126 , terminate_on_blank_line(
true)
127 , quote_unescape(
""")
129 , first_line_read(
false)
139 void open(
const char * file)
143 istm.open(file, std::ios_base::in);
148 char tt[3] = { 0, 0, 0 };
150 istm.read(tt,
sizeof(tt));
152 if (tt[0] == (
char)0xEF || tt[1] == (
char)0xBB || tt[2] == (
char)0xBF)
155 istm.seekg(0, istm.beg);
163 trim_quote_on_str =
false;
165 terminate_on_blank_line =
true;
167 first_line_read =
false;
178 return istm.is_open();
180 void enable_trim_quote_on_str(
bool enable,
char quote,
const std::string& unescape =
""")
182 trim_quote_on_str = enable;
184 quote_unescape = unescape;
188 void set_delimiter(
char delimiter_, std::string
const & unescape_str_)
191 unescape_str = unescape_str_;
197 std::string
const & get_unescape_str()
const 205 std::getline(istm, str);
208 if (first_line_read ==
false)
210 first_line_read =
true;
219 std::getline(istm, this->str);
222 if (first_line_read ==
false)
224 first_line_read =
true;
227 this->str = this->str.substr(3);
231 if (this->str.empty())
233 if (terminate_on_blank_line)
245 std::string get_delimited_str()
247 std::string str =
"";
249 bool within_quote =
false;
252 if (pos >= this->str.size())
257 return unescape(str);
261 if (trim_quote_on_str)
263 if (within_quote ==
false && ch == trim_quote && ((pos > 0 && this->str[pos - 1] ==
delimiter[0]) || pos == 0))
265 else if (within_quote && ch == trim_quote)
266 within_quote =
false;
271 if (ch ==
delimiter[0] && within_quote ==
false)
273 if (ch ==
'\r' || ch ==
'\n')
280 return unescape(str);
282 std::string unescape(std::string & src)
286 if (trim_quote_on_str)
288 std::string s =
trim(src, std::string(1, trim_quote));
289 return replace(s, quote_unescape, std::string(1, trim_quote));
294 size_t num_of_delimiter()
const 300 if (trim_quote_on_str)
302 bool inside_quote =
false;
303 for (
size_t i = 0; i < str.size(); ++i)
305 if (str[i] == trim_quote)
306 inside_quote = !inside_quote;
317 cnt = std::count(str.begin(), str.end(),
delimiter[0]);
321 std::string get_rest_of_line()
const 323 return str.substr(pos);
325 const std::string& get_line()
const 329 void enable_terminate_on_blank_line(
bool enable)
331 terminate_on_blank_line = enable;
333 bool is_terminate_on_blank_line()
const 335 return terminate_on_blank_line;
337 std::string error_line(
const std::string& token)
339 std::ostringstream is;
340 is <<
"csv::ifstream Conversion error at line no.:" << line_num <<
", filename:" << filename <<
", token position:" << token_num <<
", token:" << token;
349 std::string unescape_str;
350 bool trim_quote_on_str;
352 bool terminate_on_blank_line;
353 std::string quote_unescape;
355 bool first_line_read;
356 std::string filename;
366 : after_newline(
true)
369 , surround_quote_on_str(
false)
370 , surround_quote(
'\"')
371 , quote_escape(
""")
378 void open(
const char * file)
381 ostm.open(file, std::ios_base::out);
385 after_newline =
true;
388 surround_quote_on_str =
false;
389 surround_quote =
'\"';
390 quote_escape =
""";
402 return ostm.is_open();
404 void enable_surround_quote_on_str(
bool enable,
char quote,
const std::string&
escape =
""")
406 surround_quote_on_str = enable;
407 surround_quote = quote;
410 void set_delimiter(
char delimiter_, std::string
const & escape_str_)
413 escape_str = escape_str_;
419 std::string
const & get_escape_str()
const 423 void set_after_newline(
bool after_newline_)
425 after_newline = after_newline_;
427 bool get_after_newline()
const 429 return after_newline;
431 std::ofstream& get_ofstream()
435 void escape_and_output(std::string src)
439 void escape_str_and_output(std::string src)
442 if (surround_quote_on_str)
444 if (!quote_escape.empty())
446 src =
replace(src, std::string(1, surround_quote), quote_escape);
448 ostm << surround_quote << src << surround_quote;
459 std::string escape_str;
460 bool surround_quote_on_str;
462 std::string quote_escape;
472 std::string str = istm.get_delimited_str();
474 #ifdef USE_BOOST_LEXICAL_CAST 477 val = boost::lexical_cast<T>(str);
479 catch (boost::bad_lexical_cast& e)
481 throw std::runtime_error(istm.error_line(str).c_str());
484 std::istringstream is(str);
488 throw std::runtime_error(istm.error_line(str).c_str());
497 val = istm.get_delimited_str();
513 if(!ostm.get_after_newline())
514 ostm.get_ofstream() << ostm.get_delimiter();
516 std::ostringstream os_temp;
520 ostm.escape_and_output(os_temp.str());
522 ostm.set_after_newline(
false);
530 if (!ostm.get_after_newline())
531 ostm.get_ofstream() << ostm.get_delimiter();
533 std::ostringstream os_temp;
537 ostm.escape_and_output(os_temp.str());
539 ostm.set_after_newline(
false);
547 if (!ostm.get_after_newline())
548 ostm.get_ofstream() << ostm.get_delimiter();
550 std::string temp = val;
551 ostm.escape_str_and_output(temp);
553 ostm.set_after_newline(
false);
571 ostm.get_ofstream() <<
NEWLINE;
573 ostm.set_after_newline(
true);
577 std::ostringstream os_temp;
581 ostm.escape_and_output(os_temp.str());
589 const std::string temp = val;
609 , trim_quote_on_str(
false)
611 , terminate_on_blank_line(
true)
612 , quote_unescape(
""")
618 void enable_trim_quote_on_str(
bool enable,
char quote,
const std::string& unescape =
""")
620 trim_quote_on_str = enable;
622 quote_unescape = unescape;
624 void set_delimiter(
char delimiter_, std::string
const & unescape_str_)
627 unescape_str = unescape_str_;
633 std::string
const & get_unescape_str()
const 639 std::getline(istm, str);
647 std::getline(istm, this->str);
650 if (this->str.empty())
652 if (terminate_on_blank_line)
664 std::string get_delimited_str()
666 std::string str =
"";
668 bool within_quote =
false;
671 if (pos >= this->str.size())
676 return unescape(str);
680 if (trim_quote_on_str)
682 if (within_quote ==
false && ch == trim_quote && ((pos > 0 && this->str[pos - 1] ==
delimiter[0]) || pos == 0))
684 else if (within_quote && ch == trim_quote)
685 within_quote =
false;
690 if (ch ==
delimiter[0] && within_quote ==
false)
692 if (ch ==
'\r' || ch ==
'\n')
699 return unescape(str);
702 std::string unescape(std::string & src)
705 if (trim_quote_on_str)
707 std::string s =
trim(src, std::string(1, trim_quote));
708 return replace(s, quote_unescape, std::string(1, trim_quote));
713 size_t num_of_delimiter()
const 719 if (trim_quote_on_str)
721 bool inside_quote =
false;
722 for (
size_t i = 0; i < str.size(); ++i)
724 if (str[i] == trim_quote)
725 inside_quote = !inside_quote;
736 cnt = std::count(str.begin(), str.end(),
delimiter[0]);
740 std::string get_rest_of_line()
const 742 return str.substr(pos);
744 const std::string& get_line()
const 748 void enable_terminate_on_blank_line(
bool enable)
750 terminate_on_blank_line = enable;
752 bool is_terminate_on_blank_line()
const 754 return terminate_on_blank_line;
756 std::string error_line(
const std::string& token)
758 std::ostringstream is;
759 is <<
"csv::istringstream conversion error at line no.:" << line_num <<
", token position:" << token_num <<
", token:" << token;
764 std::istringstream istm;
768 std::string unescape_str;
769 bool trim_quote_on_str;
771 bool terminate_on_blank_line;
772 std::string quote_unescape;
782 : after_newline(
true)
785 , surround_quote_on_str(
false)
786 , surround_quote(
'\"')
787 , quote_escape(
""")
790 void enable_surround_quote_on_str(
bool enable,
char quote,
const std::string&
escape =
""")
792 surround_quote_on_str = enable;
793 surround_quote = quote;
796 void set_delimiter(
char delimiter_, std::string
const & escape_str_)
799 escape_str = escape_str_;
805 std::string
const & get_escape_str()
const 809 void set_after_newline(
bool after_newline_)
811 after_newline = after_newline_;
813 bool get_after_newline()
const 815 return after_newline;
817 std::ostringstream& get_ostringstream()
821 std::string get_text()
825 void escape_and_output(std::string src)
829 void escape_str_and_output(std::string src)
832 if (surround_quote_on_str)
834 if (!quote_escape.empty())
836 src =
replace(src, std::string(1, surround_quote), quote_escape);
838 ostm << surround_quote << src << surround_quote;
847 std::ostringstream ostm;
850 std::string escape_str;
851 bool surround_quote_on_str;
853 std::string quote_escape;
863 std::string str = istm.get_delimited_str();
865 #ifdef USE_BOOST_LEXICAL_CAST 868 val = boost::lexical_cast<T>(str);
870 catch (boost::bad_lexical_cast& e)
872 throw std::runtime_error(istm.error_line(str).c_str());
875 std::istringstream is(str);
879 throw std::runtime_error(istm.error_line(str).c_str());
888 val = istm.get_delimited_str();
904 if (!ostm.get_after_newline())
905 ostm.get_ostringstream() << ostm.get_delimiter();
907 std::ostringstream os_temp;
911 ostm.escape_and_output(os_temp.str());
913 ostm.set_after_newline(
false);
920 if (!ostm.get_after_newline())
921 ostm.get_ostringstream() << ostm.get_delimiter();
923 std::ostringstream os_temp;
927 ostm.escape_and_output(os_temp.str());
929 ostm.set_after_newline(
false);
936 if (!ostm.get_after_newline())
937 ostm.get_ostringstream() << ostm.get_delimiter();
939 std::string temp = val;
940 ostm.escape_str_and_output(temp);
942 ostm.set_after_newline(
false);
959 ostm.get_ostringstream() <<
NEWLINE;
961 ostm.set_after_newline(
true);
965 std::ostringstream os_temp;
969 ostm.escape_and_output(os_temp.str());
977 const std::string temp = val;
std::string trim_left(const std::string &str, const std::string &trimChars)
mini::csv::ifstream & operator>>(mini::csv::ifstream &istm, T &val)
sep(const char delimiter_, const std::string &escape_)
std::string trim_right(const std::string &str, const std::string &trimChars)
std::string const & replace(std::string &src, std::string const &to_find, std::string const &to_replace)
const std::string & get_escape() const
const char get_delimiter() const
mini::csv::ofstream & operator<<(mini::csv::ofstream &ostm, const T &val)
std::string trim(const std::string &str, const std::string &trimChars)