U7file.cc

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2000  Dancer A.L Vesperman
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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   // Not in our cache. Attempt to figure it out.
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   // Failed
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();  // self gets the pointer, so it's okay
00081           // This might look like it creates a
00082           // leak, but this is a singleton object
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);  // &&&& Should check return value
00146   std::fclose(fp);
00147   delete [] n;
00148   return true;
00149 }

Generated on Mon Jul 9 14:42:45 2007 for ExultEngine by  doxygen 1.5.1