00001
00002
00003 #ifndef NMWR_GB_SKIP_COMMENTS_H
00004 #define NMWR_GB_SKIP_COMMENTS_H
00005
00006
00013 #include <iostream>
00014 #include <string>
00015 #include <climits>
00016 #include <cctype>
00017
00018 class skip_comments_istream {
00019 public:
00020 skip_comments_istream(std::istream& in,
00021 char comment_begin = '#',
00022 char comment_end = '\n')
00023 : in_(&in),
00024 comment_begin_(comment_begin),
00025 comment_end_(comment_end) {}
00026
00027 std::istream& the_istream() { return skip_comments(*in_);}
00028 char begin_comment() const {return comment_begin_;}
00029
00030 std::istream& skip_comments(std::istream& in)
00031 {
00032 while(true) {
00033 in >> std::ws;
00034 if (in.peek() != comment_begin_)
00035 break;
00036 in.get();
00037 in.ignore(INT_MAX,comment_end_);
00038 }
00039 return in;
00040 }
00041
00042 private:
00043 std::istream* in_;
00044 char comment_begin_, comment_end_;
00045 };
00046
00047 inline std::istream& skip_comment(std::istream& in) {
00048 if(in) {
00049 skip_comments_istream sk(in);
00050 sk.skip_comments(in);
00051 }
00052 return in;
00053 }
00054
00055 inline skip_comments_istream& operator>>(skip_comments_istream& in,
00056 std::string& s)
00057 {
00058 std::istream& in1(in.the_istream());
00059 int max_len = 256;
00060 char* ss = new char[max_len];
00061
00062 int i = 0;
00063 char c = 'a';
00064 while(in1 && i < max_len-1 && (c != in.begin_comment()) && ! std::isspace(c)) {
00065 c = in1.get();
00066 ss[i] = c;
00067 i++;
00068 };
00069
00070 if(c == in.begin_comment() || std::isspace(c)) {
00071 if(in1)
00072 in1.putback(c);
00073 i--;
00074 }
00075
00076 ss[i] = '\0';
00077 s = ss;
00078 return in;
00079 }
00080
00081
00082 template<class T>
00083 inline skip_comments_istream& operator>>(skip_comments_istream& in, T& t)
00084 {
00085 in.the_istream() >> t;
00086 return in;
00087 }
00088
00089
00090
00091
00092 #endif