00001 #ifndef NMWR_GB_SKIP_COMMENTS_H 00002 #define NMWR_GB_SKIP_COMMENTS_H 00003 00004 00005 00006 // $LICENSE 00007 00008 00009 #include <iostream.h> 00010 #include <limits.h> 00011 #include <string> 00012 00013 class skip_comments_istream { 00014 public: 00015 skip_comments_istream(std::istream& in, 00016 char comment_begin = '#', 00017 char comment_end = '\n') 00018 : in_(&in), 00019 comment_begin_(comment_begin), 00020 comment_end_(comment_end) {} 00021 00022 istream& the_istream() { return skip_comments(*in_);} 00023 char begin_comment() const {return comment_begin_;} 00024 00025 istream& skip_comments(std::istream& in) 00026 { 00027 while(true) { 00028 in >> ws; 00029 if (in.peek() != comment_begin_) 00030 break; 00031 in.get(); 00032 in.ignore(INT_MAX,comment_end_); 00033 } 00034 return in; 00035 } 00036 00037 private: 00038 std::istream* in_; 00039 char comment_begin_, comment_end_; 00040 }; 00041 00042 inline std::istream& skip_comment(std::istream& in) { 00043 if(in) { 00044 skip_comments_istream sk(in); 00045 sk.skip_comments(in); 00046 } 00047 return in; 00048 } 00049 00050 // Spezialfall fuer strings: sonst probleme bei 00051 // xyz#Kommentar : wird als *ein* string gelesen, 00052 // und nicht als: xyz ( #Kommentar wird ueberlesen). 00053 // scheint soweit zu klappen. 00054 00055 00056 template<class T> 00057 inline skip_comments_istream& operator>>(skip_comments_istream& in, T& t) 00058 { 00059 in.the_istream() >> t; 00060 return in; 00061 } 00062 00063 // special case for strings: 00064 // if we had e.g. blurb#comment in an input, 00065 // it would be read as one string. 00066 00067 template<> 00068 inline skip_comments_istream& operator>>(skip_comments_istream& in, std::string& s); 00069 00070 00071 00072 #endif