From 2baf7bd2c449045fc5f694430d15ec0fd0eec463 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Sat, 20 Sep 2008 03:04:21 +0000 Subject: [PATCH] Changed how time is dealt with in Athenasub. Originally committed to SVN as r2368. --- aegilib/aegilib_2008.vcproj | 1 - .../{src => include/athenasub}/athenastring.h | 0 aegilib/include/athenasub/athenasub.h | 2 + .../{src => include/athenasub}/athenatime.h | 24 ++--- aegilib/include/athenasub/interfaces.h | 33 +------ aegilib/src/formats/format_ass_dialogue.cpp | 96 ++++++++++++++++++- aegilib/src/formats/format_ass_dialogue.h | 10 +- aegilib/src/section_entry_dialogue.h | 8 +- aegilib/src/time.cpp | 57 +++++------ aegilib/test/src/main.cpp | 4 +- 10 files changed, 147 insertions(+), 88 deletions(-) rename aegilib/{src => include/athenasub}/athenastring.h (100%) rename aegilib/{src => include/athenasub}/athenatime.h (69%) diff --git a/aegilib/aegilib_2008.vcproj b/aegilib/aegilib_2008.vcproj index bd56ef584..ea22d56b4 100644 --- a/aegilib/aegilib_2008.vcproj +++ b/aegilib/aegilib_2008.vcproj @@ -131,7 +131,6 @@ PrecompiledHeaderThrough="prec.h" WarningLevel="4" WarnAsError="true" - Detect64BitPortabilityProblems="false" DebugInformationFormat="3" DisableSpecificWarnings="4996" ForcedIncludeFiles="prec.h" diff --git a/aegilib/src/athenastring.h b/aegilib/include/athenasub/athenastring.h similarity index 100% rename from aegilib/src/athenastring.h rename to aegilib/include/athenasub/athenastring.h diff --git a/aegilib/include/athenasub/athenasub.h b/aegilib/include/athenasub/athenasub.h index 361a40cf8..72c9df62e 100644 --- a/aegilib/include/athenasub/athenasub.h +++ b/aegilib/include/athenasub/athenasub.h @@ -38,6 +38,8 @@ #include "api.h" #include "tr1.h" #include "exception.h" +#include "athenastring.h" +#include "athenatime.h" #include "interfaces.h" #include "range.h" diff --git a/aegilib/src/athenatime.h b/aegilib/include/athenasub/athenatime.h similarity index 69% rename from aegilib/src/athenatime.h rename to aegilib/include/athenasub/athenatime.h index 2e9687829..daaba9c35 100644 --- a/aegilib/src/athenatime.h +++ b/aegilib/include/athenasub/athenatime.h @@ -34,27 +34,29 @@ // #pragma once -#include "athenasub.h" -#include "utils.h" namespace Athenasub { // Time class - class CTime : public ITime { + class Time { private: - int ms; + int ms; // Time in milliseconds public: - CTime() { ms = 0; } - CTime(int _ms) { ms = _ms; } + Time() { ms = 0; } + Time(int milliseconds) { ms = milliseconds; } inline void SetMS(int milliseconds) { ms = milliseconds; } inline int GetMS() const { return ms; } - - String GetString(int ms_precision,int h_precision) const; - void ParseString(const String &data); - - Time Clone() const { return Time(new CTime(*this)); } }; + // Operators + inline Time operator+ (const Time& p1,int p2) { return Time(p1.GetMS()+p2); } + inline Time operator- (const Time& p1,int p2) { return Time(p1.GetMS()-p2); } + inline bool operator== (const Time& p1,const Time& p2) { return p1.GetMS() == p2.GetMS(); } + inline bool operator!= (const Time& p1,const Time& p2) { return p1.GetMS() != p2.GetMS(); } + inline bool operator<= (const Time& p1,const Time& p2) { return p1.GetMS() <= p2.GetMS(); } + inline bool operator>= (const Time& p1,const Time& p2) { return p1.GetMS() >= p2.GetMS(); } + inline bool operator< (const Time& p1,const Time& p2) { return p1.GetMS() < p2.GetMS(); } + inline bool operator> (const Time& p1,const Time& p2) { return p1.GetMS() > p2.GetMS(); } } diff --git a/aegilib/include/athenasub/interfaces.h b/aegilib/include/athenasub/interfaces.h index 304605dc4..381dd3099 100644 --- a/aegilib/include/athenasub/interfaces.h +++ b/aegilib/include/athenasub/interfaces.h @@ -36,6 +36,7 @@ #pragma once #include "tr1.h" +#include "athenatime.h" #include #include #include @@ -53,7 +54,6 @@ namespace Athenasub { class IController; class IView; class IFormatHandler; - class ITime; class IColour; class IEntry; class IDialogue; @@ -73,7 +73,6 @@ namespace Athenasub { typedef shared_ptr Controller; typedef shared_ptr View; typedef shared_ptr FormatHandler; - typedef shared_ptr Time; typedef shared_ptr Colour; typedef shared_ptr Entry; typedef shared_ptr Dialogue; @@ -196,21 +195,6 @@ namespace Athenasub { }; - // Time - class ITime { - public: - virtual ~ITime() {} - - virtual void SetMS(int milliseconds) = 0; - virtual int GetMS() const = 0; - - virtual String GetString(int ms_precision,int h_precision) const = 0; - virtual void ParseString(const String &data) = 0; - - virtual Time Clone() const = 0; - }; - - // Color class IColour { public: @@ -276,8 +260,8 @@ namespace Athenasub { // Read accessors virtual const String& GetText() const = 0; - virtual const ITime& GetStartTime() const = 0; - virtual const ITime& GetEndTime() const = 0; + virtual const Time& GetStartTime() const = 0; + virtual const Time& GetEndTime() const = 0; virtual int GetStartFrame() const = 0; virtual int GetEndFrame() const = 0; virtual bool IsComment() const = 0; @@ -289,8 +273,8 @@ namespace Athenasub { // Write accessors virtual void SetText(const String& text) = 0; - virtual void SetStartTime(const ITime& start) = 0; - virtual void SetEndTime(const ITime& end) = 0; + virtual void SetStartTime(const Time& start) = 0; + virtual void SetEndTime(const Time& end) = 0; virtual void SetStartFrame(int start) = 0; virtual void SetEndFrame(int end) = 0; virtual void SetComment(bool isComment) = 0; @@ -437,11 +421,4 @@ namespace Athenasub { virtual Model CreateModel()=0; }; typedef shared_ptr LibAthenaSub; - - - // Operators - inline Time operator+(const ITime& p1,int p2) { Time res = p1.Clone(); res->SetMS(res->GetMS()+p2); return res; } - inline Time operator-(const ITime& p1,int p2) { Time res = p1.Clone(); res->SetMS(res->GetMS()-p2); return res; } - inline bool operator==(const ITime& p1,const ITime& p2) { return p1.GetMS() == p2.GetMS(); } - } diff --git a/aegilib/src/formats/format_ass_dialogue.cpp b/aegilib/src/formats/format_ass_dialogue.cpp index 56f93ac66..f0a995104 100644 --- a/aegilib/src/formats/format_ass_dialogue.cpp +++ b/aegilib/src/formats/format_ass_dialogue.cpp @@ -62,6 +62,96 @@ DialogueASS::DialogueASS(const String &data,int version) } +////////////////////////////////// +// Generates a string from a time +String GetTimeString(const Time& time,int ms_precision,int h_precision) +{ + // Enforce sanity + ms_precision = Mid(0,ms_precision,3); + h_precision = Mid(0,h_precision,2); + + // Generate values + int _ms = time.GetMS(); + int h = _ms / 3600000; + _ms -= h*3600000; + int min = _ms / 60000; + _ms -= min*60000; + int s = _ms / 1000; + _ms -= s*1000; + + // Cap hour value + if (h > 9 && h_precision == 1) { + h = 9; + min = 59; + s = 59; + _ms = 999; + } + + // Modify ms to account for precision + if (ms_precision == 2) _ms /= 10; + else if (ms_precision == 1) _ms /= 100; + else if (ms_precision == 0) _ms = 0; + + // Get write buffer + String final; + size_t size = 7+h_precision+ms_precision; + size_t pos = 0; + wxChar *buffer = final.GetWriteBuf(size); + wxChar temp[16]; + + // Write time + WriteNumber(buffer,temp,h,h_precision,pos); + WriteChar(buffer,_T(':'),pos); + WriteNumber(buffer,temp,min,2,pos); + WriteChar(buffer,_T(':'),pos); + WriteNumber(buffer,temp,s,2,pos); + WriteChar(buffer,_T('.'),pos); + WriteNumber(buffer,temp,_ms,ms_precision,pos); + + // Write terminator + WriteText(buffer,_T("\0"),1,pos); + + // Restore string's state and return + final.UngetWriteBuf(pos-1); + return final; +} + + +/////////////////////////////// +// Parses a string into a time +Time ParseTimeString(const String &data) +{ + // Break into an array of values + array values; + size_t last = 0; + size_t len = data.Length(); + size_t curIndex = 0; + wxChar cur = 0; + for (size_t i=0;i=0;) { + accum += values[i] * mult[curIndex-i]; + } + return Time((int)accum); +} + + ////////////////// // Parse ASS Data bool DialogueASS::Parse(wxString rawData, int version) @@ -97,8 +187,8 @@ bool DialogueASS::Parse(wxString rawData, int version) } // Get times - time[0].ParseString(tkn.GetString()); - time[1].ParseString(tkn.GetString()); + time[0] = ParseTimeString(tkn.GetString()); + time[1] = ParseTimeString(tkn.GetString()); // Get style and actor text[1] = tkn.GetString(true); @@ -169,7 +259,7 @@ String DialogueASS::ToText(int version) const // Write times for (size_t i=0;i<2;i++) { - wxString tempStr = time[i].GetString(2,1); + wxString tempStr = GetTimeString(time[i],2,1); WriteText(buffer,&tempStr[0],10,pos); WriteChar(buffer,_T(','),pos); } diff --git a/aegilib/src/formats/format_ass_dialogue.h b/aegilib/src/formats/format_ass_dialogue.h index 82c9f6e63..94b07dfbd 100644 --- a/aegilib/src/formats/format_ass_dialogue.h +++ b/aegilib/src/formats/format_ass_dialogue.h @@ -48,7 +48,7 @@ namespace Athenasub { private: array text; // 0 = text, 1 = style, 2 = actor, 3 = effect - array time; + array time; array margin; int layer; bool isComment; @@ -73,8 +73,8 @@ namespace Athenasub { bool HasMargins() const { return true; } // Read accessors - const ITime& GetStartTime() const { return time[0]; } - const ITime& GetEndTime() const { return time[1]; } + const Time& GetStartTime() const { return time[0]; } + const Time& GetEndTime() const { return time[1]; } bool IsComment() const { return isComment; } int GetLayer() const { return layer; } int GetMargin(int n) const { return margin.at(n); } @@ -84,8 +84,8 @@ namespace Athenasub { const String& GetUserField() const { return text[3]; } // Write acessors - void SetStartTime(const ITime &setStart) { time[0].SetMS(setStart.GetMS()); } - void SetEndTime(const ITime &setEnd) { time[1].SetMS(setEnd.GetMS()); } + void SetStartTime(const Time &setStart) { time[0].SetMS(setStart.GetMS()); } + void SetEndTime(const Time &setEnd) { time[1].SetMS(setEnd.GetMS()); } void SetComment(bool _isComment) { isComment = _isComment; } void SetLayer(int _layer) { layer = _layer; } void SetMargin(int _margin,int value) { margin.at(_margin) = value; } diff --git a/aegilib/src/section_entry_dialogue.h b/aegilib/src/section_entry_dialogue.h index eb09bf31c..2e8dfbedd 100644 --- a/aegilib/src/section_entry_dialogue.h +++ b/aegilib/src/section_entry_dialogue.h @@ -67,8 +67,8 @@ namespace Athenasub { // Read accessors virtual const String& GetText() const { ThrowUnsupported(); return EmptyString(); } -// virtual ITime& GetStartTime() const { ThrowUnsupported(); return Time(); } -// virtual ITime& GetEndTime() const { ThrowUnsupported(); return Time(); } + //virtual Time& GetStartTime() const { ThrowUnsupported(); return Time(); } + //virtual Time& GetEndTime() const { ThrowUnsupported(); return Time(); } virtual int GetStartFrame() const { ThrowUnsupported(); return 0; } virtual int GetEndFrame() const { ThrowUnsupported(); return 0; } virtual bool IsComment() const { ThrowUnsupported(); return false; } @@ -80,8 +80,8 @@ namespace Athenasub { // Write accessors virtual void SetText(const String& text) { (void) text; ThrowUnsupported(); } - virtual void SetStartTime(const ITime& start) { (void) start; ThrowUnsupported(); } - virtual void SetEndTime(const ITime& end) { (void) end; ThrowUnsupported(); } + virtual void SetStartTime(const Time& start) { (void) start; ThrowUnsupported(); } + virtual void SetEndTime(const Time& end) { (void) end; ThrowUnsupported(); } virtual void SetStartFrame(int start) { (void) start; ThrowUnsupported(); } virtual void SetEndFrame(int end) { (void) end; ThrowUnsupported(); } virtual void SetComment(bool isComment) { (void) isComment; ThrowUnsupported(); } diff --git a/aegilib/src/time.cpp b/aegilib/src/time.cpp index 29a82329e..130359b97 100644 --- a/aegilib/src/time.cpp +++ b/aegilib/src/time.cpp @@ -35,9 +35,11 @@ #include "Athenasub.h" #include "athenatime.h" +#include "utils.h" using namespace Athenasub; +#if 0 ////////////////////// // Generates a string String CTime::GetString(int ms_precision,int h_precision) const @@ -68,43 +70,28 @@ String CTime::GetString(int ms_precision,int h_precision) const else if (ms_precision == 1) _ms /= 100; else if (ms_precision == 0) _ms = 0; - // Old code - if (false) { - // Generate mask string - wxString mask = wxString::Format(_T("%%0%ii:%%0%ii:%%0%ii.%%0%ii"),h_precision,2,2,ms_precision); + // Get write buffer + String final; + size_t size = 7+h_precision+ms_precision; + size_t pos = 0; + wxChar *buffer = final.GetWriteBuf(size); + wxChar temp[16]; - // Generate final string - wxString final = wxString::Format(mask,h,min,s,_ms); + // Write time + WriteNumber(buffer,temp,h,h_precision,pos); + WriteChar(buffer,_T(':'),pos); + WriteNumber(buffer,temp,min,2,pos); + WriteChar(buffer,_T(':'),pos); + WriteNumber(buffer,temp,s,2,pos); + WriteChar(buffer,_T('.'),pos); + WriteNumber(buffer,temp,_ms,ms_precision,pos); - // Done - return final; - } + // Write terminator + WriteText(buffer,_T("\0"),1,pos); - // New code - else { - // Get write buffer - wxString final; - size_t size = 7+h_precision+ms_precision; - size_t pos = 0; - wxChar *buffer = final.GetWriteBuf(size); - wxChar temp[16]; - - // Write time - WriteNumber(buffer,temp,h,h_precision,pos); - WriteChar(buffer,_T(':'),pos); - WriteNumber(buffer,temp,min,2,pos); - WriteChar(buffer,_T(':'),pos); - WriteNumber(buffer,temp,s,2,pos); - WriteChar(buffer,_T('.'),pos); - WriteNumber(buffer,temp,_ms,ms_precision,pos); - - // Write terminator - WriteText(buffer,_T("\0"),1,pos); - - // Restore string's state and return - final.UngetWriteBuf(pos-1); - return final; - } + // Restore string's state and return + final.UngetWriteBuf(pos-1); + return final; } @@ -141,3 +128,5 @@ void CTime::ParseString(const String &data) } ms = (int)accum; } + +#endif diff --git a/aegilib/test/src/main.cpp b/aegilib/test/src/main.cpp index 48821cd09..960fbfd53 100644 --- a/aegilib/test/src/main.cpp +++ b/aegilib/test/src/main.cpp @@ -108,8 +108,8 @@ int main() size_t len = entries.size(); for (size_t i=0;i (entries[i]); - diag->SetStartTime(*(diag->GetStartTime() - 55555)); - diag->SetEndTime(*(diag->GetEndTime() + 5555)); + diag->SetStartTime(diag->GetStartTime() - 55555); + diag->SetEndTime(diag->GetEndTime() + 5555); } actions->Finish(); }