From 59b447dc917e782354c54058ae049d23d09af5ce Mon Sep 17 00:00:00 2001 From: brian khuu Date: Sun, 19 Jan 2025 16:41:53 +1100 Subject: [PATCH] v0.1.1 documentation changes and C util program renaming --- .gitignore | 2 +- README.md | 45 ++++++++++++++++++++------ clib.json | 4 +-- makefile | 29 ++++++++++------- sexp_prettify.c => src/sexp_prettify.c | 0 sexp_prettify.h => src/sexp_prettify.h | 0 test_all.sh | 2 +- time_test_all.sh | 2 +- 8 files changed, 59 insertions(+), 25 deletions(-) rename sexp_prettify.c => src/sexp_prettify.c (100%) rename sexp_prettify.h => src/sexp_prettify.h (100%) diff --git a/.gitignore b/.gitignore index 9ddfdd4..628d262 100644 --- a/.gitignore +++ b/.gitignore @@ -170,7 +170,7 @@ sexp_formatter *.o temp/ -sexp_prettify_cli +sexp_prettify sexp_prettify_cpp_cli sexp_prettify_kicad_cli sexp_prettify_kicad_original_cli diff --git a/README.md b/README.md index 31c7f66..5cd3a57 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,20 @@ # sexp_formatter -![CI/CD Status Badge](https://github.com/mofosyne/sexp_formatter/actions/workflows/ci.yml/badge.svg) +![Version 0.1.1](https://img.shields.io/badge/version-0.1.1-blue.svg) +[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) +[![C](https://img.shields.io/badge/Language-C-blue.svg)](https://en.wikipedia.org/wiki/C_(programming_language)) +[![CI/CD Status Badge](https://github.com/mofosyne/sexp_formatter/actions/workflows/ci.yml/badge.svg)](https://github.com/mofosyne/sexp_prettify/actions) Prettifies KiCad-like S-expressions according to a KiCADv8-style formatting Via Python. -This is both a cli program as well as a C/CPP/Python implementation library. + +This is primarily targeted at C but there is multiple implementations in +C/CPP/Python as well that you can use for integration with your code. ```bash -# Build sexp_prettify_cli +# Build sexp_prettify make -# Install sexp_prettify_cli as sexp_prettify system wide +# Install sexp_prettify as sexp_prettify system wide make install # Remove sexp_prettify cli system wide @@ -19,13 +24,13 @@ make uninstall make cicd ``` -Expected Help Message when running `sexp_prettify_cli` +Expected Help Message when running `sexp_prettify` ``` S-Expression Formatter (Brian Khuu 2024) Usage: - ./sexp_prettify_cli [OPTION]... SOURCE [DESTINATION] + ./sexp_prettify [OPTION]... SOURCE [DESTINATION] SOURCE Source file path. If '-' then use standard stream input DESTINATION Destination file path. If omitted or '-' then use standard stream output @@ -39,7 +44,7 @@ Options: Example: - Use standard input and standard output. Also use KiCAD's standard compact list and shortform setting. - ./sexp_prettify_cli -l pts -s font -s stroke -s fill -s offset -s rotate -s scale - - + ./sexp_prettify -l pts -s font -s stroke -s fill -s offset -s rotate -s scale - - ``` When integrating into your project, copy over `sexp_prettify.c` and `sexp_prettify.h` and use these functions: @@ -55,6 +60,28 @@ typedef void (*PrettifySExprPutcFunc)(char c, void *context); void sexp_prettify(struct PrettifySExprState *state, const char c, PrettifySExprPutcFunc output_func, void *output_func_context); ``` +An example of how to use this C library in your project: + +First make a callback function + +```c + void putc_handler(char c, void *context_putc) { fputc(c, (FILE *)context_putc); } +``` + +Then implement this in your main program as a loop of this form + +```c + struct PrettifySExprState state = {0}; + sexp_prettify_init(&state, PRETTIFY_SEXPR_KICAD_DEFAULT_INDENT_CHAR, PRETTIFY_SEXPR_KICAD_DEFAULT_INDENT_SIZE, PRETTIFY_SEXPR_KICAD_DEFAULT_CONSECUTIVE_TOKEN_WRAP_THRESHOLD); + + int ch; + FILE *dst_file = stdout; + while ((ch = getc(message)) != EOF) + { + sexp_prettify(&state, ch, &putc_handler, dst_file); + } +``` + ## Developer * Run `make` to build all the c and cpp binaries shown above. @@ -63,11 +90,11 @@ void sexp_prettify(struct PrettifySExprState *state, const char c, PrettifySExpr ### About Files -* sexp_prettify_cli.py : This is a python implementation +* sexp_prettify.py : This is a python implementation * sexp_prettify_kicad_original_cli : This is a cpp original logic from the KiCAD repository as of 2024-12-03 * sexp_prettify_kicad_cli : This is the new cpp logic from the KiCAD repository being proposed for KiCAD * sexp_prettify_cpp_cli : This is a cpp cli wrapper around the c function `sexp_prettify()` in `sexp_prettify.c/h` -* sexp_prettify_cli : This is a c cli wrapper around the c function `sexp_prettify()` in `sexp_prettify.c/h` +* sexp_prettify : This is a c cli wrapper around the c function `sexp_prettify()` in `sexp_prettify.c/h` ## History diff --git a/clib.json b/clib.json index 9fec486..a339e72 100644 --- a/clib.json +++ b/clib.json @@ -1,11 +1,11 @@ { "name": "S-Expressions Prettify", - "version": "0.1.0", + "version": "0.1.1", "repo": "mofosyne/sexp_prettify", "description": "Prettifies S-expressions (Based On KiCAD formatting)", "keywords": ["no heap", "malloc free", "formatter", "kicad", "sexp", "s-expressions", "no malloc"], "license": "GPL-3.0-or-later", - "src": ["sexp_prettify.c", "sexp_prettify.h"], + "src": ["src/sexp_prettify.c", "src/sexp_prettify.h"], "install": "make install", "uninstall": "make uninstall" } \ No newline at end of file diff --git a/makefile b/makefile index cb212f2..981a7f1 100644 --- a/makefile +++ b/makefile @@ -1,25 +1,32 @@ -CFLAGS = -std=c99 -Wall -pedantic +CFLAGS = -std=c99 -Wall -pedantic -Isrc PREFIX ?= /usr/local -main: sexp_prettify_cli +main: sexp_prettify readme_update -all: sexp_prettify_cli sexp_prettify_cpp_cli sexp_prettify_kicad_cli sexp_prettify_kicad_original_cli +all: sexp_prettify sexp_prettify_cpp_cli sexp_prettify_kicad_cli sexp_prettify_kicad_original_cli -sexp_prettify_cli.o: sexp_prettify.c - $(CC) -c -o $@ $^ +src/sexp_prettify.o: src/sexp_prettify.c + $(CC) $(CFLAGS) -c -o $@ $^ -sexp_prettify_cli: sexp_prettify_cli.c sexp_prettify.o sexp_prettify.h - $(CC) -o $@ $^ +sexp_prettify: sexp_prettify_cli.c src/sexp_prettify.o + $(CC) $(CFLAGS) -o $@ $^ -sexp_prettify_cpp_cli: sexp_prettify_cpp_cli.cpp sexp_prettify.o sexp_prettify.h - $(CXX) -o $@ $^ +sexp_prettify_cpp_cli: sexp_prettify_cpp_cli.cpp src/sexp_prettify.o src/sexp_prettify.h + $(CXX) $(CFLAGS) -o $@ $^ sexp_prettify_kicad_cli: sexp_prettify_kicad_cli.cpp - $(CXX) -o $@ $^ + $(CXX) $(CFLAGS) -o $@ $^ sexp_prettify_kicad_original_cli: sexp_prettify_kicad_original_cli.cpp - $(CXX) -o $@ $^ + $(CXX) $(CFLAGS) -o $@ $^ + +# Dev Note: $ is used by both make and AWK. Must escape $ for use in AWK within makefile. +.PHONY: readme_update +readme_update: + # Library Version (From clib package metadata) + jq -r '.version' clib.json | xargs -I{} sed -i 's|.*|{}|' README.md + jq -r '.version' clib.json | xargs -I{} sed -i 's|.*|![Version {}](https://img.shields.io/badge/version-{}-blue.svg)|' README.md .PHONY: install install: sexp_prettify_cli diff --git a/sexp_prettify.c b/src/sexp_prettify.c similarity index 100% rename from sexp_prettify.c rename to src/sexp_prettify.c diff --git a/sexp_prettify.h b/src/sexp_prettify.h similarity index 100% rename from sexp_prettify.h rename to src/sexp_prettify.h diff --git a/test_all.sh b/test_all.sh index e737a7a..38f6e2e 100755 --- a/test_all.sh +++ b/test_all.sh @@ -10,8 +10,8 @@ echo "========================================================================" # ./test_standard_single.sh ./test_standard_single.sh ./sexp_prettify_kicad_cli true ./test_standard_single.sh ./sexp_prettify_cpp_cli false -./test_standard_single.sh ./sexp_prettify_cli false ./test_standard_single.sh ./sexp_prettify_cli.py true +./test_standard_single.sh ./sexp_prettify false echo "All tests passed in all executables!" exit 0 \ No newline at end of file diff --git a/time_test_all.sh b/time_test_all.sh index a20c4a1..7fb802b 100755 --- a/time_test_all.sh +++ b/time_test_all.sh @@ -7,7 +7,7 @@ executables=( "./sexp_prettify_kicad_original_cli" "./sexp_prettify_kicad_cli" "./sexp_prettify_cpp_cli" - "./sexp_prettify_cli" + "./sexp_prettify" )