Skip to content

Commit 47e67a3

Browse files
author
Micah N Gorrell
committed
Merged changes from internal releases of wjelement
1 parent b94f6e4 commit 47e67a3

File tree

8 files changed

+65
-90
lines changed

8 files changed

+65
-90
lines changed

include/hulautil.h renamed to include/nmutil.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717

1818
/****************************************************************************
19-
* stub replacement for Messaging Architects' internal Hula utility library
19+
* stub replacement for Messaging Architects' internal netmail utility library
2020
****************************************************************************/
2121
#include "xpl.h"
2222
#include <stdio.h>
2323
#include <errno.h>
2424

2525

26-
#ifndef HULAUTIL_H
27-
#define HULAUTIL_H
26+
#ifndef NMUTIL_H
27+
#define NMUTIL_H
2828

2929

3030
#endif

include/wjelement.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ typedef WJElementPublic * WJElement;
165165
C source file without having to escape double quote characters. Example:
166166
doc = _WJEParse("{ 'foo': true, 'bar': 'yup' }", '\'');
167167
*/
168-
EXPORT WJElement _WJEParse(const char *json, char quote);
169-
#define WJEParse(j) _WJEParse((j), '"')
170-
#define WJEFromString(j) _WJEParse((j), '"')
168+
#define WJEFromString(j) __WJEFromString((j), '"', __FILE__, __LINE__)
169+
#define WJEParse(j) __WJEFromString((j), '"', __FILE__, __LINE__)
170+
#define _WJEFromString(j, q) __WJEFromString((j), (q), __FILE__, __LINE__)
171+
#define _WJEParse(j, q) __WJEFromString((j), (q), __FILE__, __LINE__)
172+
EXPORT WJElement __WJEFromString(const char *json, char quote, const char *file, const int line);
171173

172174
/*
173175
Allocate a string and write the JSON source for the provided WJElement to
@@ -198,13 +200,15 @@ EXPORT XplBool _WJEWriteDocument(WJElement document, WJWriter writer, char *nam
198200
#define WJEWriteDocument(d, w, n) _WJEWriteDocument((d), (w), (n), NULL, NULL, NULL)
199201

200202
/* Write a WJElement object to the provided FILE* */
201-
EXPORT void WJEWriteFILE(WJElement document, FILE* fd);
203+
EXPORT void WJEWriteFILE(WJElement document, FILE* fd);
202204

203205
/* Read a WJElement object from the provided FILE* */
204-
EXPORT WJElement WJEReadFILE(FILE* fd);
206+
EXPORT WJElement WJEReadFILE(FILE* fd);
205207

206208
/* Destroy a WJElement object */
207-
EXPORT XplBool WJECloseDocument(WJElement document);
209+
EXPORT XplBool _WJECloseDocument(WJElement document, const char *file, const int line);
210+
#define WJECloseDocument(d) _WJECloseDocument((d), __FILE__, __LINE__)
211+
208212
/*
209213
WJECloseDocument is also used to delete/remove an item from a parent
210214
document:

src/wjelement/element.c

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
along with WJElement. If not, see <http://www.gnu.org/licenses/>.
1515
*/
1616

17-
1817
#include "element.h"
1918
#include <time.h>
2019

@@ -63,13 +62,12 @@ _WJElement * _WJENew(_WJElement *parent, char *name, size_t len, const char *fil
6362
if (!parent->pub.child) {
6463
parent->pub.child = (WJElement) result;
6564
} else {
66-
/* Find the last child so it can be added at the end */
67-
for (prev = parent->pub.child; prev && prev->next; prev = prev->next);
68-
65+
prev = parent->pub.last;
6966
prev->next = (WJElement) result;
7067
result->pub.prev = prev;
7168
}
7269

70+
parent->pub.last = (WJElement) result;
7371
parent->pub.count++;
7472
}
7573

