2011-12-09 21:50:25 +01:00
|
|
|
|
2012-05-03 21:27:05 +02:00
|
|
|
template<class R> class module;
|
|
|
|
|
|
|
|
class algebra_writer;
|
|
|
|
class algebra_reader;
|
|
|
|
|
2011-12-09 21:50:25 +01:00
|
|
|
class writer
|
|
|
|
{
|
2012-05-03 21:27:05 +02:00
|
|
|
public:
|
2012-07-28 18:55:46 +02:00
|
|
|
bool raw;
|
2012-05-03 21:27:05 +02:00
|
|
|
algebra_writer *aw;
|
2012-07-28 18:55:46 +02:00
|
|
|
|
2011-12-09 21:50:25 +01:00
|
|
|
public:
|
2012-07-27 21:37:47 +02:00
|
|
|
writer (const writer &) = delete;
|
2012-05-03 21:27:05 +02:00
|
|
|
writer (bool raw_ = false) : raw(raw_), aw(0) { }
|
2012-07-27 21:37:47 +02:00
|
|
|
virtual ~writer () = default;
|
2011-12-09 21:50:25 +01:00
|
|
|
|
2012-07-27 21:37:47 +02:00
|
|
|
writer &operator = (const writer &) = delete;
|
2011-12-09 21:50:25 +01:00
|
|
|
|
2012-07-27 21:37:47 +02:00
|
|
|
virtual void write_raw (const void *p, size_t itemsize, size_t nitems) = 0;
|
2011-12-09 21:50:25 +01:00
|
|
|
|
2012-07-27 21:37:47 +02:00
|
|
|
void write_bool (bool x) { write_raw (&x, sizeof x, 1); }
|
|
|
|
void write_char (char x) { write_raw (&x, sizeof x, 1); }
|
|
|
|
void write_uint8 (uint8 x) { write_raw (&x, sizeof x, 1); }
|
2011-12-09 21:50:25 +01:00
|
|
|
void write_int (int x);
|
|
|
|
void write_unsigned (unsigned x);
|
|
|
|
void write_uint64 (uint64 x);
|
2012-05-03 21:27:05 +02:00
|
|
|
|
|
|
|
template<class R> void write_mod (ptr<const module<R> > m);
|
2011-12-09 21:50:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class reader
|
|
|
|
{
|
2012-07-28 18:55:46 +02:00
|
|
|
bool raw;
|
2012-05-03 21:27:05 +02:00
|
|
|
algebra_reader *ar;
|
2012-07-28 18:55:46 +02:00
|
|
|
|
2011-12-09 21:50:25 +01:00
|
|
|
public:
|
2012-07-27 21:37:47 +02:00
|
|
|
reader (const reader &) = delete;
|
2012-05-03 21:27:05 +02:00
|
|
|
reader (bool raw_) : raw(raw_), ar(0) { }
|
2012-07-27 21:37:47 +02:00
|
|
|
virtual ~reader () = default;
|
|
|
|
|
|
|
|
reader &operator = (const reader &) = delete;
|
|
|
|
|
|
|
|
virtual void read_raw (void *p, size_t itemsize, size_t nitems) = 0;
|
2011-12-09 21:50:25 +01:00
|
|
|
|
2012-07-27 21:37:47 +02:00
|
|
|
bool read_bool ()
|
|
|
|
{
|
|
|
|
bool x;
|
|
|
|
read_raw (&x, sizeof x, 1);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
char read_char ()
|
|
|
|
{
|
|
|
|
char x;
|
|
|
|
read_raw (&x, sizeof x, 1);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 read_uint8 ()
|
|
|
|
{
|
|
|
|
uint8 x;
|
|
|
|
read_raw (&x, sizeof x, 1);
|
|
|
|
return x;
|
|
|
|
}
|
2011-12-09 21:50:25 +01:00
|
|
|
|
|
|
|
int read_int ();
|
|
|
|
unsigned read_unsigned ();
|
|
|
|
uint64 read_uint64 ();
|
2012-07-28 18:55:46 +02:00
|
|
|
virtual void read_mpz (mpz_t x);
|
2012-05-03 21:27:05 +02:00
|
|
|
|
|
|
|
template<class R> ptr<const module<R> > read_mod ();
|
2012-07-27 21:37:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern FILE *open_file (const std::string &file, const char *mode);
|
|
|
|
extern void close_file (FILE *fp);
|
|
|
|
|
|
|
|
extern gzFile open_gzfile (const std::string &file, const char *mode);
|
|
|
|
extern void close_gzfile (gzFile fp);
|
|
|
|
|
|
|
|
class file_writer : public writer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
public:
|
2012-07-28 18:55:46 +02:00
|
|
|
file_writer (const std::string &file, bool raw = false)
|
|
|
|
: writer(raw),
|
|
|
|
fp(open_file (file, "w"))
|
2012-07-27 21:37:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
file_writer (const file_writer &) = delete;
|
|
|
|
~file_writer () { close_file (fp); }
|
|
|
|
|
|
|
|
file_writer &operator = (const file_writer &) = delete;
|
|
|
|
|
|
|
|
void write_raw (const void *p, size_t itemsize, size_t nitems);
|
2012-07-28 18:55:46 +02:00
|
|
|
void write_mpz (const mpz_t x);
|
2012-07-27 21:37:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class file_reader : public reader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
public:
|
2012-07-28 18:55:46 +02:00
|
|
|
file_reader (const std::string &file, bool raw = false)
|
|
|
|
: reader(raw),
|
|
|
|
fp(open_file (file, "r"))
|
2012-07-27 21:37:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
file_reader (const file_reader &) = delete;
|
|
|
|
~file_reader () { close_file (fp); }
|
|
|
|
|
|
|
|
file_reader &operator = (const file_reader &) = delete;
|
|
|
|
|
|
|
|
void read_raw (void *p, size_t itemsize, size_t nitems);
|
2012-07-28 18:55:46 +02:00
|
|
|
void read_mpz (mpz_t x);
|
2012-07-27 21:37:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class gzfile_writer : public writer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
gzFile gzfp;
|
|
|
|
|
|
|
|
public:
|
|
|
|
gzfile_writer (const std::string &file)
|
2012-07-28 18:55:46 +02:00
|
|
|
: writer(false),
|
|
|
|
gzfp(open_gzfile (file, "w9"))
|
2012-07-27 21:37:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
gzfile_writer (const gzfile_writer &) = delete;
|
|
|
|
~gzfile_writer () { close_gzfile (gzfp); }
|
|
|
|
|
|
|
|
gzfile_writer &operator = (const gzfile_writer &) = delete;
|
|
|
|
|
|
|
|
void write_raw (const void *p, size_t itemsize, size_t nitems);
|
|
|
|
};
|
|
|
|
|
|
|
|
class gzfile_reader : public reader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
gzFile gzfp;
|
|
|
|
|
|
|
|
public:
|
|
|
|
gzfile_reader (const std::string &file)
|
2012-07-28 18:55:46 +02:00
|
|
|
: reader(false),
|
|
|
|
gzfp(open_gzfile (file, "r"))
|
2012-07-27 21:37:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
gzfile_reader (const gzfile_reader &) = delete;
|
|
|
|
~gzfile_reader () { close_gzfile (gzfp); }
|
|
|
|
|
|
|
|
gzfile_reader &operator = (const gzfile_reader &) = delete;
|
|
|
|
|
|
|
|
void read_raw (void *p, size_t itemsize, size_t nitems);
|
2011-12-09 21:50:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline void read (reader &r, bool &x) { x = r.read_bool (); }
|
|
|
|
inline void read (reader &r, char &x) { x = r.read_char (); }
|
|
|
|
inline void read (reader &r, int &x) { x = r.read_int (); }
|
|
|
|
inline void read (reader &r, unsigned &x) { x = r.read_unsigned (); }
|
|
|
|
inline void read (reader &r, uint64 &x) { x = r.read_uint64 (); }
|
2012-07-27 21:37:47 +02:00
|
|
|
extern void read (reader &r, std::string &s);
|
2011-12-09 21:50:25 +01:00
|
|
|
template<class T> inline void read (reader &r, T &x) { x = T(r); }
|
|
|
|
|
|
|
|
inline void ctor_read (reader &r, bool *p) { *p = r.read_bool (); }
|
|
|
|
inline void ctor_read (reader &r, char *p) { *p = r.read_char (); }
|
|
|
|
inline void ctor_read (reader &r, int *p) { *p = r.read_int (); }
|
|
|
|
inline void ctor_read (reader &r, unsigned *p) { *p = r.read_unsigned (); }
|
|
|
|
inline void ctor_read (reader &r, uint64 *p) { *p = r.read_uint64 (); }
|
|
|
|
template<class T> inline void ctor_read (reader &r, T *p) { new (p) T (r); }
|
|
|
|
|
|
|
|
inline void write (writer &w, bool x) { w.write_bool (x); }
|
|
|
|
inline void write (writer &w, char x) { w.write_char (x); }
|
|
|
|
inline void write (writer &w, int x) { w.write_int (x); }
|
|
|
|
inline void write (writer &w, unsigned x) { w.write_unsigned (x); }
|
|
|
|
inline void write (writer &w, uint64 x) { w.write_uint64 (x); }
|
2012-07-27 21:37:47 +02:00
|
|
|
extern void write (writer &w, const std::string &s);
|
2011-12-09 21:50:25 +01:00
|
|
|
template<class T> inline void write (writer &w, const T &x) { x.write_self (w); }
|