Giter VIP home page Giter VIP logo

struct's Introduction

Introduction

struct is a binary data formatting library inspired by 'The Practice of Programming (Brian W. Kernighan, Rob Pike)' and Python struct module.

Format

struct uses following format characters (note that struct does not fully support the Python struct module's format):

Table 1. Byte order

Character Byte order
= native
< little-endian
> big-endian
! network (= big-endian)

Table 2. Format characters

Format C/C++ Type Standard size
b char 1
B unsigned char 1
h short 2
H unsigned short 2
i int 4
I unsigned int 4
l long 4
L unsigned long 4
q long long 8
Q unsigned long long 8
f float 4
d double 8
s char[]
p char[]
x pad bytes
v go/pbuf svarint
V go/pbuf varint

Pack

#include "struct.h"
...
char buf1[BUFSIZ] = {'\0',};
char buf2[BUFSIZ] = {'\0',};
char str[BUFSIZ] = {'\0',};
char fmt[BUFSIZ] = {'\0',};
int val = 42;

struct_pack(buf1, "i", val);

strcpy(str, "test");
snprintf(fmt, sizeof(fmt), "%ds", strlen(str));

struct_pack(buf2, fmt, str);

Unpack

...
int rval;
char rstr[32] = {'\0',};

struct_unpack(buf1, "i", &rval);

struct_unpack(buf2, fmt, rstr);

Install

CMake

Compile

mkdir build
cd build
cmake ..
make
make install

headers: build/release/include/struct/. library: build/release/lib/.

Test

cmake -DSTRUCT_BUILD_TEST=ON ..
make
make test

or run struct_test.

valgrind memory check:

ctest -T memcheck

Bazel

Compile

bazel build //:struct

Test

bazel test //test:struct_test

or

bazel test --test_output=all //test:struct_test

you can use git_repository to fetch struct library.

WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "struct",
    branch = "master",
    remote = "https://github.com/svperbeast/struct",
)

BUILD:

cc_binary(
    name = ...,
    srcs = [...],
    deps = [
        "@struct//:struct",
    ],
)

References

The Practice of Programming (9.1 Formatting Data)

Python struct

License

Code released under the MIT license.

struct's People

Contributors

sleepybishop avatar svperbeast avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

struct's Issues

Does this library work on Arduino IDE?

How can I convert this library so that I can use it with Arduino IDE. I am sending struct.pack() data from RPi4 to ESP32 through UART. In order to decode the received message, I want this library to work on my Arduino IDE for ESP32. How can I do that?

is it possible to unpack strings in place ?

Is it possible to pack/unpack strings in place (I mean source buffer = destination buffer) ? Like this for example:

 char str[32] = {'\0', };
 char fmt[32] = {'\0', };
 
 strcpy(str, "test");
 sprintf(fmt, "%ds", strlen(str));

 struct_pack(str, fmt, str);
 struct_unpack(str, fmt, str);

In my case I am receiving a character string from a TCP socket in a buffer. Then I have to unpack it, and assign it to a std::string object. So I was wondering if I had to recreate a new buffer or if I could just reuse the one that I had.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.