Ejemplo n.º 1
0
TEST(Writer,DoublePrecision) {
	const char json[] = "[1.2345,1.2345678,0.123456789012,1234567.8]";

	StringBuffer buffer;
	Writer<StringBuffer> writer(buffer);

	const int kDefaultDoublePrecision = 6;
	// handling the double precision
	EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
	writer.SetDoublePrecision(17);
	EXPECT_EQ(writer.GetDoublePrecision(), 17);
	writer.SetDoublePrecision(-1); // negative equivalent to reset
	EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
	writer.SetDoublePrecision(1);
	writer.SetDoublePrecision();   // reset again
	EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);

	{ // write with explicitly increased precision
		StringStream s(json);
		Reader reader;
		reader.Parse<0>(s, writer.SetDoublePrecision(12));
		EXPECT_EQ(writer.GetDoublePrecision(), 12);
		EXPECT_STREQ(json, buffer.GetString());
		buffer.Clear();
	}
	{ // explicit individual double precisions
		writer.SetDoublePrecision(2)
			.StartArray()
			.Double(1.2345,5)
			.Double(1.2345678,9)
			.Double(0.123456789012,12)
			.Double(1234567.8,8)
			.EndArray();

		EXPECT_EQ(writer.GetDoublePrecision(), 2);
		EXPECT_STREQ(json, buffer.GetString());
		buffer.Clear();
	}
	{ // write with default precision (output with precision loss)
		Document d;
		d.Parse<0>(json);
		d.Accept(writer.SetDoublePrecision());

		// parsed again to avoid platform-dependent floating point outputs
		// (e.g. width of exponents)
		d.Parse<0>(buffer.GetString());
		EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
		EXPECT_DOUBLE_EQ(d[0u].GetDouble(), 1.2345);
		EXPECT_DOUBLE_EQ(d[1u].GetDouble(), 1.23457);
		EXPECT_DOUBLE_EQ(d[2u].GetDouble(), 0.123457);
		EXPECT_DOUBLE_EQ(d[3u].GetDouble(), 1234570);
		buffer.Clear();
	}
}
Ejemplo n.º 2
0
AVES_API NATIVE_FUNCTION(aves_StringBuffer_clear)
{
	StringBuffer *buf = THISV.Get<StringBuffer>();
	buf->Clear();
	VM_Push(thread, THISP);
	RETURN_SUCCESS;
}
Ejemplo n.º 3
0
TEST(StringBuffer, Clear) {
    StringBuffer buffer;
    buffer.Put('A');
    buffer.Put('B');
    buffer.Put('C');
    buffer.Clear();

    EXPECT_EQ(0u, buffer.GetSize());
    EXPECT_STREQ("", buffer.GetString());
}
Ejemplo n.º 4
0
string XJson::encode(const ::urbi::UValue& data) {
    if (data.type != ::urbi::DATA_DICTIONARY && data.type != ::urbi::DATA_LIST) {
        send("throw \"Only UList or UDictionary are supported by UJson.\";");
        return "";
    }
    Document doc;
    StringBuffer stream;

    Writer<StringBuffer> writer(stream);

    unique_ptr<Value> valPtr(fromUValue(data, doc.GetAllocator()));
    (*valPtr).Accept(writer);

    string result(stream.GetString());
    stream.Clear();
    return result;
}
Ejemplo n.º 5
0
TEST(SchemaValidator, ValidateMetaSchema) {
    CrtAllocator allocator;
    char* json = ReadFile("draft-04/schema", allocator);
    Document d;
    d.Parse(json);
    ASSERT_FALSE(d.HasParseError());
    SchemaDocument sd(d);
    SchemaValidator validator(sd);
    if (!d.Accept(validator)) {
        StringBuffer sb;
        validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
        printf("Invalid schema: %s\n", sb.GetString());
        printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());
        sb.Clear();
        validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
        printf("Invalid document: %s\n", sb.GetString());
        ADD_FAILURE();
    }
    CrtAllocator::Free(json);
}
Ejemplo n.º 6
0
TEST(SchemaValidatingWriter, Simple) {
    Document sd;
    sd.Parse("{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}");
    SchemaDocument s(sd);

    Document d;
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
    GenericSchemaValidator<SchemaDocument, Writer<StringBuffer> > validator(s, writer);

    d.Parse("\"red\"");
    EXPECT_TRUE(d.Accept(validator));
    EXPECT_TRUE(validator.IsValid());
    EXPECT_STREQ("\"red\"", sb.GetString());

    sb.Clear();
    validator.Reset();
    d.Parse("\"ABCD\"");
    EXPECT_FALSE(d.Accept(validator));
    EXPECT_FALSE(validator.IsValid());
    EXPECT_TRUE(validator.GetInvalidSchemaPointer() == SchemaDocument::PointerType(""));
    EXPECT_TRUE(validator.GetInvalidDocumentPointer() == SchemaDocument::PointerType(""));
}
Ejemplo n.º 7
0
void convertMultiple(const char* nameJSON, const char* nameCSV)	{
	//Prepare Input
	struct stat statbuf;
	stat(nameJSON, &statbuf);
	size_t fsize = statbuf.st_size;

	int fd = open(nameJSON, O_RDONLY);
	if (fd == -1) {
		throw runtime_error(string("json.open"));
	}

	const char *bufJSON = (const char*) mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0);
	if (bufJSON == MAP_FAILED) {
		throw runtime_error(string("json.mmap"));
	}

	//Prepare output
	ofstream outFile;
	outFile.open(nameCSV);

	//Input loop
	size_t obj_start = 0;
	size_t obj_end = 0;
	char *line_bufJSON = NULL;
	StringBuffer buffer;
	Writer<StringBuffer> writer(buffer);
	stringstream ss;


	int flushCount = 0;
	while (obj_start < fsize) {
		size_t i = obj_start;
		for (; bufJSON[i] != '\n'; i++) {}
		obj_end = i;
		line_bufJSON = new char[obj_end - obj_start + 1];
		line_bufJSON[obj_end - obj_start] = '\0';
		memcpy(line_bufJSON, bufJSON + obj_start, obj_end - obj_start);

		//Triggering parser
		Document d;
		d.Parse(line_bufJSON);

		Value::ConstMemberIterator itrDoc = d.MemberBegin();
		Value::ConstMemberIterator itrDocEnd = d.MemberEnd();
		buffer.Clear();
		writer.Reset(buffer);

		//1st iteration unrolled to avoid unnecessary "if(first)" checks
		if (itrDoc != itrDocEnd) {
			if (itrDoc->value.IsObject()) {
				Value::ConstMemberIterator itr_ = itrDoc->value.MemberBegin();
				Value::ConstMemberIterator itrEnd_ = itrDoc->value.MemberEnd();
				iterateObject(itr_, itrEnd_, &buffer, &writer, ss);
			} else if (itrDoc->value.IsArray()) {
				Value::ConstValueIterator itr_ = itrDoc->value.Begin();
				Value::ConstValueIterator itrEnd_ = itrDoc->value.End();
				iterateArray(itr_, itrEnd_, &buffer, &writer, ss);
			} else if (itrDoc->value.IsBool()) {
				ss << itrDoc->value.GetBool();
			} else if (itrDoc->value.IsInt()) {
				ss << itrDoc->value.GetInt();
			} else if (itrDoc->value.IsInt64()) {
				ss << itrDoc->value.GetInt64();
			} else if (itrDoc->value.IsDouble()) {
				ss << itrDoc->value.GetDouble();
			} else if (itrDoc->value.IsString()) {
				ss << "\"" << itrDoc->value.GetString() << "\"";
			} else {
				throw runtime_error(string("Case missing from tokenizer"));
			}
		}
		itrDoc++;
		iterateObject(itrDoc, itrDocEnd, &buffer, &writer, ss);
		ss << "\n";

		//flushing to output file every 1000 entries
		flushCount++;
		if(flushCount % 1000 == 0)	{
			outFile << ss.rdbuf();
			ss.clear();
		}

		//Prepare next loop + cleanup
		delete line_bufJSON;
		obj_start = ++i;

	}

	outFile << ss.rdbuf();
	outFile.close();

	close(fd);
	munmap((void*) bufJSON,fsize);
}
// Removes Fallout 4 from GS, and it's config from My Games.
int RemoveFromSSGS() {

	//////// Remove Game Events & Game Name from SSGS ////////

	//////////////// -RemoveGameEvent- ////////////////

	// Data.
	const char* GameDataJson = "{\"game\":\"FALLOUT4\",\"event\":\"HEALTH\"}";
	const char* GameData2Json = "{\"game\":\"FALLOUT4\",\"event\":\"STAMINA\"}";
	const char* GameData3Json = "{\"game\":\"FALLOUT4\",\"event\":\"RADS\"}";
	const char* GameData4Json = "{\"game\":\"FALLOUT4\",\"event\":\"WEIGHT\"}";

	// Parsed JSON.
	Document GameJsonParsed;
	// Processed JSON ready be posted.
	StringBuffer Buffer;

	//// -HEALTH- ////

	// Parse.
	GameJsonParsed.Parse(GameDataJson);
	// Format into buffer.
	Writer<StringBuffer>writer(Buffer);
	GameJsonParsed.Accept(writer);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RemoveGameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -STAMINA- ////

	// Parse.
	GameJsonParsed.Parse(GameData2Json);
	// Format into buffer.
	Writer<StringBuffer>writer2(Buffer);
	GameJsonParsed.Accept(writer2);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RemoveGameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -RADS- ////

	// Parse.
	GameJsonParsed.Parse(GameData3Json);
	// Format into buffer.
	Writer<StringBuffer>writer3(Buffer);
	GameJsonParsed.Accept(writer3);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RemoveGameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -WEIGHT- ////

	// Parse.
	GameJsonParsed.Parse(GameData4Json);
	// Format into buffer.
	Writer<StringBuffer>writer4(Buffer);
	GameJsonParsed.Accept(writer4);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RemoveGameEvent);
	// Cleanup.
	Buffer.Clear();

	//////////////// -RemoveGame- ////////////////

	// Data.
	const char* GameNameJson = "{\"game\":\"FALLOUT4\"}";

	// Parse.
	GameJsonParsed.Parse(GameNameJson);
	// Format into buffer.
	Writer<StringBuffer>writer5(Buffer);
	GameJsonParsed.Accept(writer5);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RemoveGame);
	// Cleanup.
	Buffer.Clear();

	// Setup INI.
	TCHAR INIPath[MAX_PATH];

	GetEnvironmentVariable(L"USERPROFILE", INIPath, MAX_PATH);
	_tcscat_s(INIPath, MAX_PATH, L"\\Documents\\My Games\\Fallout4\\SSGSPlugin_Fallout4.ini");
	DeleteFile(INIPath);

	return 0;
}
// Adds the game to SteelSeries Engine 3 GameSense.
int AddToSSGS() {

	//////////////// -GameDataJson- ////////////////

	// Data.
	const char* GameDataJson = "{\"game\":\"FALLOUT4\",\"event\":\"HEALTH\",\"data\":{\"value\":0}}";
	const char* GameData2Json = "{\"game\":\"FALLOUT4\",\"event\":\"STAMINA\",\"data\":{\"value\":0}}";
	const char* GameData3Json = "{\"game\":\"FALLOUT4\",\"event\":\"RADS\",\"data\":{\"value\":0}}";
	const char* GameData4Json = "{\"game\":\"FALLOUT4\",\"event\":\"WEIGHT\",\"data\":{\"value\":0}}";

	// Parsed JSON.
	Document GameJsonParsed;
	// Processed JSON ready be posted.
	StringBuffer Buffer;

	//// -HEALTH- ////

	// Parse.
	GameJsonParsed.Parse(GameDataJson);
	// Insert default value.
	Value& STR = GameJsonParsed["data"]["value"];
	STR.SetInt(100); // 100 Health default.
					 // Format into buffer.
	Writer<StringBuffer>writer(Buffer);
	GameJsonParsed.Accept(writer);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_GameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -STAMINA- ////

	// Parse.
	GameJsonParsed.Parse(GameData2Json);
	// Insert default value.
	Value& STR2 = GameJsonParsed["data"]["value"];
	STR2.SetInt(100); // 100 Stamina default.
					  // Format into buffer.
	Writer<StringBuffer>writer2(Buffer);
	GameJsonParsed.Accept(writer2);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_GameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -RADS- ////

	// Parse.
	GameJsonParsed.Parse(GameData3Json);
	// Insert default value.
	Value& STR3 = GameJsonParsed["data"]["value"];
	STR3.SetInt(0); // 0 RADS default.
					// Format into buffer.
	Writer<StringBuffer>writer3(Buffer);
	GameJsonParsed.Accept(writer3);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_GameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -WEIGHT- ////

	// Parse.
	GameJsonParsed.Parse(GameData4Json);
	// Insert default value.
	Value& STR4 = GameJsonParsed["data"]["value"];
	STR4.SetInt(100); // 100 WEIGHT default.
					  // Format into buffer.
	Writer<StringBuffer>writer4(Buffer);
	GameJsonParsed.Accept(writer4);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_GameEvent);
	// Cleanup.
	Buffer.Clear();

	//////////////// -GameInterfaceJson- ////////////////

	// Data.
	const char* GameInterfaceJson = "{\"game\":\"FALLOUT4\",\"game_display_name\":\"Fallout 4\",\"icon_color_id\":5}";

	//// -Interface- ////

	// Parse.
	GameJsonParsed.Parse(GameInterfaceJson);
	// Insert default value.
	Writer<StringBuffer>writer5(Buffer);
	GameJsonParsed.Accept(writer5);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_GameMetadata);
	// Cleanup.
	Buffer.Clear();

	//////////////// -GameInterfaceEventsJson- ////////////////

	// Data.
	const char* GameInterfaceEventsJson = "{\"game\":\"FALLOUT4\",\"event\":\"HEALTH\",\"min_value\":0,\"max_value\":100,\"icon_id\":1}";
	const char* GameInterfaceEvents2Json = "{\"game\":\"FALLOUT4\",\"event\":\"STAMINA\",\"min_value\":0,\"max_value\":100,\"icon_id\":11}";
	const char* GameInterfaceEvents3Json = "{\"game\":\"FALLOUT4\",\"event\":\"RADS\",\"min_value\":0,\"max_value\":100,\"icon_id\":6}";
	const char* GameInterfaceEvents4Json = "{\"game\":\"FALLOUT4\",\"event\":\"WEIGHT\",\"min_value\":0,\"max_value\":100,\"icon_id\":17}";

	//// -HEALTH- ////

	// Parse.
	GameJsonParsed.Parse(GameInterfaceEventsJson);
	// Insert default value.
	Writer<StringBuffer>writer6(Buffer);
	GameJsonParsed.Accept(writer6);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RegisterGameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -STAMINA- ////

	// Parse.
	GameJsonParsed.Parse(GameInterfaceEvents2Json);
	// Insert default value.
	Writer<StringBuffer>writer7(Buffer);
	GameJsonParsed.Accept(writer7);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RegisterGameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -RADS- ////

	// Parse.
	GameJsonParsed.Parse(GameInterfaceEvents3Json);
	// Insert default value.
	Writer<StringBuffer>writer8(Buffer);
	GameJsonParsed.Accept(writer8);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RegisterGameEvent);
	// Cleanup.
	Buffer.Clear();

	//// -WEIGHT- ////

	// Parse.
	GameJsonParsed.Parse(GameInterfaceEvents4Json);
	// Insert default value.
	Writer<StringBuffer>writer9(Buffer);
	GameJsonParsed.Accept(writer9);
	// Post.
	PostJson(Buffer, SSGS_ServerAddress_RegisterGameEvent);
	// Cleanup.
	Buffer.Clear();

	// Setup INI.
	PWSTR INIPathPTR = 0;
	TCHAR INIPath[MAX_PATH];

	// Get paths.
	SHGetKnownFolderPath(FOLDERID_Documents, NULL, NULL, &INIPathPTR);
	_tcscpy_s(INIPath, MAX_PATH, INIPathPTR);
	_tcscat_s(INIPath, MAX_PATH, L"\\My Games\\Fallout4\\SSGSPlugin_Fallout4.ini");
	// Create fresh copy.
	HANDLE hFile = CreateFile(INIPath, GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	CloseHandle(hFile);
	// Write default settings.
	WritePrivateProfileString(L"General", L"bEnabled", L"1", INIPath);
	WritePrivateProfileString(L"General", L"iUpdateInterval", L"250", INIPath);
	WritePrivateProfileString(L"General", L"bConsoleLoggingEnabled", L"0", INIPath);

	return 0;
}