00001 #ifndef DVUTIL_NOCOMMENTSTREAM_H 00002 #define DVUTIL_NOCOMMENTSTREAM_H 00003 // $Id: nocomment.h,v 1.8 2003/08/21 09:27:17 dvermeir Exp $ 00004 00005 #include <string> 00006 #include <iostream> 00007 #include <dvutil/filterstreambuf.h> 00008 00009 /** 00010 * @file 00011 * This files defines a class nocommentstream, which uses 00012 * a filterstreambuf to remove shell style comments. 00013 */ 00014 00015 namespace Dv { 00016 namespace Util { 00017 /** 00018 * A nocommentstream is an istream that strips '#' comments. 00019 * All characters following (and including) a '#' are ignored 00020 * up to (but not including) the next newline character. 00021 * 00022 * Example usage: 00023 * @code 00024 * std::ifstream ifs("config.txt"); 00025 * if (!ifs) 00026 * throw std::runtime_error("config.txt: cannot open"); 00027 * Dv::Util::nocommentstream nocifs(ifs); 00028 * std::string line; 00029 * while ( std::getline(nocifs, line) ) { 00030 * // line does not contain comments 00031 * } 00032 * @endcode 00033 */ 00034 class nocommentstream: public std::istream { 00035 private: 00036 /** Implements Filter interface for filterstreambuf */ 00037 class NoCommentFilter { 00038 public: 00039 NoCommentFilter(std::istream& is): is_(is) {} 00040 ~NoCommentFilter(); 00041 int put(int c) { return EOF; } 00042 int get(); 00043 int sync() { return -1; } 00044 void close() {} 00045 std::ios::iostate state() const; 00046 private: 00047 /** Underlying istream from where the filter takes its input. */ 00048 std::istream& is_; 00049 }; 00050 public: 00051 /** Constructor. 00052 * @param is open stream from which original input is taken 00053 */ 00054 nocommentstream(std::istream& is); 00055 /** Destructor. */ 00056 ~nocommentstream(); 00057 private: 00058 /** Filter */ 00059 NoCommentFilter filter_; 00060 }; 00061 }} 00062 #endif
dvutil-1.0.10 | [ 5 December, 2009] |