Added prefetching code to textfilereader, that presumably doesn't work because I'm working on a console.

Originally committed to SVN as r2055.
This commit is contained in:
Rodrigo Braz Monteiro 2008-03-14 08:00:55 +00:00
parent 78cb8f53ff
commit fd8e6952da
3 changed files with 103 additions and 8 deletions

View file

@ -49,11 +49,13 @@ using namespace Gorgonsub;
/////////////// ///////////////
// Constructor // Constructor
TextFileReader::TextFileReader(wxInputStream &stream,Gorgonsub::String enc,bool _trim) TextFileReader::TextFileReader(wxInputStream &stream,Gorgonsub::String enc,bool _trim,bool prefetch)
: file(stream) : file(stream)
{ {
// Setup // Setup
trim = _trim; trim = _trim;
threaded = prefetch;
thread = NULL;
// Set encoding // Set encoding
encoding = enc.c_str(); encoding = enc.c_str();
@ -64,13 +66,15 @@ TextFileReader::TextFileReader(wxInputStream &stream,Gorgonsub::String enc,bool
////////////// //////////////
// Destructor // Destructor
TextFileReader::~TextFileReader() { TextFileReader::~TextFileReader()
{
} }
////////////////////////////// //////////////////////////////
// Set encoding configuration // Set encoding configuration
void TextFileReader::SetEncodingConfiguration() { void TextFileReader::SetEncodingConfiguration()
{
// Set encoding configuration // Set encoding configuration
swap = false; swap = false;
Is16 = false; Is16 = false;
@ -99,7 +103,8 @@ void TextFileReader::SetEncodingConfiguration() {
////////////////////////// //////////////////////////
// Reads a line from file // Reads a line from file
Gorgonsub::String TextFileReader::ReadLineFromFile() { Gorgonsub::String TextFileReader::ActuallyReadLine()
{
wxString wxbuffer; wxString wxbuffer;
size_t bufAlloc = 1024; size_t bufAlloc = 1024;
wxbuffer.Alloc(bufAlloc); wxbuffer.Alloc(bufAlloc);
@ -175,14 +180,17 @@ Gorgonsub::String TextFileReader::ReadLineFromFile() {
////////////////////////////////// //////////////////////////////////
// Checks if there's more to read // Checks if there's more to read
bool TextFileReader::HasMoreLines() { bool TextFileReader::HasMoreLines()
{
wxCriticalSectionLocker locker(mutex);
return (!file.Eof()); return (!file.Eof());
} }
//////////////////////////////// ////////////////////////////////
// Ensure that charset is valid // Ensure that charset is valid
void TextFileReader::EnsureValid(Gorgonsub::String enc) { void TextFileReader::EnsureValid(Gorgonsub::String enc)
{
if (enc == _T("unknown") || enc == _T("UTF-32BE") || enc == _T("UTF-32LE")) { if (enc == _T("unknown") || enc == _T("UTF-32BE") || enc == _T("UTF-32LE")) {
wxString error = _T("Character set "); wxString error = _T("Character set ");
error += enc; error += enc;
@ -194,6 +202,72 @@ void TextFileReader::EnsureValid(Gorgonsub::String enc) {
/////////////////////////// ///////////////////////////
// Get encoding being used // Get encoding being used
Gorgonsub::String TextFileReader::GetCurrentEncoding() { String TextFileReader::GetCurrentEncoding()
{
return encoding.c_str(); return encoding.c_str();
} }
///////////////////////////
// Reads a line from cache
String TextFileReader::ReadLineFromFile()
{
// Not threaded, just return it
if (!threaded) return ActuallyReadLine();
// Load into cache if needed
String final;
if (cache.size() == 0) {
wxCriticalSectionLocker locker(mutex);
cache.push_back(ActuallyReadLine());
}
bool startThread = false;
{
// Retrieve from cache
wxCriticalSectionLocker locker(mutex);
if (cache.size()) {
final = cache.front();
cache.pop_front();
}
// Start the thread to prefetch more
if (cache.size() < 3 && thread == NULL) {
thread = new PrefetchThread(this);
startThread = true;
}
}
// Starts the thread
if (startThread) {
thread->Create();
thread->Run();
}
return final;
}
////////////////
// Thread entry
wxThread::ExitCode PrefetchThread::Entry()
{
// Lock
wxCriticalSectionLocker locker(parent->mutex);
while (parent->cache.size() < 3 && !parent->file.Eof()) {
// So I need to do this for whatever reason
if (TestDestroy()) {
parent->thread = NULL;
return 0;
}
// Get line
parent->cache.push_back(parent->ActuallyReadLine());
}
// Die
//wxCriticalSectionLocker locker(parent->mutex);
parent->thread = NULL;
Delete();
return 0;
}

View file

@ -43,19 +43,28 @@
namespace Gorgonsub { namespace Gorgonsub {
// Text file reader
class TextFileReader { class TextFileReader {
friend class PrefetchThread;
private: private:
wxCriticalSection mutex;
std::list<String> cache;
wxString encoding; wxString encoding;
wxInputStream &file; wxInputStream &file;
shared_ptr<wxMBConv> conv; shared_ptr<wxMBConv> conv;
bool Is16; bool Is16;
bool swap; bool swap;
bool trim; bool trim;
bool threaded;
wxThread *thread;
void SetEncodingConfiguration(); void SetEncodingConfiguration();
String ActuallyReadLine();
public: public:
TextFileReader(wxInputStream &stream,String encoding=_T(""),bool trim=true); TextFileReader(wxInputStream &stream,String encoding=_T(""),bool trim=true,bool prefetch=false);
~TextFileReader(); ~TextFileReader();
String ReadLineFromFile(); String ReadLineFromFile();
@ -65,5 +74,16 @@ namespace Gorgonsub {
String GetCurrentEncoding(); String GetCurrentEncoding();
static String GetEncoding(const String filename); static String GetEncoding(const String filename);
}; };
// Prefetch thread
class PrefetchThread : public wxThread {
private:
TextFileReader *parent;
public:
wxThread::ExitCode Entry();
PrefetchThread(TextFileReader *_parent) : parent(_parent) {}
};
} }

View file

@ -62,6 +62,7 @@ int main () {
control.LoadFile(L"subs_in.ass",L"UTF-8"); control.LoadFile(L"subs_in.ass",L"UTF-8");
timer.Pause(); timer.Pause();
cout << "Done in " << timer.Time() << "ms.\n"; cout << "Done in " << timer.Time() << "ms.\n";
system("pause");
// Create line to be inserted // Create line to be inserted
cout << "Creating data... "; cout << "Creating data... ";