Endian-independent binary IO utilities for C
at main 2.9 kB view raw
1Bini 2==== 3 4Endian-independent* binary IO utilities for C. 5 6*I only have access to little-endian machines, so it's 7currently untested on big endian, however it *should* 8work. If you have a big endian machine and are willing to 9test that Bini runs as expected, I'd be very grateful. 10 11Bini is written in plain ANSI C. If you're compiling in C99 12or later, functions for reading/writing long long and bool 13will be added automatically. You can override the C version 14Bini sees by passing -Dbini_c=<some year, i.e, 1989, 1999, 152011, etc>. 16 17Usage 18----- 19 20Examples can be found in the </examples/> folder. 21 22I usually run examples with one of these commands: 23 cc examples/<example>.c -I. -g -Dbini_dbg -Dx_writebin && gdb -q -ex=r --args a.out 24 cc examples/<example>.c -I. -g -Dbini_dbg -Dx_writebin && ./a.out &> out.txt 25 26Sample: 27 28 /* Only define this once! You can use the bini.c file to 29 ensure you don't accidentally define bini_impl twice. */ 30 #define bini_impl 31 #include <bini.h> 32 33 int main(void) 34 { 35 /* Allocate a new binary stream. */ 36 struct bini_stream *bs = bini_new(); 37 /* Write the meaning of life, the universe, and everything to our binary stream. */ 38 bini_wi(bs, 42); 39 /* Read the meaning of life, the universe, and everything from our binary stream. */ 40 int life = bini_ri(bs); 41 ASSERT(life == 42); 42 /* Close our stream, flushing+freeing the buffer and the stream itself. */ 43 bini_close(bs); 44 } 45 46License 47------- 48 49Copyright 2025 Emmeline Coats 50 51Redistribution and use in source and binary forms, with or without 52modification, are permitted provided that the following conditions are met: 53 541. Redistributions of source code must retain the above copyright notice, this 55 list of conditions and the following disclaimer. 56 572. Redistributions in binary form must reproduce the above copyright notice, 58 this list of conditions and the following disclaimer in the documentation 59 and/or other materials provided with the distribution. 60 613. Neither the name of the copyright holder nor the names of its contributors 62 may be used to endorse or promote products derived from this software 63 without specific prior written permission. 64 65THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND 66ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 67WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 68DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 69FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 71SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 72CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 73OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 74OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.