Skip to content

Commit

Permalink
Refactoring and better file format detection. Also added a unit test …
Browse files Browse the repository at this point in the history
…for that.

Originally committed to SVN as r2480.
  • Loading branch information
Rodrigo Braz Monteiro committed Nov 23, 2008
1 parent 087ef67 commit 5797473
Show file tree
Hide file tree
Showing 22 changed files with 5,029 additions and 74 deletions.
53 changes: 39 additions & 14 deletions athenasub/include/athenasub/interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ namespace Athenasub {

// Forward references
class Range;
class Reader;
class Writer;
class ISelection;
class IController;
class IView;
Expand All @@ -65,6 +63,8 @@ namespace Athenasub {
class ISection;
class IDeltaCoder;
class IAction;
class IReader;
class IWriter;


// Smart pointers
Expand All @@ -83,6 +83,8 @@ namespace Athenasub {
typedef shared_ptr<ISection> Section;
typedef shared_ptr<IDeltaCoder> DeltaCoder;
typedef shared_ptr<IAction> Action;
typedef shared_ptr<IReader> Reader;
typedef shared_ptr<IWriter> Writer;


// Const smart pointers
Expand All @@ -106,7 +108,7 @@ namespace Athenasub {
virtual Controller CreateController() = 0;
virtual void AddListener(View listener) = 0;

virtual void Save(Writer &output,Format format=Format()) const = 0;
virtual void Save(Writer output,Format format=Format()) const = 0;

virtual String GetUndoMessage(const String owner="") const = 0;
virtual String GetRedoMessage(const String owner="") const = 0;
Expand All @@ -121,15 +123,6 @@ namespace Athenasub {
};


// View
class IView {
public:
virtual ~IView() {}

virtual void OnNotify(Notification notification) = 0;
};


// Controller
class IController {
public:
Expand Down Expand Up @@ -158,6 +151,15 @@ namespace Athenasub {
};


// View
class IView {
public:
virtual ~IView() {}

virtual void OnNotify(Notification notification) = 0;
};


// Selection
class ISelection {
public:
Expand Down Expand Up @@ -308,8 +310,8 @@ namespace Athenasub {
public:
virtual ~IFormatHandler() {}

virtual void Load(IModel &model,Reader &file) = 0;
virtual void Save(const IModel &model,Writer &file) const = 0;
virtual void Load(IModel &model,Reader file) = 0;
virtual void Save(const IModel &model,Writer file) const = 0;
};


Expand Down Expand Up @@ -382,6 +384,29 @@ namespace Athenasub {
};


// File reader
class IReader {
public:
virtual ~IReader() {}

virtual String GetFileName() = 0;
virtual void Rewind() = 0;

virtual bool HasMoreLines() = 0;
virtual String ReadLineFromFile() = 0;
virtual String GetCurrentEncoding() = 0;
};


// File writer
class IWriter {
public:
virtual ~IWriter() {}
virtual void WriteLineToFile(String line,bool addLineBreak=true) = 0;
virtual void Flush() = 0;
};


// Library
class ILibAthenaSub {
public:
Expand Down
4 changes: 2 additions & 2 deletions athenasub/src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ActionList CController::CreateActionList(const String title,const String owner,b
// Load a file
void CController::LoadFile(const String filename,const String encoding)
{
Reader reader(filename,encoding);
Reader reader(new CReader(filename,encoding));
std::vector<Format> handlers = FormatManager::GetCompatibleFormatList(reader);
size_t len = handlers.size();
bool success = false;
Expand All @@ -89,7 +89,7 @@ void CController::LoadFile(const String filename,const String encoding)
void CController::SaveFile(const String filename,const String encoding)
{
Format handler = FormatManager::GetFormatFromFilename(filename,true);
Writer writer(filename,encoding);
Writer writer(new CWriter(filename,encoding));
model->Save(writer,handler);
}

Expand Down
4 changes: 2 additions & 2 deletions athenasub/src/format_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ namespace Athenasub {
public:
//CFormatHandler(IModel& _model) : model(_model) {}

virtual void Load(IModel &model,Reader &file) = 0;
virtual void Save(const IModel &model,Writer &file) const = 0;
virtual void Load(IModel &model,Reader file) = 0;
virtual void Save(const IModel &model,Writer file) const = 0;
};

}
32 changes: 23 additions & 9 deletions athenasub/src/format_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
using namespace Athenasub;


////////
// List
///////////
// Statics
std::vector<Format> FormatManager::formats;
bool FormatManager::formatsInitialized = false;


////////////////
Expand All @@ -66,9 +67,13 @@ void FormatManager::AddFormat(Format format)
// Initialize all built-in formats
void FormatManager::InitializeFormats()
{
AddFormat(Format(new FormatASS()));
AddFormat(Format(new FormatSSA()));
AddFormat(Format(new FormatASS2()));
if (!formatsInitialized) {
AddFormat(Format(new FormatASS()));
AddFormat(Format(new FormatSSA()));
AddFormat(Format(new FormatASS2()));

formatsInitialized = true;
}
}


Expand All @@ -77,6 +82,7 @@ void FormatManager::InitializeFormats()
void FormatManager::ClearFormats()
{
formats.clear();
formatsInitialized = false;
}


Expand Down Expand Up @@ -133,22 +139,22 @@ Format FormatManager::GetFormatFromName(const String &name)

///////////////////////////////////////////////////////
// Get a list of all formats compatible with this file
std::vector<Format> FormatManager::GetCompatibleFormatList(Reader &reader)
std::vector<Format> FormatManager::GetCompatibleFormatList(Reader reader)
{
// Find all compatible formats and store them with their certainty
std::vector<std::pair<float,Format> > results;
size_t len = formats.size();
for (size_t i=0;i<len;i++) {
// Reset reader
reader.Rewind();
reader->Rewind();

// Check how certain it is that it can read the format
float certainty = formats[i]->CanReadFile(reader);

// Compare to extension
StringArray exts = formats[i]->GetReadExtensions();
for (size_t j=0;j<exts.size();j++) {
if (reader.GetFileName().EndsWith(exts[j],false)) {
if (reader->GetFileName().EndsWith(exts[j],false)) {
certainty *= 2.0f;
break;
}
Expand Down Expand Up @@ -176,6 +182,14 @@ std::vector<Format> FormatManager::GetCompatibleFormatList(Reader &reader)
}

// Reset reader again and return results
reader.Rewind();
reader->Rewind();
return finalResults;
}


//////////////////////////////////////
// Same as above, but from a filename
std::vector<Format> FormatManager::GetCompatibleFormatList(const String &filename,const String &encoding)
{
return GetCompatibleFormatList(Reader(new CReader(filename,encoding)));
}
6 changes: 3 additions & 3 deletions athenasub/src/format_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@

namespace Athenasub {

class Reader;

// Format manager class
class FormatManager {
private:
static bool formatsInitialized;
static std::vector<Format> formats;
FormatManager() {}

Expand All @@ -55,7 +54,8 @@ namespace Athenasub {
static Format GetFormatByIndex(const int index);
static Format GetFormatFromFilename(const String &filename,bool read);
static Format GetFormatFromName(const String &name);
static std::vector<Format> GetCompatibleFormatList(Reader &reader);
static std::vector<Format> GetCompatibleFormatList(Reader reader);
static std::vector<Format> GetCompatibleFormatList(const String &filename,const String &encoding="");
};

}
20 changes: 6 additions & 14 deletions athenasub/src/formats/format_ass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ StringArray FormatASS2::GetWriteExtensions() const

//////////////////////////////////
// Check if it can read this file
float FormatASSFamily::CanReadFile(Reader &reader) const
float FormatASSFamily::CanReadFile(Reader &file) const
{
shared_ptr<TextReader> file = reader.GetTextReader();

// Check header
if (!file->HasMoreLines()) return 0.0f;
String line = file->ReadLineFromFile();
Expand All @@ -106,7 +104,7 @@ float FormatASSFamily::CanReadFile(Reader &reader) const
float version = 0.0f;
float sections = 0.25f;
String section = line;
while (file->HasMoreLines()) {
for (int i=0; i < 100 && file->HasMoreLines(); i++) {
// Get line
line = file->ReadLineFromFile();
line.AsciiMakeLower();
Expand All @@ -116,7 +114,7 @@ float FormatASSFamily::CanReadFile(Reader &reader) const

// Check version
if (section == "[script info]") {
if (line.StartsWith("ScriptType")) {
if (line.StartsWith("scripttype")) {
int formatVersion = GetVersion();
String expected = "";
switch (formatVersion) {
Expand Down Expand Up @@ -158,11 +156,8 @@ FormatHandlerASS::~FormatHandlerASS()

///////////////
// Load a file
void FormatHandlerASS::Load(IModel &model,Reader &file)
void FormatHandlerASS::Load(IModel &model,Reader reader)
{
// Get text file reader
shared_ptr<TextReader> reader = file.GetTextReader();

// Variables
int version = 1;
String curGroup = "-";
Expand Down Expand Up @@ -201,11 +196,8 @@ void FormatHandlerASS::Load(IModel &model,Reader &file)

/////////////////////
// Save file to disc
void FormatHandlerASS::Save(const IModel& model,Writer &file) const
void FormatHandlerASS::Save(const IModel& model,Writer writer) const
{
// Make text file writer
shared_ptr<TextWriter> writer = file.GetTextWriter();

// Set up list of sections to write
StringArray sections;
sections.push_back("Script Info");
Expand Down Expand Up @@ -389,7 +381,7 @@ void FormatHandlerASS::ProcessGroup(String cur,String &curGroup,int &version) {

///////////////////////////////
// Write a section to the file
void FormatHandlerASS::WriteSection(shared_ptr<TextWriter> writer,ConstSection section) const
void FormatHandlerASS::WriteSection(Writer writer,ConstSection section) const
{
// Write name
String name = section->GetName();
Expand Down
6 changes: 3 additions & 3 deletions athenasub/src/formats/format_ass.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ namespace Athenasub {

Entry MakeEntry(const String &data,Section section,int version);
void ProcessGroup(String cur,String &curGroup,int &version);
void WriteSection(shared_ptr<TextWriter> writer,ConstSection section) const;
void WriteSection(Writer writer,ConstSection section) const;
void MakeValid(IModel &model);

public:
FormatHandlerASS(int version);
~FormatHandlerASS();

void Load(IModel &model,Reader &file);
void Save(const IModel &model,Writer &file) const;
void Load(IModel &model,Reader file);
void Save(const IModel &model,Writer file) const;
};

// Advanced Substation Alpha format base class
Expand Down
2 changes: 1 addition & 1 deletion athenasub/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void CModel::Load(Reader &input,const Format _format)

//////////////////
// Save subtitles
void CModel::Save(Writer &output,const Format _format) const
void CModel::Save(Writer output,const Format _format) const
{
// Use another format
if (_format && _format != format) {
Expand Down
2 changes: 1 addition & 1 deletion athenasub/src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace Athenasub {
Format GetFormat() const { return format; }

void AddListener(View listener);
void Save(Writer &output,Format format=Format()) const;
void Save(Writer output,Format format=Format()) const;

ConstSection GetSection(String name) const;
ConstSection GetSectionByIndex(size_t index) const;
Expand Down
30 changes: 20 additions & 10 deletions athenasub/src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,40 @@
using namespace Athenasub;


Reader::Reader(String fn,String encoding)
CReader::CReader(String fn,String encoding)
: filename(fn)
{
stream = shared_ptr<wxFFileInputStream>(new wxFFileInputStream(filename.GetWxString()));
text = TextReader::GetReader(*stream,encoding);
}

shared_ptr<TextReader> Athenasub::Reader::GetTextReader()
{
return text;
}

Athenasub::String Athenasub::Reader::GetFileName()
String CReader::GetFileName()
{
return filename;
}

Athenasub::Reader::~Reader()
CReader::~CReader()
{
text = shared_ptr<TextReader>();
stream = shared_ptr<wxFFileInputStream>();
}

void Reader::Rewind()
void CReader::Rewind()
{
text->Rewind();
}
}

bool CReader::HasMoreLines()
{
return text->HasMoreLines();
}

String CReader::ReadLineFromFile()
{
return text->ReadLineFromFile();
}

String CReader::GetCurrentEncoding()
{
THROW_ATHENA_EXCEPTION(Exception::TODO);
}
Loading

0 comments on commit 5797473

Please sign in to comment.