SCENEGRAPH* S3D_CACHE::checkCache( const wxString& aFileName, S3D_CACHE_ENTRY** aCachePtr ) { if( aCachePtr ) *aCachePtr = NULL; unsigned char sha1sum[20]; if( !getSHA1( aFileName, sha1sum ) || m_CacheDir.empty() ) { // just in case we can't get a hash digest (for example, on access issues) // or we do not have a configured cache file directory, we create an // entry to prevent further attempts at loading the file S3D_CACHE_ENTRY* ep = new S3D_CACHE_ENTRY; m_CacheList.push_back( ep ); wxFileName fname( aFileName ); ep->modTime = fname.GetModificationTime(); if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* > ( aFileName, ep ) ).second == false ) { #ifdef DEBUG do { std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] duplicate entry in map file; key = '"; ostr << aFileName.ToUTF8() << "'"; wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() ); } while( 0 ); #endif m_CacheList.pop_back(); delete ep; } else { if( aCachePtr ) *aCachePtr = ep; } return NULL; } S3D_CACHE_ENTRY* ep = new S3D_CACHE_ENTRY; m_CacheList.push_back( ep ); wxFileName fname( aFileName ); ep->modTime = fname.GetModificationTime(); if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* > ( aFileName, ep ) ).second == false ) { #ifdef DEBUG do { std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] duplicate entry in map file; key = '"; ostr << aFileName.ToUTF8() << "'"; wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() ); } while( 0 ); #endif m_CacheList.pop_back(); delete ep; return NULL; } if( aCachePtr ) *aCachePtr = ep; ep->SetSHA1( sha1sum ); wxString bname = ep->GetCacheBaseName(); wxString cachename = m_CacheDir + bname + wxT( ".3dc" ); if( wxFileName::FileExists( cachename ) && loadCacheData( ep ) ) return ep->sceneData; ep->sceneData = m_Plugins->Load3DModel( aFileName, ep->pluginInfo ); if( NULL != ep->sceneData ) saveCacheData( ep ); return ep->sceneData; }
fCache::fCache(unsigned int cacheSize, const std::string &fname) : _db(SystemConfig::getFilesDir() + "/" + getSHA1(fname) + ".dat"), _fname(fname), _cacheSize(cacheSize), _mutex("fCache") {}
SCENEGRAPH* S3D_CACHE::load( const wxString& aModelFile, S3D_CACHE_ENTRY** aCachePtr ) { if( aCachePtr ) *aCachePtr = NULL; wxString full3Dpath = m_FNResolver->ResolvePath( aModelFile ); if( full3Dpath.empty() ) { // the model cannot be found; we cannot proceed wxLogTrace( MASK_3D_CACHE, " * [3D model] could not find model '%s'\n", aModelFile.ToUTF8() ); return NULL; } // check cache if file is already loaded wxCriticalSectionLocker lock( lock3D_cache ); std::map< wxString, S3D_CACHE_ENTRY*, S3D::rsort_wxString >::iterator mi; mi = m_CacheMap.find( full3Dpath ); if( mi != m_CacheMap.end() ) { wxFileName fname( full3Dpath ); wxDateTime fmdate = fname.GetModificationTime(); bool reload = false; if( fmdate != mi->second->modTime ) { unsigned char hashSum[20]; getSHA1( full3Dpath, hashSum ); mi->second->modTime = fmdate; if( !isSHA1Same( hashSum, mi->second->sha1sum ) ) { mi->second->SetSHA1( hashSum ); reload = true; } } if( reload ) { if( NULL != mi->second->sceneData ) { S3D::DestroyNode( mi->second->sceneData ); mi->second->sceneData = NULL; } if( NULL != mi->second->renderData ) S3D::Destroy3DModel( &mi->second->renderData ); mi->second->sceneData = m_Plugins->Load3DModel( full3Dpath, mi->second->pluginInfo ); } if( NULL != aCachePtr ) *aCachePtr = mi->second; return mi->second->sceneData; } // a cache item does not exist; search the Filename->Cachename map return checkCache( full3Dpath, aCachePtr ); }
/*--Function for compress file--*/ void compress(char * str_in,lz78_compressor * in, struct bitio* file) { char* buf=malloc(16); uint64_t inp=0; int father=0; int child=0; int go=1; uint64_t c=0; int flag=0; int ret=0; struct bitio*file_out=bit_open(in->output_file,1); /*After compress i put the info of the file at the beginning of the file*/ info* ret_info=malloc(sizeof(info)); /*Put the info at the beginning of the file and return a structure with the info*/ ret_info=addinfo(file_out,file,in->file_to_compress,0,in->d_max); in->number_of_code^=in->number_of_code; //XOR reset: if(flag==0){ /*Only for the first character*/ ret=bit_read(file,(u_int)8,&inp); buf[1]=(char)(inp&((1UL<<8)-1)); //printf("Ric: %X\n",buf[1] & 0xff); } printf("-----------\n"); while(go==1){ while(look_up_and_add(father,buf[1],in)==0){//repeat the operation until i don't fint a node =>scroll the tree buf[0]=buf[1]; father=hash_table[hash(child,(char)(inp&((1UL<<8)-1)))].child_index; //printf("Padre: %d\n",father); ret=bit_read(file,(u_int)8,&inp); buf[1]=(char)(inp&((1UL<<8)-1)); //printf("Ric: %X\n",buf[1] & 0xff); child=father; } //printf("Output: %X Char %c\n",father & 0xff,buf[1]); bit_write(file_out,16, (int) father); in->number_of_code++; father=0; // restart from the root child=0; c++; if(ret==0)break; if(in->counter_child_tree>=in->d_max-1){ //I need to empty the tree and restart the compressio from the current position free(hash_table); in->counter_child_tree=0; hash_init(6700417,in); goto reset; }; } flush_out_buffer(file_out); //add the SHA-1 of the compressed-file in the header long old_index=ftell(file_out->f);//save the curren pointer to position of the file struct bitio* file_out_rd=bit_open(in->output_file,0);//I re-open the output file but in in read mode this time getSHA1(88,in->number_of_code,file_out_rd,ret_info); //I do the SHA-1 over the whole file bit_close(file_out_rd); fseek(file_out->f,(long)0x40,SEEK_SET); //I write the SHA-1 hash in the correct position //printf("SHA-1: "); for (int i = 0; i < 20; i ++) { bit_write(file_out,(u_int)8*sizeof(unsigned char),(unsigned char)ret_info->sha1[i]); // printf(" %2x", (unsigned char)ret_info->sha1[i]); } bit_write(file_out,(u_int)32,in->number_of_code); //printf("\n"); flush_out_buffer(file_out); fseek(file_out->f,old_index,SEEK_SET); // restore the pointer of the file in order to close the file in the rigth way //close the file descriptor bit_close(file_out); }