Change PRS header, fixed some exception handling and added a (not coded) PRSFile::LoadText function

Originally committed to SVN as r297.
This commit is contained in:
Rodrigo Braz Monteiro 2006-04-03 16:23:53 +00:00
parent b973d58bee
commit 454297253c
3 changed files with 52 additions and 16 deletions

View File

@ -54,8 +54,11 @@ DrawPRS::DrawPRS (IScriptEnvironment* _env, PClip _child, const char *filename)
} }
// Catch exception // Catch exception
catch (std::exception e) { catch (const std::exception &e) {
env->ThrowError(e.what()); env->ThrowError("Error in PRS loading: %s",e.what());
}
catch (...) {
env->ThrowError("Unhandled exception in PRS loading.");
} }
} }

View File

@ -96,11 +96,11 @@ void PRSFile::Save(std::string path) {
// Open file // Open file
FILE *fp = fopen(path.c_str(),"wb"); FILE *fp = fopen(path.c_str(),"wb");
if (!fp) throw new std::exception("Failed to open file"); if (!fp) throw std::exception("Failed to open file");
try { try {
// Write the "PRS" (zero-terminated) string ID (4 bytes) // Write the "PRS" (zero-terminated) string ID (4 bytes)
fwrite("PRS",1,4,fp); fwrite("PRS-BIN",1,8,fp);
// Write version number (4 bytes) // Write version number (4 bytes)
unsigned __int32 temp = 1; unsigned __int32 temp = 1;
@ -144,19 +144,27 @@ void PRSFile::Load(std::string path, bool reset) {
// Open file // Open file
FILE *fp = fopen(path.c_str(),"rb"); FILE *fp = fopen(path.c_str(),"rb");
if (!fp) throw new std::exception("Failed to open file"); if (!fp) throw std::exception("Failed to open file");
try { try {
// Read first four bytes // Read first eight bytes
char buf[5]; char buf[16];
buf[4] = 0; fread(buf,1,8,fp);
fread(buf,1,4,fp); if (memcmp(buf,"PRS-BIN",8) != 0) {
if (strcmp(buf,"PRS") != 0) throw new std::exception("Invalid file type."); // Is actually ASCII, read as that
if (memcmp(buf,"PRS-ASC",7) == 0) {
LoadText(path,false);
return;
}
// Invalid
throw std::exception("Invalid file type.");
}
// Read version number // Read version number
unsigned __int32 temp = 0; unsigned __int32 temp = 0;
fread(&temp,4,1,fp); fread(&temp,4,1,fp);
if (temp != 1) throw new std::exception("Invalid version."); if (temp != 1) throw std::exception("Invalid version.");
// Read stream name length // Read stream name length
fread(&temp,4,1,fp); fread(&temp,4,1,fp);
@ -309,11 +317,8 @@ PRSVideoFrame* PRSFile::CachedGetFrameByID(int id) {
cached.frame = NULL; cached.frame = NULL;
cacheMemSize += frame->GetSize(); cacheMemSize += frame->GetSize();
// If memory has been exceeded, remove stuff from the back until it isn't anymore // Update cache
while (cacheMemSize > maxCache && frameCache.size() > 1) { EnforceCacheLimit();
cacheMemSize -= frameCache.back().frame->GetSize();
frameCache.pop_back();
}
} }
// Return it // Return it
@ -321,6 +326,25 @@ PRSVideoFrame* PRSFile::CachedGetFrameByID(int id) {
} }
/////////////////////////////////////////////////////////////
// Enforce the cache limit, that is, delete anything over it
// This function will always leave at least one image on cache
void PRSFile::EnforceCacheLimit() {
while (cacheMemSize > maxCache && frameCache.size() > 1) {
cacheMemSize -= frameCache.back().frame->GetSize();
frameCache.pop_back();
}
}
////////////////////////////
// Set maximum cache memory
void PRSFile::SetCacheLimit(int bytes) {
maxCache = bytes;
EnforceCacheLimit();
}
//////////////////////////////////////////////// ////////////////////////////////////////////////
// Finds which display blocks are at a position // Finds which display blocks are at a position
void PRSFile::GetDisplayBlocksAtFrame(int n,std::vector<PRSDisplay*> &blocks) { void PRSFile::GetDisplayBlocksAtFrame(int n,std::vector<PRSDisplay*> &blocks) {
@ -430,3 +454,9 @@ void PRSFile::SaveText(std::string path) {
// Close file // Close file
file.close(); file.close();
} }
///////////////////
// Load plain-text
void PRSFile::LoadText(std::string path, bool reset) {
}

View File

@ -60,6 +60,7 @@ private:
int cacheMemSize; int cacheMemSize;
int maxCache; int maxCache;
void Reset(); void Reset();
void EnforceCacheLimit();
public: public:
PRSFile(); PRSFile();
@ -77,8 +78,10 @@ public:
bool HasDataAtFrame(int n); bool HasDataAtFrame(int n);
void DrawFrame(int n,PRSVideoFrame *frame); void DrawFrame(int n,PRSVideoFrame *frame);
PRSImage *GetImageByID(int id); PRSImage *GetImageByID(int id);
PRSVideoFrame *CachedGetFrameByID(int id); PRSVideoFrame *CachedGetFrameByID(int id);
void ClearCache(); void ClearCache();
void SetCacheLimit(int bytes);
PRSImage *FindDuplicateImage(PRSImage *img); PRSImage *FindDuplicateImage(PRSImage *img);
}; };