00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022
00023 #ifndef ALPHA_LINUX_CXX
00024 # include <cstdio>
00025 #endif
00026 #include <iostream>
00027
00028 #include "U7file.h"
00029 #include "Flex.h"
00030 #include "IFF.h"
00031 #include "Table.h"
00032 #include "Flat.h"
00033 #include "exceptions.h"
00034 #include "utils.h"
00035
00036 using std::cerr;
00037 using std::endl;
00038 using std::size_t;
00039 using std::FILE;
00040 using std::string;
00041
00042
00043 #define TRY_FILE_TYPE(uf,CLASS_NAME) \
00044 if(!uf) \
00045 try { \
00046 uf=new CLASS_NAME(s); \
00047 } catch(const wrong_file_type_exception &) \
00048 { \
00049 }
00050
00051 U7file *U7FileManager::get_file_object(const string &s)
00052 {
00053 U7file *uf=0;
00054 if(file_list.count(s))
00055 {
00056 return file_list[s];
00057 }
00058
00059
00060 TRY_FILE_TYPE(uf,IFF);
00061 #ifndef PENTAGRAM
00062 TRY_FILE_TYPE(uf,Flex);
00063 #endif
00064 TRY_FILE_TYPE(uf,Table);
00065 TRY_FILE_TYPE(uf,Flat);
00066
00067
00068 if (!uf) {
00069 throw (file_open_exception((const string &) s));
00070 return 0;
00071 }
00072
00073 file_list[s]=uf;
00074 return uf;
00075 }
00076
00077 U7FileManager *U7FileManager::get_ptr(void)
00078 {
00079 if(!self)
00080 new U7FileManager();
00081
00082
00083 return self;
00084 }
00085
00086 void U7FileManager::reset()
00087 {
00088 std::map<std::string,U7file *>::iterator i;
00089
00090 for (i = file_list.begin(); i != file_list.end(); ++i)
00091 delete (*i).second;
00092
00093 file_list.clear();
00094 }
00095
00096
00097 U7FileManager::~U7FileManager() {}
00098
00099 U7FileManager *U7FileManager::self=0;
00100
00101 U7FileManager::U7FileManager()
00102 {
00103 if(self) {
00104 throw exclusive();
00105 std::exit(-1);
00106 }
00107 else
00108 self=this;
00109 }
00110
00111 uint32 U7object::number_of_objects(void)
00112 {
00113 U7file *uf=U7FileManager::get_ptr()->get_file_object(filename);
00114 if (!uf) return 0;
00115 return uf->number_of_objects();
00116 }
00117
00118 char* U7object::retrieve(size_t &len)
00119 {
00120 U7file *uf=U7FileManager::get_ptr()->get_file_object(filename);
00121 if (!uf) return 0;
00122 return uf->retrieve(objnumber,len);
00123 }
00124
00125 bool U7object::retrieve(const char *fname)
00126 {
00127 FILE *fp=U7open(fname,"wb");
00128
00129 char *n;
00130 size_t l;
00131
00132 try
00133 {
00134 n = retrieve(l);
00135 }
00136 catch( const std::exception & err )
00137 {
00138 std::fclose(fp);
00139 throw (err);
00140 }
00141 if (!n) {
00142 std::fclose(fp);
00143 return false;
00144 }
00145 std::fwrite(n,l,1,fp);
00146 std::fclose(fp);
00147 delete [] n;
00148 return true;
00149 }