Exemple #1
0
void ConfigSaveJSON(const Config *config, const char *filename)
{
	FILE *f = fopen(filename, "w");
	char *text = NULL;
	json_t *root;

	if (f == NULL)
	{
		printf("Error saving config '%s'\n", filename);
		return;
	}

	setlocale(LC_ALL, "");

	root = json_new_object();
	json_insert_pair_into_object(root, "Version", json_new_number(VERSION));
	ConfigSaveVisit(config, root);

	json_tree_to_string(root, &text);
	char *formatText = json_format_string(text);
	fputs(formatText, f);

	// clean up
	CFREE(formatText);
	CFREE(text);
	json_free_value(&root);

	fclose(f);
}
Exemple #2
0
bool TrySaveJSONFile(json_t *node, const char *filename)
{
	bool res = true;
	char *text;
	json_tree_to_string(node, &text);
	char *ftext = json_format_string(text);
	FILE *f = fopen(filename, "w");
	if (f == NULL)
	{
		LOG(LM_MAIN, LL_ERROR, "failed to open file(%s) for saving: %s",
			filename, strerror(errno));
		res = false;
		goto bail;
	}
	size_t writeLen = strlen(ftext);
	const size_t rc = fwrite(ftext, 1, writeLen, f);
	if (rc != writeLen)
	{
		LOG(LM_MAIN, LL_ERROR, "Wrote (%d) of (%d) bytes: %s",
			(int)rc, (int)writeLen, strerror(errno));
		res = false;
		goto bail;
	}

bail:
	CFREE(text);
	CFREE(ftext);
	if (f != NULL) fclose(f);
	return res;
}
Exemple #3
0
void AutosaveSave(Autosave *autosave, const char *filename)
{
	FILE *f = fopen(filename, "w");
	char *text = NULL;
	json_t *root;
	
	if (f == NULL)
	{
		printf("Error saving autosave '%s'\n", filename);
		return;
	}
	
	setlocale(LC_ALL, "");
	
	root = json_new_object();
	json_insert_pair_into_object(root, "Version", json_new_number("2"));
	json_insert_pair_into_object(
		root, "LastMission", CreateMissionNode(&autosave->LastMission));
	AddMissionNodes(autosave, root, "Missions");

	json_tree_to_string(root, &text);
	char *formatText = json_format_string(text);
	fputs(formatText, f);
	
	// clean up
	free(formatText);
	free(text);
	json_free_value(&root);
	
	fclose(f);
}
bool TrySaveJSONFile(json_t *node, const char *filename)
{
	bool res = true;
	char *text;
	json_tree_to_string(node, &text);
	char *ftext = json_format_string(text);
	FILE *f = fopen(filename, "w");
	if (f == NULL)
	{
		printf("failed to open. Reason: [%s].\n", strerror(errno));
		res = false;
		goto bail;
	}
	size_t writeLen = strlen(ftext);
	const size_t rc = fwrite(ftext, 1, writeLen, f);
	if (rc != writeLen)
	{
		printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
			(int)rc, (int)writeLen, strerror(errno));
		res = false;
		goto bail;
	}

