stream_wrapper.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
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     } // end of namespace internal
00266 
00267   } // end of namespace io
00268 
00269 } // end of namespace oln
00270 
00271 #endif // ! OLENA_IO_STREAM_WRAPPER_HH

Generated on Thu Apr 15 20:13:14 2004 for Olena by doxygen 1.3.6-20040222