Table.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 #include "Table.h"
00024 
00025 #ifndef ALPHA_LINUX_CXX
00026 #  include <cstdio>
00027 #endif
00028 #include <iostream>
00029 #include "exceptions.h"
00030 #include "utils.h"
00031 
00032 using std::string;
00033 using std::vector;
00034 
00035 using std::cout;
00036 using std::cerr;
00037 using std::endl;
00038 using std::FILE;
00039 using std::size_t;
00040 
00041 
00042 Table::Table(const string &n) : U7file(n)
00043 {
00044   IndexTableFile();
00045 }
00046 
00047 
00048 void  Table::IndexTableFile(void)
00049 {
00050   Table &ret=*this;
00051   FILE  *fp;
00052   try {
00053     fp=U7open(ret.filename.c_str(),"rb");
00054   } catch (const file_open_exception &e)
00055   {
00056     cerr << e.what() << ". exiting." << endl;
00057     std::exit(1);
00058   }
00059   fseek(fp,0,SEEK_END);
00060   size_t file_size=ftell(fp);
00061   fseek(fp,0,SEEK_SET);
00062   unsigned int i=0;
00063   while(1)
00064     {
00065     Table::Reference f;
00066     f.size = Read2(fp);
00067 //    fread(&f.size,sizeof(uint16),1,fp);
00068     if(f.size==65535)
00069       break;
00070     f.offset = Read4(fp);
00071 //    fread(&f.offset,sizeof(uint32),1,fp);
00072     if(f.size>file_size||f.offset>file_size)
00073       throw wrong_file_type_exception(filename,"Table");
00074 #if 0
00075     cout << "Item " << i << ": " << f.size << " @ " << f.offset << endl;
00076 #endif
00077     ret.object_list.push_back(f);
00078     i++;
00079     }
00080   fclose(fp);
00081   return;
00082 }
00083 
00084 
00085 char* Table::retrieve(uint32 objnum,size_t &len)
00086 { throw exult_exception("Illegal call to Table::retrieve()"); }
00087 
00088 #if 0
00089 char  *Table::read_object(int objnum,uint32 &length)
00090 {
00091   if((unsigned)objnum>=object_list.size())
00092     {
00093     cerr << "objnum too large in read_object()" << endl;
00094     return 0;
00095     }
00096   FILE *fp;
00097   try {
00098     fp=U7open(filename.c_str(),"rb");
00099   } catch (const file_open_exception &e)
00100   {
00101     cerr << e.what() << ". exiting." << endl;
00102     exit(1);
00103   }
00104   if(!fp)
00105     {
00106     cerr << "File open failed in read_object: " << filename << endl;
00107     return 0;
00108     }
00109   fseek(fp,object_list[objnum].offset,SEEK_SET);
00110   // length=object_list[objnum].size;
00111   uint16  sz;
00112   sz = Read2(fp);
00113 //  fread(&sz,sizeof(sz),1,fp);
00114   length=sz-2;
00115   char  *ret=new char[length];
00116   fread(ret,length,1,fp);
00117   fclose(fp);
00118   return ret;
00119 }
00120 #endif

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