Fixed file/format refactoring.

Originally committed to SVN as r2473.
This commit is contained in:
Rodrigo Braz Monteiro 2008-11-22 00:14:39 +00:00
parent 248dbddfdf
commit 2a0f9b7447
10 changed files with 70 additions and 7 deletions

View File

@ -108,5 +108,10 @@ std::string Exception::GetExceptionMessage(int code,String message,const char *f
Stack stack(str);
stack.Walk(2);
// Append extra message
if (!message.IsEmpty()) {
str = str + "\nExtra message: " + message;
}
return str;
}

View File

@ -38,6 +38,7 @@
#include "reader.h"
#include "text_reader.h"
#include <wx/string.h>
#include <algorithm>
using namespace Athenasub;
@ -48,8 +49,15 @@ std::vector<Format> FormatManager::formats;
////////////////
// Add a format
void FormatManager::AddFormat(const Format format)
void FormatManager::AddFormat(Format format)
{
// Abort if there is already a format with this name
String name = format->GetName();
for (size_t i=0;i<formats.size();i++) {
if (formats[i]->GetName() == name) return;
}
// Add
formats.push_back(format);
}
@ -131,8 +139,22 @@ std::vector<Format> FormatManager::GetCompatibleFormatList(Reader &reader)
std::vector<std::pair<float,Format> > results;
size_t len = formats.size();
for (size_t i=0;i<len;i++) {
// Reset reader
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)) {
certainty *= 2.0f;
break;
}
}
// If it thinks that it can read the format, add to list.
if (certainty > 0.0f) {
results.push_back(std::pair<float,Format>(certainty,formats[i]));
}
@ -152,5 +174,8 @@ std::vector<Format> FormatManager::GetCompatibleFormatList(Reader &reader)
for (size_t i=0;i<len;i++) {
finalResults.push_back(results[i].second);
}
// Reset reader again and return results
reader.Rewind();
return finalResults;
}

View File

@ -199,6 +199,8 @@ void FormatHandlerASS::Save(const IModel& model,Writer &file) const
WriteSection(writer,section);
}
}
writer->Flush();
}

View File

@ -40,7 +40,8 @@
using namespace Athenasub;
Reader::Reader(String filename,String encoding)
Reader::Reader(String fn,String encoding)
: filename(fn)
{
stream = shared_ptr<wxFFileInputStream>(new wxFFileInputStream(filename.GetWxString()));
text = TextReader::GetReader(*stream,encoding);
@ -51,6 +52,17 @@ shared_ptr<TextReader> Athenasub::Reader::GetTextReader()
return text;
}
Athenasub::String Athenasub::Reader::GetFileName()
{
return filename;
}
Athenasub::Reader::~Reader()
{
text = shared_ptr<TextReader>();
stream = shared_ptr<wxFFileInputStream>();
}
void Reader::Rewind()
{
text->Rewind();

View File

@ -52,6 +52,7 @@ namespace Athenasub {
public:
Reader(String filename,String encoding="");
~Reader();
shared_ptr<TextReader> GetTextReader();
String GetFileName();

View File

@ -58,8 +58,8 @@ TextFileWriter::TextFileWriter(wxOutputStream &stream,String enc)
//////////////
// Destructor
TextFileWriter::~TextFileWriter() {
// Flush
if (bufferPos) file.Write(&buffer[0],(std::streamsize)bufferPos);
Flush();
file.Close();
}
@ -96,9 +96,7 @@ void TextFileWriter::WriteLineToFile(String line,bool addLineBreak) {
// Resize buffer if it won't fit
if (buffer.size() < bufferPos+len) {
// Flush
file.Write(&buffer[0],(std::streamsize)bufferPos);
bufferPos = 0;
Flush();
// Resize if it still doesn't fit
if (buffer.size() < len) buffer.resize(len);
@ -143,3 +141,13 @@ void TextFileWriter::SetEncoding(String enc) {
}
}
}
/////////
// Flush
void TextFileWriter::Flush() {
if (bufferPos) {
file.Write(&buffer[0],(std::streamsize)bufferPos);
bufferPos = 0;
}
}

View File

@ -60,5 +60,6 @@ namespace Athenasub {
~TextFileWriter();
void WriteLineToFile(Athenasub::String line,bool addLineBreak=true);
void Flush();
};
}

View File

@ -47,6 +47,7 @@ namespace Athenasub {
virtual ~TextWriter() {}
virtual void WriteLineToFile(String line,bool addLineBreak=true) = 0;
virtual void Flush() = 0;
static shared_ptr<TextWriter> GetWriter(wxOutputStream &stream,String encoding);
};

View File

@ -47,6 +47,13 @@ Writer::Writer(String filename,String encoding)
}
Writer::~Writer()
{
text = shared_ptr<TextWriter>();
stream = shared_ptr<wxFFileOutputStream>();
}
shared_ptr<TextWriter> Writer::GetTextWriter()
{
return text;

View File

@ -52,6 +52,7 @@ namespace Athenasub {
public:
Writer(String filename,String encoding="");
~Writer();
shared_ptr<TextWriter> GetTextWriter();
};