segfile.cc

Go to the documentation of this file.
00001 /*
00002  *  segfile.cc - Handle access to to a data file consisting of segments.
00003  *
00004  *  Copyright (C) 2000-2001  The Exult Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #  include <config.h>
00024 #endif
00025 
00026 #include "exceptions.h"
00027 #include "segfile.h"
00028 #include "utils.h"
00029 
00030 /*
00031  *  Open file.
00032  */
00033 
00034 Segment_file::Segment_file
00035   (
00036   std::string nm      // Path to file.
00037   ) : filename(nm), num_segments(0)
00038 {
00039   U7open(file, filename.c_str());
00040   file.seekg(0x54);   // Get # of segments.
00041   num_segments = Read4(file);
00042   if( !file.good() )
00043     throw file_read_exception(filename);
00044 }
00045 
00046 /*
00047  *  Read in a given segment.
00048  *
00049  *  returns the data in a new allocated buffer so it should be freed by the caller.
00050  */
00051 
00052 char* Segment_file::retrieve
00053   (
00054   uint32 index,     // Number desired.
00055   std::size_t& len
00056   )
00057 {
00058   char *buffer;
00059   if (index >= num_segments)
00060     throw exult_exception("objnum too large in read_object()");
00061 
00062   file.seekg(0x80 + 8*index); // Get to info.
00063   long offset = Read4(file);  // Get offset, length.
00064   len = Read4(file);
00065   if (!len)
00066     throw file_read_exception(filename);
00067 
00068   file.seekg(offset);   // Get to data.
00069   buffer = new char[len];   // Allocate buffer.
00070   file.read(buffer, len);   // Read it.
00071   if( !file.good() )
00072     throw file_read_exception(filename);
00073 
00074   return buffer;
00075 }
00076 

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