1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* hpiutil.cpp
* HPIUtil main implementation
* Copyright (C) 2005 Christopher Han <xiphux@gmail.com>
*
* This file is part of hpiutil2.
*
* hpiutil2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* hpiutil2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hpiutil2; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hpiutil.h"
#include <string.h>
#ifdef _MSC_VER
#define STRCASECMP stricmp
#else
#define STRCASECMP strcasecmp
#endif
/*
* HPIOpen
* Opens a given hpi file and returns a pointer to its hpifile object
* filename - path to hpi file
*/
hpiutil::hpifile* hpiutil::HPIOpen(const char *filename)
{
hpifile *hpi = new hpifile(filename);
if (hpi->valid)
return hpi;
else {
delete hpi;
return NULL;
}
}
/*
* HPIClose
* Closes an hpi file
* hpi - hpifile object to close
*/
void hpiutil::HPIClose(hpifile &hpi)
{
delete &hpi;
}
/*
* HPIReadFlatList
* searches an hpi file's flatlist for a given entry, should not be necessary to call manually
* returns pointer to hpientry of this object if found
* hpi - hpi file to search
* name - path of file to search for
* dir - whether to search directories or files
*/
hpiutil::hpientry_ptr hpiutil::HPIReadFlatList(hpifile const &hpi, const char *name, const bool dir)
{
std::vector<hpientry_ptr> const& tmp = hpi.flatlist;
int len = strlen(name);
/*
* Make an alternate name in case of platform differences
*/
char *altname = (char*)calloc(len+1,sizeof(char));
for (int i = 0; i < len; i++) {
if (name[i] == PATHSEPARATOR)
altname[i] = OTHERPATHSEPARATOR;
else
altname[i] = name[i];
}
altname[len] = '\0';
for (std::vector<hpientry_ptr>::const_iterator it = tmp.begin(); it != tmp.end(); it++) {
if ((!STRCASECMP((*it)->path().c_str(),name) || !STRCASECMP((*it)->path().c_str(),altname)) && ((*it)->directory == dir)) {
free(altname);
return *it;
}
}
free(altname);
return hpientry_ptr();
}
/*
* HPIOpenFile
* searches for a file inside an hpi
* returns a pointer to the hpientry if found
* hpi - hpi file to search
* filename - name of file to find
*/
hpiutil::hpientry_ptr hpiutil::HPIOpenFile(hpifile const &hpi, const char *filename)
{
return HPIReadFlatList(hpi,filename,false);
}
/*
* HPICloseFile
* closes an hpientry
* he - hpientry to close
*/
void hpiutil::HPICloseFile(hpientry_ptr &he)
{
// no need since shared_ptr removes the object!
/*
* Should really only be deleted if you never intend
* to read the file again
*/
// delete &he;
}
/*
* HPIGet
* loads a file's data into a buffer
* returns the number of bytes read
* dest - buffer to load into
* he - hpientry representing file to load
* offset - offset in hpi file (unused, for compatibility only)
* bytecount - length of file to load (unused, for compatibility only)
*/
boost::uint32_t hpiutil::HPIGet(char *dest, hpientry_ptr const &he, const int offset, const int bytecount)
{
return he->file->getdata(he,(boost::uint8_t*)dest);
}
/*
* HPIGetFiles
* returns a listing of all the files in an hpi
* as a vector of hpientry pointers
* hpi - hpi file to list
*/
std::vector<hpiutil::hpientry_ptr> hpiutil::HPIGetFiles(hpifile const &hpi)
{
return hpi.flatlist;
}
/*
* HPIDir
* gets the contents of a directory in an hpi file
* returns a vector of hpientry pointers
* hpi - hpi file to use
* dirname - name of directory to list
*/
std::vector<hpiutil::hpientry_ptr> hpiutil::HPIDir(hpifile const &hpi,const char *dirname)
{
hpientry_ptr dir = HPIReadFlatList(hpi,dirname,true);
if (dir)
return dir->subdir;
else
return std::vector<hpientry_ptr>();
}
|