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 (std::exception e) {
env->ThrowError(e.what());
catch (const std::exception &e) {
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
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 {
// 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)
unsigned __int32 temp = 1;
@ -144,19 +144,27 @@ void PRSFile::Load(std::string path, bool reset) {
// Open file
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 {
// Read first four bytes
char buf[5];
buf[4] = 0;
fread(buf,1,4,fp);
if (strcmp(buf,"PRS") != 0) throw new std::exception("Invalid file type.");
// Read first eight bytes
char buf[16];
fread(buf,1,8,fp);
if (memcmp(buf,"PRS-BIN",8) != 0) {
// 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
unsigned __int32 temp = 0;
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
fread(&temp,4,1,fp);
@ -309,11 +317,8 @@ PRSVideoFrame* PRSFile::CachedGetFrameByID(int id) {
cached.frame = NULL;
cacheMemSize += frame->GetSize();
// If memory has been exceeded, remove stuff from the back until it isn't anymore
while (cacheMemSize > maxCache && frameCache.size() > 1) {
cacheMemSize -= frameCache.back().frame->GetSize();
frameCache.pop_back();
}
// Update cache
EnforceCacheLimit();
}
// 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
void PRSFile::GetDisplayBlocksAtFrame(int n,std::vector<PRSDisplay*> &blocks) {
@ -430,3 +454,9 @@ void PRSFile::SaveText(std::string path) {
// Close file
file.close();
}
///////////////////
// Load plain-text
void PRSFile::LoadText(std::string path, bool reset) {
}

View file

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