@@ -115,6 +113,9 @@ EXPORT XplBool _WJEDetach(WJElement document, const char *file, const int line)
115113
if (document->parent->child == document) {
116114
document->parent->child = document->next;
117115
}
116+
if (document->parent->last == document) {
117+
document->parent->last = document->prev;
118+
}
118119
document->parent->count--;
119120
document->parent = NULL;
120121
}
@@ -141,6 +142,10 @@ EXPORT XplBool WJEAttach(WJElement container, WJElement document)
141142
return(FALSE);
142143
}
143144

145+
if (document->parent == container) {
146+
return(TRUE);
147+
}
148+
144149
if (document->name) {
145150
while ((prev = WJEChild(container, document->name, WJE_GET))) {
146151
WJEDetach(prev);
@@ -155,12 +160,11 @@ EXPORT XplBool WJEAttach(WJElement container, WJElement document)
155160
if (!container->child) {
156161
container->child = document;
157162
} else {
158-
/* Find the last child so it can be added at the end */
159-
for (prev = container->child; prev && prev->next; prev = prev->next);
160-
163+
prev = container->last;
161164
prev->next = document;
162165
document->prev = prev;
163166
}
167+
container->last = document;
164168
container->count++;
165169
WJEChanged(container);
166170

@@ -307,6 +311,17 @@ static WJElement _WJELoad(_WJElement *parent, WJReader reader, char *where, WJEL
307311
return((WJElement) l);
308312
}
309313

314+
EXPORT WJElement _WJEOpenDocument(WJReader reader, char *where, WJELoadCB loadcb, void *data, const char *file, const int line)
315+
{
316+
WJElement element;
317+
318+
if ((element = _WJELoad(NULL, reader, where, loadcb, data, file, line))) {
319+
MemUpdateOwner(element, file, line);
320+
}
321+
322+
return(element);
323+
}
324+
310325
typedef struct WJEMemArgs
311326
{
312327
char *json;
@@ -315,7 +330,7 @@ typedef struct WJEMemArgs
315330
size_t len;
316331
} WJEMemArgs;
317332

318-
EXPORT size_t WJEMemCallback(char *buffer, size_t length, size_t seen, void *userdata)
333+
static size_t WJEMemCallback(char *buffer, size_t length, size_t seen, void *userdata)
319334
{
320335
WJEMemArgs *args = (WJEMemArgs *) userdata;
321336
char *json;
@@ -371,7 +386,7 @@ EXPORT size_t WJEMemCallback(char *buffer, size_t length, size_t seen, void *use
371386
and allows parsing documents with a non standard quote char for the sake of
372387
embedding documents directly in C code.
373388
*/
374-
EXPORT WJElement _WJEParse(const char *json, char quote)
389+
EXPORT WJElement __WJEFromString(const char *json, char quote, const char *file, const int line)
375390
{
376391
WJElement doc;
377392
WJEMemArgs args;
@@ -385,24 +400,13 @@ EXPORT WJElement _WJEParse(const char *json, char quote)
385400
}
386401

387402
if (json && (reader = WJROpenDocument(WJEMemCallback, &args, NULL, 0))) {
388-
doc = WJEOpenDocument(reader, NULL, NULL, NULL);
403+
doc = _WJEOpenDocument(reader, NULL, NULL, NULL, file, line);
389404
WJRCloseDocument(reader);
390405
}
391406

392407
return(doc);
393408
}
394409

395-
EXPORT WJElement _WJEOpenDocument(WJReader reader, char *where, WJELoadCB loadcb, void *data, const char *file, const int line)
396-
{
397-
WJElement element;
398-
399-
if ((element = _WJELoad(NULL, reader, where, loadcb, data, file, line))) {
400-
MemUpdateOwner(element, file, line);
401-
}
402-
403-
return(element);
404-
}
405-
406410
EXPORT char * _WJEToString(WJElement document, XplBool pretty, const char *file, const int line)
407411
{
408412
WJWriter writer;
@@ -656,7 +660,7 @@ EXPORT XplBool _WJEWriteDocument(WJElement document, WJWriter writer, char *name
656660
return(TRUE);
657661
}
658662

659-
EXPORT XplBool WJECloseDocument(WJElement document)
663+
EXPORT XplBool _WJECloseDocument(WJElement document, const char *file, const int line)
660664
{
661665
_WJElement *current = (_WJElement *) document;
662666
WJElement child;
@@ -697,26 +701,26 @@ EXPORT XplBool WJECloseDocument(WJElement document)
697701
/* Destroy all children */
698702
while ((child = document->child)) {
699703
WJEDetach(child);
700-
WJECloseDocument(child);
704+
_WJECloseDocument(child, file, line);
701705
}
702706

703707
if (current->pub.type == WJR_TYPE_STRING) {
704-
MemFree(current->value.string);
708+
MemFreeEx(current->value.string, file, line);
705709
current->pub.length = 0;
706710
}
707711

708712
if (document->name && current->_name != document->name) {
709-
MemRelease(&document->name);
713+
MemReleaseEx(&document->name, file, line);
710714
}
711715

712-
MemFree(current);
716+
MemFreeEx(current, file, line);
713717

714718
return(TRUE);
715719
}
716720

717721
EXPORT void WJEDump(WJElement document)
718722
{
719-
WJEWriteFILE(document, stdout);
723+
WJEWriteFILE(document, stdout);
720724
}
721725

722726
EXPORT void WJEWriteFILE(WJElement document, FILE* fd)
@@ -728,6 +732,7 @@ EXPORT void WJEWriteFILE(WJElement document, FILE* fd)
728732
WJWCloseDocument(dumpWriter);
729733
}
730734
fprintf(fd, "\n");
735+
fflush(fd);
731736
}
732737

733738
EXPORT void WJEDumpFile(WJElement document)
@@ -750,47 +755,26 @@ EXPORT void WJEDumpFile(WJElement document)
750755
}
751756
}
752757

