From 454297253cbf3b7ff262792c0e7c9a69f5cfb6b9 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Mon, 3 Apr 2006 16:23:53 +0000 Subject: [PATCH] Change PRS header, fixed some exception handling and added a (not coded) PRSFile::LoadText function Originally committed to SVN as r297. --- avisynth_prs/draw_prs.cpp | 7 +++-- prs/prs_file.cpp | 58 +++++++++++++++++++++++++++++---------- prs/prs_file.h | 3 ++ 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/avisynth_prs/draw_prs.cpp b/avisynth_prs/draw_prs.cpp index bb140a8c8..e4bb048fa 100644 --- a/avisynth_prs/draw_prs.cpp +++ b/avisynth_prs/draw_prs.cpp @@ -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."); } } diff --git a/prs/prs_file.cpp b/prs/prs_file.cpp index 9415dd3d3..3765f7911 100644 --- a/prs/prs_file.cpp +++ b/prs/prs_file.cpp @@ -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 &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) { +} diff --git a/prs/prs_file.h b/prs/prs_file.h index 455931840..96cdd67fb 100644 --- a/prs/prs_file.h +++ b/prs/prs_file.h @@ -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); };