コード例 #1
0
ファイル: scenemanagement.cpp プロジェクト: jarikomppa/munray
Scene *loadScene(char *aFilename)
{
	Scene *t = new Scene;
	json_stream json;
	FILE * f = fopen(aFilename, "rb");

	json_open_stream(&json, f);
	json_type type = json_next(&json);
	assert(type == JSON_OBJECT);
	while (json_peek(&json) != JSON_OBJECT_END && json_peek(&json) != JSON_ERROR)
	{
		type = json_next(&json);
		assert(type == JSON_STRING);
		const char *otype = json_get_string(&json, 0);
		if (strcmp(otype, "material") == 0)
		{
			const char *name = "[untitled]";
			glm::vec3 diffuse(1);
			glm::vec3 specular(0);
			glm::vec3 ambient(0);
			float opacity = 1;
			float reflection = 0;


			type = json_next(&json);
			assert(type == JSON_OBJECT);
			while (json_peek(&json) != JSON_OBJECT_END)
			{
				type = json_next(&json);
				assert(type == JSON_STRING);
				otype = json_get_string(&json, 0);

				if (!getJSONString(&json, "name", otype, (char**)&name))
					if (!getJSONVec3(&json, "diffuse", otype, diffuse))
						if (!getJSONVec3(&json, "ambient", otype, ambient))
							if (!getJSONVec3(&json, "specular", otype, specular))
								if (!getJSONNumber(&json, "opacity", otype, opacity))
									if (!getJSONNumber(&json, "reflection", otype, reflection))
										assert(0 && "error parsing material");
			}
			type = json_next(&json);
			assert(type == JSON_OBJECT_END);
			Material *m = new Material();
			m->mName = (char*)name;
			m->mDiffuse = diffuse;
			m->mAmbient = ambient;
			m->mSpecular = specular;
			m->mOpacity = opacity;
			m->mReflection = reflection;
			m->mNext = t->mMaterial;
			t->mMaterial = m;
		}
		else
			if (strcmp(otype, "box") == 0)
			{
				const char *name = "[untitled]";
				const char *material = "default";
				glm::vec3 center;
				glm::vec3 size;
				float dynamic = 0;

				type = json_next(&json);
				assert(type == JSON_OBJECT);
				while (json_peek(&json) != JSON_OBJECT_END)
				{
					type = json_next(&json);
					assert(type == JSON_STRING);
					otype = json_get_string(&json, 0);
					if (!getJSONString(&json, "name", otype, (char**)&name))
						if (!getJSONString(&json, "material", otype, (char**)&material))
							if (!getJSONNumber(&json, "dynamic", otype, dynamic))
								if (!getJSONVec3(&json, "position", otype, center))
									if (!getJSONVec3(&json, "center", otype, center))
										if (!getJSONVec3(&json, "size", otype, size))
											assert(0 && "error parsing box");
				}
				type = json_next(&json);
				assert(type == JSON_OBJECT_END);
				SceneObject *so;
				t->insert(so = new Box((char*)name, center, size, t->getMaterialByName((char*)material)));
				so->mDynamic = dynamic != 0;
			}
			else
				if (strcmp(otype, "plane") == 0)
				{
					const char *name = "[untitled]";
					const char *material = "default";
					glm::vec3 point;
					glm::vec3 normal;
					float dynamic = 0;

					type = json_next(&json);
					assert(type == JSON_OBJECT);
					while (json_peek(&json) != JSON_OBJECT_END)
					{
						type = json_next(&json);
						assert(type == JSON_STRING);
						otype = json_get_string(&json, 0);
						if (!getJSONString(&json, "name", otype, (char**)&name))
							if (!getJSONString(&json, "material", otype, (char**)&material))
								if (!getJSONNumber(&json, "dynamic", otype, dynamic))
									if (!getJSONVec3(&json, "point", otype, point))
										if (!getJSONVec3(&json, "normal", otype, normal))
											assert(0 && "error parsing box");
					}
					type = json_next(&json);
					assert(type == JSON_OBJECT_END);
					SceneObject *so;
					t->insert(so = new Plane((char*)name, point, normal, t->getMaterialByName((char*)material)));
					so->mDynamic = dynamic != 0;
				}
				else
					if (strcmp(otype, "sphere") == 0)
					{
						const char *name = "[untitled]";
						const char *material = "default";
						glm::vec3 center;
						float radius = 5;
						float dynamic = 0;

						type = json_next(&json);
						assert(type == JSON_OBJECT);
						while (json_peek(&json) != JSON_OBJECT_END)
						{
							type = json_next(&json);
							assert(type == JSON_STRING);
							otype = json_get_string(&json, 0);
							if (!getJSONString(&json, "name", otype, (char**)&name))
								if (!getJSONString(&json, "material", otype, (char**)&material))
									if (!getJSONNumber(&json, "dynamic", otype, dynamic))
										if (!getJSONVec3(&json, "center", otype, center))
											if (!getJSONVec3(&json, "position", otype, center))
												if (!getJSONNumber(&json, "radius", otype, radius))
												assert(0 && "error parsing sphere");
						}
						type = json_next(&json);
						assert(type == JSON_OBJECT_END);
						SceneObject *so;
						t->insert(so = new Sphere((char*)name, center, radius, t->getMaterialByName((char*)material)));
						so->mDynamic = dynamic != 0;
					}
					else
						if (strcmp(otype, "light") == 0)
						{
							const char *name = "[untitled]";
							const char *material = "default";
							glm::vec3 position;

							type = json_next(&json);
							assert(type == JSON_OBJECT);
							while (json_peek(&json) != JSON_OBJECT_END)
							{
								type = json_next(&json);
								assert(type == JSON_STRING);
								otype = json_get_string(&json, 0);
								if (!getJSONString(&json, "name", otype, (char**)&name))
									if (!getJSONString(&json, "material", otype, (char**)&material))
										if (!getJSONVec3(&json, "position", otype, position))
											assert(0 && "error parsing light");
							}
							type = json_next(&json);
							assert(type == JSON_OBJECT_END);
							t->insert(new Light((char*)name, position, t->getMaterialByName((char*)material)));
						}
						else
						{
							assert(0);
						}
	}
	type = json_next(&json);
	if (type == JSON_ERROR)
	{
		const char * err = json_get_error(&json);
		err = err;
	}
	assert(type == JSON_OBJECT_END);
	type = json_next(&json);
	assert(type == JSON_DONE);
	json_close(&json);

	setupScene(t);

	t->optimize();
	return t;
};
コード例 #2
0
ファイル: ParserImpl.cpp プロジェクト: obiltschnig/poco
void ParserImpl::handle()
{
	enum json_type type = json_next(_pJSON);
	switch (type)
	{
		case JSON_DONE:
			return;
		case JSON_NULL:
			_pHandler->null();
			break;
		case JSON_TRUE:
			if (_pHandler) _pHandler->value(true);
			break;
		case JSON_FALSE:
			if (_pHandler) _pHandler->value(false);
			break;
		case JSON_NUMBER:
		{
			if (_pHandler)
			{
				std::string str(json_get_string(_pJSON, NULL));
				if (str.find(_decimalPoint) != str.npos || str.find('e') != str.npos || str.find('E') != str.npos)
				{
					_pHandler->value(NumberParser::parseFloat(str));
				}
				else
				{
					Poco::Int64 val;
					if (NumberParser::tryParse64(str, val))
						_pHandler->value(val);
					else
						_pHandler->value(NumberParser::parseUnsigned64(str));
				}
			}
			break;
		}
		case JSON_STRING:
			if (_pHandler) _pHandler->value(std::string(json_get_string(_pJSON, NULL)));
			break;
		case JSON_OBJECT:
			if (_pHandler) _pHandler->startObject();
			handleObject();
			break;
		case JSON_OBJECT_END:
			if (_pHandler) _pHandler->endObject();
			return;
		case JSON_ARRAY:
			if (_pHandler) _pHandler->startArray();
			handleArray();
			break;
		case JSON_ARRAY_END:
			if (_pHandler) _pHandler->endArray();
			return;
		case JSON_ERROR:
		{
			const char* pErr = json_get_error(_pJSON);
			std::string err(pErr ? pErr : "JSON parser error.");
			throw JSONException(err);
		}
	}
}
コード例 #3
0
ファイル: ParserImpl.cpp プロジェクト: obiltschnig/poco
bool ParserImpl::checkError()
{
	const char* err = json_get_error(_pJSON);
	if (err) throw Poco::JSON::JSONException(err);
	return true;
}
コード例 #4
0
ファイル: config.cpp プロジェクト: chenbk85/tps5
int config_put_json(DB_OBJECT *object, unsigned char *json, size_t len)
{
	char *ptr;

	error_ptr = no_error;
	error_len = sizeof(no_error) - 1;

	if (json_parse(json, len, keys, KEYS_COUNT)) {

		unsigned char *err = json_get_error(json, len);

		error_buf[sizeof(error_buf) - 1] = '\0';
		strncpy((char *)error_buf, (char *)err, sizeof(error_buf) - 1);
		error_len = strlen((char *)error_buf);

		error_ptr = error_buf;

		json_free_error(err);

		return -1;
	}

	BLOB_RECORD *config = (BLOB_RECORD *)object->module_data;

	if ((config == NULL)||(object->module_data_size != sizeof(BLOB_RECORD))) {
		strncpy((char *)error_buf, "Invalid config blob", sizeof(error_buf) - 1);
		error_ptr = error_buf;
		error_len = 19;
		return -1;
	}

	for (int i = 0; i < KEYS_COUNT; i++) {
		switch (fields[i].type) {

		case VALUE_TYPE_NUMERIC:

			switch (fields[i].size) {
			case sizeof(unsigned char):
				*(unsigned char *)((unsigned char*)config + fields[i].offset) = keys[i].int_val;
				break;
			case sizeof(unsigned short):
				*(unsigned short *)((unsigned char*)config + fields[i].offset) = keys[i].int_val;
				break;
			case sizeof(unsigned int):
				*(unsigned int *)((unsigned char*)config + fields[i].offset) = keys[i].int_val;
				break;
			}

			break;

		case VALUE_TYPE_FLOAT:

			*(float *)((unsigned char*)config + fields[i].offset) = keys[i].float_val;

			break;

		case VALUE_TYPE_BOOL:

			*(bool *)((unsigned char*)config + fields[i].offset) = keys[i].bool_val;

			break;

		case VALUE_TYPE_STRING:

			ptr = (char*)config + fields[i].offset;

			ptr[fields[i].size - 1] = '\0';

			strncpy(ptr, keys[i].string_val.c_str(), fields[i].size - 1);

			break;

		case VALUE_TYPE_NULL:
		case VALUE_TYPE_OBJECT:
		case VALUE_TYPE_ARRAY:
			break;
		}
	}

	config->need_profile = true;
	config->timestamp = time(NULL);

	if (memcmp(config->fw, "default", 8)) {
		char *p = config->fw;
		while ((*p != '\0')&&((*p < '0')||(*p > '9')))
			p++;
		config->requested_fw_ver = strtoul(p, NULL, 10);
	}
	else
		config->requested_fw_ver = 0;

	if (api_db_update_object_blob(object) != 0) {

		error_buf[sizeof(error_buf) - 1] = '\0';
		strncpy((char *)error_buf, (char *)api_db_get_error(), sizeof(error_buf) - 1);
		error_len = strlen((char *)error_buf);
		error_ptr = error_buf;
				
		return -1;
	}

	return 0;
}