Skip to content

Commit d67df1b

Browse files
authored
Merge pull request #42 from camgunz/develop
Merge from develop
2 parents 346f22c + c680023 commit d67df1b

File tree

12 files changed

+3635
-1586
lines changed

12 files changed

+3635
-1586
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
*.swp
2727
*.swo
2828

29+
# Coverage files
30+
*.gcda
31+
*.gcno
32+
2933
# Testing executable
3034
test/test-cmp
35+
cmptest
36+
cmptest2
37+
38+
# Example executables
39+
examples/example1
40+
examples/example2
41+
example1
42+
example2
43+
44+
# Test data
45+
cmp_data.dat
3146

.travis.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
language: cpp
2-
compiler: clang
1+
dist: trusty
2+
sudo: false
3+
language: c
4+
compiler: gcc
5+
6+
addons:
7+
apt:
8+
packages:
9+
- build-essential
10+
- python-dev
11+
- libffi-dev
12+
- libssl-dev
13+
- libcmocka-dev
14+
15+
before_install:
16+
- pip install --user cpp-coveralls
317

418
script:
5-
- examples/build_examples.sh
6-
- examples/example1
7-
- examples/example2
19+
- make test
20+
21+
after_success:
22+
- coveralls --gcov-options '\-lp' -i cmp.c -i cmp.h -e test/utils.c -e test/buf.c -e test/test.c -e test/buf.h -e test/utils.h -e examples/example1.c -e examples/example2.c

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
CC ?= gcc
2+
3+
CFLAGS ?= -Werror -Wall -Wextra -funsigned-char -fwrapv -Wconversion -Wno-sign-conversion -Wmissing-format-attribute -Wpointer-arith -Wformat-nonliteral -Winit-self -Wwrite-strings -Wshadow -Wenum-compare -Wempty-body -Wparentheses -Wcast-align -Wstrict-aliasing --pedantic-errors
4+
5+
.PHONY: all clean test coverage
6+
7+
all: cmptest example1 example2
8+
9+
test: cmptest
10+
@./cmptest
11+
12+
cmp.o:
13+
$(CC) $(CFLAGS) --std=c89 -fprofile-arcs -ftest-coverage -g -O0 -I. -c cmp.c
14+
15+
cmptest: cmp.o
16+
$(CC) $(CFLAGS) -Wno-error=deprecated-declarations -Wno-deprecated-declarations --std=c99 -I. -fprofile-arcs -ftest-coverage -g -O0 -o cmptest cmp.o test/test.c test/buf.c test/utils.c -lcmocka
17+
18+
example1:
19+
$(CC) $(CFLAGS) --std=c89 -O3 -I. -o example1 cmp.c examples/example1.c
20+
21+
example2:
22+
$(CC) $(CFLAGS) --std=c89 -O3 -I. -o example2 cmp.c examples/example2.c
23+
24+
coverage:
25+
@rm -f base_coverage.info test_coverage.info total_coverage.info
26+
@rm -rf coverage
27+
@lcov -q -c -i -d . -o base_coverage.info
28+
@lcov -q -c -d . -o test_coverage.info
29+
@lcov -q -a base_coverage.info -a test_coverage.info -o total_coverage.info
30+
@lcov -q --summary total_coverage.info
31+
@mkdir coverage
32+
@genhtml -q -o coverage total_coverage.info
33+
34+
clean:
35+
@rm -f cmptest
36+
@rm -f example1
37+
@rm -f example2
38+
@rm -f *.o
39+
@rm -f *.gcno *.gcda *.info
40+
@rm -f cmp_data.dat

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# cmp
1+
# CMP
2+
3+
[![Build Status](https://travis-ci.org/camgunz/cmp.svg?branch=master)](https://travis-ci.org/camgunz/cmp) [![Coverage Status](https://coveralls.io/repos/github/camgunz/cmp/badge.svg?branch=develop)](https://coveralls.io/github/camgunz/cmp?branch=develop)
24

35
CMP is a C implementation of the MessagePack serialization format. It
46
currently implements version 5 of the [MessagePack
@@ -32,6 +34,10 @@ static bool file_reader(cmp_ctx_t *ctx, void *data, size_t limit) {
3234
return read_bytes(data, limit, (FILE *)ctx->buf);
3335
}
3436

37+
static bool file_skipper(cmp_ctx_t *ctx, size_t count) {
38+
return fseek((FILE *)ctx->buf, count, SEEK_CUR);
39+
}
40+
3541
static size_t file_writer(cmp_ctx_t *ctx, const void *data, size_t count) {
3642
return fwrite(data, sizeof(uint8_t), count, (FILE *)ctx->buf);
3743
}
@@ -54,7 +60,7 @@ int main(void) {
5460
if (fh == NULL)
5561
error_and_exit("Error opening data.dat");
5662

57-
cmp_init(&cmp, fh, file_reader, file_writer);
63+
cmp_init(&cmp, fh, file_reader, file_skipper, file_writer);
5864

5965
if (!cmp_write_array(&cmp, 2))
6066
error_and_exit(cmp_strerror(&cmp));
@@ -90,13 +96,14 @@ int main(void) {
9096
if (!cmp_read_str(&cmp, message_pack, &str_size))
9197
error_and_exit(cmp_strerror(&cmp));
9298

93-
printf("Array Length: %zu.\n", array_size);
99+
printf("Array Length: %u.\n", array_size);
94100
printf("[\"%s\", \"%s\"]\n", hello, message_pack);
95101

96102
fclose(fh);
97103

98104
return EXIT_SUCCESS;
99105
}
106+
100107
```
101108
102109
## Advanced Usage
@@ -108,36 +115,35 @@ See the `examples` folder.
108115
CMP uses no internal buffers; conversions, encoding and decoding are done on
109116
the fly.
110117
111-
CMP's source and header file together are ~3,300 LOC.
118+
CMP's source and header file together are ~4k LOC.
112119
113120
CMP makes no heap allocations.
114121
115122
CMP uses standardized types rather than declaring its own, and it depends only
116-
on `stdbool.h`, `stdint.h` and `string.h`. It has no link-time dependencies,
117-
not even the C Standard Library.
123+
on `stdbool.h`, `stdint.h` and `string.h`.
118124
119125
CMP is written using C89 (ANSI C), aside, of course, from its use of
120126
fixed-width integer types and `bool`.
121127
122-
On the other hand, CMP's test suite depends upon the C Standard Library and
123-
requires C99.
128+
On the other hand, CMP's test suite requires C99.
124129
125-
CMP only requires the programmer supply a read function and a write function.
126-
In this way, the programmer can use CMP on memory, files, sockets, etc.
130+
CMP only requires the programmer supply a read function, a write function, and
131+
an optional skip function. In this way, the programmer can use CMP on memory,
132+
files, sockets, etc.
127133
128134
CMP is portable. It uses fixed-width integer types, and checks the endianness
129135
of the machine at runtime before swapping bytes (MessagePack is big-endian).
130136
131137
CMP provides a fairly comprehensive error reporting mechanism modeled after
132138
`errno` and `strerror`.
133139
134-
CMP is threadsafe; while contexts cannot be shared between threads, each thread
135-
may use its own context freely.
140+
CMP is thread aware; while contexts cannot be shared between threads, each
141+
thread may use its own context freely.
136142
137143
CMP is tested using the MessagePack test suite as well as a large set of custom
138144
test cases. Its small test program is compiled with clang using `-Wall -Werror
139145
-Wextra ...` along with several other flags, and generates no compilation
140-
errors.
146+
errors in either Clang or GCC.
141147
142148
CMP's source is written as readably as possible, using explicit, descriptive
143149
variable names and a consistent, clear style.

TODO.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1+
# To Do
2+
3+
- Work on some kind of skipping
4+
- Add a `skip` callback to `cmp_ctx_t`, because skipping is not always
5+
reading
6+
- Add `bool cmp_skip_object(cmp_ctx_t *ctx, cmp_object_t *obj, uint32_t limit)`
7+
- Add `bool cmp_skip_object_no_limit(cmp_ctx_t *ctx, cmp_object_t *obj)`
8+
9+
- Work on fixing double-copy issue
10+
- Essentially everything is written to a `cmp_object_t` before it's written
11+
out to the caller, which is inefficient. The reasoning for this is to not
12+
pollute the caller's environment should an error occur, but in practice
13+
it's probably better to say, "you can't trust the contents of output
14+
arguments you pass to CMP if the call fails".
15+
16+
- Build real docs
17+
- Probably still just a Markdown file, but still, things have gotten complex
18+
enough that `cmp.h` and `README.md` don't really cover it anymore.
19+
20+
- Consider using a real test framework
21+
122
- Prevent users from using extended types < 0 (reserved by MessagePack)

0 commit comments

Comments
 (0)