00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "mxclamp.h"
00017 #include<stdio.h>
00018 #include<string.h>
00019 #include<stdlib.h>
00020
00021
00022
00023 namespace mx
00024 {
00025
00026
00027 mxClampOutFile::mxClampOutFile(string filename)
00028 {
00029 fname = filename;
00030 }
00031
00032 mxClampOutFile::~mxClampOutFile()
00033 {
00034
00035 if(file.is_open())
00036 file.close();
00037
00038 }
00039
00040
00041 bool mxClampOutFile::openFile()
00042 {
00043
00044 file.open(fname.c_str(), std::ios::out | std::ios::binary);
00045 if(!file.is_open())
00046 return false;
00047
00048 return true;
00049 }
00050
00051 bool mxClampOutFile::openFile(string fname)
00052 {
00053
00054 this->fname = fname;
00055 return openFile();
00056
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 bool mxClampOutFile::writeToFile()
00068 {
00069
00070 vector<mxClamp_info>::iterator vi;
00071 vector<string>::iterator si;
00072
00073
00074
00075 for(vi = files.begin(), si = file_names.begin(); vi != files.end(); vi++, si++)
00076 {
00077 void *temp = 0, *tempc = 0;
00078
00079 try
00080 {
00081
00082 uLongf size_compressed = vi->size_bytes;
00083 fstream ifile;
00084 temp = (void*) malloc ( sizeof(char) * vi->size_bytes+1 );
00085 tempc = (void*) malloc ( sizeof(char) * vi->size_bytes+1 );
00086 ifile.open(si->c_str(), std::ios::in | std::ios::binary);
00087 ifile.read((char*)temp, vi->size_bytes);
00088 ifile.close();
00089 if(compress((Bytef*)tempc, (uLongf*)&size_compressed, (const Bytef*) temp, (uLong) vi->size_bytes) != Z_OK)
00090 {
00091 throw mx::mxException<string>(" compression error ");
00092 }
00093
00094 cout << *si << " " << vi->size_bytes << "/" << size_compressed << "\n";
00095 vi->size_compressed = size_compressed;
00096 file.write((char*)&*vi, sizeof(mxClamp_info));
00097 file.write((char*)tempc, vi->size_compressed);
00098 free(temp);
00099 free(tempc);
00100 temp = 0;
00101 tempc = 0;
00102
00103 }
00104 catch (...)
00105 {
00106
00107 if(temp != 0)
00108 free(temp);
00109
00110 if(tempc != 0)
00111 free(tempc);
00112
00113 file.close();
00114
00115 throw;
00116 }
00117
00118 }
00119
00120
00121 file.close();
00122
00123 return true;
00124 }
00125
00126
00127
00128
00129
00130 bool mxClampOutFile::addFile(string name)
00131 {
00132
00133 if(name.length()==0) return false;
00134
00135
00136 fstream testfile;
00137 testfile.open(name.c_str(), std::ios::in);
00138 if(!testfile.is_open())
00139 throw mx::mxException<std::string>(" Error file " + name + " not found cannot add to file list ");
00140
00141 testfile.seekg(0, std::ios::end);
00142 mxClamp_info info;
00143
00144 info.size_bytes = testfile.tellg();
00145
00146 testfile.close();
00147
00148 string pless;
00149 int pos = 0;
00150
00151 pos = name.rfind("/");
00152 if(pos != -1)
00153 {
00154
00155 pless = name.substr(pos+1, name.length()-pos);
00156
00157
00158 } else pless = name;
00159 snprintf(info.filename, 255, "%s", pless.c_str());
00160
00161 files.push_back(info);
00162 file_names.push_back(name);
00163 return true;
00164 }
00165
00166
00167
00168
00169 mxClampResourceFile::mxClampResourceFile(string filename)
00170 {
00171
00172
00173 fname = filename;
00174
00175 the_file.open(filename.c_str(), std::ios::in | std::ios::binary);
00176 if(!the_file.is_open())
00177 throw mx::mxException<string>(" error could not open .mxd resource file ");
00178
00179
00180 buildInfo();
00181
00182
00183
00184 }
00185
00186 mxClampResourceFile::~mxClampResourceFile()
00187 {
00188
00189 if(the_file.is_open())
00190 the_file.close();
00191
00192 }
00193
00194 void mxClampResourceFile::reopen()
00195 {
00196
00197 return ;
00198
00199 the_file.close();
00200
00201 the_file.open(fname.c_str(), std::ios::in | std::ios::binary );
00202 if(!the_file.is_open())
00203 throw mx::mxException<string> (" Could not reopen .mxd archive \n");
00204
00205 the_file.seekg(0, std::ios::beg);
00206
00207 }
00208
00209
00210 void mxClampResourceFile::buildInfo()
00211 {
00212
00213 mxClamp_info i;
00214 std::cout << "building information about compressed .mxd archive ...\n";
00215
00216
00217 while(!the_file.eof())
00218 {
00219
00220 int position;
00221
00222 memset(&i, 0, sizeof(i));
00223
00224 the_file.read((char*)&i, sizeof(i));
00225
00226 position = the_file.tellg();
00227
00228 if(position == -1) break;
00229
00230 std::cout << "file in archive: " << i.filename << " " << i.size_compressed << " / " << i.size_bytes << " \n";
00231 ;
00232 file_names.push_back(i);
00233 clamp_off.push_back(position);
00234
00235 the_file.seekg(i.size_compressed, std::ios::cur);
00236
00237 }
00238
00239 the_file.close();
00240
00241 }
00242
00243
00244
00245
00246
00247
00248 void *mxClampResourceFile::operator[](string filename)
00249 {
00250 unsigned int s;
00251 void *bytes = accessRawBytes(filename, s);
00252 if(bytes == 0)
00253 throw mxException<string>(" Invalid file name, does not exisit within archive ");
00254
00255 return bytes;
00256
00257
00258 }
00259
00260
00261 void *mxClampResourceFile::accessRawBytes(string filename, unsigned int &size)
00262 {
00263
00264 vector<mxClamp_info>::iterator i;
00265 vector<unsigned int>::iterator ui;
00266 std::fstream the_file;
00267
00268 the_file.open(fname.c_str(), std::ios::in | std::ios::binary);
00269
00270 if(!the_file.is_open()) { return 0; }
00271
00272 the_file.seekg(0, std::ios::beg);
00273
00274
00275 for(i=file_names.begin(),ui=clamp_off.begin(); i!=file_names.end(); i++, ui++)
00276 {
00277
00278 if(i->filename == filename)
00279 {
00280
00281 uLongf ulen = i->size_bytes;
00282 the_file.seekg(*ui, std::ios::beg);
00283 void *ctemp, *temp;
00284 ctemp = (void*) malloc ( i->size_compressed + 1 );
00285 temp = (void *) malloc ( i->size_bytes + 1 );
00286 the_file.read((char*)ctemp, i->size_compressed );
00287
00288
00289
00290 if(uncompress((Bytef*)temp, &ulen,(const Bytef*)ctemp, (uLongf)i->size_compressed) == Z_DATA_ERROR)
00291 {
00292 the_file.close();
00293
00294 free(ctemp);
00295 free(temp);
00296 the_file.close();
00297 throw mx::mxException<string>(" error on decompression of file " + string(filename));
00298
00299 }
00300 free(ctemp);
00301
00302 std::cout << "Read: " << filename << " [OK]\n";
00303 size = i->size_bytes;
00304 the_file.close();
00305 return temp;
00306 }
00307
00308 }
00309
00310 the_file.close();
00311 return 0;
00312 }
00313
00314
00315 }
00316