00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __loaded__data_io_h__
00021 #define __loaded__data_io_h__
00022 using namespace std;
00023 #line 1 "data-io.h++"
00024 using namespace std;
00025 #include "data.h"
00026 #include "data-visitor.h"
00027
00028
00029
00030
00031 class DataIo: public DataVisitor
00032 {
00033 protected:
00034 FILE * text;
00035 bool opened_file_myself;
00036 int version;
00037 public:
00038
00039 DataIo(QString fn, const char* mode);
00040 DataIo(FILE *f);
00041
00042 virtual ~DataIo();
00043
00044 Data read();
00045 virtual void write(Data data) = 0;
00046 void visit(Data & data);
00047 virtual void start_writing();
00048 virtual void start_reading();
00049 protected:
00050 virtual void read_into(Data & target) = 0;
00051 virtual void write_fileformat_versionnr() =0;
00052 virtual int read_fileformat_versionnr() =0;
00053 };
00054
00055
00056
00057
00058 class DataTexter: public DataIo
00059 {
00060 private:
00061 bool newline;
00062 int lines;
00063 int written;
00064 int margin;
00065 private:
00066 int get_pos()
00067 {
00068 return written;
00069 };
00070 int get_line()
00071 {
00072 return lines;
00073 };
00074 bool newline_waiting()
00075 {
00076 return newline;
00077 };
00078 int get_margin()
00079 {
00080 return margin;
00081 };
00082 void set_margin(int p) { margin = p;};
00083 void set_margin() { set_margin(get_pos()); };
00084 void padded(const char* text);
00085 void padded(QString c);
00086 int push(QString d);
00087 int push(const char * d);
00088 int push(const char d);
00089 void pop(int w, const char* d);
00090 void pop(int w, const char d);
00091 virtual void visitUnsignedNumber(unsigned long long a, char sig);
00092 virtual void visitSignedNumber(signed long long a, char sig);
00093 protected:
00094 virtual void write_fileformat_versionnr();
00099 virtual int read_fileformat_versionnr();
00100 protected:
00101 virtual void visit(Symbol & symbol);
00102 virtual void visit(Token & token);
00103 virtual void visit(Float4 & f4);
00104 virtual void visit(Float8 & f4);
00105
00106 #define ARRAY_TYPE(D,T) \
00107 virtual void visit(Array<D,T> &array) {visitArray(array);};
00108 ARRAY_TYPES;
00109 #undef ARRAY_TYPE
00110 template <int D, class T> void visitArray(Array<D,T> & array);
00111 template <class T> void dumpSequence(Array<1,T> &array);
00112 template <class T> void dumpSequence(Array<2,T> &array);
00113 template <class T> void dumpSequence(Array<3,T> &array);
00114 template <class T> void dumpSequence(Array<4,T> &array);
00115 template <class T> void dumpSequence(Array<5,T> &array);
00116 template <class T> void dumpSequence(Array<6,T> &array);
00117 template <class T> void dumpSequence(Array<7,T> &array);
00118 template <class T> void dumpSequence(Array<8,T> &array);
00119 virtual void visit(String & str);
00120 public:
00121
00122 DataTexter(QString fn, const char* mode);
00123 DataTexter(FILE * f);
00124
00125 virtual void read_into(Data & target);
00126 virtual void write(Data data);
00127
00128 static Data read_file(QString filename);
00129 static Data read_file(FILE * file);
00130 static void write(Data data, QString filename);
00131 static void write(Data data, FILE *target);
00132 };
00133
00134
00135
00136
00137 class DataBinner: public DataIo
00138 {
00139 private:
00140 static const int type_data = 0;
00141 static const int type_array = 1;
00142 static const int type_token = 2;
00143 static const int type_symbol = 3;
00144 static const int type_string = 4;
00145 static const int type_signed1 = 5;
00146 static const int type_signed2 = 6;
00147 static const int type_signed4 = 7;
00148 static const int type_signed8 = 8;
00149 static const int type_unsigned1 = 9;
00150 static const int type_unsigned2 = 10;
00151 static const int type_unsigned4 = 11;
00152 static const int type_unsigned8 = 12;
00153 static const int type_float4 = 13;
00154 static const int type_float8 = 14;
00155
00156 QString file_in_use;
00157 char * mapped_region;
00158 char * cur_ptr;
00159 unsigned4 mapped_size;
00160 unsigned4 file_size;
00161 void mmap_textfile();
00162 bool munmap_textfile();
00163 void write_internal(const char* ptr, int size);
00164 void read_internal(char* ptr, int size);
00165 protected:
00166 virtual void write_fileformat_versionnr();
00167 virtual int read_fileformat_versionnr();
00168 virtual void read_into(Data & target);
00169 public:
00170 DataBinner(QString fn, const char* mode);
00171 DataBinner(FILE *f);
00172 virtual ~DataBinner();
00173
00174 virtual void start_reading();
00175 virtual void write(Data);
00176 static Data read_file(QString filename);
00177 public:
00178 static bool write(Data data, QString filename);
00179 static bool write(Data data, FILE *target);
00180
00181
00182 #define ARRAY_TYPE(D,T) \
00183 virtual void visit(Array<D,T> &array) {visitArray(array);};
00184 ARRAY_TYPES;
00185 #undef ARRAY_TYPE
00186 template <int D, class T> void visitArray(Array<D,T> & array);
00187 virtual void visit(Token & token);
00188 virtual void visit(Symbol & symbol);
00189 virtual void visit(String & str);
00190 virtual void visit(Signed1 &nr);
00191 virtual void visit(Signed2 &nr);
00192 virtual void visit(Signed4 &nr);
00193 virtual void visit(Signed8 &nr);
00194 virtual void visit(Unsigned1 &nr);
00195 virtual void visit(Unsigned2 &nr);
00196 virtual void visit(Unsigned4 &nr);
00197 virtual void visit(Unsigned8 &nr);
00198 virtual void visit(Float4 &nr);
00199 virtual void visit(Float8 &nr);
00200 private:
00201 void write_typenr(int type);
00202 int read_typenr();
00203
00204 String read_string();
00205 void write_string(String s);
00206
00207 Symbol read_symbol();
00208 void write_symbol(Symbol s);
00209
00210 Signed8 read_signed8();
00211 Signed4 read_signed4();
00212 Signed2 read_signed2();
00213 Signed1 read_signed1();
00214 Unsigned8 read_unsigned8();
00215 Unsigned4 read_unsigned4();
00216 Unsigned2 read_unsigned2();
00217 Unsigned1 read_unsigned1();
00218 Float4 read_float4();
00219 Float8 read_float8();
00220
00221
00222 Token read_token();
00223 void write_internal(Data data);
00224 void write_internal(String data);
00225 void write_internal(signed1 s);
00226 void write_internal(signed2 s);
00227 void write_internal(signed4 s);
00228 void write_internal(signed8 s);
00229 void write_internal(unsigned1 u);
00230 void write_internal(unsigned2 u);
00231 void write_internal(unsigned4 u);
00232 void write_internal(unsigned8 u);
00233 void write_internal(float4 s);
00234 void write_internal(float8 s);
00235
00236 void write_internal(QString s);
00237 QString read_internal_qstring();
00238 Data read_array();
00239 template <int D, class T> Data read_array_internal();
00240 void read_internal(Data &data);
00241 void read_internal(String &data);
00242 void read_internal(signed1 &s);
00243 void read_internal(signed2 &s);
00244 void read_internal(signed4 &s);
00245 void read_internal(signed8 &s);
00246 void read_internal(unsigned1 &u);
00247 void read_internal(unsigned2 &u);
00248 void read_internal(unsigned4 &u);
00249 void read_internal(unsigned8 &u);
00250 void read_internal(float4 &s);
00251 void read_internal(float8 &s);
00252
00253
00254 void write_typenr_of(Data s);
00255 void write_typenr_of(signed1 s);
00256 void write_typenr_of(signed2 s);
00257 void write_typenr_of(signed4 s);
00258 void write_typenr_of(signed8 s);
00259 void write_typenr_of(unsigned1 u);
00260 void write_typenr_of(unsigned2 u);
00261 void write_typenr_of(unsigned4 u);
00262 void write_typenr_of(unsigned8 u);
00263 void write_typenr_of(float4 s);
00264 void write_typenr_of(float8 s);
00265 };
00266 #endif // __loaded__data_io_h__