trpage_readbuf.cpp
来自「最新osg包」· C++ 代码 · 共 714 行 · 第 1/2 页
CPP
714 行
bool trpgMemReadBuffer::isEmpty(){ if (!data) return true; if (pos >= len) return true; // Also test the limits for (unsigned int i=0;i<limits.size();i++) if (limits[i] == 0) return true; return false;}// Set Length// Allocate the given spacevoid trpgMemReadBuffer::SetLength(int newLen){ if (newLen > totLen) { if (data) delete [] data; data = new char[newLen]; totLen = newLen; } len = newLen; pos = 0;}// Get Data Ptr// Return a pointer to our data so it can be written tochar *trpgMemReadBuffer::GetDataPtr(){ return data;}// Get Data// Protected method for actually retrieving a piece of databool trpgMemReadBuffer::GetData(char *ret,int rlen){ if (rlen < 0) return false; // Test against limits imposed from without if (!TestLimit(rlen)) throw 1; // See if we've actually got the data if (pos+rlen > len) throw 1; // Copy into the return buffer memcpy(ret,&data[pos],rlen); // Update any limits we might have UpdateLimits(rlen); pos += rlen; return true;}// Get Reference to Data// Protected method that retrieves a reference to the given amoutn of databool trpgMemReadBuffer::GetDataRef(char **ret,int rlen){ if (rlen < 0) return false; // Test against limits if (!TestLimit(rlen)) throw 1; if (pos + rlen > len) throw 1; // Set up reference *ret = &data[pos]; UpdateLimits(rlen); pos += rlen; return true;}// Skip// Same as read except we're not, uh, readingbool trpgMemReadBuffer::Skip(int rlen){ if (rlen == 0) return true; if (rlen < 0) return false; // Test against limits if (!TestLimit(rlen)) return false; if (pos + rlen > len) return false; UpdateLimits(rlen); pos += rlen; return true;}/* Appendable File read class */trpgrAppFile::trpgrAppFile(trpgEndian inNess,const char *fileName){ Init(inNess,fileName);}void trpgrAppFile::Init(trpgEndian inNess,const char *fileName){ valid = false; ness = inNess; cpuNess = trpg_cpu_byte_order(); if (!(fp = osgDB::fopen(fileName,"rb"))) return; valid = true;}trpgrAppFile::~trpgrAppFile(){ if (fp) fclose(fp); valid = false;}bool trpgrAppFile::isValid(void) const{ return valid;}// Read a section of data from the given file// and dump it into the given bufferbool trpgrAppFile::Read(trpgMemReadBuffer *buf,int32 offset){ if (!valid) return false; // Seek to the right location if (fseek(fp,offset,SEEK_SET)) return false; // Read a length int32 len; if (fread(&len,sizeof(int32),1,fp) != 1) { valid = false; return false; } // Byteswap if necessary if (ness != cpuNess) len = trpg_byteswap_int(len); if (len < 0) { valid = false; return false; } buf->SetLength(len); char *data = buf->GetDataPtr(); if (!data) { valid = false; return false; } if (fread(data,sizeof(char),len,fp) != (uint32)len) { valid = false; return false; } return true;}/* Read a section of data from the given file and dump it into the given memory. Sanity check the length against the size of the memory passed into dataSize.*/bool trpgrAppFile::Read(char *data,int32 baseOffset,int32 objOffset,int32 dataSize){ if (!valid) return false; // Seek to the right place int result; if ((result = fseek(fp,baseOffset,SEEK_SET))) { valid = false; return false; } // Read the total object length int32 len; if (fread(&len,sizeof(int32),1,fp) != 1) { valid = false; return false; } // Byteswap if necessary if (ness != cpuNess) len = trpg_byteswap_int(len); if (len < 0) { valid = false; return false; } // It's all right to read less than the whole data block if (objOffset+dataSize > len) return false; // Skip to the object offset if (fseek(fp,objOffset,SEEK_CUR)) { valid = false; return false; } // Read the raw data // Note: What about byte swapping? if (fread(data,sizeof(char),dataSize,fp) != (uint32)dataSize) { valid = false; return false; } return true;}/* App File Cache This class manages a group of appendable files with the same base name and extension. It will keep a certain number of them open to facilitate caching.*/trpgrAppFileCache::trpgrAppFileCache(const char *inPre,const char *inExt,int noFiles){ Init(inPre,inExt,noFiles);}void trpgrAppFileCache::Init(const char *inPre,const char *inExt,int noFiles){ strcpy(baseName,inPre); strcpy(ext,inExt); files.resize(noFiles); timeCount = 0;}trpgrAppFileCache::~trpgrAppFileCache(){ unsigned int len = files.size(); for (unsigned int i=0;i<len;i++) { if (files[i].afile) { delete files[i].afile; files[i].afile = NULL; } }}trpgrAppFile *trpgrAppFileCache::GetNewRAppFile(trpgEndian ness, const char *fileName){ return new trpgrAppFile(ness,fileName);}trpgrAppFile *trpgrAppFileCache::GetFile(trpgEndian ness,int id){ return GetFile(ness,id,-1,-1);}/* */trpgrAppFile *trpgrAppFileCache::GetFile(trpgEndian ness,int id,int col,int row){ // Look for it already here int foundID = -1; unsigned int i; for (i=0;i<files.size();i++) { if ((files[i].id == id)&& (files[i].col == col) && (files[i].row == row)) { foundID = i; break; } } // Found it in cache, just return if (foundID != -1) { OpenFile &of = files[foundID]; if (of.afile->isValid()) { of.lastUsed = timeCount; return of.afile; } else { if (of.afile) delete of.afile; of.afile = NULL; } } // Didn't find it. Need to reclaim one // Look for the oldest used int oldTime=-1,oldID=-1; for (i=0;i<files.size();i++) { OpenFile &of = files[i]; if (!of.afile || (oldTime == -1) || (of.lastUsed < oldTime)) { oldID = i; oldTime = of.lastUsed; if (!of.afile) break; } } // Reclaim this one OpenFile &of = files[oldID]; if (of.afile) delete of.afile; char fileName[1024]; if(col==-1) { sprintf(fileName,"%s_%d.%s",baseName,id,ext); } else { char dir[1024]; char filebase[1024]; //this is very ugly, but it avoids radical API changes // find the last PATHSEPERATOR in the baseName string int len = strlen(baseName); while(--len > 0) { if(baseName[len]==PATHSEPERATOR[0]) { strcpy(filebase,&baseName[len+1]); strcpy(dir,baseName); dir[len]='\0'; break; } } sprintf(fileName,"%s" PATHSEPERATOR "%d" PATHSEPERATOR "%d" PATHSEPERATOR "%s_%d.%s", dir,col,row,filebase,id,ext); } of.afile = GetNewRAppFile(ness,fileName); of.id = id; of.row = row; of.col = col; of.lastUsed = timeCount; timeCount++; return of.afile;}// Constructor for OpenFile classtrpgrAppFileCache::OpenFile::OpenFile(){ afile = NULL; lastUsed = 0; id = -1;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?