tclpkg-io.cc

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2000-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Mon Oct 30 10:00:39 2000
00011 // commit: $Id: tclpkg-io.cc 10065 2007-04-12 05:54:56Z rjpeters $
00012 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/tcl-io/tclpkg-io.cc $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00034 
00035 #ifndef GROOVX_TCL_IO_TCLPKG_IO_CC_UTC20050712162004_DEFINED
00036 #define GROOVX_TCL_IO_TCLPKG_IO_CC_UTC20050712162004_DEFINED
00037 
00038 #include "tcl-io/tclpkg-io.h"
00039 
00040 #include "io/io.h"
00041 #include "io/iolegacy.h"
00042 #include "io/ioutil.h"
00043 #include "io/outputfile.h"
00044 #include "io/xmlreader.h"
00045 
00046 #include "tcl/objpkg.h"
00047 #include "tcl/list.h"
00048 #include "tcl/pkg.h"
00049 
00050 #include "rutz/error.h"
00051 #include "rutz/fstring.h"
00052 #include "rutz/sfmt.h"
00053 
00054 #include <fstream>
00055 
00056 #include "rutz/trace.h"
00057 
00058 using nub::ref;
00059 
00060 namespace
00061 {
00062   const int ALL = -1; // indicates to read all objects until eof
00063 
00064   tcl::list loadObjects(const char* file, int num_to_read)
00065   {
00066     std::ifstream ifs(file);
00067     if (ifs.fail())
00068       {
00069         throw rutz::error("unable to open file", SRC_POS);
00070       }
00071 
00072     int num_read = 0;
00073 
00074     ifs >> std::ws;
00075 
00076     tcl::list result;
00077 
00078     while ( (num_to_read == ALL || num_read < num_to_read)
00079             && (ifs.peek() != EOF) )
00080       {
00081         // allow for whole-line comments between objects beginning
00082         // with '#'
00083         if (ifs.peek() == '#')
00084           {
00085             ifs.ignore(10000000, '\n');
00086             continue;
00087           }
00088 
00089         io::legacy_reader reader(ifs);
00090 
00091         ref<io::serializable> obj(reader.read_root(0));
00092 
00093         result.append(obj.id());
00094 
00095         ++num_read;
00096 
00097         ifs >> std::ws;
00098       }
00099 
00100     return result;
00101   }
00102 
00103   void saveObjects(tcl::list objids, const char* filename,
00104                    bool use_bases)
00105   {
00106     std::ofstream ofs(filename);
00107     if (ofs.fail())
00108       {
00109         throw rutz::error(rutz::sfmt("error opening file: %s",
00110                                      filename), SRC_POS);
00111       }
00112 
00113     io::legacy_writer writer(ofs, use_bases);
00114     writer.use_pretty_print(false);
00115 
00116     for (tcl::list::iterator<ref<io::serializable> >
00117            itr = objids.begin<ref<io::serializable> >(),
00118            end = objids.end<ref<io::serializable> >();
00119          itr != end;
00120          ++itr)
00121       {
00122         writer.write_root((*itr).get());
00123       }
00124   }
00125 }
00126 
00127 extern "C"
00128 int Io_Init(Tcl_Interp* interp)
00129 {
00130 GVX_TRACE("Io_Init");
00131 
00132   GVX_PKG_CREATE(pkg, interp, "io", "4.$Revision: 10065 $");
00133   pkg->inherit_pkg("Obj");
00134   tcl::def_basic_type_cmds<io::serializable>(pkg, SRC_POS);
00135 
00136   pkg->def( "loadObjects", "filename num_to_read=-1", &loadObjects, SRC_POS );
00137   pkg->def( "loadObjects", "filename", rutz::bind_last(&loadObjects, ALL), SRC_POS );
00138   pkg->def( "saveObjects", "objids filename use_bases=yes", &saveObjects, SRC_POS );
00139   pkg->def( "saveObjects", "objids filename",
00140             rutz::bind_last(&saveObjects, true), SRC_POS );
00141 
00142   const unsigned int keyarg = 1;
00143 
00144   pkg->def_vec( "write_lgx", "objref(s)", io::write_lgx, keyarg, SRC_POS );
00145   pkg->def_vec( "read_lgx", "objref(s) string(s)", io::read_lgx, keyarg, SRC_POS );
00146 
00147   pkg->def_vec( "write_asw", "objref(s)", io::write_asw, keyarg, SRC_POS );
00148   pkg->def_vec( "read_asw", "objref(s) string(s)", io::read_asw, keyarg, SRC_POS );
00149   pkg->def( "save_asw", "objref filename", io::save_asw, SRC_POS );
00150   pkg->def( "load_asw", "objref filename", io::load_asw, SRC_POS );
00151   pkg->def( "retrieve_asw", "filename", io::retrieve_asw, SRC_POS );
00152 
00153   pkg->def_vec( "write_gvx", "objref(s)", io::write_gvx, keyarg, SRC_POS );
00154   pkg->def( "save_gvx", "objref filename", io::save_gvx, SRC_POS );
00155   pkg->def( "load_gvx", "filename", io::load_gvx, SRC_POS );
00156 
00157   pkg->def( "xml_debug", "filename", io::xml_debug, SRC_POS );
00158 
00159   GVX_PKG_RETURN(pkg);
00160 }
00161 
00162 extern "C"
00163 int Outputfile_Init(Tcl_Interp* interp)
00164 {
00165 GVX_TRACE("Outputfile_Init");
00166 
00167   GVX_PKG_CREATE(pkg, interp, "output_file", "4.$Revision: 10065 $");
00168   pkg->inherit_pkg("io");
00169   tcl::def_creator<output_file>(pkg);
00170   tcl::def_basic_type_cmds<io::serializable>(pkg, SRC_POS);
00171 
00172   pkg->def_get_set("filename",
00173                    &output_file::get_filename,
00174                    &output_file::set_filename,
00175                    SRC_POS);
00176 
00177   GVX_PKG_RETURN(pkg);
00178 }
00179 
00180 static const char __attribute__((used)) vcid_groovx_tcl_io_tclpkg_io_cc_utc20050712162004[] = "$Id: tclpkg-io.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00181 #endif // !GROOVX_TCL_IO_TCLPKG_IO_CC_UTC20050712162004_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:41 2008 by Doxygen version 1.5.5.