00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pent_include.h"
00020 #include "FlexWriter.h"
00021 #include "FlexFile.h"
00022 #include "ODataSource.h"
00023 #include "IDataSource.h"
00024 #include "FileSystem.h"
00025
00026 void usage();
00027
00028 enum Arch_mode { NONE, LIST, EXTRACT, CREATE, ADD, REPLACE, RESPONSE };
00029
00030 void set_mode(Arch_mode &mode, Arch_mode new_mode)
00031 {
00032 if(mode!=NONE) {
00033 perr << "Error: cannot specify multiple modes" << std::endl;
00034 exit(1);
00035 } else
00036 mode = new_mode;
00037 }
00038
00039 static char * program;
00040
00041 int main(int argc, char **argv)
00042 {
00043 Arch_mode mode = NONE;
00044 std::string flexfile = c_empty_string;
00045 std::string file_name;
00046 std::string option;
00047 std::vector<std::string> file_names;
00048 IDataSource *ids = 0;
00049 ODataSource *ods = 0;
00050 FlexWriter * fw;
00051 char * ext = "fxo";
00052 int i = 0;
00053 uint32 index = 0;
00054 program = argv[0];
00055
00056 if (argc < 3) {
00057 usage();
00058 return 1;
00059 }
00060
00061 con.DisableWordWrap();
00062 option = argv[1];
00063
00064 FileSystem filesys(true);
00065 if((argv[1][0]=='-')&&(strlen(argv[1])==2))
00066 {
00067 flexfile = argv[2];
00068
00069 switch(argv[1][1]) {
00070 case 'a':
00071 {
00072 set_mode(mode, ADD);
00073 for (i = 3; i < argc; ++i)
00074 {
00075 file_names.push_back(argv[i]);
00076 }
00077 }
00078 break;
00079 case 'c':
00080 {
00081 set_mode(mode, CREATE);
00082 for (i = 3; i < argc; ++i)
00083 {
00084 file_names.push_back(argv[i]);
00085 }
00086 }
00087 break;
00088 case 'i':
00089 {
00090 set_mode(mode, RESPONSE);
00091 ids = filesys.ReadFile(flexfile);
00092 if (ids)
00093 {
00094 ids->readline(flexfile);
00095 while(! ids->eof())
00096 {
00097 ids->readline(file_name);
00098 file_names.push_back(argv[i]);
00099 }
00100 FORGET_OBJECT(ids);
00101 }
00102 else
00103 {
00104 perr << "Manifest not found." << std::endl;
00105 return 1;
00106 }
00107 }
00108 break;
00109 case 'l':
00110 {
00111 set_mode(mode, LIST);
00112 }
00113 break;
00114 case 'x':
00115 {
00116 set_mode(mode, EXTRACT);
00117 }
00118 break;
00119 case 'r':
00120 {
00121 set_mode(mode, REPLACE);
00122 if (argc < 5)
00123 {
00124 perr << "Missing parameters!" << std::endl << std::endl;
00125 usage();
00126 return 1;
00127 }
00128 index = strtoul(argv[3],0,10);
00129 file_name = argv[4];
00130 }
00131 break;
00132 default:
00133 mode = NONE;
00134 };
00135 }
00136
00137 switch (mode)
00138 {
00139 case EXTRACT:
00140 {
00141 ids = filesys.ReadFile(flexfile);
00142 if (!ids)
00143 {
00144 perr << "Flex not found" << std::endl;
00145 return 1;
00146 }
00147 else if (! FlexFile::isFlexFile(ids))
00148 {
00149 perr << "File is not a Flex" << std::endl;
00150 delete ids;
00151 return 1;
00152 }
00153 FlexFile * flex = new FlexFile(ids);
00154 for(index=0; index < flex->getIndexCount(); index++)
00155 {
00156 uint32 size;
00157 uint8* data = flex->getObject(index, &size);
00158 if (!data || size == 0) continue;
00159 char outfile[32];
00160 snprintf(outfile,32,"%04X.%s", index, ext);
00161 ods = filesys.WriteFile(outfile);
00162 if (!ods)
00163 {
00164 perr << "Could not create file" << std::endl;
00165 delete flex;
00166 return 1;
00167 }
00168 ods->write(data, size);
00169 delete[] data;
00170 FORGET_OBJECT(ods);
00171 }
00172 delete flex;
00173 }
00174 break;
00175 case LIST:
00176 {
00177 ids = filesys.ReadFile(flexfile);
00178 if (!ids)
00179 {
00180 perr << "Flex not found" << std::endl;
00181 return 1;
00182 }
00183 else if (! FlexFile::isFlexFile(ids))
00184 {
00185 perr << "File is not a Flex" << std::endl;
00186 delete ids;
00187 return 1;
00188 }
00189 FlexFile * flex = new FlexFile(ids);
00190 pout << "Archive: " << flexfile << std::endl;
00191 pout << "Size: " << flex->getIndexCount() << std::endl;
00192 pout << "-------------------------" << std::endl;
00193 pout << "Object\tLength" << std::endl;
00194 for(index=0; index < flex->getIndexCount(); index++) {
00195 uint32 size = flex->getSize(index);
00196 if (size > 0)
00197 pout << index << "\t" << flex->getSize(index) << std::endl;
00198 }
00199 delete flex;
00200 }
00201 break;
00202 case REPLACE:
00203 {
00204 ids = filesys.ReadFile(flexfile);
00205 if (!ids)
00206 {
00207 perr << "Flex not found" << std::endl;
00208 return 1;
00209 }
00210 else if (! FlexFile::isFlexFile(ids))
00211 {
00212 perr << "File is not a Flex" << std::endl;
00213 delete ids;
00214 return 1;
00215 }
00216
00217 FlexFile * flex = new FlexFile(ids);
00218 fw = new FlexWriter(flex);
00219 ids = filesys.ReadFile(file_name);
00220 if (!ids)
00221 {
00222 perr << "File Not Found: " << file_name << std::endl;
00223 delete fw;
00224 return 1;
00225 }
00226
00227 fw->set_datasource(index, ids);
00228
00229 ods = filesys.WriteFile(flexfile);
00230 if (!ods)
00231 {
00232 perr << "Could not create flex" << std::endl;
00233 delete fw;
00234 return 1;
00235 }
00236 fw->write(ods);
00237 delete fw;
00238 }
00239 break;
00240 case ADD:
00241 {
00242 ids = filesys.ReadFile(flexfile);
00243 if (!ids)
00244 {
00245 perr << "Flex not found" << std::endl;
00246 return 1;
00247 }
00248 else if (! FlexFile::isFlexFile(ids))
00249 {
00250 perr << "File is not a Flex" << std::endl;
00251 delete ids;
00252 return 1;
00253 }
00254
00255 FlexFile * flex = new FlexFile(ids);
00256 fw = new FlexWriter(flex);
00257 std::vector<std::string>::iterator it;
00258 for (it = file_names.begin(); it != file_names.end(); ++it)
00259 {
00260 ids = filesys.ReadFile(*it);
00261 if (!ids)
00262 {
00263 perr << "File Not Found: " << *it << std::endl;
00264 delete fw;
00265 return 1;
00266 }
00267 fw->add_datasource(ids);
00268 }
00269
00270 ods = filesys.WriteFile(flexfile);
00271 if (!ods)
00272 {
00273 perr << "Could not create flex" << std::endl;
00274 delete fw;
00275 return 1;
00276 }
00277 fw->write(ods);
00278 delete fw;
00279 }
00280 break;
00281 case RESPONSE:
00282 case CREATE:
00283 {
00284 fw = new FlexWriter();
00285 std::vector<std::string>::iterator it;
00286 for (it = file_names.begin(); it != file_names.end(); ++it)
00287 {
00288 ids = filesys.ReadFile(*it);
00289 if (!ids)
00290 {
00291 perr << "File Not Found: " << *it << std::endl;
00292 delete fw;
00293 return 1;
00294 }
00295 fw->add_datasource(ids);
00296 }
00297 ods = filesys.WriteFile(flexfile);
00298 if (!ods)
00299 {
00300 perr << "Could not create flex" << std::endl;
00301 delete fw;
00302 return 1;
00303 }
00304 fw->write(ods);
00305 delete fw;
00306 }
00307 break;
00308 case NONE:
00309 default:
00310 {
00311 perr << "Invalid switch!" << std::endl << std::endl;
00312 usage();
00313 return 1;
00314 }
00315 };
00316 return 0;
00317 }
00318
00319 void usage()
00320 {
00321 perr << "Usage: " << program << " [-a|-c] <flexfile> <file>..." << std::endl;
00322 perr << "or" << std::endl;
00323 perr << "Usage: " << program << " [-x|-l] <flexfile>" << std::endl;
00324 perr << "or" << std::endl;
00325 perr << "Usage: " << program << " -r <flexfile> <object number> <file>" << std::endl;
00326 perr << "or" << std::endl;
00327 perr << "Usage: " << program << " -i MANIFEST" << std::endl;
00328 }