Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef PGNDATABASE_H
00025 #define PGNDATABASE_H
00026
00027 #ifndef USE_PCH
00028 #include <string>
00029 #include <glibmm/refptr.h>
00030 #include <glibmm/dispatcher.h>
00031 #include <giomm/file.h>
00032 #endif
00033
00034 #include "Referenceable.h"
00035 #include "MemoryBlockList.h"
00036
00037 namespace cwchess {
00038 namespace pgn {
00039
00040 using util::MemoryBlockList;
00041 using util::MemoryBlockNode;
00042
00043 class Database : public util::Referenceable {
00044
00045 enum state_type {
00046 white_space,
00047 string_token,
00048 comment_token
00049 };
00050
00051 private:
00052 bool M_saw_carriage_return;
00053 size_t M_line_wrapped;
00054 int M_number_of_lines;
00055 size_t M_number_of_characters;
00056 state_type M_state;
00057 unsigned char const* M_search_table;
00058 unsigned char const* M_search_table_storeR;
00059 static unsigned char* S_state_tables[11];
00060
00061 protected:
00062 MemoryBlockList* M_buffer;
00063 Glib::RefPtr<MemoryBlockNode> M_new_block;
00064
00065 Database(void) : M_buffer(NULL), M_saw_carriage_return(false), M_line_wrapped(0),
00066 M_number_of_lines(0), M_number_of_characters(0), M_state(white_space) { }
00067
00068
00069
00070
00071
00072 void process_next_data_block(char const* data, size_t size);
00073 public:
00074
00075 virtual std::string get_path(void) const = 0;
00076
00077 int number_of_lines(void) const { return M_number_of_lines; }
00078 size_t number_of_characters(void) const { return M_number_of_characters; }
00079 };
00080
00081 class DatabaseSeekable : public Database {
00082 public:
00083 typedef sigc::slot<void, size_t> SlotOpenFinished;
00084
00085
00086
00087
00088 static size_t const S_buffer_size = 6 * 4096 - 64;
00089 private:
00090 Glib::RefPtr<Gio::File> M_file;
00091 Glib::RefPtr<Gio::Cancellable> M_cancellable;
00092 gsize M_bytes_read;
00093 Glib::RefPtr<Gio::FileInputStream> M_file_input_stream;
00094 SlotOpenFinished M_slot_open_finished;
00095 Glib::Thread* M_read_thread;
00096 Glib::Dispatcher M_processing_finished;
00097 public:
00098 static Glib::RefPtr<Database> open(std::string const& path, SlotOpenFinished const& slot)
00099 { return Glib::RefPtr<Database>(new DatabaseSeekable(path, slot)); }
00100 protected:
00101 DatabaseSeekable(std::string const& path, SlotOpenFinished const& slot_open_finished) :
00102 M_file(Gio::File::create_for_path(path)), M_cancellable(Gio::Cancellable::create()),
00103 M_bytes_read(0), M_slot_open_finished(slot_open_finished) { load(); }
00104 virtual ~DatabaseSeekable();
00105 private:
00106 void load(void);
00107 void read_async_open_ready(Glib::RefPtr<Gio::AsyncResult>& result);
00108 static void read_async_ready(GObject* source_object, GAsyncResult* async_res, gpointer user_data);
00109 void read_async_ready(GObject* source_object, GAsyncResult* async_res);
00110 void need_more_data(void);
00111 void processing_finished(void);
00112
00113
00114 virtual std::string get_path(void) const { M_file->get_path(); }
00115
00116 private:
00117 void read_thread(void);
00118 };
00119
00120 }
00121 }
00122
00123 #endif // PGNDATABASE_H