Esempio n. 1
0
EXPORT XplBool WJERename(WJElement document, const char *name)
{
	_WJElement	*current = (_WJElement *) document;
	WJElement	e;

	if (!document) {
		return(FALSE);
	}

	/* Look for any siblings with that name, and fail if found */
	if (name && document->parent) {
		for (e = document->parent->child; e; e = e->next) {
			if (e != document && !stricmp(e->name, name)) {
				return(FALSE);
			}
		}
	}

	/* Free the previous name if needed */
	if (document->name && current->_name != document->name) {
		MemRelease(&document->name);
	}

	/* Set the new name */
	if (name) {
		if (!(document->name = MemStrdup(name))) {
			return(FALSE);
		}
	} else {
		document->name = NULL;
	}

	return(TRUE);
}
Esempio n. 2
0
int main(int argc, char **argv)
{
	int			r		= 0;
	WJElement	doc		= NULL;
	WJReader	reader;
	int			i, a, line;
	char		*j, *x;

	MemoryManagerOpen("wjeunit");

	/* Begin tests */
	for (a = 1; a < argc; a++) {
		for (i = 0; tests[i].name && tests[i].cb; i++) {
			if (!stricmp(argv[a], tests[i].name)) {
				break;
			}
		}

		if (!tests[i].cb) {
			fprintf(stderr, "Ignoring unknown test \"%s\"\n", argv[a]);
		} else {
			/* Reopen the JSON for each test in case a test modified it */
			if ((j = MemStrdup(json))) {
				/* Correct the quotes */
				for (x = j; *x; x++) {
					if (*x == '\'') *x = '"';
				}

				// printf("JSON:\n%s\n", j);
				if ((reader = WJROpenMemDocument(j, NULL, 0))) {
					doc = WJEOpenDocument(reader, NULL, NULL, NULL);

					WJRCloseDocument(reader);
				}

				MemRelease(&j);
			}

			if (!doc) {
				fprintf(stderr, "error: Could not parse JSON document\n");
				MemoryManagerClose("wjeunit");
				return(1);
			}

			if ((line = tests[i].cb(doc))) {
				fprintf(stderr, "error: %s:%d: Failed test \"%s\"\n",
					__FILE__, line, tests[i].name);
				r = 1;
			}

			WJECloseDocument(doc);
		}
	}

	/* All done */
	MemoryManagerClose("wjeunit");
	return(r);
}
Esempio n. 3
0
EXPORT XplBool WJECloseDocument(WJElement document)
{
	_WJElement	*current = (_WJElement *) document;

	if (!document) {
		return(FALSE);
	}

	if (document->freecb && !document->freecb(document)) {
		/* The callback has prevented free'ing the document */
		return(TRUE);
	}

	WJEChanged(document);

	/* Remove references to this object */
	if (document->parent) {
		if (document->parent->child == document) {
			document->parent->child = document->next;
		}
		document->parent->count--;
	}

	if (document->prev) {
		document->prev->next = document->next;
	}

	if (document->next) {
		document->next->prev = document->prev;
	}


	/* Destroy all children */
	while (document->child) {
		WJECloseDocument(document->child);
	}

	if (current->pub.type == WJR_TYPE_STRING) {
		MemFree(current->value.string);
		current->pub.length = 0;
	}

	if (document->name && current->_name != document->name) {
		MemRelease(&document->name);
	}

	MemFree(current);

	return(TRUE);
}
Esempio n. 4
0
_WJElement * _WJEReset(_WJElement *e, WJRType type)
{
	if (!e) return(NULL);

	while (e->pub.child) {
		WJECloseDocument(e->pub.child);
	}

	if (WJR_TYPE_STRING == e->pub.type && e->value.string) {
		MemRelease(&(e->value.string));
	}
	e->value.string	= NULL;
	e->pub.length	= 0;
	e->pub.type		= type;

	return(e);
}
Esempio n. 5
0
EXPORT int64 WJEInt64F(WJElement container, WJEAction action, WJElement *last, int64 value, const char *pathf, ...)
{
	int64		ret;
	va_list		args;
	char		*path;
	char		buffer[1024];

	va_start(args, pathf);

	path = buffer;
	path = WJEPathF(buffer, sizeof(buffer), pathf, args);

	va_end(args);

	ret = __WJEInt64(container, path, action, last, value, __FILE__, __LINE__);

	if (path != buffer) {
		MemRelease(&path);
	}

	return(ret);
}
Esempio n. 6
0
EXPORT char * WJEStringNF(WJElement container, WJEAction action, WJElement *last, const char *value, size_t len, const char *pathf, ...)
{
	char *		ret;
	va_list		args;
	char		*path;
	char		buffer[1024];

	va_start(args, pathf);

	path = buffer;
	path = WJEPathF(buffer, sizeof(buffer), pathf, args);

	va_end(args);

	ret = __WJEStringN(container, path, action, last, value, len, __FILE__, __LINE__);

	if (path != buffer) {
		MemRelease(&path);
	}

	return(ret);
}
Esempio n. 7
0
EXPORT WJElement WJEGetF(WJElement container, WJElement last, const char *pathf, ...)
{
	WJElement	ret;
	va_list		args;
	char		*path;
	char		buffer[1024];

	va_start(args, pathf);

	path = buffer;
	path = WJEPathF(buffer, sizeof(buffer), pathf, args);

	va_end(args);

	ret = _WJEGet(container, path, last, __FILE__, __LINE__);

	if (path != buffer) {
		MemRelease(&path);
	}

	return(ret);
}
Esempio n. 8
0
EXPORT double WJEDoubleF(WJElement container, WJEAction action, WJElement *last, double value, const char *pathf, ...)
{
	double		ret;
	va_list		args;
	size_t		needed;
	char		*path;
	char		buffer[1024];

	va_start(args, pathf);

	path = buffer;
	path = WJEPathF(buffer, sizeof(buffer), pathf, args);

	va_end(args);

	ret = __WJEDouble(container, path, action, last, value, __FILE__, __LINE__);

	if (path != buffer) {
		MemRelease(&path);
	}

	return(ret);
}
Esempio n. 9
0
int main(int argc, char **argv)
{
	FILE		*in			= NULL;
	WJElement	doc			= NULL;
	WJElement	current		= NULL;
	int			r			= 0;
	WJReader	reader;
	char		*cmd;
	char		line[1024];

	/* Print pretty documents by default */
	wje.pretty = TRUE;

	/* Print base 10 by default */
	wje.base = 10;

	if (argc > 2) {
		/* Umm, we only allow one argument... a filename */
		usage(argv[0]);
		return(1);
	}

	MemoryManagerOpen("wje-cli");

	if (argc == 2) {
		if (!stricmp(argv[1], "--help") || !stricmp(argv[1], "-h")) {
			usage(argv[0]);
			MemoryManagerClose("wje-cli");
			return(0);
		}

		if (!(in = fopen(argv[1], "rb"))) {
			perror(NULL);
			MemoryManagerClose("wje-cli");
			return(2);
		}

		/*
			A filename was specified on the command line. Does this look
			like a script, or a JSON document?
		*/
		if (fgets(line, sizeof(line), in) &&
			!strncmp(line, "#!", 2)
		) {
			/* This looks like a script, read commands from this file */
			;
		} else {
			/* Assume it is a JSON document, rewind back to the start. */
			rewind(in);

			if ((reader = WJROpenFILEDocument(in, NULL, 0))) {
				doc = WJEOpenDocument(reader, NULL, NULL, NULL);
				WJRCloseDocument(reader);

				wje.filename = MemStrdup(argv[1]);
			}

			fclose(in);
			in = NULL;

			if (!doc) {
				fprintf(stderr, "Could not parse JSON document: %s\n", argv[1]);
			MemoryManagerClose("wje-cli");
				return(3);
			}
		}
	}

	if (!in) {
		/* Read commands from standard in */
		in = stdin;
	}

	if (!doc) {
		/* Start with an empty document if one wasn't specified */
		doc = WJEObject(NULL, NULL, WJE_SET);
	}
	current = doc;

	for (;;) {
		/* Read the next command */
		if (in == stdin && isatty(fileno(stdin))) {
			fprintf(stdout, "wje");

			if (r) {
				fprintf(stdout, " (%d)", r);
			}

			fprintf(stdout, "> ");
			fflush(stdout);
		}

		if (fgets(line, sizeof(line), in)) {
			cmd = skipspace(line);
		} else {
			cmd = NULL;
		}

		if (!cmd || !*cmd) {
			/* Ignore blank lines */
		} else {
			r = runcmd(&doc, &current, cmd);
		}
		cmd = NULL;

		if (feof(in) || wje.exiting) {
			break;
		}
	}

	if (doc) {
		WJECloseDocument(doc);
		doc = NULL;
	}

	if (in && in != stdin) {
		fclose(in);
		in = NULL;
	}

	if (wje.filename) {
		MemRelease(&wje.filename);
	}

	MemoryManagerClose("wje-cli");
	return(r);
}