00001
00002
00003
00004
00005 #include <stdexcept>
00006 #include <string>
00007 #include <fstream>
00008
00009 #include <boost/iostreams/filtering_stream.hpp>
00010 #include <boost/iostreams/device/file.hpp>
00011 #include <boost/iostreams/filter/newline.hpp>
00012 #include <boost/iostreams/filter/line.hpp>
00013 #include <boost/iostreams/filter/counter.hpp>
00014
00015 #include "loader0.hpp"
00016
00018 #define BUFFER_LEN 256
00019
00020 namespace eepgwde { namespace detail {
00021
00022 namespace io = boost::iostreams;
00023
00026
00030
00031 struct loader::Impl {
00032 friend class loader;
00033
00035 char buffer[BUFFER_LEN];
00036
00037 const std::string &n;
00038 std::ifstream fin;
00039 io::file_source buf;
00040 io::filtering_istream in_;
00041 io::counter lcounter;
00042 io::newline_filter lfilter;
00043
00045 void init0() {
00046 in_.push(boost::ref(lfilter));
00047 in_.push(boost::ref(lcounter));
00048 in_.push(boost::ref(buf));
00049 }
00050
00052 explicit Impl(const std::string &n) throw(std::exception) :
00053 n(n),
00054 buf(n, std::ifstream::in),
00055 lcounter(1),
00056 lfilter(io::newline::posix) {
00057 init0();
00058 }
00059
00061 ~Impl() throw() {
00062 try {
00063 buf.close();
00064 } catch (...) {
00065 }
00066 }
00067
00068 };
00069
00070 loader::loader(const std::string & n) throw(std::exception) :
00071 impl(new Impl(n)) { }
00072
00073 char * loader::get() throw(std::exception) {
00074 if (!impl->in_.good()) {
00075 return 0;
00076 }
00077 impl->in_.getline (impl->buffer, BUFFER_LEN - 1);
00078 return impl->buffer;
00079 }
00080
00081
00082
00083
00084
00085 int loader::line() const {
00086 int lines = impl->lcounter.lines();
00087 return lines;
00088 }
00089
00090 loader::~loader() {
00091 }
00092
00094
00095 }}