mx::mximg_Reader Class Reference

#include <mximg.h>

List of all members.

Public Member Functions

 mximg_Reader ()
bool openArchive (const char *filen)
void * accessBytes (const char *fname, size_t &length)
void listItems (std::ostream &stream)
void extractItem (const char *path, const char *fname)
void extractAll (const char *path)
void closeArchive ()

Protected Attributes

std::vector< mximg_Itemitems_
std::string file_name


Detailed Description

class to encapsulate reading of mximg files and load contents of files into memory uncompressed

Definition at line 95 of file mximg.h.


Constructor & Destructor Documentation

mx::mximg_Reader::mximg_Reader (  ) 

default constructor, builds object

Definition at line 157 of file mximg.cpp.

00157 {}


Member Function Documentation

void * mx::mximg_Reader::accessBytes ( const char *  fname,
size_t &  length 
)

accessBytes access the raw uncompressed bytes of file stored in archive

Parameters:
fname filename of data to be loaded and uncompressed and returned
length reference to a unsigned integer to hold the length of the data
Returns:
void * containing the location in memory where the uncompressed bytes are stored.

Definition at line 199 of file mximg.cpp.

References file_name, and items_.

Referenced by extractItem().

00199                                                                          {
00200 
00201                 std::vector<mximg_Item>::iterator i,last;
00202 
00203                 for(i = items_.begin(), last = items_.end(); i != last; i++) {
00204 
00205                         if(strcmp(i->name, fname) == 0) {
00206 
00207 
00208                                 std::fstream f;
00209                                 f.open(file_name.c_str(), std::ios::in | std::ios::binary);
00210                                 if(!f.is_open()) return 0;
00211 
00212                                 length = i->data_length;
00213                                 unsigned char *compressed_bytes = new unsigned char [ i->compressed_length + 1 ];
00214                                 unsigned char *uncompressed = new unsigned char [ i->data_length + 1 ];
00215 
00216                                 f.seekg(i->offset, std::ios::beg);
00217                                 f.read((char*)compressed_bytes, i->compressed_length);
00218                                 f.close();
00219                                 if( uncompress((Bytef*)uncompressed, (uLongf*)&i->data_length,(const Bytef*)compressed_bytes, (uLongf)i->compressed_length) != Z_OK ) {
00220 
00221                                         std::cerr << "error on decompression\n";
00222                                         delete [] uncompressed;
00223                                         delete [] compressed_bytes;
00224                                         return 0;
00225                                 }
00226 
00227                                 delete [] compressed_bytes;
00228                                 return uncompressed;
00229                         }
00230 
00231                 }
00232                 return 0;
00233         }

void mx::mximg_Reader::closeArchive (  ) 

closeArchive will close the currently open archive

Definition at line 265 of file mximg.cpp.

References items_.

00265                                         {
00266                 if(!items_.empty())
00267                 items_.clear();
00268         }

void mx::mximg_Reader::extractAll ( const char *  path  ) 

extractAll will extract All of the archive to path

Parameters:
path path to extract to

Definition at line 257 of file mximg.cpp.

References extractItem(), and items_.

00257                                                       {
00258 
00259                 std::vector<mximg_Item>::iterator i, last;
00260                 for(i = items_.begin(), last = items_.end(); i != last; i++)
00261                                 extractItem(path, i->name);
00262 
00263         }

void mx::mximg_Reader::extractItem ( const char *  path,
const char *  fname 
)

extractItem will extract a item from the archive to a file on file system

Parameters:
path filename in archive,
fname filename on disk

Definition at line 235 of file mximg.cpp.

References accessBytes().

Referenced by extractAll().

00235                                                                           {
00236 
00237                 std::string p = path;
00238                 if(p[p.length()-1] == '/') p += fname; else p = p + '/' + fname;
00239 
00240                 std::fstream file;
00241                 file.open(p.c_str(), std::ios::out | std::ios::binary);
00242                 if(!file.is_open()) {
00243                         std::cerr << "error path is incorrect, supply correct path for extraction.";
00244                         return;
00245                 }
00246 
00247                 size_t length = 0;
00248                 char *bytes = (char*) accessBytes(fname, length);
00249                 if(bytes == 0) std::cerr << "invalid identifier for extraction.";
00250 
00251                 file.write((char*)bytes, length);
00252                 file.close();
00253                 delete [] bytes;
00254 
00255         }

void mx::mximg_Reader::listItems ( std::ostream &  stream  ) 

listItems list index of file's compressed in archive

Parameters:
stream ostream object for output to be copied to.

Definition at line 186 of file mximg.cpp.

References items_.

00186                                                        {
00187 
00188                 std::vector<mximg_Item>::iterator i, last;
00189 
00190                 using namespace std;
00191 
00192                 stream << setw(25) << "filename" << setw(15) << "compressed" << setw(15) << "uncompressed" << "\n";
00193 
00194                 for(i = items_.begin(), last = items_.end(); i != last; i++) {
00195                         stream << setw(25) <<  i->name << setw(15) << i->compressed_length << setw(15) << i->data_length << "\n";
00196                 }
00197         }

bool mx::mximg_Reader::openArchive ( const char *  filen  ) 

opens the archive

Parameters:
filen filename of archive
Returns:
boolean value for success

Definition at line 160 of file mximg.cpp.

References mx::mximg_Item::compressed_length, mx::mximg_Item::data_length, file_name, items_, mx::mximg_Item::name, mx::mximg_Item::name_length, and mx::mximg_Item::offset.

00160                                                         {
00161 
00162                 std::fstream f;
00163 
00164                 f.open(filen, std::ios::in | std::ios::binary);
00165 
00166                 if(!f.is_open()) return false;
00167 
00168                 while(!f.eof() ) {
00169 
00170                         mximg_Item item;
00171                         f.read((char*)&item.name_length, sizeof(unsigned int));
00172                         f.read((char*)&item.name, item.name_length);
00173                         f.read((char*)&item.data_length, sizeof(unsigned int));
00174                         f.read((char*)&item.compressed_length, sizeof(unsigned int));
00175                         item.offset = f.tellg();
00176                         f.seekg(item.compressed_length, std::ios::cur);
00177 
00178                         if(item.compressed_length != 0)
00179                         items_.push_back(item);
00180                 }
00181                 f.close();
00182                 file_name = filen;
00183                 return true;
00184         }


Member Data Documentation

std::string mx::mximg_Reader::file_name [protected]

filename of currently open archive

Definition at line 141 of file mximg.h.

Referenced by accessBytes(), and openArchive().

std::vector<mximg_Item> mx::mximg_Reader::items_ [protected]

items vector of mximg_Item structures for items in archive

Definition at line 139 of file mximg.h.

Referenced by accessBytes(), closeArchive(), extractAll(), listItems(), and openArchive().


The documentation for this class was generated from the following files:

Generated on Wed Jun 10 14:52:02 2009 for libmx by  doxygen 1.5.8