Public Member Functions | |
| mximg_Writer () | |
| bool | addItem (const char *item) |
| bool | addItem (const mximg_Item &item) |
| bool | writeItems (const char *filen) |
| bool | concatItems (const char *filen) |
Protected Member Functions | |
| bool | writeData (std::fstream &f) |
Protected Attributes | |
| std::vector< mximg_Item > | items_ |
Definition at line 52 of file mximg.h.
| mx::mximg_Writer::mximg_Writer | ( | ) |
| bool mx::mximg_Writer::addItem | ( | const mximg_Item & | item | ) |
addItem
| item | item of type mximg_Item to be added |
Definition at line 76 of file mximg.cpp.
References mx::mximg_Item::data_length, items_, and mx::mximg_Item::name_length.
00076 { 00077 00078 assert(item.data_length != 0); 00079 assert(item.name_length != 0); 00080 00081 items_.push_back(item); 00082 return true; 00083 }
| bool mx::mximg_Writer::addItem | ( | const char * | item | ) |
addItem, adds a file to the list of files to be compressed
| item | filename of item to be padded |
Definition at line 43 of file mximg.cpp.
References mx::mximg_Item::data_length, mx::mximg_Item::fullname, mx::mximg_Item::name, and mx::mximg_Item::name_length.
00043 { 00044 00045 std::fstream f; 00046 f.open(item, std::ios::in | std::ios::binary); 00047 if(!f.is_open()) 00048 return false; 00049 f.seekg(0, std::ios::end); 00050 size_t len = f.tellg(); 00051 f.close(); 00052 00053 if(len == 0) { 00054 std::cout << item << " is zero bytes, skipping..\n"; 00055 return true; 00056 } 00057 00058 00059 mximg_Item it; 00060 it.data_length = len; 00061 00062 it.fullname = item; 00063 00064 std::string ex = item; 00065 if(ex.rfind("/")) { 00066 ex = ex.substr(ex.rfind("/")+1, ex.length()); 00067 std::cout << ex << "\n"; 00068 } 00069 00070 it.name_length = ex.length()+1; 00071 snprintf(it.name, it.name_length, "%s", ex.c_str()); 00072 00073 return addItem(it); 00074 }
| bool mx::mximg_Writer::concatItems | ( | const char * | filen | ) |
concatItems append the items to a existing mximg archive
| filen | name of archive to append to |
Definition at line 137 of file mximg.cpp.
References writeData().
00137 { 00138 00139 std::fstream file; 00140 00141 file.open(filen, std::ios::binary | std::ios::app | std::ios::out); 00142 if(!file.is_open()) return false; 00143 00144 return writeData(file); 00145 }
| bool mx::mximg_Writer::writeData | ( | std::fstream & | f | ) | [protected] |
protected function, write current items in items_ to fstream object f
| f | fstream object |
Definition at line 85 of file mximg.cpp.
References items_.
Referenced by concatItems(), and writeItems().
00085 { 00086 00087 std::vector<mximg_Item>::iterator i, last; 00088 00089 for(i = items_.begin(), last = items_.end(); i != last; i++) { 00090 00091 if(i->name_length <= 0) continue; 00092 if(i->data_length <= 0) continue; 00093 00094 00095 file.write((char*)&i->name_length, sizeof(unsigned int)); 00096 file.write((char*)i->name, i->name_length); 00097 00098 unsigned char *temp_bytes = new unsigned char [ i->data_length + 1 ]; 00099 std::fstream temp_file; 00100 temp_file.open(i->fullname.c_str(), std::ios::in | std::ios::binary); 00101 if(!temp_file.is_open()) { 00102 std::cerr << " could not open input file: " << i->name << "\n"; 00103 file.close(); 00104 return false; 00105 } 00106 temp_file.read((char*)temp_bytes, i->data_length); 00107 temp_file.close(); 00108 i->compressed_length = i->data_length; 00109 00110 int err; 00111 00112 i->compressed_length = compressBound(i->data_length); 00113 unsigned char *comp_bytes = new unsigned char [ i->compressed_length + 1 ]; 00114 00115 if((err = compress((Bytef*)comp_bytes, (uLongf*)&i->compressed_length, (const Bytef*)temp_bytes, (uLong) i->data_length)) != Z_OK) { 00116 file.close(); 00117 delete [] temp_bytes; 00118 delete [] comp_bytes; 00119 std::cerr << "error on compression of " << i->name << " " << zError(err) << " ...\n"; 00120 return false; 00121 } 00122 00123 file.write((char*)&i->data_length, sizeof(unsigned int)); 00124 file.write((char*)&i->compressed_length, sizeof(unsigned int)); 00125 file.write((char*)comp_bytes, i->compressed_length); 00126 00127 delete [] temp_bytes; 00128 delete [] comp_bytes; 00129 } 00130 00131 file.close(); 00132 00133 return true; 00134 }
| bool mx::mximg_Writer::writeItems | ( | const char * | filen | ) |
writeItems write the items stored in the mximg_Items vector to file compressed
| filen | filename of compressed file |
Definition at line 147 of file mximg.cpp.
References writeData().
00148 { 00149 std::fstream file; 00150 file.open(filen, std::ios::out | std::ios::binary); 00151 if(!file.is_open()) 00152 return false; 00153 00154 return writeData(file); 00155 }
std::vector<mximg_Item> mx::mximg_Writer::items_ [protected] |
vector of mximg_Item 's
Definition at line 82 of file mximg.h.
Referenced by addItem(), and writeData().
1.5.8