Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ruk33/struct-property-generator
https://github.com/ruk33/struct-property-generator
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/ruk33/struct-property-generator
- Owner: Ruk33
- License: mit
- Created: 2022-12-11T03:07:05.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T13:41:12.000Z (about 2 years ago)
- Last Synced: 2023-05-05T16:20:08.363Z (over 1 year ago)
- Language: C
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme
- License: license
Awesome Lists containing this project
README
STRUCT PROPERTY GENERATOR
This is a simple program to generate functions to "print" structs to a
destination buffer for C programs.USAGE
Download the executable from the releases page.
You are more than welcome to build from the source code using the
build.bat (Windows) script or build.sh (Linux) The project uses tcc as
the C compiler but GCC, Clang or Zig should also work.Windows:
spg.exe file1.h file2.c file3.h > generated.cLinux:
spg.linux file1.h file2.c file3.h > generated.cEXAMPLE
Given the following file input:
--
// examples/file.h
#define generate_propertiesgenerate_properties struct foo {
int bar;
char *baz;
};
--You can use the generator as spg.exe examples/file.h > examples/generated.c,
which creates the following file result:--
// generated.c
#include
#include
int print_foo(char *dest, int n, struct foo *src)
{
if (!dest || !src) return 0;
int written = 0;
int tmp = 0;
{
tmp = snprintf(dest + written, n - written, "bar: %d\n", src->bar);
if (tmp > 0) written += tmp;
}
{
tmp = snprintf(dest + written, n - written, "baz: %s\n", src->baz);
if (tmp > 0) written += tmp;
}
dest[written] = 0;
return written;
}--
Which can later be used as:
--
struct foo f = {0};
f.bar = 42;
f.baz = "lorem ipsum dolor sit amet";
char buf[128] = {0};
print_foo(buf, sizeof(buf), &f);
printf("%s\n", buf);
// prints
// bar: 42
// baz: lorem ipsum dolor sit amet
--HOW IT WORKS
The program parses and scans all the files passed as parameters and searches
for structs with the prefix "generate_properties". Only those structs
will be processed. In order to not have any warnings/errors in your code,
make sure to #define generate_properties. This will get replaced with
and empty string in the end result but will help this program identify the
structs that should be handled.LIMITATIONS
- The program only parses simple structs.
- Only one level of pointers is supported.
- Unions will be ignored.
- No weird macros in types is supported.WARNING
Please keep in mind this is very early version and may contain
a lot of bugs.