Skip to content

Commit

Permalink
import of version 0.1
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@2 327403b1-1117-474d-bef2-5cb71233fd97
  • Loading branch information
michaeljclark committed Mar 13, 2007
1 parent 6d59966 commit f0d0888
Show file tree
Hide file tree
Showing 22 changed files with 3,750 additions and 0 deletions.
1,153 changes: 1,153 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions Makefile
@@ -0,0 +1,40 @@
# $Id: Makefile,v 1.4 2004/07/22 01:37:44 mclark Exp $

CFLAGS += -g -Wall -std=gnu99 -D_GNU_SOURCE -D_REENTRANT
LDFLAGS +=
LDLIBS +=

LIB_OBJS = debug.o \
linkhash.o \
printbuf.o \
arraylist.o \
json_object.o \
json_tokener.o

LIB_HDRS = debug.h \
linkhash.h \
printbuf.h \
arraylist.h \
json_object.h \
json_tokener.h

TESTS = test1 test2

all: tests

tests: $(TESTS)
test1: test1.o $(LIB_OBJS)
test2: test2.o $(LIB_OBJS)

clean:
rm -f *.o *~ $(TESTS)

cex.o: cex.c cex.h
debug.o: debug.c debug.h
linkhash.o: linkhash.c linkhash.h
arraylist.o: arraylist.c arraylist.h
json_object.o: json_object.c $(LIB_HDRS)
json_tokener.o: json_tokener.c $(LIB_HDRS)
test1.o: test1.c $(LIB_HDRS)
test2.o: test2.c $(LIB_HDRS)

32 changes: 32 additions & 0 deletions README.html
@@ -0,0 +1,32 @@
<h2>JSON-C - A JSON implementation in C</h2>
<p>Latest release: <a href="json-c-0.1.tar.gz">json-c-0.1.tar.gz</a></p>

<p>JSON-C implements a reference counting object model that allows you
to easily construct JSON objects in C, output them as JSON formatted strings
and parse JSON formatted strings back into the C representation of JSON
objects.</p>

<p>Minimal documentation exists <a href="doc/html/json__object_8h.html">here</a>,
Although you are probably better reading the example code in test1.c.</p>

<p>JSON-C currently depends on some gcc 3.0+ features so can probably only be
compiled with gcc 3.0+. It also uses some specifc glibc functions such as
vasprintf. Patches welcome to port to other compilers / platforms.</p>

<p>Please send bug reports to <a href="mailto:michael@metaparadigm.com">michael@metaparadigm.com</a></p>

<h3>Anonymous CVS</h3>
<p><code># <b>export CVSROOT=:pserver:anoncvs@cvs.metaparadigm.com:/cvsroot</b><br>
# <b>cvs login</b><br>
Logging in to :pserver:anoncvs@cvs.metaparadigm.com:2401/cvsroot<br>
CVS password: &lt;enter '<b>anoncvs</b>'&gt;<br>
# <b>cvs co json-c</b></code></p>

<p>Copyright Metaparadigm Pte. Ltd. 2004. <a href="mailto:michael@metaparadigm.com">Michael Clark </a></p>

<p>This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public (LGPL)
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.</p>

