Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

dataformat.cc

Go to the documentation of this file.
00001 
00025 #include <fstream>
00026 #include <magic/mobject.h>
00027 #include <magic/mtextstream.h>
00028 #include "inanna/dataformat.h"
00029 #include "inanna/dataformats.h"
00030 
00031 
00032 
00034 //    ___                   -----                          |     o          //
00035 //    |  \   ___   |   ___  |                     ___   |  |       |        //
00036 //    |   |  ___| -+-  ___| |---   __  |/\ |/|/|  ___| -+- |     | |---     //
00037 //    |   | (   |  |  (   | |     /  \ |   | | | (   |  |  |     | |   )    //
00038 //    |__/   \__|   \  \__| |     \__/ |   | | |  \__|   \ |____ | |__/     //
00040 
00045 void DataFormatLib::load (const String& filename, PatternSet& set)
00046     throw (file_not_found, invalid_format, assertion_failed)
00047 {
00048     ASSERTWITH (!isempty(filename), "Filename required (was empty)");
00049 
00050     // Open the file
00051     TextIStream* in = &stin;
00052     if (filename != "-" && !filename.isEmpty()) {
00053         try {
00054             in = new TextIStream (new File (filename));
00055         } catch (Exception e) {
00056             throw file_not_found (e.what());
00057         }
00058 
00059         /* The success of creation should be checked.
00060         if (!*in)
00061             throw file_not_found (format ("Pattern set file '%s' not found", (CONSTR) filename));
00062         */
00063     }
00064 
00065     load (*in, set, filename);
00066 
00067     if (in != &stin)
00068         delete in;
00069 }
00070 
00075 void DataFormatLib::load (
00076     TextIStream&  in,              
00077     PatternSet&   set,             
00078     const String& filetype         )
00079     throw (invalid_format, assertion_failed)
00080 {
00081     DataFormat* handler = create (filetype);
00082     try {
00083         handler->load (in, set);
00084     } catch (...) {
00085         delete handler;
00086         throw; // Rethrow
00087     }
00088     delete handler;
00089 }
00090 
00091 void DataFormatLib::save (const String& filename, const PatternSet& set) throw (invalid_filename) {
00092     FILE* out=stdout;
00093     if (filename!="-" && !filename.isEmpty())
00094         out = fopen (filename, "w");
00095 
00096     ASSERTWITH (out, format ("Pattern save file '%s' couldn't be opened",
00097                              (CONSTR) filename));
00098 
00099     DataFormat* handler = create (filename);
00100 
00101     try {
00102         handler->save (out, set);
00103     } catch (must_overload e) {
00104         delete handler;
00105         throw invalid_filename (format ("Save-to-file operation not supported for file type\n%s", (CONSTR) e.what()));
00106     }
00107     delete handler;
00108 
00109     if (out != stdout)
00110         fclose (out);
00111 }
00112 
00113 DataFormat* DataFormatLib::create (const String& filename) {
00114     // Parse the contents according to the file name extension
00115     if (filename.length()>4 && filename.right(4)==".pat")
00116         return new SNNSDataFormat ();
00117     else if (filename.length()>3 && filename.right(3)==".dt")
00118         return new Proben1DataFormat ();
00119     else
00120         return new RawDataFormat ();
00121 }
00122 
00123 

Generated on Thu Feb 10 20:06:44 2005 for Inanna by doxygen1.2.18