bail:
	CFREE(text);
	CFREE(ftext);
	if (f != NULL) fclose(f);
	return res;
}
Exemple #5
0
void ConfigSaveJSON(Config *config, const char *filename)
{
	FILE *f = fopen(filename, "w");
	char *text = NULL;
	json_t *root;

	if (f == NULL)
	{
		printf("Error saving config '%s'\n", filename);
		return;
	}

	setlocale(LC_ALL, "");

	root = json_new_object();
	json_insert_pair_into_object(root, "Version", json_new_number(VERSION));
	AddGameConfigNode(&config->Game, root);
	AddGraphicsConfigNode(&config->Graphics, root);
	AddInputConfigNode(&config->Input, root);
	AddInterfaceConfigNode(&config->Interface, root);
	AddSoundConfigNode(&config->Sound, root);
	AddQuickPlayConfigNode(&config->QuickPlay, root);

	json_tree_to_string(root, &text);
	char *formatText = json_format_string(text);
	fputs(formatText, f);

	// clean up
	CFREE(formatText);
	CFREE(text);
	json_free_value(&root);

	fclose(f);
}
Exemple #6
0
void formatSelectedJSON(){
	// Get the current scintilla
	int which = -1;
	::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
	if (which == -1)
		return;

	HWND curScintilla = (which == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
	size_t start = ::SendMessage(curScintilla, SCI_GETSELECTIONSTART, 0, 0);
	size_t end = ::SendMessage(curScintilla, SCI_GETSELECTIONEND, 0, 0);
	if (end < start)
	{
		size_t tmp = start;
		start = end;
		end = tmp;
	}

	size_t asciiTextLen = end - start;
	selectAllIfUnselectedAndSetCurJSON(asciiTextLen, curScintilla);	

	CHAR* fJson=json_format_string(curJSON);
	::SendMessage(curScintilla,SCI_REPLACESEL,0,(LPARAM)fJson);
	
	free(fJson);
	delete [] curJSON;
}
Exemple #7
0
std::string
YtkUtils::jsonToString(json_t* root, bool format)
{
    std::string ret;
    char *text = 0;
    if( JSON_OK != json_tree_to_string (root, &text) )
        return ret;


    if(format) {
        char *o = json_format_string(text);
        ret.assign(o);
        delete[] o;

    } else {
        ret.assign(text);
    }
    delete[] text;

    return ret;
}
Exemple #8
0
int linda_rm(hlinda *h, const char *s)
{
	json *j = json_open(s);
	json *j1 = json_get_object(j);
	int is_int = 0, is_string = 0;
	const char *string_id = NULL;
	long long int_id = 0;
	uuid u;

	json *joid = json_find(j1, LINDA_OID);

	if (!joid)
	{
		json_close(j);
		return 0;
	}

	uuid_from_string(json_get_string(joid), &u);
	json *jid = json_find(j1, LINDA_ID);

	if (jid)
	{
		if (h->l->is_int && !json_is_integer(jid))
		{
			printf("linda_read: expected integer id\n");
			json_close(j);
			return 0;
		}
		else if (h->l->is_string && !json_is_string(jid))
		{
			printf("linda_read: expected string id\n");
			json_close(j);
			return 0;
		}

		if (json_is_integer(jid))
		{
			int_id = json_get_integer(jid);
			is_int = 1;
		}
		else if (json_is_string(jid))
		{
			string_id = json_get_string(jid);
			json_close(h->jquery);
			is_string = 1;
		}
		else
		{
			json_close(j);
			return 0;
		}
	}

	json_close(j);

	if (is_int)
	{
		char tmpbuf[1024];
		int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, int_id);
		store_hrem2(h->hst, &u, tmpbuf, tmplen);
		sb_int_uuid_erase(h->l->sl, int_id, &u);
	}
	else if (is_string)
	{
		char tmpbuf[1024], tmpbuf2[1024];
		json_format_string(string_id, tmpbuf2, sizeof(tmpbuf2));
		int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2);
		store_hrem2(h->hst, &u, tmpbuf, tmplen);
		sb_string_uuid_erase(h->l->sl, string_id, &u);
	}
	else
	{
		store_rem(h->l->st, &u);
		sb_uuid_efface(h->l->sl, &u);
	}

	return 1;
}
Exemple #9
0
static int linda_read(hlinda *h, const char *s, const char **buf, int rm, int nowait)
{
	json *j = json_open(s);
	json *j1 = json_get_object(j);
	h->oid.u1 = h->oid.u2 = 0;
	int is_int = 0, is_string = 0;

	json *jid = json_find(j1, LINDA_ID);

	if (jid)
	{
		if (h->l->is_int && !json_is_integer(jid))
		{
			printf("linda_read: expected integer id\n");
			json_close(j);
			return 0;
		}
		else if (h->l->is_string && !json_is_string(jid))
		{
			printf("linda_read: expected string id\n");
			json_close(j);
			return 0;
		}

		if (json_is_integer(jid))
		{
			h->int_id = json_get_integer(jid);
			h->jquery = json_open(s);
			sb_int_uuid_find(h->l->sl, h->int_id, &read_int_handler, h);
			json_close(h->jquery);
			is_int = 1;
		}
		else if (json_is_string(jid))
		{
			h->string_id = json_get_string(jid);
			h->jquery = json_open(s);
			sb_string_uuid_find(h->l->sl, h->string_id, &read_string_handler, h);
			json_close(h->jquery);
			is_string = 1;
		}
		else
		{
			json_close(j);
			return 0;
		}
	}
	else
	{
		h->jquery = json_open(s);
		sb_iter(h->l->sl, &read_handler, h);
		json_close(h->jquery);
	}

	json_close(j);

	if (!h->oid.u1 && !h->oid.u2)
		return 0;

	if (rm)
	{
		if (is_int)
		{
			char tmpbuf[1024];
			int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, h->int_id);
			store_hrem2(h->hst, &h->oid, tmpbuf, tmplen);
			sb_int_uuid_erase(h->l->sl, h->int_id, &h->oid);
		}
		else if (is_string)
		{
			char tmpbuf[1024], tmpbuf2[1024];
			json_format_string(h->string_id, tmpbuf2, sizeof(tmpbuf2));
			int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2);
			store_hrem2(h->hst, &h->oid, tmpbuf, tmplen);
			sb_string_uuid_erase(h->l->sl, h->string_id, &h->oid);
		}
		else
		{
			store_hrem(h->hst, &h->oid);
			sb_uuid_efface(h->l->sl, &h->oid);
		}
	}

	*buf = h->dst;
	return 1;
}
Exemple #10
0
}
Config gConfig;
int gPicManager;


FEATURE(json_format_string, "String format")
SCENARIO("Quote handling")
GIVEN("a JSON structure containing escaped quotes")
json_t *root = json_new_object();
AddStringPair(root, "Foo", "bar \"baz\"");
AddStringPair(root, "Spam", "I like \"spam\" and \"eggs\"");

WHEN("I format the structure into a string")
char *text;
json_tree_to_string(root, &text);
char *ftext = json_format_string(text);

THEN("the result should be valid JSON")
json_t *parsed = NULL;
SHOULD_INT_EQUAL(
    (int)json_parse_document(&parsed, ftext), (int)JSON_OK);
AND("contain the same strings")
SHOULD_BE_TRUE(strstr(ftext, "I like \\\"spam\\\" and \\\"eggs\\\"") != NULL);
CFREE(text);
CFREE(ftext);
json_free_value(&root);
json_free_value(&parsed);
SCENARIO_END
FEATURE_END

CBEHAVE_RUN("JSON features are:", TEST_FEATURE(json_format_string))