753-
typedef struct _MemWriterData {
754-
size_t maxlength;
755-
size_t length;
756-
char *buffer;
757-
} _MemWriterData;
758-
static size_t MemWriteCB(char *data, size_t size, void *writedata) {
759-
size_t write;
760-
_MemWriterData *w = (_MemWriterData *)writedata;
761-
if(w->maxlength) {
762-
write = w->maxlength - strlen(w->buffer) - 1;
763-
} else {
764-
if(size > w->length - strlen(w->buffer)) {
765-
w->length += 4096;
766-
w->buffer = MemReallocWait(w->buffer, w->length);
767-
}
768-
write = size;
769-
}
770-
if(size < write) {
771-
write = size;
772-
}
773-
if(w->buffer) {
774-
strncat(w->buffer, data, write);
758+
static size_t fileReaderCB( char *data, size_t length, size_t seen, void *client )
759+
{
760+
DebugAssert(length);
761+
DebugAssert(data);
762+
if(!data) {
763+
return 0;
775764
}
776-
return size;
765+
return fread(data, 1, length, client);
777766
}
778767

779-
EXPORT char * WJEWriteMEM(WJElement document, XplBool pretty, size_t maxlength)
768+
EXPORT WJElement WJEReadFILE(FILE* fd)
780769
{
781-
WJWriter memWriter;
782-
_MemWriterData data;
783-
784-
data.length = 0;
785-
data.maxlength = maxlength;
786-
data.buffer = MemMalloc(maxlength);
787-
if(data.buffer) {
788-
*data.buffer = '\0';
789-
}
770+
WJReader reader;
771+
WJElement obj = NULL;
790772

791-
if ((memWriter = _WJWOpenDocument(pretty, MemWriteCB, &data, maxlength))) {
792-
WJEWriteDocument(document, memWriter, NULL);
793-
WJWCloseDocument(memWriter);
773+
if ((reader = WJROpenDocument(fileReaderCB, fd, NULL, 0))) {
774+
obj = WJEOpenDocument(reader, NULL, NULL, NULL);
775+
WJRCloseDocument(reader);
794776
}
795-
return MemRealloc(data.buffer, strlen(data.buffer) + 1);
777+
return obj;
796778
}
779+
780+

src/wjelement/element.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <stdlib.h>
2323

2424
#include <xpl.h>
25-
#include <hulautil.h>
25+
#include <nmutil.h>
2626
#include <memmgr.h>
2727

2828
#include <wjelement.h>

src/wjelement/types.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ EXPORT WJElement WJEGetF(WJElement container, WJElement last, const char *pathf,
737737
{
738738
WJElement ret;
739739
va_list args;
740-
size_t needed;
741740
char *path;
742741
char buffer[1024];
743742

@@ -761,7 +760,6 @@ EXPORT XplBool WJEBoolF(WJElement container, WJEAction action, WJElement *last,
761760
{
762761
XplBool ret;
763762
va_list args;
764-
size_t needed;
765763
char *path;
766764
char buffer[1024];
767765

@@ -785,7 +783,6 @@ EXPORT char * WJEStringF(WJElement container, WJEAction action, WJElement *last,
785783
{
786784
char * ret;
787785
va_list args;
788-
size_t needed;
789786
char *path;
790787
char buffer[1024];
791788

@@ -809,7 +806,6 @@ EXPORT char * WJEStringNF(WJElement container, WJEAction action, WJElement *last
809806
{
810807
char * ret;
811808
va_list args;
812-
size_t needed;
813809
char *path;
814810
char buffer[1024];
815811

@@ -833,7 +829,6 @@ EXPORT WJElement WJEObjectF(WJElement container, WJEAction action, WJElement *la
833829
{
834830
WJElement ret;
835831
va_list args;
836-
size_t needed;
837832
char *path;
838833
char buffer[1024];
839834

@@ -857,7 +852,6 @@ EXPORT WJElement WJEArrayF(WJElement container, WJEAction action, WJElement *las
857852
{
858853
WJElement ret;
859854
va_list args;
860-
size_t needed;
861855
char *path;
862856
char buffer[1024];
863857

@@ -881,7 +875,6 @@ EXPORT WJElement WJENullF(WJElement container, WJEAction action, WJElement *last
881875
{
882876
WJElement ret;
883877
va_list args;
884-
size_t needed;
885878
char *path;
886879
char buffer[1024];
887880

@@ -905,7 +898,6 @@ EXPORT int32 WJEInt32F(WJElement container, WJEAction action, WJElement *last, i
905898
{
906899
int32 ret;
907900
va_list args;
908-
size_t needed;
909901
char *path;
910902
char buffer[1024];
911903

@@ -929,7 +921,6 @@ EXPORT uint32 WJEUInt32F(WJElement container, WJEAction action, WJElement *last,
929921
{
930922
uint32 ret;
931923
va_list args;
932-
size_t needed;
933924
char *path;
934925
char buffer[1024];
935926

@@ -953,7 +944,6 @@ EXPORT int64 WJEInt64F(WJElement container, WJEAction action, WJElement *last, i
953944
{
954945
int64 ret;
955946
va_list args;
956-
size_t needed;
957947
char *path;
958948
char buffer[1024];
959949

@@ -977,7 +967,6 @@ EXPORT uint64 WJEUInt64F(WJElement container, WJEAction action, WJElement *last,
977967
{
978968
uint64 ret;
979969
va_list args;
980-
size_t needed;
981970
char *path;
982971
char buffer[1024];
983972

@@ -1001,7 +990,6 @@ EXPORT double WJEDoubleF(WJElement container, WJEAction action, WJElement *last,
1001990
{
1002991
double ret;
1003992
va_list args;
1004-
size_t needed;
1005993
char *path;
1006994
char buffer[1024];
1007995

0 commit comments

Comments
 (0)