Skip to content

Commit fdcef3e

Browse files
authored
Modernize (#149)
* 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
1 parent 18e9fe4 commit fdcef3e

File tree

10 files changed

+1084
-940
lines changed

10 files changed

+1084
-940
lines changed

.clang-format

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: false
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlinesLeft: false
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: true
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: All
15+
AllowShortIfStatementsOnASingleLine: false
16+
AllowShortLoopsOnASingleLine: false
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: None
19+
AlwaysBreakBeforeMultilineStrings: false
20+
AlwaysBreakTemplateDeclarations: false
21+
BinPackArguments: true
22+
BinPackParameters: true
23+
BraceWrapping:
24+
AfterClass: false
25+
AfterControlStatement: false
26+
AfterEnum: false
27+
AfterFunction: false
28+
AfterNamespace: false
29+
AfterObjCDeclaration: false
30+
AfterStruct: false
31+
AfterUnion: false
32+
BeforeCatch: false
33+
BeforeElse: false
34+
IndentBraces: false
35+
BreakBeforeBinaryOperators: None
36+
BreakBeforeBraces: Attach
37+
BreakBeforeTernaryOperators: true
38+
BreakConstructorInitializersBeforeComma: false
39+
ColumnLimit: 80
40+
CommentPragmas: '^ IWYU pragma:'
41+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
42+
ConstructorInitializerIndentWidth: 4
43+
ContinuationIndentWidth: 4
44+
Cpp11BracedListStyle: true
45+
DerivePointerAlignment: false
46+
DisableFormat: false
47+
ExperimentalAutoDetectBinPacking: false
48+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
49+
IncludeCategories:
50+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
51+
Priority: 2
52+
- Regex: '^(<|"(gtest|isl|json)/)'
53+
Priority: 3
54+
- Regex: '.*'
55+
Priority: 1
56+
IndentCaseLabels: false
57+
IndentWidth: 2
58+
IndentWrappedFunctionNames: false
59+
KeepEmptyLinesAtTheStartOfBlocks: true
60+
MacroBlockBegin: ''
61+
MacroBlockEnd: ''
62+
MaxEmptyLinesToKeep: 1
63+
NamespaceIndentation: None
64+
ObjCBlockIndentWidth: 2
65+
ObjCSpaceAfterProperty: false
66+
ObjCSpaceBeforeProtocolList: true
67+
PenaltyBreakBeforeFirstCallParameter: 19
68+
PenaltyBreakComment: 300
69+
PenaltyBreakFirstLessLess: 120
70+
PenaltyBreakString: 1000
71+
PenaltyExcessCharacter: 1000000
72+
PenaltyReturnTypeOnItsOwnLine: 60
73+
PointerAlignment: Right
74+
ReflowComments: true
75+
SortIncludes: true
76+
SpaceAfterCStyleCast: false
77+
SpaceBeforeAssignmentOperators: true
78+
SpaceBeforeParens: ControlStatements
79+
SpaceInEmptyParentheses: false
80+
SpacesBeforeTrailingComments: 1
81+
SpacesInAngles: false
82+
SpacesInContainerLiterals: true
83+
SpacesInCStyleCastParentheses: false
84+
SpacesInParentheses: false
85+
SpacesInSquareBrackets: false
86+
Standard: Cpp11
87+
TabWidth: 8
88+
UseTab: Never
89+
...
90+

Makefile

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
# You can put your build options here
22
-include config.mk
33

4-
all: libjsmn.a
5-
6-
libjsmn.a: jsmn.o
7-
$(AR) rc $@ $^
8-
9-
%.o: %.c jsmn.h
10-
$(CC) -c $(CFLAGS) $< -o $@
11-
124
test: test_default test_strict test_links test_strict_links
13-
test_default: test/tests.c
5+
test_default: test/tests.c jsmn.h
146
$(CC) $(CFLAGS) $(LDFLAGS) $< -o test/$@
157
./test/$@
16-
test_strict: test/tests.c
8+
test_strict: test/tests.c jsmn.h
179
$(CC) -DJSMN_STRICT=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
1810
./test/$@
19-
test_links: test/tests.c
11+
test_links: test/tests.c jsmn.h
2012
$(CC) -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
2113
./test/$@
22-
test_strict_links: test/tests.c
14+
test_strict_links: test/tests.c jsmn.h
2315
$(CC) -DJSMN_STRICT=1 -DJSMN_PARENT_LINKS=1 $(CFLAGS) $(LDFLAGS) $< -o test/$@
2416
./test/$@
2517

26-
jsmn_test.o: jsmn_test.c libjsmn.a
18+
simple_example: example/simple.c jsmn.h
19+
$(CC) $(LDFLAGS) $< -o $@
20+
21+
jsondump: example/jsondump.c jsmn.h
22+
$(CC) $(LDFLAGS) $< -o $@
2723

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

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

3430
clean:
3531
rm -f *.o example/*.o
36-
rm -f *.a *.so
3732
rm -f simple_example
3833
rm -f jsondump
3934

40-
.PHONY: all clean test
35+
.PHONY: clean test
4136

README.md

+24-10
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,35 @@ object hierarchy.
7676
This approach provides enough information for parsing any JSON data and makes
7777
it possible to use zero-copy techniques.
7878

79-
Install
80-
-------
79+
Usage
80+
-----
8181

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

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

92-
If build was successful, you should get a `libjsmn.a` library.
93-
The header file you should include is called `"jsmn.h"`.
95+
Since jsmn is a single-header, header-only library, for more complex use cases
96+
you might need to define additional macros. `#define JSMN_STATIC` hides all
97+
jsmn API symbols by making them static. Also, if you want to include `jsmn.h`
98+
from multiple C files, to avoid duplication of symbols you may define `JSMN_HEADER` macro.
99+
100+
```
101+
/* In every .c file that uses jsmn include only declarations: */
102+
#define JSMN_HEADER
103+
#include "jsmn.h"
104+
105+
/* Additionally, create one jsmn.c file for jsmn implementation: */
106+
#include "jsmn.h"
107+
```
94108

95109
API
96110
---

0 commit comments

Comments
 (0)