Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some basic win32 (mingw) support

test uses standard C io instead of open and unistd.h
Also, extra Makefile (can be handy!)
  • Loading branch information...
commit a484126596d90dfc3a3437ccb875003cc6843918 1 parent 173976d
@mnunberg authored
Showing with 53 additions and 32 deletions.
  1. +22 −8 Makefile
  2. +12 −0 Makefile.mingw
  3. +19 −24 tests/json_test.c
View
30 Makefile
@@ -1,6 +1,6 @@
-LIBJSONSL_DIR=$(shell pwd)
-LDFLAGS=-L$(LIBJSONSL_DIR) -Wl,-rpath $(LIBJSONSL_DIR) -ljsonsl $(PROFILE)
-CFLAGS=\
+LIBJSONSL_DIR+=$(shell pwd)
+LDFLAGS+=-L$(LIBJSONSL_DIR) -Wl,-rpath $(LIBJSONSL_DIR)
+CFLAGS+=\
-Wall -std=gnu89 -pedantic \
-O3 -ggdb3 \
-I$(LIBJSONSL_DIR) -DJSONSL_STATE_GENERIC \
@@ -8,7 +8,20 @@ CFLAGS=\
export CFLAGS
export LDFLAGS
-all: libjsonsl.so
+LIB_BASENAME=jsonsl
+LIB_PREFIX?=lib
+LIB_SUFFIX?=.so
+LIB_FQNAME = $(LIB_PREFIX)$(LIB_BASENAME)$(LIB_SUFFIX)
+
+ifdef STATIC_LIB
+ LDFLAGS+=$(shell pwd)/$(LIB_FQNAME)
+ LIBFLAGS=-c
+else
+ LDFLAGS+=-l$(LIB_BASENAME)
+ LIBFLAGS=-shared -fPIC
+endif
+
+all: $(LIB_FQNAME)
.PHONY: examples
examples:
@@ -21,14 +34,14 @@ json_examples_tarball:
rm -f json_samples.tgz
tar -czf json_samples.tgz share
-check: libjsonsl.so share jsonsl.c
+check: $(LIB_FQNAME) share jsonsl.c
JSONSL_QUIET_TESTS=1 $(MAKE) -C tests
bench:
$(MAKE) -C perf run-benchmarks
-libjsonsl.so: jsonsl.c
- $(CC) $(CFLAGS) -shared -fPIC -o $@ $^
+$(LIB_FQNAME): jsonsl.c
+ $(CC) $(CFLAGS) $(LIBFLAGS) -o $@ $^
.PHONY: doc
@@ -39,7 +52,8 @@ doc: Doxyfile
.PHONY: clean
clean:
- rm -f *.o *.so
+ rm -f *.o *.so *.a
+ rm -f $(LIB_FQNAME)
rm -f -r share
rm -f -r *.dSYM
$(MAKE) -C examples clean
View
12 Makefile.mingw
@@ -0,0 +1,12 @@
+# I don't claim for this Makefile to be 'correct'
+# but it let me test some basic win32 stuff on debian
+# using the mingw32 package
+
+LIB_PREFIX=
+LIB_SUFFIX=.o
+STATIC_LIB=1
+CC=i586-mingw32msvc-gcc
+export STATIC_LIB
+export CC
+
+include Makefile
View
43 tests/json_test.c
@@ -1,9 +1,6 @@
#include "jsonsl.h"
#include <stdio.h>
#include <sys/types.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/stat.h>
#include <assert.h>
static int WantFail = 0;
@@ -81,33 +78,18 @@ int error_callback(jsonsl_t jsn,
void parse_single_file(const char *path)
{
char *buf, *bufp;
- struct stat sb;
- int status;
+ long fsize;
size_t nread = 0;
- int fd;
+ FILE *fh;
jsonsl_t jsn;
WantError = 0;
-
/* open our file */
- fd = open(path, O_RDONLY);
- if (fd == -1) {
+ fh = fopen(path, "r");
+ if (fh == NULL) {
perror(path);
return;
}
- status = fstat(fd, &sb);
- if (S_ISDIR(sb.st_mode)) {
- fprintf(stderr, "\tS_ISDIR..\n");
- close(fd);
- return;
- }
- assert(status == 0);
- assert(sb.st_size);
- assert(sb.st_size < 0x1000000);
- buf = malloc(sb.st_size);
- bufp = buf;
-
-
/* Declare that we will support up to 512 nesting levels.
* Each level of nesting requires about ~40 bytes (allocated at initialization)
* to maintain state information.
@@ -130,16 +112,29 @@ void parse_single_file(const char *path)
* To avoid recomputing offsets and relative positioning,
* we will maintain the buffer, but this is not strictly required.
*/
-
- while ( (nread = read(fd, bufp, 4096)) > 0) {
+ fseek(fh, 0, SEEK_END);
+ fsize = ftell(fh);
+ if (fsize == -1) {
+ perror(path);
+ fclose(fh);
+ return;
+ }
+ assert(fsize < 0x1000000);
+ buf = malloc(fsize);
+ bufp = buf;
+ fseek(fh, 0, SEEK_SET);
+ while ( (nread = fread(bufp, 1, 4096, fh)) ) {
jsonsl_feed(jsn, bufp, nread);
bufp += nread;
}
+
if (WantFail && WantError == 0) {
fprintf(stderr, "Expected error but didn't find any!\n");
abort();
}
jsonsl_destroy(jsn);
+ fclose(fh);
+
free(buf);
}
Please sign in to comment.
Something went wrong with that request. Please try again.