Generic time parsing and writing works.

Originally committed to SVN as r1983.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-08 23:33:00 +00:00
parent 1b7746e99f
commit c5a582a23b
7 changed files with 139 additions and 14 deletions

View File

@ -296,6 +296,10 @@
RelativePath=".\src\text_file_reader.h"
>
</File>
<File
RelativePath=".\src\time.cpp"
>
</File>
</Filter>
<Filter
Name="Formats"

View File

@ -46,3 +46,17 @@
#include "manipulator.h"
#include "section.h"
#include "section_entry_dialogue.h"
#include "time.h"
//////////
// Macros
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MID
#define MID(a,b,c) MIN(MAX((a),(b)),(c))
#endif

View File

@ -36,8 +36,8 @@
#pragma once
#include "exception.h"
#include "section_entry.h"
#include "time.h"
#include "section_entry.h"
namespace Aegilib {

View File

@ -34,6 +34,7 @@
//
#pragma once
#include "aegistring.h"
namespace Aegilib {
@ -45,6 +46,12 @@ namespace Aegilib {
public:
Time() { ms = 0; }
Time(int ms) { (void)ms; }
void SetMS(int milliseconds) { ms = milliseconds; }
int GetMS() { return ms; }
String GetString(int ms_precision,int h_precision);
void Parse(String data);
};
};

View File

@ -138,7 +138,8 @@ SectionEntry *FormatHandlerASS::MakeEntry(String data,String group,int version)
final = diag;
// Debug
std::cout << "[" << group.mb_str(wxConvUTF8) << "] " << diag->GetText().mb_str(wxConvUTF8) << std::endl;
wxString out = diag->GetStartTime().GetString(2,1) + _T(",") + diag->GetEndTime().GetString(2,1) + _T(",") + diag->GetText();
std::cout << out.mb_str(wxConvUTF8) << std::endl;
}
// Format lines
@ -295,11 +296,11 @@ bool DialogueASS::Parse(wxString rawData, int version)
// Get start time
if (!tkn.HasMoreTokens()) return false;
start = ParseTime(tkn.GetNextToken());
start.Parse(tkn.GetNextToken());
// Get end time
if (!tkn.HasMoreTokens()) return false;
end = ParseTime(tkn.GetNextToken());
end.Parse(tkn.GetNextToken());
// Get style
if (!tkn.HasMoreTokens()) return false;
@ -353,12 +354,3 @@ bool DialogueASS::Parse(wxString rawData, int version)
return true;
}
/////////////////////
// Parse time string
Time DialogueASS::ParseTime(String time)
{
(void) time;
return 0;
}

View File

@ -87,7 +87,6 @@ namespace Aegilib {
bool comment;
bool Parse(String data,int version);
Time ParseTime(String data);
public:
// Constructors

109
aegilib/src/time.cpp Normal file
View File

@ -0,0 +1,109 @@
// Copyright (c) 2008, Rodrigo Braz Monteiro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// -----------------------------------------------------------------------------
//
// AEGISUB/AEGILIB
//
// Website: http://www.aegisub.net
// Contact: mailto:amz@aegisub.net
//
#include "aegilib.h"
using namespace Aegilib;
//////////////////////
// Generates a string
String Time::GetString(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 = ms;
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;
// Generate mask string
wxString mask = wxString::Format(_T("%%0%ii:%%0%ii:%%0%ii.%%0%ii"),h_precision,2,2,ms_precision);
// Generate final string
wxString final = wxString::Format(mask,h,min,s,_ms);
// Done
return final;
}
///////////////////
// Parses a string
void Time::Parse(String data)
{
// Break into an array of values
std::vector<double> values;
values.reserve(4);
size_t last = 0;
data += _T(":");
size_t len = data.Length();
for (size_t i=0;i<len;i++) {
if (data[i] == ':' || data[i] == ';') {
wxString temp = data.Mid(last,i-last);
last = i+1;
double tempd;
temp.ToDouble(&tempd);
values.push_back(tempd);
}
}
// Turn into milliseconds
ms = 0;
double mult = 1000.0;
int len2 = (int) values.size();
for (int i=len2;--i>=0;) {
ms += (int)(values[i] * mult);
mult *= 60.0;
}
}