lemon/bits/item_writer.h
changeset 1533 43c7b3085212
parent 1435 8e85e6bbefdf
child 1535 e667cd5c0886
equal deleted inserted replaced
0:5d0919b736f5 1:287650cb7c72
    54     ///
    54     ///
    55     /// Writes a quoted string to the given stream.
    55     /// Writes a quoted string to the given stream.
    56     void write(std::ostream& os, const std::string& value) {
    56     void write(std::ostream& os, const std::string& value) {
    57       os << "\"";
    57       os << "\"";
    58       if (escaped) {
    58       if (escaped) {
    59 	std::ostringstream ls;
    59 	std::ostringstream ls(value);
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    61 	  writeEscape(ls, value[i]);
    61 	  writeEscape(ls, value[i]);
    62 	}
    62 	}
    63 	os << ls.str();
    63 	os << ls.str();
    64       } else {
    64       } else {
   114       }     
   114       }     
   115     }
   115     }
   116   private:
   116   private:
   117     bool escaped;
   117     bool escaped;
   118   };
   118   };
       
   119 
       
   120   /// \ingroup item_io
       
   121   /// \brief Writer class for quoted char array.
       
   122   ///
       
   123   /// Writer class for quoted char array. It can process the escape
       
   124   /// sequences in the char array.
       
   125   /// \author Balazs Dezso
       
   126   class QuotedCharArrayWriter {
       
   127   public:
       
   128     typedef const char* Value;
       
   129 
       
   130     /// \brief Constructor for the writer.
       
   131     ///
       
   132     /// Constructor for the writer. If the given parameter is true
       
   133     /// the writer creates escape sequences from special characters.
       
   134     QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
       
   135 
       
   136     /// \brief Writes a quoted char array to the given stream.
       
   137     ///
       
   138     /// Writes a quoted char array to the given stream.
       
   139     void write(std::ostream& os, const char* value) {
       
   140       QuotedStringWriter(escaped).write(os, std::string(value));
       
   141     }
       
   142 
       
   143   private:    
       
   144     bool escaped;
       
   145   };
       
   146 
   119 
   147 
   120   /// \ingroup item_io
   148   /// \ingroup item_io
   121   ///
   149   ///
   122   /// \brief Writer for standard containers.
   150   /// \brief Writer for standard containers.
   123   ///
   151   ///
   179 
   207 
   180   template <>
   208   template <>
   181   class DefaultWriter<std::string> 
   209   class DefaultWriter<std::string> 
   182     : public QuotedStringWriter {};
   210     : public QuotedStringWriter {};
   183 
   211 
       
   212   template <int length>
       
   213   class DefaultWriter<char[length]> 
       
   214     : public QuotedCharArrayWriter {};
       
   215 
       
   216   template <int length>
       
   217   class DefaultWriter<const char[length]> 
       
   218     : public QuotedCharArrayWriter {};
       
   219 
   184   template <typename Item>
   220   template <typename Item>
   185   class DefaultWriter<std::vector<Item> > 
   221   class DefaultWriter<std::vector<Item> > 
   186     : public IterableWriter<std::vector<Item> > {};
   222     : public IterableWriter<std::vector<Item> > {};
   187 
   223 
   188   template <typename Item>
   224   template <typename Item>
   208   /// It defines standard writing method for all type of value. 
   244   /// It defines standard writing method for all type of value. 
   209   /// \author Balazs Dezso
   245   /// \author Balazs Dezso
   210   struct DefaultWriterTraits {
   246   struct DefaultWriterTraits {
   211 
   247 
   212     template <typename _Value>
   248     template <typename _Value>
   213     struct Writer : DefaultWriter<_Value> {};
   249     struct Writer : DefaultWriter<_Value> {
       
   250       typedef DefaultWriter<_Value> Parent;
       
   251     };
   214 
   252 
   215   };
   253   };
   216 
   254 
   217 }
   255 }
   218 
   256