2006-02-26 23:45:34 +01:00
|
|
|
// Copyright (c) 2006, 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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-02-26 23:45:34 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file subtitle_format_srt.cpp
|
|
|
|
/// @brief Reading/writing SubRip format subtitles (.SRT)
|
|
|
|
/// @ingroup subtitle_io
|
|
|
|
///
|
2006-02-26 23:45:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
#include "subtitle_format_srt.h"
|
|
|
|
#include "text_file_reader.h"
|
2006-02-27 22:57:10 +01:00
|
|
|
#include "text_file_writer.h"
|
2006-02-26 23:45:34 +01:00
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Can read?
|
|
|
|
/// @param filename
|
|
|
|
/// @return
|
|
|
|
///
|
2006-02-27 22:57:10 +01:00
|
|
|
bool SRTSubtitleFormat::CanReadFile(wxString filename) {
|
2006-02-26 23:45:34 +01:00
|
|
|
return (filename.Right(4).Lower() == _T(".srt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Can write?
|
|
|
|
/// @param filename
|
|
|
|
/// @return
|
|
|
|
///
|
2006-12-26 19:26:13 +01:00
|
|
|
bool SRTSubtitleFormat::CanWriteFile(wxString filename) {
|
|
|
|
return (filename.Right(4).Lower() == _T(".srt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Get name
|
|
|
|
/// @return
|
|
|
|
///
|
2006-12-26 19:26:13 +01:00
|
|
|
wxString SRTSubtitleFormat::GetName() {
|
|
|
|
return _T("SubRip");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Get read wildcards
|
|
|
|
/// @return
|
|
|
|
///
|
2006-12-26 19:26:13 +01:00
|
|
|
wxArrayString SRTSubtitleFormat::GetReadWildcards() {
|
|
|
|
wxArrayString formats;
|
|
|
|
formats.Add(_T("srt"));
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Get write wildcards
|
|
|
|
/// @return
|
|
|
|
///
|
2006-12-26 19:26:13 +01:00
|
|
|
wxArrayString SRTSubtitleFormat::GetWriteWildcards() {
|
|
|
|
return GetReadWildcards();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Read file
|
|
|
|
/// @param filename
|
|
|
|
/// @param encoding
|
|
|
|
///
|
2006-02-27 22:57:10 +01:00
|
|
|
void SRTSubtitleFormat::ReadFile(wxString filename,wxString encoding) {
|
2006-02-26 23:45:34 +01:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// Reader
|
|
|
|
TextFileReader file(filename,encoding);
|
|
|
|
|
|
|
|
// Default
|
2007-01-06 06:04:57 +01:00
|
|
|
LoadDefault(false);
|
2006-02-26 23:45:34 +01:00
|
|
|
|
|
|
|
// Parse file
|
|
|
|
int linen = 1;
|
|
|
|
int fileLine = 0;
|
|
|
|
int mode = 0;
|
2007-01-06 06:04:57 +01:00
|
|
|
int lines = 0;
|
2006-02-26 23:45:34 +01:00
|
|
|
long templ;
|
|
|
|
AssDialogue *line = NULL;
|
|
|
|
while (file.HasMoreLines()) {
|
|
|
|
// Reads line
|
|
|
|
wxString curLine = file.ReadLineFromFile();
|
|
|
|
fileLine++;
|
|
|
|
|
2007-01-15 22:35:34 +01:00
|
|
|
if (mode == 0) {
|
|
|
|
// Checks if there is anything to read
|
|
|
|
if (curLine.IsEmpty()) continue;
|
|
|
|
|
|
|
|
// Check if it's a line number
|
|
|
|
if (!curLine.IsNumber()) {
|
|
|
|
Clear();
|
|
|
|
throw wxString::Format(_T("Parse error on entry %i at line %i (expecting line number). Possible malformed file."),linen,fileLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read line number
|
|
|
|
curLine.ToLong(&templ);
|
|
|
|
if (templ != linen) {
|
|
|
|
linen = templ;
|
|
|
|
}
|
|
|
|
line = new AssDialogue();
|
|
|
|
mode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (mode == 1) {
|
|
|
|
// Read timestamps
|
|
|
|
if (curLine.substr(13,3) != _T("-->")) {
|
|
|
|
Clear();
|
|
|
|
throw wxString::Format(_T("Parse error on entry %i at line %i (expecting timestamps). Possible malformed file."),linen,fileLine);
|
|
|
|
}
|
|
|
|
line->Start.ParseSRT(curLine.substr(0,12));
|
|
|
|
line->End.ParseSRT(curLine.substr(17,12));
|
|
|
|
mode = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (mode == 2) {
|
|
|
|
// Checks if it's done
|
|
|
|
bool eof = !file.HasMoreLines();
|
|
|
|
bool isDone = curLine.IsEmpty();
|
|
|
|
|
|
|
|
// Append text
|
|
|
|
if (!isDone) {
|
2006-02-26 23:45:34 +01:00
|
|
|
if (line->Text != _T("")) line->Text += _T("\\N");
|
|
|
|
line->Text += curLine;
|
2007-01-15 22:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done
|
|
|
|
if (isDone || eof) {
|
|
|
|
mode = 0;
|
|
|
|
linen++;
|
|
|
|
line->group = _T("[Events]");
|
|
|
|
line->Style = _T("Default");
|
|
|
|
line->Comment = false;
|
|
|
|
line->UpdateData();
|
|
|
|
line->ParseSRTTags();
|
2009-05-15 01:45:20 +02:00
|
|
|
line->FixStartMS();
|
2007-01-15 22:35:34 +01:00
|
|
|
Line->push_back(line);
|
|
|
|
lines++;
|
|
|
|
}
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-06 06:04:57 +01:00
|
|
|
|
|
|
|
// No lines?
|
|
|
|
if (lines == 0) {
|
|
|
|
AssDialogue *line = new AssDialogue();
|
|
|
|
line->group = _T("[Events]");
|
|
|
|
line->Style = _T("Default");
|
2009-05-15 01:45:20 +02:00
|
|
|
line->SetStartMS(0);
|
|
|
|
line->SetEndMS(5000);
|
2007-01-06 06:04:57 +01:00
|
|
|
Line->push_back(line);
|
|
|
|
}
|
2006-02-26 23:45:34 +01:00
|
|
|
}
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @brief Write file
|
|
|
|
/// @param _filename
|
|
|
|
/// @param encoding
|
|
|
|
///
|
2006-02-27 22:57:10 +01:00
|
|
|
void SRTSubtitleFormat::WriteFile(wxString _filename,wxString encoding) {
|
|
|
|
// Open file
|
|
|
|
TextFileWriter file(_filename,encoding);
|
|
|
|
|
|
|
|
// Convert to SRT
|
|
|
|
CreateCopy();
|
2007-06-19 05:34:53 +02:00
|
|
|
SortLines();
|
2008-07-18 17:39:34 +02:00
|
|
|
StripComments();
|
2009-05-15 03:34:00 +02:00
|
|
|
// Tags must be converted in two passes
|
|
|
|
// First ASS style overrides are converted to SRT but linebreaks are kept
|
|
|
|
ConvertTags(2,_T("\\N"));
|
|
|
|
// Then we can recombine overlaps, this requires ASS style linebreaks
|
2008-07-18 17:39:34 +02:00
|
|
|
RecombineOverlaps();
|
|
|
|
MergeIdentical();
|
2009-05-15 03:34:00 +02:00
|
|
|
// And finally convert linebreaks
|
|
|
|
ConvertTags(0,_T("\r\n"));
|
|
|
|
// Otherwise unclosed overrides might affect lines they shouldn't, see bug #809 for example
|
2006-02-27 22:57:10 +01:00
|
|
|
|
|
|
|
// Write lines
|
|
|
|
int i=1;
|
|
|
|
using std::list;
|
|
|
|
for (list<AssEntry*>::iterator cur=Line->begin();cur!=Line->end();cur++) {
|
|
|
|
AssDialogue *current = AssEntry::GetAsDialogue(*cur);
|
2008-01-25 21:57:58 +01:00
|
|
|
if (current && !current->Comment) {
|
2006-02-27 22:57:10 +01:00
|
|
|
// Write line
|
|
|
|
file.WriteLineToFile(wxString::Format(_T("%i"),i));
|
|
|
|
file.WriteLineToFile(current->Start.GetSRTFormated() + _T(" --> ") + current->End.GetSRTFormated());
|
|
|
|
file.WriteLineToFile(current->Text);
|
|
|
|
file.WriteLineToFile(_T(""));
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-19 05:34:53 +02:00
|
|
|
// Clean up
|
|
|
|
ClearCopy();
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|
2009-07-29 07:43:02 +02:00
|
|
|
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|