Added raw support (no leb128) to io for backward compatiblity.
This commit is contained in:
parent
5f4dfe8cf1
commit
a0e543e4df
214
lib/io.cpp
214
lib/io.cpp
@ -4,86 +4,103 @@
|
||||
void
|
||||
writer::write_int (int x)
|
||||
{
|
||||
uint8 buf[5];
|
||||
unsigned n = 0;
|
||||
|
||||
bool more = 1;
|
||||
while (more)
|
||||
if (raw)
|
||||
write_raw (&x, sizeof x, 1);
|
||||
else
|
||||
{
|
||||
uint8 b = (uint8)(x & 0x7f);
|
||||
x >>= 7;
|
||||
uint8 buf[5];
|
||||
unsigned n = 0;
|
||||
|
||||
if ((x == 0
|
||||
&& ! (b & 0x40))
|
||||
|| (x == -1
|
||||
&& (b & 0x40) == 0x40))
|
||||
more = 0;
|
||||
else
|
||||
b |= 0x80;
|
||||
bool more = 1;
|
||||
while (more)
|
||||
{
|
||||
uint8 b = (uint8)(x & 0x7f);
|
||||
x >>= 7;
|
||||
|
||||
assert (n < 5);
|
||||
buf[n] = b;
|
||||
n ++;
|
||||
if ((x == 0
|
||||
&& ! (b & 0x40))
|
||||
|| (x == -1
|
||||
&& (b & 0x40) == 0x40))
|
||||
more = 0;
|
||||
else
|
||||
b |= 0x80;
|
||||
|
||||
assert (n < 5);
|
||||
buf[n] = b;
|
||||
n ++;
|
||||
}
|
||||
|
||||
write_raw (buf, sizeof buf[0], n);
|
||||
}
|
||||
|
||||
write_raw (buf, sizeof buf[0], n);
|
||||
}
|
||||
|
||||
void
|
||||
writer::write_unsigned (unsigned x)
|
||||
{
|
||||
uint8 buf[5];
|
||||
unsigned n = 0;
|
||||
|
||||
bool more = 1;
|
||||
while (more)
|
||||
if (raw)
|
||||
write_raw (&x, sizeof x, 1);
|
||||
else
|
||||
{
|
||||
uint8 b = (uint8)(x & 0x7f);
|
||||
x >>= 7;
|
||||
|
||||
if ((x == 0
|
||||
&& ! (b & 0x40)))
|
||||
more = 0;
|
||||
else
|
||||
b |= 0x80;
|
||||
|
||||
assert (n < 5);
|
||||
buf[n] = b;
|
||||
n ++;
|
||||
}
|
||||
uint8 buf[5];
|
||||
unsigned n = 0;
|
||||
|
||||
write_raw (buf, sizeof buf[0], n);
|
||||
bool more = 1;
|
||||
while (more)
|
||||
{
|
||||
uint8 b = (uint8)(x & 0x7f);
|
||||
x >>= 7;
|
||||
|
||||
if ((x == 0
|
||||
&& ! (b & 0x40)))
|
||||
more = 0;
|
||||
else
|
||||
b |= 0x80;
|
||||
|
||||
assert (n < 5);
|
||||
buf[n] = b;
|
||||
n ++;
|
||||
}
|
||||
|
||||
write_raw (buf, sizeof buf[0], n);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
writer::write_uint64 (uint64 x)
|
||||
{
|
||||
uint8 buf[10];
|
||||
unsigned n = 0;
|
||||
|
||||
bool more = 1;
|
||||
while (more)
|
||||
if (raw)
|
||||
write_raw (&x, sizeof x, 1);
|
||||
else
|
||||
{
|
||||
uint8 b = (uint8)(x & 0x7f);
|
||||
x >>= 7;
|
||||
|
||||
if ((x == 0
|
||||
&& ! (b & 0x40)))
|
||||
more = 0;
|
||||
else
|
||||
b |= 0x80;
|
||||
|
||||
assert (n < 10);
|
||||
buf[n] = b;
|
||||
n ++;
|
||||
}
|
||||
uint8 buf[10];
|
||||
unsigned n = 0;
|
||||
|
||||
write_raw (buf, sizeof buf[0], n);
|
||||
bool more = 1;
|
||||
while (more)
|
||||
{
|
||||
uint8 b = (uint8)(x & 0x7f);
|
||||
x >>= 7;
|
||||
|
||||
if ((x == 0
|
||||
&& ! (b & 0x40)))
|
||||
more = 0;
|
||||
else
|
||||
b |= 0x80;
|
||||
|
||||
assert (n < 10);
|
||||
buf[n] = b;
|
||||
n ++;
|
||||
}
|
||||
|
||||
write_raw (buf, sizeof buf[0], n);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
writer::write_mpz (const mpz_t x)
|
||||
{
|
||||
assert (!raw);
|
||||
|
||||
size_t count;
|
||||
void *buf = mpz_export (nullptr, &count, -1, 1, -1, 0, x);
|
||||
|
||||
@ -97,20 +114,27 @@ int
|
||||
reader::read_int ()
|
||||
{
|
||||
int x = 0;
|
||||
int shift = 0;
|
||||
for (;;)
|
||||
|
||||
if (raw)
|
||||
read_raw (&x, sizeof x, 1);
|
||||
else
|
||||
{
|
||||
uint8 b = read_uint8 ();
|
||||
x |= ((int)(b & 0x7f) << shift);
|
||||
shift += 7;
|
||||
if (! (b & 0x80))
|
||||
int shift = 0;
|
||||
for (;;)
|
||||
{
|
||||
if (shift < int_bits
|
||||
&& (b & 0x40))
|
||||
x = (x << (int_bits - shift)) >> (int_bits - shift);
|
||||
break;
|
||||
uint8 b = read_uint8 ();
|
||||
x |= ((int)(b & 0x7f) << shift);
|
||||
shift += 7;
|
||||
if (! (b & 0x80))
|
||||
{
|
||||
if (shift < int_bits
|
||||
&& (b & 0x40))
|
||||
x = (x << (int_bits - shift)) >> (int_bits - shift);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
@ -118,15 +142,22 @@ unsigned
|
||||
reader::read_unsigned ()
|
||||
{
|
||||
unsigned x = 0;
|
||||
unsigned shift = 0;
|
||||
for (;;)
|
||||
|
||||
if (raw)
|
||||
read_raw (&x, sizeof x, 1);
|
||||
else
|
||||
{
|
||||
uint8 b = read_uint8 ();
|
||||
x |= ((unsigned)(b & 0x7f) << shift);
|
||||
shift += 7;
|
||||
if (! (b & 0x80))
|
||||
break;
|
||||
unsigned shift = 0;
|
||||
for (;;)
|
||||
{
|
||||
uint8 b = read_uint8 ();
|
||||
x |= ((unsigned)(b & 0x7f) << shift);
|
||||
shift += 7;
|
||||
if (! (b & 0x80))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
@ -134,21 +165,30 @@ uint64
|
||||
reader::read_uint64 ()
|
||||
{
|
||||
uint64 x = 0;
|
||||
uint64 shift = 0;
|
||||
for (;;)
|
||||
|
||||
if (raw)
|
||||
read_raw (&x, sizeof x, 1);
|
||||
else
|
||||
{
|
||||
uint8 b = read_uint8 ();
|
||||
x |= ((uint64)(b & 0x7f) << shift);
|
||||
shift += 7;
|
||||
if (! (b & 0x80))
|
||||
break;
|
||||
uint64 shift = 0;
|
||||
for (;;)
|
||||
{
|
||||
uint8 b = read_uint8 ();
|
||||
x |= ((uint64)(b & 0x7f) << shift);
|
||||
shift += 7;
|
||||
if (! (b & 0x80))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
void
|
||||
reader::read_mpz (mpz_t x)
|
||||
{
|
||||
assert (!raw);
|
||||
|
||||
unsigned count = read_unsigned ();
|
||||
void *p = malloc (count);
|
||||
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
|
||||
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
|
||||
gzfile_writer::write_raw (const void *p, size_t itemsize, size_t nitems)
|
||||
{
|
||||
|
31
lib/io.h
31
lib/io.h
@ -1,9 +1,12 @@
|
||||
|
||||
class writer
|
||||
{
|
||||
// don't use leb128 enconding. for backward compatibility.
|
||||
bool raw;
|
||||
|
||||
public:
|
||||
writer (const writer &) = delete;
|
||||
writer () = default;
|
||||
writer (bool raw_ = false) : raw(raw_) { }
|
||||
virtual ~writer () = default;
|
||||
|
||||
writer &operator = (const writer &) = delete;
|
||||
@ -16,14 +19,16 @@ class writer
|
||||
void write_int (int x);
|
||||
void write_unsigned (unsigned x);
|
||||
void write_uint64 (uint64 x);
|
||||
void write_mpz (const mpz_t x);
|
||||
virtual void write_mpz (const mpz_t x);
|
||||
};
|
||||
|
||||
class reader
|
||||
{
|
||||
bool raw;
|
||||
|
||||
public:
|
||||
reader (const reader &) = delete;
|
||||
reader () = default;
|
||||
reader (bool raw_) : raw(raw_) { }
|
||||
virtual ~reader () = default;
|
||||
|
||||
reader &operator = (const reader &) = delete;
|
||||
@ -54,7 +59,7 @@ class reader
|
||||
int read_int ();
|
||||
unsigned read_unsigned ();
|
||||
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);
|
||||
@ -69,8 +74,9 @@ class file_writer : public writer
|
||||
FILE *fp;
|
||||
|
||||
public:
|
||||
file_writer (const std::string &file)
|
||||
: fp(open_file (file, "w"))
|
||||
file_writer (const std::string &file, bool raw = false)
|
||||
: writer(raw),
|
||||
fp(open_file (file, "w"))
|
||||
{
|
||||
}
|
||||
file_writer (const file_writer &) = delete;
|
||||
@ -79,6 +85,7 @@ class file_writer : public writer
|
||||
file_writer &operator = (const file_writer &) = delete;
|
||||
|
||||
void write_raw (const void *p, size_t itemsize, size_t nitems);
|
||||
void write_mpz (const mpz_t x);
|
||||
};
|
||||
|
||||
class file_reader : public reader
|
||||
@ -87,8 +94,9 @@ class file_reader : public reader
|
||||
FILE *fp;
|
||||
|
||||
public:
|
||||
file_reader (const std::string &file)
|
||||
: fp(open_file (file, "r"))
|
||||
file_reader (const std::string &file, bool raw = false)
|
||||
: reader(raw),
|
||||
fp(open_file (file, "r"))
|
||||
{
|
||||
}
|
||||
file_reader (const file_reader &) = delete;
|
||||
@ -97,6 +105,7 @@ class file_reader : public reader
|
||||
file_reader &operator = (const file_reader &) = delete;
|
||||
|
||||
void read_raw (void *p, size_t itemsize, size_t nitems);
|
||||
void read_mpz (mpz_t x);
|
||||
};
|
||||
|
||||
class gzfile_writer : public writer
|
||||
@ -106,7 +115,8 @@ class gzfile_writer : public writer
|
||||
|
||||
public:
|
||||
gzfile_writer (const std::string &file)
|
||||
: gzfp(open_gzfile (file, "w9"))
|
||||
: writer(false),
|
||||
gzfp(open_gzfile (file, "w9"))
|
||||
{
|
||||
}
|
||||
gzfile_writer (const gzfile_writer &) = delete;
|
||||
@ -124,7 +134,8 @@ class gzfile_reader : public reader
|
||||
|
||||
public:
|
||||
gzfile_reader (const std::string &file)
|
||||
: gzfp(open_gzfile (file, "r"))
|
||||
: reader(false),
|
||||
gzfp(open_gzfile (file, "r"))
|
||||
{
|
||||
}
|
||||
gzfile_reader (const gzfile_reader &) = delete;
|
||||
|
Loading…
Reference in New Issue
Block a user