<hr>
94 changes: 94 additions & 0 deletions arraylist.c
@@ -0,0 +1,94 @@
/*
* $Id: arraylist.c,v 1.2 2004/07/21 01:24:33 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/

#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "bits.h"
#include "arraylist.h"


struct array_list*
array_list_new(array_list_free_fn *free_fn)
{
struct array_list *this;

if(!(this = calloc(1, sizeof(struct array_list)))) return NULL;
this->size = ARRAY_LIST_DEFAULT_SIZE;
this->length = 0;
this->free_fn = free_fn;
if(!(this->array = calloc(sizeof(void*), this->size))) {
free(this);
return NULL;
}
return this;
}

extern void
array_list_free(struct array_list *this)
{
int i;
for(i = 0; i < this->length; i++)
if(this->array[i]) this->free_fn(this->array[i]);
free(this->array);
free(this);
}

void*
array_list_get_idx(struct array_list *this, int i)
{
if(i >= this->length) return NULL;
return this->array[i];
}

static int array_list_expand_internal(struct array_list *this, int max)
{
void *t;
int new_size;

if(max < this->size) return 0;
new_size = max(this->size << 1, max);
if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1;
this->array = t;
bzero(this->array + this->size, (new_size-this->size)*sizeof(void*));
this->size = new_size;
return 0;
}

int
array_list_put_idx(struct array_list *this, int idx, void *data)
{
if(array_list_expand_internal(this, idx)) return -1;
if(this->array[idx]) this->free_fn(this->array[idx]);
this->array[idx] = data;
if(this->length <= idx) this->length = idx + 1;
return 0;
}

int
array_list_add(struct array_list *this, void *data)
{
return array_list_put_idx(this, this->length, data);
}

int
array_list_length(struct array_list *this)
{
return this->length;
}
52 changes: 52 additions & 0 deletions arraylist.h
@@ -0,0 +1,52 @@
/*
* $Id: arraylist.h,v 1.2 2004/07/21 01:24:33 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/

#ifndef _arraylist_h_
#define _arraylist_h_

#define ARRAY_LIST_DEFAULT_SIZE 32

typedef void (array_list_free_fn) (void *data);

struct array_list
{
void **array;
int length;
int size;
array_list_free_fn *free_fn;
};

extern struct array_list*
array_list_new(array_list_free_fn *free_fn);

extern void
array_list_free(struct array_list *this);

extern void*
array_list_get_idx(struct array_list *this, int i);

extern int
array_list_put_idx(struct array_list *this, int i, void *data);

extern int
array_list_add(struct array_list *this, void *data);

extern int
array_list_length(struct array_list *this);

#endif
38 changes: 38 additions & 0 deletions bits.h
@@ -0,0 +1,38 @@
/*
* $Id: bits.h,v 1.3 2004/07/21 10:10:22 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/

#ifndef _bits_h_
#define _bits_h_

#define min(x,y) ({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
(void) (&_x == &_y); \
_x < _y ? _x : _y; })

#define max(x,y) ({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
(void) (&_x == &_y); \
_x > _y ? _x : _y; })

#define hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
#define error_ptr(error) ((void*)error)
#define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)

#endif
75 changes: 75 additions & 0 deletions debug.c
@@ -0,0 +1,75 @@
/*
* $Id: debug.c,v 1.3 2004/08/07 03:11:38 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/param.h>

#include "debug.h"


static int _syslog = 0;
static int _debug = 0;

void mc_set_debug(int debug) { _debug = debug; }
int mc_get_debug() { return _debug; }

extern void mc_set_syslog(int syslog)
{
_syslog = syslog;
}

void mc_abort(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
if(_syslog) vsyslog(LOG_ERR, msg, ap);
else vprintf(msg, ap);
exit(1);
}


void mc_debug(const char *msg, ...)
{
va_list ap;
if(_debug) {
va_start(ap, msg);
if(_syslog) vsyslog(LOG_DEBUG, msg, ap);
else vprintf(msg, ap);
}
}

void mc_error(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
if(_syslog) vsyslog(LOG_ERR, msg, ap);
else vfprintf(stderr, msg, ap);
}

void mc_info(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
if(_syslog) vsyslog(LOG_INFO, msg, ap);
else vfprintf(stderr, msg, ap);
}
33 changes: 33 additions & 0 deletions debug.h
@@ -0,0 +1,33 @@
/*
* $Id: debug.h,v 1.2 2004/07/21 01:24:33 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public (LGPL)
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details: http://www.gnu.org/
*
*/

#ifndef _DEBUG_H_
#define _DEBUG_H_

#define errstr strerror(errno)

extern void mc_set_debug(int debug);
extern int mc_get_debug();

extern void mc_set_syslog(int syslog);
extern void mc_abort(const char *msg, ...);
extern void mc_debug(const char *msg, ...);
extern void mc_error(const char *msg, ...);
extern void mc_info(const char *msg, ...);

#endif

0 comments on commit f0d0888

Please sign in to comment.