Esempio n. 1
0
int main(int argc, char **argv) {
	WJElement doc = NULL;
	WJElement person = NULL;
	WJElement cameo = NULL;

	doc = WJEObject(NULL, NULL, WJE_NEW);
	WJEString(doc, "name", WJE_SET, "Serenity");
	WJEString(doc, "class", WJE_SET, "firefly");
	WJEArray(doc, "crew", WJE_SET);

	WJEObject(doc, "crew[$]", WJE_NEW);
	WJEString(doc, "crew[-1].name", WJE_SET, "Malcolm Reynolds");
	WJEString(doc, "crew[-1].job", WJE_SET, "captain");
	WJEInt64(doc, "crew[-1].born", WJE_SET, 2468);

	WJEObject(doc, "crew[$]", WJE_NEW);
	WJEString(doc, "crew[-1].name", WJE_SET, "Kaywinnet Lee Fry");
	WJEString(doc, "crew[-1].job", WJE_SET, "mechanic");
	WJEInt64(doc, "crew[-1].born", WJE_SET, 2494);

	WJEObject(doc, "crew[$]", WJE_NEW);
	WJEString(doc, "crew[-1].name", WJE_SET, "Jayne Cobb");
	WJEString(doc, "crew[-1].job", WJE_SET, "public relations");
	WJEInt64(doc, "crew[-1].born", WJE_SET, 2485);

	WJEArray(doc, "cameo", WJE_SET);
	WJEString(doc, "cameo[$]", WJE_NEW, "Battlestar Galactica");
	WJEString(doc, "cameo[$]", WJE_NEW, "Star Wars Evasive Action");
	WJEString(doc, "cameo[$]", WJE_NEW, "Dr. Horrible's Sing-Along Blog");
	WJEString(doc, "cameo[$]", WJE_NEW, "Ready Player One");

	WJEBool(doc, "shiny", WJE_SET, TRUE);

	WJEInt64(doc, "crew[].born == 2468", WJE_SET, 2486);  /* note: awesome! */
	WJECloseDocument(WJEGet(doc, "shiny", NULL));

	while((person = _WJEObject(doc, "crew[]", WJE_GET, &person))) {
		printf("%s (%s) is %"PRId64"\n",
			   WJEString(person, "name", WJE_GET, ""),
			   WJEString(person, "job", WJE_GET, ""),
			   (2517 - WJEInt64(person, "born", WJE_GET, 0)));
	}
	while((cameo = WJEGet(doc, "cameo[]", cameo))) {
		printf("Cameo: %s\n", WJEString(cameo, NULL, WJE_GET, ""));
	}

	WJEDump(doc);
	WJECloseDocument(doc);
	return 0;
}
Esempio n. 2
0
int main(int argc, char **argv) {
	WJElement doc = NULL;
	WJElement person = NULL;

	doc = WJEObject(NULL, NULL, WJE_NEW);
	WJEString(doc, "name", WJE_SET, "Serenity");
	WJEString(doc, "class", WJE_SET, "firefly");
	WJEArray(doc, "crew", WJE_SET);

	WJEObject(doc, "crew[$]", WJE_NEW);
	WJEString(doc, "crew[-1].name", WJE_SET, "Malcolm Reynolds");
	WJEString(doc, "crew[-1].job", WJE_SET, "captain");
	WJEInt64(doc, "crew[-1].born", WJE_SET, 2468);

	WJEObject(doc, "crew[$]", WJE_NEW);
	WJEString(doc, "crew[-1].name", WJE_SET, "Kaywinnet Lee Fry");
	WJEString(doc, "crew[-1].job", WJE_SET, "mechanic");
	WJEInt64(doc, "crew[-1].born", WJE_SET, 2494);

	WJEObject(doc, "crew[$]", WJE_NEW);
	WJEString(doc, "crew[-1].name", WJE_SET, "Jayne Cobb");
	WJEString(doc, "crew[-1].job", WJE_SET, "public relations");
	WJEInt64(doc, "crew[-1].born", WJE_SET, 2485);

	WJEBool(doc, "shiny", WJE_SET, TRUE);

	WJEInt64(doc, "crew[].born == 2468", WJE_SET, 2486);  /* note: awesome! */
	WJECloseDocument(WJEGet(doc, "shiny", NULL));

	while((person = _WJEObject(doc, "crew[]", WJE_GET, &person))) {
		printf("%s (%s) is %d\n",
			   WJEString(person, "name", WJE_GET, ""),
			   WJEString(person, "job", WJE_GET, ""),
			   2517 - WJEInt64(person, "born", WJE_GET, 0));
	}

	WJEDump(doc);
	WJECloseDocument(doc);
	return 0;
}
Esempio n. 3
0
static int SelfTest(WJElement doc)
{
	WJElement		a, b;

	/* Tests methods of refering to "self" */
	if (doc != WJEGet(doc, NULL,	NULL))					return(__LINE__);
	if (doc != WJEGet(doc, "",		NULL))					return(__LINE__);
	if (doc != WJEGet(doc, ".",		NULL))					return(__LINE__);

	/*
		Often in code a WJElement is accessed like this:
			if ((msg->extra = WJEObject(msg->extra, NULL, WJE_NEW))) {
				...

		The idea is that since passing a NULL refers to self that msg->extra
		will be returned if it exists, and created if it does not.	Since this
		is such a common case, verify it's behavior properly.
	*/
	if (!(a = WJEObject(NULL,	NULL, WJE_NEW)))			return(__LINE__);
	if (!(b = WJEObject(a,		NULL, WJE_NEW)))			return(__LINE__);
	if (a != b)												return(__LINE__);

	return(0);
}
Esempio n. 4
0
static int PathsTest(WJElement doc)
{
	WJElement		e;

	/* Compare multiple methods of referencing deep elements */
	if ((e = WJEGet(doc, "a.b.c.d.e.f", NULL)) == NULL)		return(__LINE__);

	if (e != WJEGet(doc, "a[\"b\"].c.d.e.f",	NULL))		return(__LINE__);
	if (e != WJEGet(doc, "a['b'].c.d.e.f",		NULL))		return(__LINE__);
	if (e != WJEGet(doc, "a.b[\"c\"].d.e.f",	NULL))		return(__LINE__);
	if (e != WJEGet(doc, "a.b['c'].d.e.f",		NULL))		return(__LINE__);
	if (e != WJEGet(doc, "a['b'].c['d'].e.f",	NULL))		return(__LINE__);
	if (e != WJEGet(doc, "a['b']['c']['d'].e.f",NULL))		return(__LINE__);

	/* Do the same thing with paths that don't exist yet */
	if (!(e = WJEObject(doc, "z[\"y\"].x.w.v.u",	WJE_NEW)) ||
		!WJECloseDocument(e))								return(__LINE__);
	if (!(e = WJEObject(doc, "z['y'].x.w.v.u",		WJE_NEW)) ||
		!WJECloseDocument(e))								return(__LINE__);
	if (!(e = WJEObject(doc, "z.y[\"x\"].w.v.u",	WJE_NEW)) ||
		!WJECloseDocument(e))								return(__LINE__);
	if (!(e = WJEObject(doc, "z.y['x'].w.v.u",		WJE_NEW)) ||
		!WJECloseDocument(e))								return(__LINE__);
	if (!(e = WJEObject(doc, "z['y'].x['w'].v.u",	WJE_NEW)) ||
		!WJECloseDocument(e))								return(__LINE__);
	if (!(e = WJEObject(doc, "z['y']['x']['w'].v.u",WJE_NEW)) ||
		!WJECloseDocument(e))								return(__LINE__);

	/* Test paths that contain spaces, and dashes */
	if (!WJEGet(doc, "space balls", NULL))					return(__LINE__);
	if (!WJEGet(doc, "space balls.the toliet paper", NULL))	return(__LINE__);
	if ( WJEGet(doc, "space balls.the what ", NULL))		return(__LINE__);
	if (!WJEGet(doc, "space balls.the what []", NULL))		return(__LINE__);

	return(0);
}
Esempio n. 5
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);
}