Added raw support (no leb128) to io for backward compatiblity.

This commit is contained in:
Cotton Seed 2012-07-28 12:55:46 -04:00
parent 5f4dfe8cf1
commit a0e543e4df
2 changed files with 154 additions and 91 deletions

View File

@ -4,6 +4,10 @@
void void
writer::write_int (int x) writer::write_int (int x)
{ {
if (raw)
write_raw (&x, sizeof x, 1);
else
{
uint8 buf[5]; uint8 buf[5];
unsigned n = 0; unsigned n = 0;
@ -27,11 +31,16 @@ writer::write_int (int x)
} }
write_raw (buf, sizeof buf[0], n); write_raw (buf, sizeof buf[0], n);
}
} }
void void
writer::write_unsigned (unsigned x) writer::write_unsigned (unsigned x)
{ {
if (raw)
write_raw (&x, sizeof x, 1);
else
{
uint8 buf[5]; uint8 buf[5];
unsigned n = 0; unsigned n = 0;
@ -53,11 +62,16 @@ writer::write_unsigned (unsigned x)
} }
write_raw (buf, sizeof buf[0], n); write_raw (buf, sizeof buf[0], n);
}
} }
void void
writer::write_uint64 (uint64 x) writer::write_uint64 (uint64 x)
{ {
if (raw)
write_raw (&x, sizeof x, 1);
else
{
uint8 buf[10]; uint8 buf[10];
unsigned n = 0; unsigned n = 0;
@ -79,11 +93,14 @@ writer::write_uint64 (uint64 x)
} }
write_raw (buf, sizeof buf[0], n); write_raw (buf, sizeof buf[0], n);
}
} }
void void
writer::write_mpz (const mpz_t x) writer::write_mpz (const mpz_t x)
{ {
assert (!raw);
size_t count; size_t count;
void *buf = mpz_export (nullptr, &count, -1, 1, -1, 0, x); void *buf = mpz_export (nullptr, &count, -1, 1, -1, 0, x);
@ -97,6 +114,11 @@ int
reader::read_int () reader::read_int ()
{ {
int x = 0; int x = 0;
if (raw)
read_raw (&x, sizeof x, 1);
else
{
int shift = 0; int shift = 0;
for (;;) for (;;)
{ {
@ -111,6 +133,8 @@ reader::read_int ()
break; break;
} }
} }
}
return x; return x;
} }
@ -118,6 +142,11 @@ unsigned
reader::read_unsigned () reader::read_unsigned ()
{ {
unsigned x = 0; unsigned x = 0;
if (raw)
read_raw (&x, sizeof x, 1);
else
{
unsigned shift = 0; unsigned shift = 0;
for (;;) for (;;)
{ {
@ -127,6 +156,8 @@ reader::read_unsigned ()
if (! (b & 0x80)) if (! (b & 0x80))
break; break;
} }
}
return x; return x;
} }
@ -134,6 +165,11 @@ uint64
reader::read_uint64 () reader::read_uint64 ()
{ {
uint64 x = 0; uint64 x = 0;
if (raw)
read_raw (&x, sizeof x, 1);
else
{
uint64 shift = 0; uint64 shift = 0;
for (;;) for (;;)
{ {
@ -143,12 +179,16 @@ reader::read_uint64 ()
if (! (b & 0x80)) if (! (b & 0x80))
break; break;
} }
}
return x; return x;
} }
void void
reader::read_mpz (mpz_t x) reader::read_mpz (mpz_t x)
{ {
assert (!raw);
unsigned count = read_unsigned (); unsigned count = read_unsigned ();
void *p = malloc (count); void *p = malloc (count);
if (!p) if (!p)
@ -212,6 +252,12 @@ file_writer::write_raw (const void *p, size_t itemsize, size_t nitems)
} }
} }
void
file_writer::write_mpz (const mpz_t x)
{
mpz_out_raw (fp, x);
}
void void
file_reader::read_raw (void *p, size_t itemsize, size_t nitems) file_reader::read_raw (void *p, size_t itemsize, size_t nitems)
{ {
@ -222,6 +268,12 @@ file_reader::read_raw (void *p, size_t itemsize, size_t nitems)
} }
} }
void
file_reader::read_mpz (mpz_t x)
{
mpz_inp_raw (x, fp);
}
void void
gzfile_writer::write_raw (const void *p, size_t itemsize, size_t nitems) gzfile_writer::write_raw (const void *p, size_t itemsize, size_t nitems)
{ {

View File

@ -1,9 +1,12 @@
class writer class writer
{ {
// don't use leb128 enconding. for backward compatibility.
bool raw;
public: public:
writer (const writer &) = delete; writer (const writer &) = delete;
writer () = default; writer (bool raw_ = false) : raw(raw_) { }
virtual ~writer () = default; virtual ~writer () = default;
writer &operator = (const writer &) = delete; writer &operator = (const writer &) = delete;
@ -16,14 +19,16 @@ class writer
void write_int (int x); void write_int (int x);
void write_unsigned (unsigned x); void write_unsigned (unsigned x);
void write_uint64 (uint64 x); void write_uint64 (uint64 x);
void write_mpz (const mpz_t x); virtual void write_mpz (const mpz_t x);
}; };
class reader class reader
{ {
bool raw;
public: public:
reader (const reader &) = delete; reader (const reader &) = delete;
reader () = default; reader (bool raw_) : raw(raw_) { }
virtual ~reader () = default; virtual ~reader () = default;
reader &operator = (const reader &) = delete; reader &operator = (const reader &) = delete;
@ -54,7 +59,7 @@ class reader
int read_int (); int read_int ();
unsigned read_unsigned (); unsigned read_unsigned ();
uint64 read_uint64 (); uint64 read_uint64 ();
void read_mpz (mpz_t x); virtual void read_mpz (mpz_t x);
}; };
extern FILE *open_file (const std::string &file, const char *mode); extern FILE *open_file (const std::string &file, const char *mode);
@ -69,8 +74,9 @@ class file_writer : public writer
FILE *fp; FILE *fp;
public: public:
file_writer (const std::string &file) file_writer (const std::string &file, bool raw = false)
: fp(open_file (file, "w")) : writer(raw),
fp(open_file (file, "w"))
{ {
} }
file_writer (const file_writer &) = delete; file_writer (const file_writer &) = delete;
@ -79,6 +85,7 @@ class file_writer : public writer
file_writer &operator = (const file_writer &) = delete; file_writer &operator = (const file_writer &) = delete;
void write_raw (const void *p, size_t itemsize, size_t nitems); void write_raw (const void *p, size_t itemsize, size_t nitems);
void write_mpz (const mpz_t x);
}; };
class file_reader : public reader class file_reader : public reader
@ -87,8 +94,9 @@ class file_reader : public reader
FILE *fp; FILE *fp;
public: public:
file_reader (const std::string &file) file_reader (const std::string &file, bool raw = false)
: fp(open_file (file, "r")) : reader(raw),
fp(open_file (file, "r"))
{ {
} }
file_reader (const file_reader &) = delete; file_reader (const file_reader &) = delete;
@ -97,6 +105,7 @@ class file_reader : public reader
file_reader &operator = (const file_reader &) = delete; file_reader &operator = (const file_reader &) = delete;
void read_raw (void *p, size_t itemsize, size_t nitems); void read_raw (void *p, size_t itemsize, size_t nitems);
void read_mpz (mpz_t x);
}; };
class gzfile_writer : public writer class gzfile_writer : public writer
@ -106,7 +115,8 @@ class gzfile_writer : public writer
public: public:
gzfile_writer (const std::string &file) gzfile_writer (const std::string &file)
: gzfp(open_gzfile (file, "w9")) : writer(false),
gzfp(open_gzfile (file, "w9"))
{ {
} }
gzfile_writer (const gzfile_writer &) = delete; gzfile_writer (const gzfile_writer &) = delete;
@ -124,7 +134,8 @@ class gzfile_reader : public reader
public: public:
gzfile_reader (const std::string &file) gzfile_reader (const std::string &file)
: gzfp(open_gzfile (file, "r")) : reader(false),
gzfp(open_gzfile (file, "r"))
{ {
} }
gzfile_reader (const gzfile_reader &) = delete; gzfile_reader (const gzfile_reader &) = delete;