Octane v1.01.20 - The Open Compression Toolkit for C++ | http://octane.sourceforge.net/ |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 //--------------------------------------------------------------------------- 00012 00013 //--------------------------------------------------------------------------- 00014 // application includes 00015 #include "compressormanager.hpp" 00016 // system includes 00017 #include <iostream> 00018 #include <iomanip> 00019 using namespace std; 00020 //--------------------------------------------------------------------------- 00021 00022 00023 00024 //--------------------------------------------------------------------------- 00025 // global single instance of the CompressorManager. 00026 // this is the object that all compressors will register with. 00027 // because this is a global, it will be initialized to NULL on startup 00028 // according to ansi c. 00029 CompressorManager* global_compressormanagerp; 00030 //--------------------------------------------------------------------------- 00031 00032 00033 00034 //--------------------------------------------------------------------------- 00035 CompressorManager::CompressorManager() 00036 { 00037 // constructor 00038 Initialize(); 00039 } 00040 00041 CompressorManager::~CompressorManager() 00042 { 00043 // destructor 00044 // free registered compressors? 00045 DeInitialize(); 00046 } 00047 00048 void CompressorManager::Initialize() 00049 { 00050 // This is called by code on startup, after all compressors have 00051 // registered with us. now we select a default compressor for testing. 00052 } 00053 00054 void CompressorManager::DeInitialize() 00055 { 00056 // Called on shutdown 00057 // We don't have to free the globally auto-registered base types, BUT we do need to free the dynamically allocated instantiated types 00058 vector<OctaneCompressor*>::iterator index; 00059 for (index=compressorvector_instantiated.begin();index!=compressorvector_instantiated.end();++index) 00060 delete (*index); 00061 compressorvector_instantiated.clear(); 00062 } 00063 //--------------------------------------------------------------------------- 00064 00065 00066 00067 //--------------------------------------------------------------------------- 00068 void CompressorManager::RegisterBaseCompressor(OctaneCompressor* compressorp) 00069 { 00070 // register a new compressor 00071 // show some debug info? 00072 // cout << "$DEBUG Registering compressor '" << compressorp->get_compressor_name() << "'."<<endl; 00073 compressorvector_basetypes.push_back(compressorp); 00074 // should we set this new compressor to be our current compressor? 00075 // if (currentcompressorp==NULL || true) 00076 // currentcompressorp=compressorp; 00077 } 00078 00079 void CompressorManager::UnRegisterBaseCompressor(OctaneCompressor* compressorp) 00080 { 00081 // remove a compressor from our list of registered components. 00082 // you don't have to call this on exit of the program, only if you are for some reason 00083 // removing compressors during the execution of the program. 00084 vector<OctaneCompressor*>::iterator index; 00085 for (index=compressorvector_basetypes.begin();index!=compressorvector_basetypes.end();++index) 00086 { 00087 if (*index==compressorp) 00088 { 00089 // found it, so remove it 00090 compressorvector_basetypes.erase(index); 00091 break; 00092 } 00093 } 00094 } 00095 00096 OctaneCompressor* CompressorManager::FindBaseCompressorClass(const std::string &compressorname) 00097 { 00098 // remove a compressor from our list of registered components. 00099 // you don't have to call this on exit of the program, only if you are for some reason 00100 // removing compressors during the execution of the program. 00101 vector<OctaneCompressor*>::iterator index; 00102 for (index=compressorvector_basetypes.begin();index!=compressorvector_basetypes.end();++index) 00103 { 00104 if ((*index)->GetClassName()==compressorname) 00105 return (*index); 00106 } 00107 return NULL; 00108 } 00109 //--------------------------------------------------------------------------- 00110 00111 00112 00113 //--------------------------------------------------------------------------- 00114 // Create a new compressor based on file info and load state 00115 OctaneCompressor* CompressorManager::CreateCompressorFromSavedStream(bitreader &from) 00116 { 00117 // in the saved stream we expect to find a 'header' which tells us the 1)compressorclass, 2)compressorname, and 3)compressorguid 00118 // then we create a compressor of that type, and tell it to read its state info from the remainder of stream 00119 00120 // read standard compressor header (see OctaneCompressor::Save() for where this is written) 00121 string compressorclassname; 00122 string compressorname; 00123 short int compressorguid; 00124 from.get(compressorclassname); 00125 from.get(compressorname); 00126 from.get(compressorguid); 00127 00128 // select base compressor class type 00129 OctaneCompressor *compressorp = FindBaseCompressorClass(compressorclassname); 00130 if (compressorp==NULL) 00131 { 00132 // couldn't find base class 00133 cout << "ERROR: compressor in file is based on compressor class '"<<compressorclassname<<"' which is not known."<<endl; 00134 return NULL; 00135 } 00136 00137 // ask base class to make us a derived class 00138 compressorp=compressorp->MakeCompressorInstantiation(); 00139 if (compressorp==NULL) 00140 { 00141 // couldn't find base class 00142 cout << "ERROR: failed to instantiate a derived compressor from base class '"<<compressorclassname<<"'."<<endl; 00143 return NULL; 00144 } 00145 00146 // now set details (compressor name and guid) from file 00147 compressorp->SetInfo(compressorname,compressorguid); 00148 00149 // now load the details from the file 00150 bool bretv=compressorp->LoadState(from); 00151 if (!bretv) 00152 { 00153 delete compressorp; 00154 compressorp=NULL; 00155 } 00156 00157 // return pointer to newly created compressor 00158 return compressorp; 00159 } 00160 00161 00162 // Add an instantiated compressor to our instantiated list 00163 bool CompressorManager::AddCompressor (OctaneCompressor *compressorp) 00164 { 00165 // just add a compressor to the instantiated vector 00166 // show some debug info? 00167 // cout << "$DEBUG Adding instantiated compressor '" << compressorp->get_compressor_name() << "'."<<endl; 00168 compressorvector_instantiated.push_back(compressorp); 00169 return true; 00170 } 00171 00172 00173 // Delete an instantiated compressor to our instantiated list 00174 bool CompressorManager::DeleteCompressor (OctaneCompressor *compressorp) 00175 { 00176 // just add a compressor to the instantiated vector 00177 vector<OctaneCompressor*>::iterator index; 00178 for (index=compressorvector_instantiated.begin();index!=compressorvector_instantiated.end();++index) 00179 { 00180 if (*index==compressorp) 00181 { 00182 // found it, so remove it 00183 compressorvector_instantiated.erase(index); 00184 return true; 00185 } 00186 } 00187 return false; 00188 } 00189 00190 00191 // Find the pointer to the instantiated compressor referred to by idstring 00192 // idstring can be [$name|$num|G$guid] 00193 OctaneCompressor* CompressorManager::FindInstantiatedCompressorpFromStringId(string idstring) 00194 { 00195 // look up a compressor based on different strings 00196 if (idstring=="") 00197 return NULL; 00198 00199 // there are 3 ways to specify an instantiated compressor 00200 OctaneCompressor* compressorp=NULL; 00201 int indexnum; 00202 indexnum = atoi(idstring.c_str()); 00203 00204 if (indexnum>0 || idstring=="0") 00205 { 00206 // user gave us an index 00207 if (indexnum<0 || (unsigned int)indexnum>=compressorvector_instantiated.size()) 00208 return NULL; 00209 compressorp=compressorvector_instantiated[indexnum]; 00210 return compressorp; 00211 } 00212 00213 // ok its either a full name or a guid 00214 // first we try to look it up by name 00215 vector<OctaneCompressor*>::iterator index; 00216 for (index=compressorvector_instantiated.begin();index!=compressorvector_instantiated.end();++index) 00217 { 00218 if ((*index)->GetInstantiatedName()==idstring) 00219 return (*index); 00220 } 00221 00222 // we didn't find it, so try to check its guid 00223 if (idstring[0]!='g' && idstring[0]!='G') 00224 return NULL; 00225 idstring=idstring.substr(1,idstring.length()-1); 00226 indexnum=atoi(idstring.c_str()); 00227 if (indexnum>0 || idstring=="0") 00228 { 00229 for (index=compressorvector_instantiated.begin();index!=compressorvector_instantiated.end();++index) 00230 { 00231 if ((*index)->GetGuid()==indexnum) 00232 return (*index); 00233 } 00234 } 00235 return NULL; 00236 } 00237 00238 00239 OctaneCompressor* CompressorManager::FindInstantiatedCompressorpFromGuid(unsigned short int guidindex) 00240 { 00241 // Find the pointer to the instantiated compressor referred to by a guid 00242 // return NULL if not found 00243 vector<OctaneCompressor*>::iterator index; 00244 for (index=compressorvector_instantiated.begin();index!=compressorvector_instantiated.end();++index) 00245 { 00246 if ((*index)->GetGuid()==guidindex) 00247 return (*index); 00248 } 00249 return NULL; 00250 } 00251 //---------------------------------------------------------------------------