]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/libbinio-1.4/doc/libbinio.texi
Please enter the commit message for your changes. Lines starting
[16.git] / 16 / adplug / libbinio-1.4 / doc / libbinio.texi
1 \input texinfo   @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename libbinio.info
4 @include version.texi
5 @settitle Binary I/O stream class library @value{VERSION}
6 @c %**end of header
7
8 @copying
9 This manual documents the binary I/O stream class library, version
10 @value{VERSION}. It was last updated on @value{UPDATED}.
11
12 Copyright @copyright{} 2002 - 2004 Simon Peter
13
14 @quotation
15 Permission is granted to copy, distribute and/or modify this document
16 under the terms of the GNU Free Documentation License, Version 1.1 or
17 any later version published by the Free Software Foundation; with no
18 Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
19 and with the Back-Cover Texts as in (a) below.  A copy of the license is
20 included in the section entitled ``GNU Free Documentation License.''
21
22 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
23 this GNU Manual, like GNU software.  Copies published by the Free
24 Software Foundation raise funds for GNU development.''
25 @end quotation
26 @end copying
27
28 @dircategory Software Libraries
29 @direntry
30 * libbinio: (libbinio)          Binary I/O stream class library @value{VERSION}
31 @end direntry
32
33 @titlepage
34 @title Binary I/O stream class library
35 @subtitle Version @value{VERSION}
36 @subtitle Last updated on @value{UPDATED}
37 @author Simon Peter
38
39 @page
40 @vskip 0pt plus 1filll
41 @insertcopying
42 @end titlepage
43
44 @contents
45
46 @ifnottex
47 @node Top
48 @top Binary I/O stream class library
49
50 @insertcopying
51 @end ifnottex
52
53 @menu
54 * Introduction::                Introduction to the library.
55 * Usage::                       A tutorial to libbinio.
56 * Reference::                   The complete reference.
57 * FAQ::                         Frequently asked questions.
58 * Copying This Manual::         Your rights and freedoms.
59
60 * Concept Index::
61 * Class Index::
62 * Method Index::
63 * Type Variable and Macro Index::
64 @end menu
65
66 @node Introduction
67 @chapter Introduction
68 @cindex Introduction
69
70 The binary I/O stream class library (libbinio) presents a
71 platform-independent way to access binary data streams in C++.
72
73 The library is hardware independent in the form that it transparently
74 converts between the different forms of machine-internal binary data
75 representation.
76
77 It further employs no special I/O protocol and can be used on
78 arbitrary binary data sources.
79
80 @menu
81 * Features::
82 @end menu
83
84 @node Features
85 @section Features
86 @cindex Features
87
88 libbinio implements a generalized binary stream class framework, with
89 the following features:
90
91 @itemize @bullet
92 @item The ability to read arbitrary-sized integers, as long as the
93 underlying system can hold the final value.
94 @item Conversion between Big- and Little-Endian storage.
95 @item Reading and writing of @acronym{IEEE-754} single and double
96 precision floating-point numbers and the conversion to/from any
97 system-internal floating-point representation.
98 @end itemize
99
100 libbinio provides classes for streaming access to normal files,
101 arbitrary data strings in memory and a wrapper around standard
102 iostreams from either the @acronym{ISO} standard C++ library, or a
103 ``traditional'' iostream implementation.
104
105 In addition, convenience methods for the @acronym{STL} @code{string}
106 class can be enabled that support reading and writing of arbitrary
107 length strings with dynamic memory allocation.
108
109 libbinio should compile and run on as much systems as possible. If it
110 doesn't work with your system/compiler combination, please tell me!
111
112 @node Usage
113 @chapter Usage
114 @cindex Usage
115
116 This chapter covers common usage of libbinio and mostly everything you
117 need to know to interface with the library. For very advanced usage,
118 refer to @ref{Reference}.
119
120 Since libbinio's class framework is modelled somewhat after the
121 traditional iostream library, it is good if you already have used the
122 C++ iostream facility before.
123
124 @menu
125 * Basics::
126 * Files::
127 * Reading and Writing::
128 * Peeking::
129 * Seeking::
130 * Alien architectures::
131 * Wrapping around iostream::
132 * Errors::
133 * Examples::
134 @end menu
135
136 @node Basics
137 @section Basics
138 @cindex Basics
139
140 The binary stream class framework divides streams into
141 @cindex input-only
142 @dfn{input-only},
143 @cindex output-only
144 @dfn{output-only} and
145 @cindex input/output
146 @dfn{input/output} supporting
147 streams. All derived classes also inherit this partition.
148
149 All derived classes that come with the library follow a uniform naming
150 scheme. You can identify what I/O facilities a stream provides, by
151 looking at the class name. All class names of libbinio start with
152 @samp{bin} and end with a descriptive name of what they are meant
153 for. In between is at most one character that describes what I/O
154 functionality is provided. If this character is an @samp{i}, the class
155 provides input-only streams, @samp{o} means output-only and no
156 character indicates both input and output facilities.
157
158 For example, @code{binifstream} is an input-only binary stream for
159 standard file access. @code{binstream} is the general input and output
160 supporting stream.
161
162 @node Files
163 @section Files
164 @cindex Files
165
166 For the most part, you will want to use libbinio to access normal
167 binary files on your filesystem.
168
169 This is what the @code{binfstream} related classes are for. To use
170 them, you first have to include the file @file{binfile.h} within your
171 code.
172
173 This file contains four class declarations -- @code{binfbase},
174 @code{binifstream}, @code{binofstream} and
175 @code{binfstream}. @code{binfbase} is a base class for the others and
176 normally not used by an ordinary libbinio user. This leaves us with
177 three usable classes.
178
179 To create a binary stream on a file, just instantiate one of the three
180 classes, according to what I/O facilities the stream should provide
181 (see @ref{Basics} for information on the class naming conventions).
182
183 @cindex Opening files
184 @cindex File open mode
185 @cindex Appending to files
186 To open a file, you use the @code{open(@var{filename}, [@var{mode}])}
187 method of one of the classes. @var{filename} is a string (both C style
188 @acronym{ASCIIZ} strings and @acronym{STL} @code{string} objects can
189 be passed, if supported by your compiler) containing the name of the
190 file to open. Additionally, you can pass a @var{mode} argument to
191 specify a special way to open the file -- this is described
192 later. There's also a special constructor, provided for convenience,
193 that opens up a file automatically when the object is created. It has
194 exactly the same syntax as @code{open()}.
195
196 @cindex Closing files
197 After usage, you can explicitly close the file with the @code{close()}
198 method. It is always a good idea to do this explicitly, though not
199 needed because the file automatically will be closed on object
200 deconstruction.
201
202 @node Reading and Writing
203 @section Reading and Writing
204
205 Every binary stream class offers the same set of binary I/O methods,
206 inherited from one of the general stream base classes
207 @code{binistream}, @code{binostream} or both of them. These, in turn,
208 inherit from the @code{binio} class, which provides additional general
209 methods, as well as some important type declarations.
210
211 The binary I/O methods differenciate between integers, floating-point
212 numbers and character strings.
213
214 @cindex Reading
215 To read an integer from a binary stream, you would use
216 @code{readInt(@var{size})}. The @var{size} argument specifies the size
217 of the integer in bytes. The method returns the integer, that has just
218 been read.
219
220 @code{readFloat(@var{type})} reads a floating-point number of type
221 @var{type} from the stream. As all floating-point formats are well
222 defined, you just have to specify the right floating-point type to
223 this method and it will figure out the correct size by itself. Refer
224 to @ref{Reference} for information of what floating-point formats are
225 supported and how to specify them. The method returns the
226 floating-point number, that has just been read.
227
228 A character string can be read by using the @code{readString()}
229 method. To read a character string into a pre-allocated C style
230 @acronym{ASCIIZ} string buffer, you would use
231 @code{readString(@var{string}, @var{max-length}, [@var{delimiter}])}
232 and give a pointer to the string buffer with @var{string}, the maximum
233 length of the string (@emph{not} including the final @samp{\0}) in
234 @var{max-length} and optionally a delimiter character @var{delimiter},
235 at which libbinio would stop reading the string, even if it could read
236 more characters. The delimiter is extracted from the stream and then
237 discarded. It will not appear in the final string. If you do not
238 supply a @var{delimiter}, always up to @var{max-length} characters are
239 read from the stream. The method returns the number of characters
240 actually read from the stream.
241
242 You can also read an @acronym{STL} @code{string} object from the
243 stream, using an overloaded version of @code{readString()}. The syntax
244 is @code{readString([@var{delimiter}])}. You also do not need to
245 supply a @var{delimiter}, as it is set to @samp{\0} by default (to
246 prevent you from reading a virtually unlimited number of characters
247 and flood your memory, since you can't supply a @var{max-length}
248 argument  this time). The method returns a @code{string} object,
249 containing all characters up to, but not including, @var{delimiter} or
250 the end of the stream, whichever was encountered first.
251
252 @cindex Writing
253 All the above mentioned ``read'' methods have a ``write'' equivalent,
254 with the same options and features. All ``write'' methods take the
255 data to be written as first argument, as in (for example)
256 @code{writeInt(@var{value}, @var{size})}, @var{value} is the actual
257 value to be written to the stream.
258
259 @node Peeking
260 @section Peeking
261
262 Analogous to the @code{readInt()} and @code{readFloat()} methods are
263 the @code{peekInt(@var{size})} and @code{peekFloat(@var{type})}
264 methods, that take the same arguments and also read an integer or a
265 floating-point number from the stream. Unlike their ``@code{read}''
266 counterparts, they don't modify the stream position. The next ``real''
267 access (i.e. with one of the @code{read} methods) to the stream will
268 continue as if the @code{peek} methods have never been called.
269
270 @node Seeking
271 @section Seeking
272 @cindex Seeking
273 @cindex Positioning
274
275 Sometimes, you need to reposition within your stream. To do this, you
276 use the @code{seek(@var{position}, [@var{offset}])}
277 method. @var{position} is the new position in the stream, counted
278 byte-wise and starting at @samp{0}. The optional argument @var{offset}
279 specifies from where the method should seek. If it is @samp{Set},
280 counting is started at the beginning of the stream. @samp{Add}
281 indicates to seek from the current stream position and @samp{End}
282 means seeking from the end of the stream. Note that @var{position} is
283 a signed integer value and you can also seek backwards by inserting a
284 negative number.
285
286 You can use the @code{pos()} method to determine your current position
287 within the stream, which is returned by the method.
288
289 @node Alien architectures
290 @section Alien architectures
291 @cindex Alien architectures
292
293 All the above mentioned procedures expect the data to be in the format
294 of your machine's own idea of how the data has to be stored. However,
295 there are situations where you have to read and even write binary data
296 for some other, incompatible architecture.
297
298 @cindex Other Architectures
299 All architectures that are incompatible with the architecture,
300 libbinio is currently running on, are called @dfn{alien}
301 architectures.
302
303 @cindex Data conversion
304 @cindex Converting from other architectures
305 libbinio makes the process of converting between your architecture's
306 and the alien architecture's idea of binary storage completely
307 transparent to you. All you have to do is to specify some
308 characteristics about the stream's binary format, after you have
309 opened it. All newly created streams are initialized with the current
310 system's binary characteristics, so you don't have to do anything if
311 you just want to access data of your own architecture.
312
313 @cindex Setting flags
314 @cindex Reading flags
315 @cindex Flags
316 You use the @code{setFlag(@var{flag}, [@var{set}])} and
317 @code{getFlag(@var{flag})} methods to set and read a stream's
318 characteristics. @var{flag} specifies the flag to be set or read
319 (refer to @ref{Reference} for information on what flags are available
320 and their meaning). The optional argument @var{set} specifies whether
321 the flag should be set (the default) or erased.
322
323 After you have set up all flags to sufficiently specify the alien
324 architecture, you can proceed to do binary I/O, just as described in
325 the previous chapters and libbinio will do all the necessary
326 conversion between your own and the alien architecture for you.
327
328 If you ever happen to have binary data of multiple, incompatbile,
329 alien architectures in a single stream (very unlikely), you can re-set
330 any of the flags to any other value, in between any of the other
331 stream access method calls. The stream will adhere to the new flag
332 values instantly with the next data access.
333
334 @node Wrapping around iostream
335 @section Wrapping around iostream
336 @cindex Wrapping around iostream
337 @cindex iostream wrappers
338
339 libbinio provides a set of classes, called @code{binwstream},
340 @code{biniwstream} and @code{binowstream}, that can be wrapped around
341 an already existing stream from the standard or traditional C++
342 iostream library.
343
344 To do this, you just have to pass a pointer to the original stream
345 object from the iostream library to the constructor of the appropriate
346 @code{binwstream} class. Of course, you have to match an input stream
347 from the iostream library to an input stream from libbinio and
348 analogous for the output-only streams. The class framework will
349 prevent you from mismatching them. You can, however, wrap an
350 unidirectional binary stream around a bidirectional stream from the
351 iostream library.
352
353 There is one important thing you have to remember: If you plan to wrap
354 a binary stream around an iostream stream, always open the iostream
355 stream with the @code{ios::bin} mode flag set! This will prevent some
356 operating systems (namely, MS-DOS and Windows) from doing nasty
357 implicit interpretation on the streams. This definitly will impose
358 problems when you try to read or write the binary stream.
359
360 All other flags, you set when opening the original iostream stream,
361 can indirectly have effect on the operations you do on the wrapped
362 binary stream.
363
364 @node Errors
365 @section Errors
366 @cindex Errors
367
368 When doing stream I/O, some unpredictable, erroneus situations can
369 occur at runtime. Binary streams are no exception to this
370 phenomenon. To provide some sort of protection against this, all
371 libbinio stream classes inherit an error reporting method called
372 @code{error()}.
373
374 It is a good idea to check the error status of a stream once in a
375 while to see if everything is still in place. @code{error()} returns a
376 variable of type @code{Error}, which is an integer that holds a bit
377 field of error values for that stream. Refer to @ref{Reference} for
378 information about the possible values of this variable and their
379 meaning.
380
381 @cindex Simple error checking
382 A status of no error is always reported with a value of @samp{0}, so
383 you can easily check if everything is still okay in a simple @code{if}
384 construct, like this:
385
386 @example
387 if(mystream.error())
388   // an error occured, do something to cure it...
389 else
390   // everything is still okay!
391 @end example
392
393 Two convenience error reporting methods are also included:
394
395 @cindex End of file error checking
396 @code{eof()} only checks for the @code{Eof} error status, indicating
397 whether the end of the stream has just been passed. It returns a
398 boolean value, indicating the past the end of the stream.
399
400 @code{ateof()} is like @code{eof()} but returns true already when the
401 stream pointer is at the last byte of the stream, not past it. This is
402 useful when you want to read an entire file into memory in a
403 @code{while(!ateof())} loop. This method is only defined on streams
404 that support reading. On write-only streams, it is not defined and not
405 useful.
406
407 Whenever you call @code{error()}, the internal error variable is reset
408 to the @code{NoError} state, indicating no error at all, and a
409 subsequent call to any of the error reporting methods will return
410 @samp{NoError} again. @code{eof()} and @code{ateof()} do not reset the
411 internal error variable. The last error status of the stream is
412 retained.
413
414 @node Examples
415 @section Examples
416 @cindex Examples
417
418 To read some data (namely integers, floats and strings) from an
419 ordinary binary file on your filesystem, which was also created on
420 your architecture:
421
422 @example
423 #include <binfile.h>
424
425 binifstream     file("test.dat");
426 int             i;
427 float           f;
428 char            string[256];
429
430 // Read a 32-bit integer
431 i = file.readInt(4);
432
433 // Read an IEEE-754 single (32 bits)
434 f = file.readFloat(binio::Single);
435
436 // Read a string until newline or max. number of chars exceeded
437 file.readString(string, 256, '\n');
438 @end example
439
440 To do I/O on a file that was created on an x86 machine (i.e. Little
441 Endian, @acronym{IEEE-754} floats), while your own architecture is
442 something different:
443
444 @example
445 #include <binfile.h>
446
447 binfstream     file;
448 int            i = 1234567;
449 long           pos;
450
451 // open the file "x86.dat" for reading and writing, but do not create
452 // it if it's not there.
453 file.open("x86.dat", binfbase::NoCreate);
454
455 // Check if we could cleanly open the file and bail out, if we
456 // couldn't.
457 if(file.error())
458   return ERROR;
459
460 // Set Little Endian mode, with IEEE-754 floats.
461 file.setFlag(binio::BigEndian, false);   // remove flag
462 file.setFlag(binio::FloatIEEE);          // set flag
463
464 // as we just want a demonstration, we discard all read data right away.
465
466 // Read a 16-bit integer
467 file.readInt(2);
468
469 // Read an IEEE-754 double (64 bits)
470 file.readFloat(binio::Double);
471
472 // Remember our current position in the stream
473 pos = file.pos();
474
475 // Seek to the end of the file
476 file.seek(0, binio::End);
477
478 // Write our variable i, determining its size using sizeof()
479 file.writeInt(i, sizeof(i));
480
481 // Seek back to our former position
482 file.seek(pos);
483
484 // Read a byte from here
485 file.readInt(1);
486
487 // close the file again
488 file.close();
489
490 // and we're finished
491 return SUCCESS;
492 @end example
493
494 To wrap around a stream from the iostream library, reading data until
495 the end of the stream:
496
497 @example
498 #include <fstream>
499 #include <binwrap.h>
500
501 // Open the iostream stream for reading and writing in binary mode.
502 fstream file("test.dat", ios::in | ios::out | ios::bin);
503
504 // Wrap a binary input-only stream around it.
505 biniwstream bfile(&file);
506
507 // Read (and immediately discard) 24-bit integers until the end
508 // of the stream.
509 while(!bfile.eof())
510   bfile.readInt(3);
511 @end example
512
513 @node Reference
514 @chapter Reference
515 @cindex Reference
516
517 This chapter comprises a complete reference of all classes, methods,
518 types and variables inside libbinio.
519
520 @menu
521 * Concepts::
522 * Configuration::
523 * Base classes::
524 * File streams::
525 * String streams::
526 * iostream wrappers::
527 @end menu
528
529 @node Concepts
530 @section Concepts
531 @cindex Concepts
532
533 The libbinio stream classes can be divided into two layers of
534 functionality:
535
536 @enumerate
537 @cindex Conversion layer
538 @item The @dfn{conversion layer} provides methods for transparently
539 converting the data, that has just been read from or is to be written
540 to a stream, between the current and the alien architecture. The
541 @code{binistream}, @code{binostream} and @code{binstream} classes
542 belong to this layer.
543 @cindex Stream layer
544 @item The @dfn{stream layer} faciliates functionality on the stream
545 level. It provides methods for byte-by-byte access to the data,
546 seeking, opening and closing a stream and anything else related to the
547 maintenance of a stream. The @code{binfstream}, @code{binwstream} and
548 @code{binsstream} related classes belong to this layer.
549 @end enumerate
550
551 @cindex Administrative classes
552 Additionally, there are some administrative and auxiliary classes
553 provided, which do not belong to any of the above defined layers. The
554 @code{binio} base class belongs here, for example.
555
556 @node Configuration
557 @section Configuration
558 @cindex Configuration
559 @cindex Compile-time configuration
560
561 libbinio can be configured in various ways at compile time and some
562 parts of the library can be disabled to save disk space and/or
563 processing speed.
564
565 In some cases, these configuration options directly change parts of
566 the interface and must thus also be known by the library user.
567
568 To make these options available to the library user, libbinio defines
569 some C preprocessor macros in @file{binio.h}. These are:
570
571 @vtable @code
572 @item BINIO_ENABLE_STRING
573 If this macro expands to @samp{1}, @acronym{STL} @code{string} object
574 support is enabled and can be used instead of traditional C
575 @acronym{ASCIIZ} pointers with all functions that expect a string
576 somewhere. It expands to @samp{0} otherwise.
577 @item BINIO_ENABLE_IOSTREAM
578 If this macro expands to @samp{1}, the iostream wrapper classes are
579 enabled and can be used by the library user. It expands to @samp{0}
580 otherwise and no iostream wrapper classes are present in this case.
581 @item BINIO_ISO_STDLIB
582 If this macro expands to @samp{1}, libbinio's iostream wrapper classes
583 are taylored for the @acronym{ISO} standard C++ library, instead of a
584 ``traditional'' iostream implementation. It expands to @samp{0}
585 otherwise. This macro is merely for libbinio's internal use and should
586 not be used by an application programmer.
587 @item BINIO_WITH_MATH
588 This macro is for libbinio's internal use and should not be used by an
589 application programmer.
590 @end vtable
591
592 All macros are @emph{always} defined. The decision whether a feature
593 is enabled in the library has to be based on the value of these
594 macros, not whether they are defined or not.
595
596 @node Base classes
597 @section Base classes
598 @cindex Base classes
599
600 The base classes provide a common base for all derived libbinio stream
601 classes. They define important methods, types and variables.
602
603 One general and three stream base classes are defined in the file
604 @file{binio.h}, which make up the libbinio base classes.
605
606 @menu
607 * binio::
608 * binistream::
609 * binostream::
610 * binstream::
611 @end menu
612
613 @node binio
614 @subsection binio
615 @tindex binio
616
617 @code{binio} is the general base class, containing important method,
618 type and variable declarations. As a user, you normally do not need it
619 directly, except for scoping types and variables to it.
620
621 @subheading Header file: @file{binio.h}
622
623 @subheading Public methods:
624
625 @ftable @code
626 @item binio()
627 The constructor.
628
629 @item ~binio()
630 The destructor.
631
632 @item void setFlag(Flag f, bool set = true)
633 Is used to set or erase flags to change the behaviour of the
634 stream. Available flags and their meaning are listed in the types
635 table for this class, below. The optional argument @code{set} is used
636 to set or erase the specified flag. If it is not specified, it
637 defaults to @code{true}, setting (activating) the specified flag.
638
639 @item bool getFlag(Flag f)
640 Returns whether the specified flag is set (active) or not. Refer to
641 @code{setFlag}, above, for more information.
642
643 @item Error error()
644 Returns the current error status of the stream. A status of no error
645 is always indicated with a value of @samp{0}, as to allow easy error
646 checks with a simple @code{if} construct.
647
648 @item bool eof()
649 Returns @samp{true} if the last access to the stream was unsuccessful
650 because the stream pointer was already past the end of the
651 stream. @samp{false} is returned otherwise. Synonymous to checking if
652 the return value of a call to @code{error()} sets the @code{Eof} error
653 code flag.
654
655 @item virtual void seek(long, Offset = Set)
656 Abstract virtual method for seeking into streams. Implemented by the
657 stream layer. The first argument specifies the relative position to
658 seek to. Use a negative value to seek backwards. The optional second
659 argument specifies from where to start seeking. @code{Set} specifies
660 the beginning of a stream, @code{Add} specifies the current position
661 and @code{End} specifies the end of the stream. If it is omitted, it
662 defaults to @code{Set}, seeking from the beginning of the stream.
663
664 @item virtual long pos()
665 Abstract virtual method that returns the current position inside the
666 stream. Implemented by the stream layer.
667 @end ftable
668
669 @subheading Protected methods:
670
671 @ftable @code
672 @item Float pow(Float base, signed int exp)
673 Is a stripped-down version of the @code{pow()} function from the
674 @file{math.h} standard C math library include file. It is used during
675 conversion between floating-point formats and can calculate powers of
676 a floating-point base argument, using a signed integer exponent.
677 @end ftable
678
679 @subheading Public data types and variables:
680
681 @vtable @code
682 @item enum Flag
683 Enumeration of all defined stream flags that can be set using the
684 @code{setFlag()} and @code{getFlag()} methods. This type defines the
685 following values:
686
687 @vtable @code
688 @item BigEndian
689 If set, sets the stream's byte-ordering to big endian
690 notation. Otherwise, little endian notation is used.
691
692 @item FloatIEEE
693 If set, all floating-point access to the stream will assume
694 @acronym{IEEE-754} standardized floating-point numbers. Only this type
695 of data is expected from and written to the stream.
696
697 If your architecture does not support @acronym{IEEE-754}
698 floating-point numbers, the value will be converted. Various
699 conversion errors can occur and sometimes the conversion is not
700 possible at all. In this case, the @code{Unsupported} error is issued
701 and a specific value is returned. The following table summarizes all
702 possible return codes for certain problematic values:
703
704 @multitable {@samp{Not a Number (NaN)}} {Return value}
705 @item Real value @tab Return value
706 @item @samp{Infinity} @tab @samp{1.0}
707 @item @samp{-Infinity} @tab @samp{-1.0}
708 @item @samp{Not a Number (NaN)} @tab @samp{0.0}
709 @end multitable
710
711 Both positive and negative zero (@samp{0}) are mapped to the same zero
712 value, if your architecture does not support both positive and
713 negative zeroes. There is no way to distinguish between the two values
714 in this case.
715
716 @end vtable
717
718 @item enum ErrorCode
719 Enumeration of all possible error values. This type defines the
720 following values:
721
722 @vtable @code
723 @item NoError
724 No error at all. This is always mapped to an integer value of
725 @samp{0}.
726
727 @item Fatal
728 An unspecified, fatal error occured. This error is issued only if
729 something really strange happened (i.e. on internal logic errors in
730 the library, or if something else, undefined happens, etc.). It is
731 advised to immediately terminate any binary stream I/O activity and
732 fail with an error message to the application user, maybe even
733 terminate the application completely.
734
735 @item Unsupported
736 You tried to access stream data in an unsupported way. This error is
737 issued whenever an (explicit or implicit) conversion between two types
738 of data storage is requested, that isn't supported by libbinio yet.
739
740 @item NotOpen
741 The stream you tried to access is not open yet.
742
743 @item Denied
744 Access to this stream is denied. This error is normally issued when
745 you try to open a file on a filesystem, but you have insufficient
746 rights to access it.
747
748 @item NotFound
749 The stream you tried to access was not found. This is mostly issued
750 on a file not found error, when accessing normal files on a
751 filesystem.
752
753 @item Eof
754 The end of the stream has been passed. Issued when you tried to read
755 past the last byte of a stream.
756 @end vtable
757
758 @item enum Offset
759 Specifies the position inside a stream, from where a seek is started
760 with the @code{seek()} method. This type defines the following values:
761
762 @vtable @code
763 @item Set
764 Start seeking at the beginning of the stream.
765
766 @item Add
767 Start seeking from the current position in the stream.
768
769 @item End
770 Start seeking from the end of the stream.
771 @end vtable
772
773 @item enum FType
774 Specifies what type of floating-point number is to be accessed next,
775 using the @code{readFloat()} or @code{writeFloat()} methods. Most
776 floating-point formats specify multiple data types to support a
777 broader range of precision. libbinio generalizes the idea by
778 categorizing them only into single and double precision numbers. This
779 type defines the following values:
780
781 @vtable @code
782 @item Single
783 Access a single precision floating-point number.
784
785 @item Double
786 Access a double precision floating-point number.
787 @end vtable
788
789 @item int Error
790 This integer holds a bit field of all error values for a
791 stream. Returned by @code{error()}. A status of ``no error'' is always
792 indicated when the whole field is set to @samp{0} (zero), i.e. the
793 integer is @samp{0}. Test for an error by binary or'ing this integer
794 with one of the error values from @code{ErrorCode}.
795 @end vtable
796
797 @subheading Protected data types and variables:
798
799 @vtable @code
800 @item Int
801 The largest integer type supported by the architecture.
802
803 @item Float
804 The largest floating-point type supported by the architecture.
805
806 @item Byte
807 This type is always one byte (8 bits) wide.
808
809 @item Flags
810 Type to hold a flag variable, containing the status of all flags of a
811 stream.
812
813 @item Flags my_flags
814 This variable holds the current status of all flags for the stream.
815
816 @item static const Flags system_flags
817 This variable holds the status of the flags, defining the standard
818 behaviour of the system. This is determined once at runtime, at
819 startup of the library.
820
821 @item Error err
822 This variable holds the error status of a stream.
823 @end vtable
824
825 @node binistream
826 @subsection binistream
827 @tindex binistream
828
829 @code{binistream} provides an input-only binary stream.
830
831 @subheading Header file: @file{binio.h}
832
833 @subheading Public methods:
834
835 @ftable @code
836 @item binistream()
837 The constructor.
838
839 @item ~binistream()
840 The destructor.
841
842 @item Int readInt(unsigned int size)
843 Reads an integer of size @code{size} (in bytes) from the stream and
844 returns it. The return value is undefined if an error occured. The
845 maximum number of bytes that can be read at once equals the size (in
846 bytes) of the largest integer type, supported by the system.
847
848 @item Float readFloat(FType ft)
849 Reads a floating-point number from the stream and returns it. Takes
850 the floating-point format to read as the argument @code{ft}. Refer to
851 the list of public types of the @code{binio} class for information
852 about what floating-point formats are supported. The return value is
853 undefined if an error occured. The value from the stream is always
854 rendered to the biggest floating-point type, supported by the system.
855
856 If your architecture is incompatible with the floating-point number
857 that has just been read, @code{readFloat()} tries to convert it. This
858 is sometimes not possible or not as accurate as the original value and
859 an error will be issued in these cases. Refer to the list of public
860 types of the @code{binio} class for information about what errors
861 could be issued.
862
863 @item unsigned long readString(char *str, unsigned long maxlen, const char delim)
864 @itemx std::string readString(const char delim = '\0')
865 Reads a character string from the stream. Both pre-allocated standard
866 C @acronym{ASCIIZ} strings and @acronym{STL} @code{string} objects are
867 supported.
868
869 The @acronym{ASCIIZ} version takes a pointer to the pre-allocated
870 string buffer as the @code{str} argument. @code{maxlen} specifies the
871 maximum number of characters to be read from the stream (@emph{not}
872 including the trailing @samp{\0} that is always appended to the string
873 buffer). The optional argument @code{delim} is a delimiter
874 character. If this character is encountered in the stream, no more
875 characters will be read. The delimiter character itself is
876 discarded. It will not appear in the final string. If the @code{delim}
877 argument is omitted, always up to @code{maxlen} characters are read.
878
879 The @code{string} object version just takes one optional argument, the
880 delimiter character, explained above. Characters are always read until
881 the delimiter character or the end of the stream is encountered. If
882 @code{delim} is omitted, it defaults to @samp{\0}. It returns a
883 @code{string} object, containing the final string.
884
885 @item Int peekInt(unsigned int size)
886 Like @code{readInt()}, but doesn't modify the stream position, so any
887 later access to the stream will appear as if @code{peekInt()} has
888 never been called.
889
890 @item Float peekFloat(FType ft)
891 Like @code{readFloat()}, but doesn't modify the stream position, so
892 any later access to the stream will appear as if @code{peekFloat()}
893 has never been called.
894
895 @item bool ateof()
896 Returns @samp{true} if the current stream position is at the last byte
897 of the stream. @samp{false} is returned otherwise.
898
899 @item void ignore(unsigned long amount = 1)
900 Reads and then immediately discards the specified amount of bytes from
901 the stream. If the optional argument @code{amount} is omitted, it
902 defaults to @samp{1}, ignoring exactly 1 byte from the stream.
903 @end ftable
904
905 @subheading Protected methods:
906
907 @ftable @code
908 @item virtual Byte getByte()
909 Abstract virtual method to extract exactly one byte (8 bits) from the
910 stream and advance stream pointer accordingly. The byte is
911 returned. Implemented by the stream layer.
912 @end ftable
913
914 @node binostream
915 @subsection binostream
916 @tindex binostream
917
918 @code{binostream} provides an output-only binary stream.
919
920 @subheading Header file: @file{binio.h}
921
922 @subheading Public methods:
923
924 @ftable @code
925 @item binostream()
926 The constructor.
927
928 @item ~binostream()
929 The destructor.
930
931 @item void writeInt(Int val, unsigned int size);
932 Writes the integer value @code{val} of size @code{size} (in bytes) to
933 the stream.
934
935 @item void writeFloat(Float f, FType ft);
936 Writes the floating-point value @code{f} of type @code{ft} to the
937 stream. Refer to the list of public types of the @code{binio} class
938 for information about what floating-point formats are supported.
939
940 If the requested floating-point type is not supported by your
941 architecture, @code{writeFloat()} tries to convert it. This is not
942 always possible and an @code{Unsupported} error is issued if the
943 conversion fails and nothing is written to the stream in this case.
944
945 @item unsigned long writeString(const char *str, unsigned long amount = 0);
946 @itemx unsigned long writeString(const std::string &str);
947 Writes a character string to the stream. Both standard C
948 @acronym{ASCIIZ} strings and @acronym{STL} @code{string} objects are
949 supported.
950
951 The standard C version takes a pointer @code{str} to the
952 @acronym{ASCIIZ} string to write as first argument and an optional
953 second argument @code{amount}, which specifies the number of
954 characters to write from that string. If it is omitted, the whole
955 string is written to the stream.
956
957 For the @code{string} object version, the only argument is the
958 @code{string} object, containing the string to write.
959
960 Both methods return the number of characters actually written to the
961 stream (which should only differ from the value you wanted to write
962 when an error occured).
963 @end ftable
964
965 @subheading Protected methods:
966
967 @ftable @code
968 @item virtual void putByte(Byte)
969 Abstract virtual method to insert exactly one byte (8 bits) into the
970 stream and advance the stream pointer accordingly. The byte to be
971 written is passed as the only argument. Implemented by the stream
972 layer.
973 @end ftable
974
975 @node binstream
976 @subsection binstream
977 @tindex binstream
978
979 @code{binstream} provides a both input and output supporting binary
980 stream. It inherits the interface from both @code{binistream} and
981 @code{binostream} and defines no additional methods. Refer to the
982 documentation of these two classes for more information instead.
983
984 @node File streams
985 @section File streams
986 @cindex File streams
987
988 File streams provide access to ordinary files on a filesystem. They
989 consist of the classes @code{binifstream}, @code{binofstream} and
990 @code{binfstream}, which are all derived from the common base class
991 @code{binfbase}.
992
993 @menu
994 * binfbase::
995 * File stream classes::
996 @end menu
997
998 @node binfbase
999 @subsection binfbase
1000 @tindex binfbase
1001
1002 @code{binfbase} provides the common base class for all file I/O binary
1003 streams. It defines important data types, variables and methods.
1004
1005 @subheading Header file: @file{binfile.h}
1006
1007 @subheading Public methods:
1008
1009 @ftable @code
1010 @item binfbase()
1011 The constructor.
1012
1013 @item ~binfbase()
1014 The destructor. Automatically closes an eventually open stream.
1015
1016 @item virtual void open(const char *filename, const Mode mode)
1017 @itemx virtual void open(const std::string &filename, const Mode mode)
1018 Two versions of an abstract virtual method to open a binary file
1019 stream. @code{filename} is the filename of the file to open and
1020 @code{mode} specifies in which mode to do so. Refer to the list of
1021 data types for this class for more information on this argument. You
1022 can combine multiple mode flags together, using the @samp{|}
1023 operator.
1024
1025 @item void close()
1026 Method to explicitly close the stream.
1027
1028 @item void seek(long pos, Offset offs = Set)
1029 @itemx long pos()
1030 Implementation of the abstract virtual methods, inherited from
1031 @code{binio}. Look there for reference.
1032 @end ftable
1033
1034 @subheading Public data types and variables:
1035
1036 @vtable @code
1037 @item enum ModeFlags
1038 Enumeration of all defined file stream open mode flags, defining how a
1039 file should be opened when the @code{open()} method is called. This
1040 type defines the following flags:
1041
1042 @vtable @code
1043 @item Append
1044 Open the stream for appending. The stream pointer is positioned at the
1045 end of the stream. Also, do not truncate a file to zero length, if it
1046 already exists.
1047 @item NoCreate
1048 Do not automatically create a nonexistant file on first access and
1049 fail instead. Also, do not truncate a file to zero length, if it
1050 already exists.
1051 @end vtable
1052
1053 @item Mode
1054 Type of the variable to hold the mode specification flags that are
1055 passed to one of the @code{open()} methods to open a file
1056 stream. Refer to @code{enum ModeFlags} above for information on what
1057 flags are defined.
1058 @end vtable
1059
1060 @subheading Protected data types and variables:
1061
1062 @vtable @code
1063 @item FILE *f
1064 Pointer to the currently opened file of this stream.
1065 @end vtable
1066
1067 @node File stream classes
1068 @subsection File stream classes
1069 @cindex File stream classes
1070 @tindex binfbase
1071
1072 The binary file streams consist of the classes @code{binifstream},
1073 @code{binofstream} and @code{binfstream}. They all inherit the
1074 interface from both @code{binfbase} and @code{binistream} and/or
1075 @code{binostream}. Refer to the class reference of these classes for
1076 more information on the interface instead.
1077
1078 @tindex binifstream
1079 @code{binifstream} provides an input-only binary file stream. It is
1080 inherited from @code{binistream}. The @code{open()} method of this
1081 class ignores all passed @code{mode} arguments, the open mode is
1082 always @code{NoCreate}. The stream pointer is always positioned at the
1083 beginning of the stream and no files will ever be created, if they
1084 don't exist.
1085
1086 @tindex binofstream
1087 @code{binofstream} provides an output-only binary file stream. It is
1088 inherited from @code{binostream}. The default open mode is
1089 @samp{0}. A file is truncated to zero length or created if it did not
1090 exist previously. The stream is positioned at the beginning of the
1091 file. The @code{NoCreate} mode flag has no effect with the
1092 @code{open()} method of this class. Only @code{Append} is honored,
1093 positioning the stream at the end of the file and not truncating
1094 it. The file is still created, if it did not exist.
1095
1096 @tindex binfstream
1097 @code{binfstream} provides an input and output supporting binary file
1098 stream. It is inherited from both @code{binifstream} and
1099 @code{binofstream}. The default open mode is @samp{0}. A file is
1100 truncated to zero length or created if it did not exist
1101 previously. The stream is positioned at the beginning of the file. All
1102 @code{mode} arguments are valid with the @code{open()} method of this
1103 class. Refer to the list of public data types and variables of the
1104 @code{binfbase} class for more information on their meaning.
1105
1106 All three classes define an extra convenience constructor that has the
1107 same syntax as the @code{open()} method and automatically open the
1108 specified file on object construction.
1109
1110 For your information, here is a list of some important redefined
1111 methods of these classes, as explained above:
1112
1113 @subheading Header file: @file{binfile.h}
1114
1115 @subheading Public methods:
1116
1117 @ftable @code
1118 @item binifstream()
1119 @itemx binofstream()
1120 @itemx binfstream()
1121 All standard constructors.
1122
1123 @item ~binifstream()
1124 @itemx ~binofstream()
1125 @itemx ~binfstream()
1126 All standard destructors. They all automatically close an eventually
1127 open stream, on object deconstruction.
1128
1129 @item binifstream(const char *filename, const Mode mode = NoCreate)
1130 @itemx binifstream(const std::string &filename, const Mode mode = NoCreate)
1131 Convenience constructors of the input-only stream class. Refer to the
1132 description of the @code{open()} method in the list of public methods
1133 of the @code{binfbase} class for syntax information.
1134
1135 @item binofstream(const char *filename, const Mode mode = 0)
1136 @itemx binofstream(const std::string &filename, const Mode mode = 0)
1137 Convenience constructors of the output-only stream class. Refer to the
1138 description of the @code{open()} method in the list of public methods
1139 of the @code{binfbase} class for syntax information.
1140
1141 @item binfstream(const char *filename, const Mode mode = 0)
1142 @itemx binfstream(const std::string &filename, const Mode mode = 0)
1143 Convenience constructors of the input and output supporting stream
1144 class. Refer to the description of the @code{open()} method in the
1145 list of public methods of the @code{binfbase} class for syntax
1146 information.
1147
1148 @end ftable
1149
1150 @node String streams
1151 @section String streams
1152 @cindex String streams
1153 @tindex binisstream
1154 @tindex binosstream
1155 @tindex binsstream
1156 @tindex binsbase
1157
1158 The string stream classes provide a means of turning arbitrary strings
1159 (or ``blocks'') in memory into a binary stream, similary to what
1160 @code{strstream} does in the iostream library.
1161
1162 Unlike @code{strstream} from the iostream library, these streams
1163 @emph{do not} support automatic enlarging of the memory area. If the
1164 user tries to write beyond the end of the stream, nothing is actually
1165 written and an @code{Eof} error is issued.
1166
1167 @subheading Header file: @file{binstr.h}
1168
1169 @subheading Public methods:
1170
1171 @ftable @code
1172 @item binsbase(void *str, unsigned long len)
1173 @item binisstream(void *str, unsigned long len)
1174 @item binosstream(void *str, unsigned long len)
1175 @item binsstream(void *str, unsigned long len)
1176 All constructors take two arguments. @code{str} is a pointer to a
1177 previously initialized data string in memory. @code{len} specifies the
1178 length in bytes of the string.
1179
1180 @item ~binsbase()
1181 @item ~binisstream()
1182 @item ~binosstream()
1183 @item ~binsstream()
1184 All deconstructors.
1185
1186 @item virtual void seek(long pos, Offset offs = Set)
1187 @itemx virtual long pos()
1188 Implementation of the abstract virtual methods, inherited from
1189 @code{binio}. Refer to the reference of that class for more
1190 information.
1191 @end ftable
1192
1193 @subheading Protected data types and variables in @code{binsbase}:
1194
1195 @vtable @code
1196 @item Byte *data
1197 This pointer is always pointing to the beginning of the initialized
1198 data string in memory, passed to the constructor.
1199 @item Byte *spos
1200 This pointer points to the current position in the string stream.
1201 @item long length
1202 Holds the length in bytes for the whole data string in memory, as
1203 passed to the constructor.
1204 @end vtable
1205
1206 @node iostream wrappers
1207 @section iostream wrappers
1208 @cindex iostream wrappers
1209 @tindex biniwstream
1210 @tindex binowstream
1211 @tindex binwstream
1212
1213 The iostream wrapper classes provide a way to @dfn{wrap} around an
1214 already existing stream of the standard or traditional C++ iostream
1215 library, thus providing binary access to such a stream. The wrapper
1216 classes consist of @code{biniwstream}, @code{binowstream} and
1217 @code{binwstream}.
1218
1219 The iostream wrapper classes do not provide any extra methods by
1220 themselves. They just inherit the interface of the general binary
1221 stream classes. Look there for reference. @code{biniwstream} inherits
1222 from @code{binistream}, @code{binowstream} inherits from
1223 @code{binostream} and @code{binwstream} inherits from both of them.
1224
1225 Remember to @emph{always} open the iostream stream with the
1226 @code{ios::bin} flag set, before you wrap it with a binary stream, or
1227 else you will get strange errors on some operating systems.
1228
1229 All other flags you set on the iostream stream will also indirectly
1230 affect the wrapped binary stream, so be careful.
1231
1232 @subheading Header file: @file{binwrap.h}
1233
1234 @subheading Public methods:
1235
1236 @ftable @code
1237 @item biniwstream(istream *istr)
1238 @itemx binowstream(ostream *ostr)
1239 @itemx binwstream(iostream *str)
1240 The constructor. @code{istr}, @code{ostr} and @code{str} are pointers
1241 to an appropriate, already existing and open stream from the iostream
1242 library.
1243
1244 @item ~biniwstream()
1245 @itemx ~binowstream()
1246 @itemx ~binwstream()
1247 The destructor.
1248
1249 @item virtual void seek(long pos, Offset offs = Set)
1250 @itemx virtual long pos()
1251 Implementation of the abstract virtual methods, inherited from
1252 @code{binio}. Refer to the reference of that class for more
1253 information.
1254 @end ftable
1255
1256 @node FAQ
1257 @appendix FAQ
1258 @cindex FAQ
1259
1260 @itemize @bullet
1261 @item Q: Why don't you supply general data block I/O methods, taking
1262 @code{void} pointers as arguments, so i can read all my data into a
1263 @code{struct} at once?
1264 @item A: This, in general, is a broken idea. It only makes sense if
1265 you plan to write your program for only one architecture and want a
1266 fast way to read your whole data into memory. This is not what
1267 libbinio was meant for, so such methods will never be
1268 implemented. libbinio cannot know what data types your @code{struct}
1269 is made of and thus cannot do any transparent data conversion. And
1270 there's even more: Most compilers align their @code{struct}s to a
1271 special multiple of bytes (mostly the current architecture's data word
1272 size). libbinio doesn't (and cannot) know about all this and would
1273 just read the data byte by byte, completely ignoring the alignment and
1274 causing you to be unable to read any of your data.
1275
1276 @item Q: Why haven't you implemented error reporting using exceptions,
1277 instead of this darn @code{error()} method polling?
1278 @item A: Because exceptions aren't supported on all compiler
1279 platforms.
1280 @end itemize
1281
1282 @node Copying This Manual
1283 @appendix Copying This Manual
1284 @cindex Copying this manual
1285
1286 @menu
1287 * GNU Free Documentation License::  License for copying this manual.
1288 @end menu
1289
1290 @include fdl.texi
1291
1292 @node Concept Index
1293 @unnumbered Concept Index
1294
1295 @printindex cp
1296
1297 @node Class Index
1298 @unnumbered Class Index
1299
1300 @printindex tp
1301
1302 @node Method Index
1303 @unnumbered Method Index
1304
1305 @printindex fn
1306
1307 @node Type Variable and Macro Index
1308 @unnumbered Type, Variable and Macro Index
1309
1310 @printindex vr
1311
1312 @bye