13 # include <vcl_msvc_warnings.h> 17 template <
typename TYPE>
21 unsigned char buf[ MAX_INT_BUFFER_LENGTH ] = {0};
23 os.
os().write((
char*)buf, nbytes );
26 template <
typename TYPE>
30 unsigned char buf[MAX_INT_BUFFER_LENGTH] = {0};
31 unsigned char *ptr=buf;
35 const std::ptrdiff_t ptr_offset_from_begin = ptr-buf;
36 if (ptr_offset_from_begin >= (std::ptrdiff_t)MAX_INT_BUFFER_LENGTH)
38 std::cerr <<
"I/O ERROR: vsl_b_read(vsl_b_istream &, int& )\n" 39 <<
" Integer too big. Likely cause either file corruption, or\n" 40 <<
" file was created on platform with larger integer sizes.\n" 41 <<
"ptr_offset_from_begin: " << ptr_offset_from_begin <<
" >= " << MAX_INT_BUFFER_LENGTH << std::endl;
42 is.
is().clear(std::ios::badbit);
47 while (!(*(ptr++) & 128));
51 #define MACRO_MAKE_INTEGER_READ_WRITE( TYPEIN ) \ 52 void vsl_b_write(vsl_b_ostream& os,TYPEIN n ) \ 54 local_vsl_b_write<TYPEIN>(os,n); \ 57 void vsl_b_read(vsl_b_istream &is,TYPEIN& n ) \ 59 local_vsl_b_read<TYPEIN>(is,n); \ 68 #if VXL_HAS_INT_64 && !VXL_INT_64_IS_LONG 80 #undef MACRO_MAKE_INTEGER_READ_WRITE 84 os.
os().write( reinterpret_cast<char *>(&n),
sizeof( n ) );
89 const int value = is.
is().get();
90 n = static_cast<signed char>(value);
95 os.
os().write( reinterpret_cast<char *>(&n),
sizeof( n ) );
100 const int value = is.
is().get();
101 n = static_cast<signed char>(value);
107 os.
os().write( reinterpret_cast<char *>(&n), 1 );
112 const int value = is.
is().get();
113 n = static_cast<unsigned char>(value);
119 std::string::const_iterator it;
122 for ( it = str.begin(); it != str.end(); ++it )
128 std::string::iterator it;
129 std::string::size_type length;
132 str.resize( length );
133 for ( it = str.begin(); it != str.end(); ++it )
144 }
while ( s[i] != 0 );
156 }
while ( s[i] != 0 );
178 os.
os().write( reinterpret_cast<char *>(&n),
sizeof( n ) );
183 is.
is().read( reinterpret_cast<char *>(&n),
sizeof( n ) );
190 os.
os().write( reinterpret_cast<char *>(&n),
sizeof( n ) );
195 is.
is().read( reinterpret_cast<char *>(&n),
sizeof( n ) );
200 static const unsigned short vsl_magic_number_part_1=0x2c4e;
201 static const unsigned short vsl_magic_number_part_2=0x472b;
210 assert(
os_ !=
nullptr);
219 assert(
os_ !=
nullptr);
226 return os_->operator!();
248 (
void *pointer,
int other_data )
250 assert(pointer !=
nullptr);
251 assert(serialisation_records_.find(pointer) == serialisation_records_.end());
252 unsigned long id = (
unsigned long)serialisation_records_.size() + 1;
253 serialisation_records_[pointer] = std::make_pair(
id, other_data);
270 return (*entry).second.first;
288 return (*entry).second.second;
295 (
void *pointer,
int )
298 serialisation_records_.find(pointer);
299 if (entry == serialisation_records_.end())
301 std::cerr <<
"vsl_b_ostream::set_serialisation_other_data():\n" 302 <<
"No such value " << pointer <<
"in records.\n";
305 return (*entry).second.second;
319 assert(
os_ !=
nullptr);
320 ((std::ofstream *)
os_)->close();
330 assert(
is_ !=
nullptr);
332 unsigned long v=0, m1=0, m2=0;
339 if (m2 != vsl_magic_number_part_2 || m1 != vsl_magic_number_part_1)
341 std::cerr <<
"\nI/O ERROR: vsl_b_istream::vsl_b_istream(std::istream *is)\n" 342 <<
" The input stream does not appear to be a binary VXL stream.\n" 343 <<
" Can't find correct magic number.\n";
344 is_->clear(std::ios::badbit);
349 std::cerr <<
"\nI/O ERROR: vsl_b_istream::vsl_b_istream(std::istream *is)\n" 350 <<
" The stream's leading version number is " 351 << v <<
". Expected value 1.\n";
352 is_->clear(std::ios::badbit);
360 assert(
is_ !=
nullptr);
374 return is_->operator!();
395 void *pointer,
int other_data )
397 assert(pointer !=
nullptr);
414 return (*entry).second.first;
421 (
unsigned long serial_number)
const 424 serialisation_records_.find(serial_number);
425 if (entry == serialisation_records_.end())
431 return (*entry).second.second;
438 (
unsigned long serial_number,
int )
440 serialisation_records_type::const_iterator entry =
441 serialisation_records_.find(serial_number);
442 if (entry == serialisation_records_.end())
444 std::cerr <<
"vsl_b_istream::set_serialisation_other_data():\n" 445 <<
" No such value " << serial_number <<
"in records.\n";
448 return (*entry).second.second;
461 assert(
is_ !=
nullptr);
462 ((std::ifstream *)
is_)->close();
473 if (!is)
return false;
475 unsigned long v=0, m1=0, m2=0;
481 is.read( (
char* )&v, 2 );
483 is.read( (
char* )&m1, 2 );
485 is.read( (
char* )&m2, 2 );
490 if (!is || m2 != vsl_magic_number_part_2 || m1 != vsl_magic_number_part_1 || v>1)
A binary output adaptor for any std::ostream.
void vsl_b_write(vsl_b_ostream &os, char n)
Write char to vsl_b_ostream.
void vsl_b_read_uint_16(vsl_b_istream &is, unsigned long &n)
Read an unsigned int as 16 bits from vsl_b_istream.
virtual int set_serialisation_other_data(void *pointer, int other_data)
Modify the user-defined data associated with the object.
virtual int set_serialisation_other_data(unsigned long serial_number, int other_data)
Modify the user-defined data associated with the unique serial number.
vsl_b_istream(std::istream *is)
Create this adaptor using an existing stream.
void vsl_swap_bytes(char *ptr, unsigned nbyte, std::size_t nelem=1)
Perform byte swapping in situ.
~vsl_b_ofstream() override
Virtual destructor.
bool operator!() const
Returns true if the underlying stream has its fail bit set.
unsigned short version_no() const
Return the version number of the IO format of the file being read.
std::ostream * os_
The member stream.
virtual void clear_serialisation_records()
Clear the stream's record of any serialisation operations.
virtual int get_serialisation_other_data(void *pointer) const
Set the user-defined data associated with the object.
bool vsl_b_istream_test(std::istream &is)
Test to see if a stream really is a binary vsl file.
Byte-swapping, arbitrary length integer conversion, and explicit I/O.
std::istream & is() const
A reference to the adaptor's stream.
std::size_t vsl_convert_from_arbitrary_length(const unsigned char *buffer, unsigned long *ints, std::size_t count=1)
Decode a buffer of arbitrary length integers.
serialisation_records_type serialisation_records_
The serialisation records,.
bool operator!() const
Returns true if the underlying stream has its fail bit set.
static constexpr unsigned short version_no_
The version number of the IO scheme.
virtual int get_serialisation_other_data(unsigned long serial_number) const
Returns the user defined data associated with the unique serial number.
void local_vsl_b_write(vsl_b_ostream &os, const TYPE n)
virtual unsigned long get_serial_number(void *pointer) const
Returns a unique identifier for the object.
#define MACRO_MAKE_INTEGER_READ_WRITE(TYPEIN)
virtual void * get_serialisation_pointer(unsigned long serial_number) const
Returns the pointer to the object identified by the unique serial number.
void close()
Close the stream.
void local_vsl_b_read(vsl_b_istream &is, TYPE &n)
virtual unsigned long add_serialisation_record(void *pointer, int other_data=0)
Adds an object pointer to the serialisation records.
~vsl_b_ifstream() override
Virtual destructor.so that it can be overloaded.
unsigned short version_no_
virtual void add_serialisation_record(unsigned long serial_number, void *pointer, int other_data=0)
Adds record of object's unique serial number, and location in memory.
#define VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(size_of_type)
The maximum length of buffer to use with arbitrary length integers.
void vsl_b_read(vsl_b_istream &is, char &n)
Read char from vsl_b_istream.
An adaptor for any std::istream to make it suitable for binary input.
std::size_t vsl_convert_to_arbitrary_length(const unsigned long *ints, unsigned char *buffer, std::size_t count=1)
Encode an array of ints into an arbitrary length format.
std::istream * is_
The member stream.
Set of functions, and objects to perform binary IO.
virtual void clear_serialisation_records()
Clear the stream's record of any serialisation operations.
serialisation_records_type serialisation_records_
The serialisation records.
std::ostream & os() const
A reference to the adaptor's stream.
void close()
Close the stream.
vsl_b_ostream(std::ostream *os)
Create this adaptor using an existing stream.
void vsl_b_write_uint_16(vsl_b_ostream &os, unsigned long n)
Write an unsigned int as 16 bits to vsl_b_ostream.