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
TextFileReader::TextFileReader(wxInputStream &stream,Gorgonsub::String enc,bool _trim)
TextFileReader::TextFileReader(wxInputStream &stream,Gorgonsub::String enc,bool _trim,bool prefetch)
: file(stream)
{
// Setup
trim = _trim;
threaded = prefetch;
thread = NULL;
// Set encoding
encoding = enc.c_str();
@ -64,13 +66,15 @@ TextFileReader::TextFileReader(wxInputStream &stream,Gorgonsub::String enc,bool
//////////////
// Destructor
TextFileReader::~TextFileReader() {
TextFileReader::~TextFileReader()
{
}
//////////////////////////////
// Set encoding configuration
void TextFileReader::SetEncodingConfiguration() {
void TextFileReader::SetEncodingConfiguration()
{
// Set encoding configuration
swap = false;
Is16 = false;
@ -99,7 +103,8 @@ void TextFileReader::SetEncodingConfiguration() {
//////////////////////////
// Reads a line from file
Gorgonsub::String TextFileReader::ReadLineFromFile() {
Gorgonsub::String TextFileReader::ActuallyReadLine()
{
wxString wxbuffer;
size_t bufAlloc = 1024;
wxbuffer.Alloc(bufAlloc);
@ -175,14 +180,17 @@ Gorgonsub::String TextFileReader::ReadLineFromFile() {
//////////////////////////////////
// Checks if there's more to read
bool TextFileReader::HasMoreLines() {
bool TextFileReader::HasMoreLines()
{
wxCriticalSectionLocker locker(mutex);
return (!file.Eof());
}
////////////////////////////////
// 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")) {
wxString error = _T("Character set ");
error += enc;
@ -194,6 +202,72 @@ void TextFileReader::EnsureValid(Gorgonsub::String enc) {
///////////////////////////
// Get encoding being used
Gorgonsub::String TextFileReader::GetCurrentEncoding() {
String TextFileReader::GetCurrentEncoding()
{
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 {
// Text file reader
class TextFileReader {
friend class PrefetchThread;
private:
wxCriticalSection mutex;
std::list<String> cache;
wxString encoding;
wxInputStream &file;
shared_ptr<wxMBConv> conv;
bool Is16;
bool swap;
bool trim;
bool threaded;
wxThread *thread;
void SetEncodingConfiguration();
String ActuallyReadLine();
public:
TextFileReader(wxInputStream &stream,String encoding=_T(""),bool trim=true);
TextFileReader(wxInputStream &stream,String encoding=_T(""),bool trim=true,bool prefetch=false);
~TextFileReader();
String ReadLineFromFile();
@ -65,5 +74,16 @@ namespace Gorgonsub {
String GetCurrentEncoding();
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");
timer.Pause();
cout << "Done in " << timer.Time() << "ms.\n";
system("pause");
// Create line to be inserted
cout << "Creating data... ";