Skip to content

Commit

Permalink
Modernize (#149)
Browse files Browse the repository at this point in the history
* add .clang-format for automated code formatting
* automatic code formatting
* move config.mk below all target to allow custom non-default build targets
* add license to the top of the file
* use correct header file guards syntax
* convert to single-header, header-only library
* update makefile to use jsmn as a header-only library
* update readme
* add changed from PR #143
* fix clang warnings
* add changes from PR #142
* add consts as per PR #134
  • Loading branch information
zserge authored Apr 20, 2019
1 parent 18e9fe4 commit fdcef3e
Show file tree
Hide file tree
Showing 10 changed files with 1,084 additions and 940 deletions.
90 changes: 90 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IndentCaseLabels: false
IndentWidth: 2
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

33 changes: 14 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
# You can put your build options here
-include config.mk

all: libjsmn.a

libjsmn.a: jsmn.o
$(AR) rc $@ $^

%.o: %.c jsmn.h
$(CC) -c $(CFLAGS) $< -o $@

test: test_default test_strict test_links test_strict_links
test_default: test/tests.c
test_default: test/tests.c jsmn.h
$(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict: test/tests.c
test_strict: test/tests.c jsmn.h
$(CC) -DJSMN_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_links: test/tests.c
test_links: test/tests.c jsmn.h
$(CC) -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@
test_strict_links: test/tests.c
test_strict_links: test/tests.c jsmn.h
$(CC) -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
./test/$@

jsmn_test.o: jsmn_test.c libjsmn.a
simple_example: example/simple.c jsmn.h
$(CC) $(LDFLAGS) $< -o $@

jsondump: example/jsondump.c jsmn.h
$(CC) $(LDFLAGS) $< -o $@

simple_example: example/simple.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@
fmt:
clang-format -i jsmn.h test/*.[ch] example/*.[ch]

jsondump: example/jsondump.o libjsmn.a
$(CC) $(LDFLAGS) $^ -o $@
lint:
clang-tidy jsmn.h --checks='*'

clean:
rm -f *.o example/*.o
rm -f *.a *.so
rm -f simple_example
rm -f jsondump

.PHONY: all clean test
.PHONY: clean test

34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,35 @@ object hierarchy.
This approach provides enough information for parsing any JSON data and makes
it possible to use zero-copy techniques.

Install
-------
Usage
-----

To clone the repository you should have Git installed. Just run:
Download `jsmn.h`, include it, done.

$ git clone https://github.com/zserge/jsmn
```
#include "jsmn.h"
Repository layout is simple: jsmn.c and jsmn.h are library files, tests are in
the jsmn\_test.c, you will also find README, LICENSE and Makefile files inside.
...
jsmn_parser p;
jsmntok_t t[128]; /* We expect no more than 128 JSON tokens */
To build the library, run `make`. It is also recommended to run `make test`.
Let me know, if some tests fail.
jsmn_init(&p);
r = jsmn_parse(&p, s, strlen(s), t, 128);
```

If build was successful, you should get a `libjsmn.a` library.
The header file you should include is called `"jsmn.h"`.
Since jsmn is a single-header, header-only library, for more complex use cases
you might need to define additional macros. `#define JSMN_STATIC` hides all
jsmn API symbols by making them static. Also, if you want to include `jsmn.h`
from multiple C files, to avoid duplication of symbols you may define `JSMN_HEADER` macro.

```
/* In every .c file that uses jsmn include only declarations: */
#define JSMN_HEADER
#include "jsmn.h"
/* Additionally, create one jsmn.c file for jsmn implementation: */
#include "jsmn.h"
```

API
---
Expand Down
Loading

0 comments on commit fdcef3e

Please sign in to comment.