vsl_b_read_block_old.h
Go to the documentation of this file.
1 // This is core/vsl/vsl_b_read_block_old.h
2 #ifndef vsl_b_read_block_old_h_
3 #define vsl_b_read_block_old_h_
4 //:
5 // \file
6 // \brief Backwards compatibility support only.
7 // \author Ian Scott (Manchester) May 2003
8 //
9 // This file should only be used by existing binary io code that
10 // wishes to maintain backwards compatibility with existing VSL files.
11 // Users should have no reason to include this file.
12 //
13 
14 #include <iostream>
15 #include <vxl_config.h>
16 #include <vcl_compiler_detection.h>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 #include <vsl/vsl_binary_io.h>
22 
23 // Whilst this file should not be used by users, it will likely never be deleted,
24 // and will remain in use by a number of files in vsl and vnl/io to provide
25 // backwards compatibility. If any of the functions are actually used, a
26 // deprecation warning will be sent to cerr.
27 
28 #include <vcl_deprecated.h>
29 
30 // The next declaration should be kept with its non-specialist definition.
31 // It was this mistake that lead to the full replacement of vsl_b_read_block
32 // and vsl_b_write_block with vsl_block_binary_{read,write}.
33 
34 //: Read a block of values from a vsl_b_istream
35 // If you want to output a block of fundamental data types very efficiently,
36 // then just #include <vsl_binary_explicit_io.h>
37 // \deprecated in favour of vsl_block_binary_read
38 template <class T>
39 inline void vsl_b_read_block_old(vsl_b_istream &is, T* begin, std::size_t nelems)
40 {
41  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
42  while (nelems--)
43  vsl_b_read(is, *(begin++));
44 }
45 
46 /////////////////////////////////////////////////////////////////////////
47 
48 //: Read a block of doubles from a vsl_b_istream
49 // This function is very speed efficient.
50 // \deprecated in favour of vsl_block_binary_read
51 template <>
52 inline void vsl_b_read_block_old(vsl_b_istream &is, double* begin, std::size_t nelems)
53 {
54  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
55  is.is().read((char*) begin, (unsigned long)(nelems*sizeof(double)));
56  vsl_swap_bytes((char *)begin, sizeof(double), nelems);
57 }
58 
59 /////////////////////////////////////////////////////////////////////////
60 
61 //: Read a block of floats from a vsl_b_istream
62 // This function is very speed efficient.
63 // \deprecated in favour of vsl_block_binary_read
64 template <>
65 inline void vsl_b_read_block_old(vsl_b_istream &is, float* begin, std::size_t nelems)
66 {
67  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
68  is.is().read((char*) begin, (unsigned long)(nelems*sizeof(float)));
69  vsl_swap_bytes((char *)begin, sizeof(float), nelems);
70 }
71 
72 /////////////////////////////////////////////////////////////////////////
73 
74 //: Read a block of signed ints from a vsl_b_istream
75 // This function is very speed efficient, but
76 // temporarily allocates a block of memory the about 1.2 times
77 // size of the block being read.
78 // \deprecated in favour of vsl_block_binary_read
79 template <>
80 inline void vsl_b_read_block_old(vsl_b_istream &is, int* begin, std::size_t nelems)
81 {
82  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
83  if (!is) return;
84  std::size_t nbytes;
85  vsl_b_read(is, nbytes);
86  if (nbytes)
87  {
88  char *block = new char[VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(sizeof(int)) * nelems];
89  is.is().read(block, nbytes);
90  std::size_t n_bytes_converted =
91  vsl_convert_from_arbitrary_length((unsigned char *)block, begin, nelems);
92  delete [] block;
93  if (n_bytes_converted != nbytes)
94  {
95  std::cerr << "\nI/O ERROR: vsl_b_read_block(.., int*,..) :\n"
96  << " Corrupted data stream\n";
97  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
98  }
99  }
100 }
101 
102 
103 /////////////////////////////////////////////////////////////////////////
104 
105 //: Read a block of unsigned ints from a vsl_b_istream
106 // This function is very speed efficient, but
107 // temporarily allocates a block of memory the about 1.2 times
108 // size of the block being read.
109 // \deprecated in favour of vsl_block_binary_read
110 template <>
111 inline void vsl_b_read_block_old(vsl_b_istream &is, unsigned int* begin, std::size_t nelems)
112 {
113  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
114  std::size_t nbytes;
115  vsl_b_read(is, nbytes);
116  if (nbytes)
117  {
118  char *block = new char[VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(sizeof(unsigned int)) * nelems];
119  is.is().read(block, nbytes);
120  std::size_t n_bytes_converted =
121  vsl_convert_from_arbitrary_length((unsigned char *)block, begin, nelems);
122  delete [] block;
123  if (n_bytes_converted != nbytes)
124  {
125  std::cerr << "\nI/O ERROR: vsl_b_read_block(.., unsigned int*,..) :\n"
126  << " Corrupted data stream\n";
127  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
128  }
129  }
130 }
131 
132 
133 /////////////////////////////////////////////////////////////////////////
134 
135 
136 //: Read a block of signed shorts from a vsl_b_istream
137 // This function is very speed efficient, but
138 // temporarily allocates a block of memory the about 1.2 times
139 // size of the block being read.
140 // \deprecated in favour of vsl_block_binary_read
141 template <>
142 inline void vsl_b_read_block_old(vsl_b_istream &is, short* begin, std::size_t nelems)
143 {
144  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
145  std::size_t nbytes;
146  vsl_b_read(is, nbytes);
147  if (nbytes)
148  {
149  char *block = new char[VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(sizeof(short)) * nelems];
150  is.is().read(block, nbytes);
151  std::size_t n_bytes_converted =
152  vsl_convert_from_arbitrary_length((unsigned char *)block, begin, nelems);
153  delete [] block;
154  if (n_bytes_converted != nbytes)
155  {
156  std::cerr << "\nI/O ERROR: vsl_b_read_block(.., short*,..) :\n"
157  << " Corrupted data stream\n";
158  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
159  }
160  }
161 }
162 
163 
164 /////////////////////////////////////////////////////////////////////////
165 
166 
167 //: Read a block of unsigned shorts from a vsl_b_istream
168 // This function is very speed efficient, but
169 // temporarily allocates a block of memory the about 1.2 times
170 // size of the block being read.
171 // \deprecated in favour of vsl_block_binary_read
172 template <>
173 inline void vsl_b_read_block_old(vsl_b_istream &is, unsigned short* begin, std::size_t nelems)
174 {
175  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
176  std::size_t nbytes;
177  vsl_b_read(is, nbytes);
178  if (nbytes)
179  {
180  char *block = new char[VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(sizeof(unsigned short)) * nelems];
181  is.is().read(block, nbytes);
182  std::size_t n_bytes_converted =
183  vsl_convert_from_arbitrary_length((unsigned char *)block, begin, nelems);
184  delete [] block;
185  if (n_bytes_converted != nbytes)
186  {
187  std::cerr << "\nI/O ERROR: vsl_b_read_block(.., unsigned short*,..) :\n"
188  << " Corrupted data stream\n";
189  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
190  }
191  }
192 }
193 
194 
195 /////////////////////////////////////////////////////////////////////////
196 
197 
198 //: Read a block of signed longs from a vsl_b_istream
199 // This function is very speed efficient, but
200 // temporarily allocates a block of memory the about 1.2 times
201 // size of the block being read.
202 // \deprecated in favour of vsl_block_binary_read
203 template <>
204 inline void vsl_b_read_block_old(vsl_b_istream &is, long* begin, std::size_t nelems)
205 {
206  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
207  std::size_t nbytes;
208  vsl_b_read(is, nbytes);
209  if (nbytes)
210  {
211  char *block = new char[VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(sizeof(long)) * nelems];
212  is.is().read(block, nbytes);
213  std::size_t n_bytes_converted =
214  vsl_convert_from_arbitrary_length((unsigned char *)block, begin, nelems);
215  delete [] block;
216  if (n_bytes_converted != nbytes)
217  {
218  std::cerr << "\nI/O ERROR: vsl_b_read_block(.., long*,..) :\n"
219  << " Corrupted data stream\n";
220  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
221  }
222  }
223 }
224 
225 
226 /////////////////////////////////////////////////////////////////////////
227 
228 
229 //: Read a block of unsigned longs from a vsl_b_istream
230 // This function is very speed efficient, but
231 // temporarily allocates a block of memory the about 1.2 times
232 // size of the block being read.
233 // \deprecated in favour of vsl_block_binary_read
234 template <>
235 inline void vsl_b_read_block_old(vsl_b_istream &is, unsigned long* begin, std::size_t nelems)
236 {
237  VXL_DEPRECATED_MACRO( "vsl_b_read_block_old()" );
238  std::size_t nbytes;
239  vsl_b_read(is, nbytes);
240  if (nbytes)
241  {
242  char *block = new char[VSL_MAX_ARBITRARY_INT_BUFFER_LENGTH(sizeof(unsigned long)) * nelems];
243  is.is().read(block, nbytes);
244  std::size_t n_bytes_converted =
245  vsl_convert_from_arbitrary_length((unsigned char *)block, begin, nelems);
246  delete [] block;
247  if (n_bytes_converted != nbytes)
248  {
249  std::cerr << "\nI/O ERROR: vsl_b_read_block(.., unsigned long*,..) :\n"
250  << " Corrupted data stream\n";
251  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
252  }
253  }
254 }
255 
256 #endif // vsl_b_read_block_old_h_
void vsl_swap_bytes(char *ptr, unsigned nbyte, std::size_t nelem=1)
Perform byte swapping in situ.
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.
void vsl_b_read_block_old(vsl_b_istream &is, T *begin, std::size_t nelems)
Read a block of values from a vsl_b_istream.
#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.
Set of functions, and objects to perform binary IO.