00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <string>
00018 #include <iostream>
00019 #include <cassert>
00020 #include <vector>
00021 #include <fstream>
00022
00023 #include <boost/spirit/include/classic_core.hpp>
00024 #include <boost/spirit/include/classic_confix.hpp>
00025 #include <boost/spirit/include/classic_lists.hpp>
00026 #include <boost/spirit/include/classic_escape_char.hpp>
00027
00029 using namespace std;
00030 using namespace BOOST_SPIRIT_CLASSIC_NS;
00031
00033
00034 class list_actor
00035 {
00036 public:
00037 list_actor (std::vector<std::string> &vec_) : vec(vec_) {}
00038
00039
00040
00041
00042 template <typename ActionIterT>
00043 void operator() (ActionIterT const &first, ActionIterT const &last) const
00044 {
00045 vec.push_back(std::string(first, last-first));
00046 }
00047
00048 private:
00049 std::vector<std::string> &vec;
00050 };
00051
00052 #include "eleve/src/loader0.hpp"
00053
00054 int main (int argc, char *argv[])
00055 {
00056
00057
00058 rule<> list_csv, list_csv_item;
00059 std::vector<std::string> vec_list;
00060 std::vector<std::string> vec_item;
00061
00062 char * plist_csv;
00063
00064
00065
00066
00067 eepgwde::detail::loader l(argv[1]);
00068
00069
00070
00071
00072
00073
00074 list_csv_item =
00075 !(
00076 confix_p('\"', *c_escape_ch_p, '\"')
00077 | lexeme_d[((alpha_p | ch_p('_')) >> *(alnum_p | ch_p('_') | ch_p(' ')))]
00078 | lexeme_d[(digit_p >> digit_p >> *(digit_p | ch_p('-'))) ]
00079 | longest_d[real_p | int_p ]
00080 );
00081
00082 list_csv =
00083 list_p(
00084 list_csv_item[push_back_a(vec_item)],
00085 ','
00086 )[push_back_a(vec_list)]
00087 ;
00088
00089 parse_info<> result;
00090
00091 while ( plist_csv = l.get() ) {
00092
00093 vec_list.clear();
00094 vec_item.clear();
00095
00096 result = parse (plist_csv, list_csv);
00097
00098 cout << "--" << endl;
00099 if (result.hit) {
00100 cout
00101 << "Parsing CSV list (comma separated values) " << endl
00102 << "\t" << plist_csv << endl
00103 << "Parsed successfully!" << endl << endl;
00104
00105 if (result.full)
00106 {
00107 cout << "Matched " << (int)vec_list.size() <<
00108 " list elements (full list): " << endl;
00109 }
00110 else
00111 {
00112 cout << "Matched " << (int)vec_list.size() <<
00113 " list elements: " << endl;
00114 }
00115
00116 cout << "The list parser matched:" << endl;
00117 for (std::vector<std::string>::iterator itl = vec_list.begin();
00118 itl != vec_list.end(); ++itl)
00119 {
00120 cout << *itl << endl;
00121 }
00122
00123 cout << endl << "Item(s) got directly from the item parser:" << endl;
00124 for (std::vector<std::string>::iterator it = vec_item.begin();
00125 it != vec_item.end(); ++it)
00126 {
00127 cout << *it << endl;
00128 }
00129
00130 }
00131 else
00132 {
00133 cout << "Failed to parse CSV list!" << endl;
00134 }
00135
00136 cout << endl;
00137 }
00138
00139
00140 return 0;
00141 }
00142