00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OLENA_IO_STREAM_WRAPPER_HH
00029 # define OLENA_IO_STREAM_WRAPPER_HH
00030
00031 # include <oln/config/system.hh>
00032
00033 # include <oln/io/utils.hh>
00034
00035 # include <list>
00036 # include <string>
00037
00038 namespace oln {
00039
00040 namespace io {
00041
00042 namespace internal {
00043
00050 enum stream_id { StreamNone = 0,
00051 StreamFile = 1,
00052 StreamGz = 2,
00053 StreamAny = 2 };
00054
00065 template< stream_id W >
00066 struct stream_wrapper
00067 {
00068 static const std::string&
00069 name()
00070 {
00071 static const std::string name_("-");
00072 return name_;
00073 }
00074
00075 static bool
00076 knows_ext(const std::string&)
00077 {
00078 return false;
00079 }
00080
00081 static std::istream*
00082 wrap_in(std::string&)
00083 {
00084 return 0;
00085 }
00086
00087 static std::ostream*
00088 wrap_out(std::string&)
00089 {
00090 return 0;
00091 }
00092
00093 static void
00094 find(std::list<std::string>&, const std::string&)
00095 {}
00096 };
00097
00102 template <stream_id W>
00103 struct stream_wrappers_find_files
00104 {
00108 static void
00109 doit(std::list<std::string>& names, const std::string& name)
00110 {
00111 stream_wrapper<W>::find(names, name);
00112 return stream_wrappers_find_files<stream_id(unsigned(W)-1)>
00113 ::doit(names, name);
00114 }
00115 };
00116
00120 template <>
00121 struct stream_wrappers_find_files<StreamNone>
00122 {
00123 static void
00124 doit(std::list<std::string>&, const std::string&)
00125 {}
00126 };
00127
00128
00134 template<stream_id W, typename T, class Reader>
00135 struct try_stream_wrappers_in
00136 {
00137
00142 static bool
00143 by_extension(T& output, const std::string& name, const std::string& ext)
00144 {
00145 if (stream_wrapper<W>::knows_ext(ext))
00146 {
00147 std::string wrapped_name = name;
00148 if (std::istream* in = stream_wrapper<W>::wrap_in(wrapped_name))
00149 {
00150 std::string wrapped_ext = utils::extension(wrapped_name);
00151 bool result = Reader::doit(output, *in, wrapped_ext);
00152 delete in;
00153 if (result)
00154 return true;
00155 }
00156 }
00157 return try_stream_wrappers_in<stream_id(unsigned(W)-1),T,Reader>
00158 ::by_extension(output, name, ext);
00159 }
00160
00170 static bool
00171 by_data(T& output, const std::string& name)
00172 {
00173 std::string wrapped_name = name;
00174 if (std::istream* in = stream_wrapper<W>::wrap_in(wrapped_name))
00175 {
00176 std::string wrapped_ext = utils::extension(wrapped_name);
00177 bool result = Reader::doit(output, *in, wrapped_ext);
00178 delete in;
00179 if (result)
00180 return true;
00181 }
00182 return try_stream_wrappers_in<stream_id(unsigned(W)-1), T, Reader>
00183 ::by_data(output, name);
00184 }
00185 };
00186
00193 template< typename T, class Reader >
00194 struct try_stream_wrappers_in<StreamNone, T, Reader>
00195 {
00197 static bool
00198 by_extension(T&, const std::string&, const std::string&)
00199 {
00200 return false;
00201 }
00202
00204 static bool
00205 by_data(T&, const std::string&)
00206 {
00207 return false;
00208 }
00209 };
00210
00211
00217 template<stream_id W, typename T, class Writer>
00218 struct try_stream_wrappers_out
00219 {
00220
00225 static bool
00226 by_extension(const T& input,
00227 const std::string& name,
00228 const std::string& ext)
00229 {
00230 if (stream_wrapper<W>::knows_ext(ext))
00231 {
00232 std::string wrapped_name = name;
00233 std::ostream* out = stream_wrapper<W>::wrap_out(wrapped_name);
00234 if (out)
00235 {
00236 std::string wrapped_ext = utils::extension(wrapped_name);
00237 bool result = Writer::doit(input, *out, wrapped_ext);
00238 delete out;
00239 if (result)
00240 return true;
00241 }
00242 }
00243 return try_stream_wrappers_out<stream_id(unsigned(W)-1),T,Writer>
00244 ::by_extension(input, name, ext);
00245 }
00246 };
00247
00254 template< typename T, class Reader >
00255 struct try_stream_wrappers_out<StreamNone, T, Reader>
00256 {
00258 static bool
00259 by_extension(const T&, const std::string&, const std::string&)
00260 {
00261 return false;
00262 }
00263 };
00264
00265 }
00266
00267 }
00268
00269 }
00270
00271 #endif // ! OLENA_IO_STREAM_WRAPPER_HH