예제 #1
0
int serialize_to_mpi(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize)
{
    int iType = 0;
    SciErr sciErr = getVarType(_pvCtx, _piAddr, &iType);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    switch (iType)
    {
        case sci_matrix:
            return serialize_double(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
            break;
        case sci_strings:
            return serialize_string(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
            break;
        case sci_boolean:
            return serialize_boolean(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
            break;
        case sci_sparse:
            return serialize_sparse(_pvCtx, _piAddr, _piBuffer, _piBufferSize, TRUE);
            break;
        case sci_boolean_sparse:
            return serialize_sparse(_pvCtx, _piAddr, _piBuffer, _piBufferSize, FALSE);
            break;
        case sci_ints:
            return serialize_int(_pvCtx, _piAddr, _piBuffer, _piBufferSize);
            break;
        default:
            return -1;
            break;
    }
}
예제 #2
0
// Should write versions numbers and base name
void Cacher::write_header(void){
  if (file.is_open()) file.close();
  file.open(m_cacheFilename.c_str(), ios::out|ios::binary);
  serialize_uint8(CACHER_MAJOR_VERSION);
  serialize_uint8(CACHER_MINOR_VERSION);
  serialize_string(m_baseName);
  serialize_nl();
}
예제 #3
0
void
serialize_message( struct message *msg, struct buffer *b )
{
    //buffer = malloc( sizeof(struct message) + 13 );
    //printf("%d \n", msg->m_length);

    //msg->m_length = htonl( msg->m_length );

    //memmove( buffer, msg, sizeof(struct message) );
    //memcpy( buffer + sizeof(struct message), msg.m_param, msg.m_length );
    serialize_int( msg->m_length, b );
    serialize_short( msg->m_code, b );
    serialize_string( msg->m_param, b );
}
예제 #4
0
int read_json(int argc, char **argv, char *fname, const char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, struct pool *include, int exclude_all, double droprate, int buffer, const char *tmpdir, double gamma, char *prevent) {
	int ret = EXIT_SUCCESS;

	char metaname[strlen(tmpdir) + strlen("/meta.XXXXXXXX") + 1];
	char geomname[strlen(tmpdir) + strlen("/geom.XXXXXXXX") + 1];
	char indexname[strlen(tmpdir) + strlen("/index.XXXXXXXX") + 1];

	sprintf(metaname, "%s%s", tmpdir, "/meta.XXXXXXXX");
	sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX");
	sprintf(indexname, "%s%s", tmpdir, "/index.XXXXXXXX");

	int metafd = mkstemp(metaname);
	if (metafd < 0) {
		perror(metaname);
		exit(EXIT_FAILURE);
	}
	int geomfd = mkstemp(geomname);
	if (geomfd < 0) {
		perror(geomname);
		exit(EXIT_FAILURE);
	}
	int indexfd = mkstemp(indexname);
	if (indexfd < 0) {
		perror(indexname);
		exit(EXIT_FAILURE);
	}

	FILE *metafile = fopen(metaname, "wb");
	if (metafile == NULL) {
		perror(metaname);
		exit(EXIT_FAILURE);
	}
	FILE *geomfile = fopen(geomname, "wb");
	if (geomfile == NULL) {
		perror(geomname);
		exit(EXIT_FAILURE);
	}
	FILE *indexfile = fopen(indexname, "wb");
	if (indexfile == NULL) {
		perror(indexname);
		exit(EXIT_FAILURE);
	}
	long long metapos = 0;
	long long geompos = 0;
	long long indexpos = 0;

	unlink(metaname);
	unlink(geomname);
	unlink(indexname);

	unsigned file_bbox[] = { UINT_MAX, UINT_MAX, 0, 0 };
	unsigned midx = 0, midy = 0;
	long long seq = 0;

	int nlayers = argc;
	if (nlayers == 0) {
		nlayers = 1;
	}

	int n;
	for (n = 0; n < nlayers; n++) {
		json_pull *jp;
		const char *reading;
		FILE *fp;
		long long found_hashes = 0;
		long long found_features = 0;

		if (n >= argc) {
			reading = "standard input";
			fp = stdin;
		} else {
			reading = argv[n];
			fp = fopen(argv[n], "r");
			if (fp == NULL) {
				perror(argv[n]);
				continue;
			}
		}

		jp = json_begin_file(fp);

		while (1) {
			json_object *j = json_read(jp);
			if (j == NULL) {
				if (jp->error != NULL) {
					fprintf(stderr, "%s:%d: %s\n", reading, jp->line, jp->error);
				}

				json_free(jp->root);
				break;
			}

			if (j->type == JSON_HASH) {
				found_hashes++;

				if (found_hashes == 50 && found_features == 0) {
					fprintf(stderr, "%s:%d: Not finding any GeoJSON features in input. Is your file just bare geometries?\n", reading, jp->line);
					break;
				}
			}

			json_object *type = json_hash_get(j, "type");
			if (type == NULL || type->type != JSON_STRING || strcmp(type->string, "Feature") != 0) {
				continue;
			}

			found_features++;

			json_object *geometry = json_hash_get(j, "geometry");
			if (geometry == NULL) {
				fprintf(stderr, "%s:%d: feature with no geometry\n", reading, jp->line);
				json_free(j);
				continue;
			}

			json_object *geometry_type = json_hash_get(geometry, "type");
			if (geometry_type == NULL) {
				static int warned = 0;
				if (!warned) {
					fprintf(stderr, "%s:%d: null geometry (additional not reported)\n", reading, jp->line);
					warned = 1;
				}

				json_free(j);
				continue;
			}

			if (geometry_type->type != JSON_STRING) {
				fprintf(stderr, "%s:%d: geometry without type\n", reading, jp->line);
				json_free(j);
				continue;
			}

			json_object *properties = json_hash_get(j, "properties");
			if (properties == NULL || (properties->type != JSON_HASH && properties->type != JSON_NULL)) {
				fprintf(stderr, "%s:%d: feature without properties hash\n", reading, jp->line);
				json_free(j);
				continue;
			}

			json_object *coordinates = json_hash_get(geometry, "coordinates");
			if (coordinates == NULL || coordinates->type != JSON_ARRAY) {
				fprintf(stderr, "%s:%d: feature without coordinates array\n", reading, jp->line);
				json_free(j);
				continue;
			}

			int t;
			for (t = 0; t < GEOM_TYPES; t++) {
				if (strcmp(geometry_type->string, geometry_names[t]) == 0) {
					break;
				}
			}
			if (t >= GEOM_TYPES) {
				fprintf(stderr, "%s:%d: Can't handle geometry type %s\n", reading, jp->line, geometry_type->string);
				json_free(j);
				continue;
			}

			{
				unsigned bbox[] = { UINT_MAX, UINT_MAX, 0, 0 };

				int nprop = 0;
				if (properties->type == JSON_HASH) {
					nprop = properties->length;
				}

				long long metastart = metapos;
				char *metakey[nprop];
				char *metaval[nprop];
				int metatype[nprop];
				int m = 0;

				int i;
				for (i = 0; i < nprop; i++) {
					if (properties->keys[i]->type == JSON_STRING) {
						if (exclude_all) {
							if (!is_pooled(include, properties->keys[i]->string, VT_STRING)) {
								continue;
							}
						} else if (is_pooled(exclude, properties->keys[i]->string, VT_STRING)) {
							continue;
						}

						metakey[m] = properties->keys[i]->string;

						if (properties->values[i] != NULL && properties->values[i]->type == JSON_STRING) {
							metatype[m] = VT_STRING;
							metaval[m] = properties->values[i]->string;
							m++;
						} else if (properties->values[i] != NULL && properties->values[i]->type == JSON_NUMBER) {
							metatype[m] = VT_NUMBER;
							metaval[m] = properties->values[i]->string;
							m++;
						} else if (properties->values[i] != NULL && (properties->values[i]->type == JSON_TRUE || properties->values[i]->type == JSON_FALSE)) {
							metatype[m] = VT_BOOLEAN;
							metaval[m] = properties->values[i]->type == JSON_TRUE ? "true" : "false";
							m++;
						} else if (properties->values[i] != NULL && (properties->values[i]->type == JSON_NULL)) {
							;
						} else {
							fprintf(stderr, "%s:%d: Unsupported property type for %s\n", reading, jp->line, properties->keys[i]->string);
							json_free(j);
							continue;
						}
					}
				}

				serialize_int(metafile, m, &metapos, fname);
				for (i = 0; i < m; i++) {
					serialize_int(metafile, metatype[i], &metapos, fname);
					serialize_string(metafile, metakey[i], &metapos, fname);
					serialize_string(metafile, metaval[i], &metapos, fname);
				}

				long long geomstart = geompos;

				serialize_byte(geomfile, mb_geometry[t], &geompos, fname);
				serialize_byte(geomfile, n, &geompos, fname);
				serialize_long_long(geomfile, metastart, &geompos, fname);
				parse_geometry(t, coordinates, bbox, &geompos, geomfile, VT_MOVETO, fname, jp);
				serialize_byte(geomfile, VT_END, &geompos, fname);

				/*
				 * Note that minzoom for lines is the dimension
				 * of the geometry in world coordinates, but
				 * for points is the lowest zoom level (in tiles,
				 * not in pixels) at which it should be drawn.
				 *
				 * So a line that is too small for, say, z8
				 * will have minzoom of 18 (if tile detail is 10),
				 * not 8.
				 */
				int minzoom = 0;
				if (mb_geometry[t] == VT_LINE) {
					for (minzoom = 0; minzoom < 31; minzoom++) {
						unsigned mask = 1 << (32 - (minzoom + 1));

						if (((bbox[0] & mask) != (bbox[2] & mask)) ||
						    ((bbox[1] & mask) != (bbox[3] & mask))) {
							break;
						}
					}
				} else if (mb_geometry[t] == VT_POINT) {
					double r = ((double) rand()) / RAND_MAX;
					if (r == 0) {
						r = .00000001;
					}
					minzoom = maxzoom - floor(log(r) / - log(droprate));
				}

				serialize_byte(geomfile, minzoom, &geompos, fname);

				struct index index;
				index.start = geomstart;
				index.end = geompos;
				index.index = encode(bbox[0] / 2 + bbox[2] / 2, bbox[1] / 2 + bbox[3] / 2);
				fwrite_check(&index, sizeof(struct index), 1, indexfile, fname);
				indexpos += sizeof(struct index);

				for (i = 0; i < 2; i++) {
					if (bbox[i] < file_bbox[i]) {
						file_bbox[i] = bbox[i];
					}
				}
				for (i = 2; i < 4; i++) {
					if (bbox[i] > file_bbox[i]) {
						file_bbox[i] = bbox[i];
					}
				}
			
				if (seq % 10000 == 0) {
					fprintf(stderr, "Read %.2f million features\r", seq / 1000000.0);
				}
				seq++;
			}

			json_free(j);

			/* XXX check for any non-features in the outer object */
		}

		json_end(jp);
		fclose(fp);
	}

	fclose(metafile);
	fclose(geomfile);
	fclose(indexfile);

	struct stat geomst;
	struct stat metast;

	if (fstat(geomfd, &geomst) != 0) {
		perror("stat geom\n");
		exit(EXIT_FAILURE);
	}
	if (fstat(metafd, &metast) != 0) {
		perror("stat meta\n");
		exit(EXIT_FAILURE);
	}

	if (geomst.st_size == 0 || metast.st_size == 0) {
		fprintf(stderr, "did not read any valid geometries\n");
		exit(EXIT_FAILURE);
	}

	char *meta = (char *) mmap(NULL, metast.st_size, PROT_READ, MAP_PRIVATE, metafd, 0);
	if (meta == MAP_FAILED) {
		perror("mmap meta");
		exit(EXIT_FAILURE);
	}

	struct pool file_keys1[nlayers];
	struct pool *file_keys[nlayers];
	int i;
	for (i = 0; i < nlayers; i++) {
		pool_init(&file_keys1[i], 0);
		file_keys[i] = &file_keys1[i];
	}

	char *layernames[nlayers];
	for (i = 0; i < nlayers; i++) {
		if (argc <= 1 && layername != NULL) {
			layernames[i] = strdup(layername);
		} else {
			char *src = argv[i];
			if (argc < 1) {
				src = fname;
			}

			char *trunc = layernames[i] = malloc(strlen(src) + 1);
			const char *ocp, *use = src;
			for (ocp = src; *ocp; ocp++) {
				if (*ocp == '/' && ocp[1] != '\0') {
					use = ocp + 1;
				}
			}
			strcpy(trunc, use);

			char *cp = strstr(trunc, ".json");
			if (cp != NULL) {
				*cp = '\0';
			}
			cp = strstr(trunc, ".mbtiles");
			if (cp != NULL) {
				*cp = '\0';
			}
			layername = trunc;

			char *out = trunc;
			for (cp = trunc; *cp; cp++) {
				if (isalpha(*cp) || isdigit(*cp) || *cp == '_') {
					*out++ = *cp;
				}
			}
			*out = '\0';

			printf("using layer %d name %s\n", i, trunc);
		}
	}

	/* Sort the index by geometry */

	{
		int bytes = sizeof(struct index);
		fprintf(stderr, "Sorting %lld features\n", (long long) indexpos / bytes);

		int page = sysconf(_SC_PAGESIZE);
		long long unit = (50 * 1024 * 1024 / bytes) * bytes;
		while (unit % page != 0) {
			unit += bytes;
		}

		int nmerges = (indexpos + unit - 1) / unit;
		struct merge merges[nmerges];

		long long start;
		for (start = 0; start < indexpos; start += unit) {
			long long end = start + unit;
			if (end > indexpos) {
				end = indexpos;
			}

			if (nmerges != 1) {
				fprintf(stderr, "Sorting part %lld of %d\r", start / unit + 1, nmerges);
			}

			merges[start / unit].start = start;
			merges[start / unit].end = end;
			merges[start / unit].next = NULL;

			void *map = mmap(NULL, end - start, PROT_READ | PROT_WRITE, MAP_PRIVATE, indexfd, start);
			if (map == MAP_FAILED) {
				perror("mmap");
				exit(EXIT_FAILURE);
			}

			qsort(map, (end - start) / bytes, bytes, indexcmp);

			// Sorting and then copying avoids the need to
			// write out intermediate stages of the sort.

			void *map2 = mmap(NULL, end - start, PROT_READ | PROT_WRITE, MAP_SHARED, indexfd, start);
			if (map2 == MAP_FAILED) {
				perror("mmap (write)");
				exit(EXIT_FAILURE);
			}

			memcpy(map2, map, end - start);

			munmap(map, end - start);
			munmap(map2, end - start);
		}

		if (nmerges != 1) {
			fprintf(stderr, "\n");
		}

		void *map = mmap(NULL, indexpos, PROT_READ, MAP_PRIVATE, indexfd, 0);
		if (map == MAP_FAILED) {
			perror("mmap");
			exit(EXIT_FAILURE);
		}

		FILE *f = fopen(indexname, "w");
		if (f == NULL) {
			perror(indexname);
			exit(EXIT_FAILURE);
		}

		merge(merges, nmerges, (unsigned char *) map, f, bytes, indexpos / bytes);

		munmap(map, indexpos);
		fclose(f);
		close(indexfd);
	}

	/* Copy geometries to a new file in index order */

	indexfd = open(indexname, O_RDONLY);
	if (indexfd < 0) {
		perror("reopen sorted index");
		exit(EXIT_FAILURE);
	}
	struct index *index_map = mmap(NULL, indexpos, PROT_READ, MAP_PRIVATE, indexfd, 0);
	if (index_map == MAP_FAILED) {
		perror("mmap index");
		exit(EXIT_FAILURE);
	}
	unlink(indexname);

	char *geom_map = mmap(NULL, geomst.st_size, PROT_READ, MAP_PRIVATE, geomfd, 0);
	if (geom_map == MAP_FAILED) {
		perror("mmap unsorted geometry");
		exit(EXIT_FAILURE);
	}
	if (close(geomfd) != 0) {
		perror("close unsorted geometry");
	}

	sprintf(geomname, "%s%s", tmpdir, "/geom.XXXXXXXX");
	geomfd = mkstemp(geomname);
	if (geomfd < 0) {
		perror(geomname);
		exit(EXIT_FAILURE);
	}
	geomfile = fopen(geomname, "wb");
	if (geomfile == NULL) {
		perror(geomname);
		exit(EXIT_FAILURE);
	}

	{
		geompos = 0;

		/* initial tile is 0/0/0 */
		serialize_int(geomfile, 0, &geompos, fname);
		serialize_uint(geomfile, 0, &geompos, fname);
		serialize_uint(geomfile, 0, &geompos, fname);

		long long i;
		long long sum = 0;
		long long progress = 0;
		for (i = 0; i < indexpos / sizeof(struct index); i++) {
			fwrite_check(geom_map + index_map[i].start, sizeof(char), index_map[i].end - index_map[i].start, geomfile, fname);
			sum += index_map[i].end - index_map[i].start;

			long long p = 1000 * i / (indexpos / sizeof(struct index));
			if (p != progress) {
				fprintf(stderr, "Reordering geometry: %3.1f%%\r", p / 10.0);
				progress = p;
			}
		}

		/* end of tile */
		serialize_byte(geomfile, -2, &geompos, fname);
		fclose(geomfile);
	}

	if (munmap(index_map, indexpos) != 0) {
		perror("unmap sorted index");
	}
	if (munmap(geom_map, geomst.st_size) != 0) {
		perror("unmap unsorted geometry");
	}
	if (close(indexfd) != 0) {
		perror("close sorted index");
	}

	/* Traverse and split the geometries for each zoom level */

	geomfd = open(geomname, O_RDONLY);
	if (geomfd < 0) {
		perror("reopen sorted geometry");
		exit(EXIT_FAILURE);
	}
	unlink(geomname);
	if (fstat(geomfd, &geomst) != 0) {
		perror("stat sorted geom\n");
		exit(EXIT_FAILURE);
	}

	int fd[4];
	off_t size[4];

	fd[0] = geomfd;
	size[0] = geomst.st_size;

	int j;
	for (j = 1; j < 4; j++) {
		fd[j] = -1;
		size[j] = 0;
	}

	fprintf(stderr, "%lld features, %lld bytes of geometry, %lld bytes of metadata\n", seq, (long long) geomst.st_size, (long long) metast.st_size);

	int written = traverse_zooms(fd, size, meta, file_bbox, file_keys, &midx, &midy, layernames, maxzoom, minzoom, outdb, droprate, buffer, fname, tmpdir, gamma, nlayers, prevent);

	if (maxzoom != written) {
		fprintf(stderr, "\n\n\n*** NOTE TILES ONLY COMPLETE THROUGH ZOOM %d ***\n\n\n", written);
		maxzoom = written;
		ret = EXIT_FAILURE;
	}

	if (munmap(meta, metast.st_size) != 0) {
		perror("munmap meta");
	}

	if (close(metafd) < 0) {
		perror("close meta");
	}

	double minlat = 0, minlon = 0, maxlat = 0, maxlon = 0, midlat = 0, midlon = 0;

	tile2latlon(midx, midy, maxzoom, &maxlat, &minlon);
	tile2latlon(midx + 1, midy + 1, maxzoom, &minlat, &maxlon);

	midlat = (maxlat + minlat) / 2;
	midlon = (maxlon + minlon) / 2;

	tile2latlon(file_bbox[0], file_bbox[1], 32, &maxlat, &minlon);
	tile2latlon(file_bbox[2], file_bbox[3], 32, &minlat, &maxlon);

	if (midlat < minlat) {
		midlat = minlat;
	}
	if (midlat > maxlat) {
		midlat = maxlat;
	}
	if (midlon < minlon) {
		midlon = minlon;
	}
	if (midlon > maxlon) {
		midlon = maxlon;
	}

	mbtiles_write_metadata(outdb, fname, layernames, minzoom, maxzoom, minlat, minlon, maxlat, maxlon, midlat, midlon, file_keys, nlayers); // XXX layers

	for (i = 0; i < nlayers; i++) {
		pool_free_strings(&file_keys1[i]);
		free(layernames[i]);
	}
	return ret;
}
예제 #5
0
// virtual
S32 LLSDNotationFormatter::format(const LLSD& data, std::ostream& ostr, U32 options) const
{
	S32 format_count = 1;
	switch(data.type())
	{
	case LLSD::TypeMap:
	{
		ostr << "{";
		bool need_comma = false;
		LLSD::map_const_iterator iter = data.beginMap();
		LLSD::map_const_iterator end = data.endMap();
		for(; iter != end; ++iter)
		{
			if(need_comma) ostr << ",";
			need_comma = true;
			ostr << '\'';
			serialize_string((*iter).first, ostr);
			ostr << "':";
			format_count += format((*iter).second, ostr);
		}
		ostr << "}";
		break;
	}

	case LLSD::TypeArray:
	{
		ostr << "[";
		bool need_comma = false;
		LLSD::array_const_iterator iter = data.beginArray();
		LLSD::array_const_iterator end = data.endArray();
		for(; iter != end; ++iter)
		{
			if(need_comma) ostr << ",";
			need_comma = true;
			format_count += format(*iter, ostr);
		}
		ostr << "]";
		break;
	}

	case LLSD::TypeUndefined:
		ostr << "!";
		break;

	case LLSD::TypeBoolean:
		if(mBoolAlpha ||
#if( LL_WINDOWS || LL_MINGW32 || __GNUC__ > 2)
		   (ostr.flags() & std::ios::boolalpha)
#else
		   (ostr.flags() & 0x0100)
#endif
			)
		{
			ostr << (data.asBoolean()
					 ? NOTATION_TRUE_SERIAL : NOTATION_FALSE_SERIAL);
		}
		else
		{
			ostr << (data.asBoolean() ? 1 : 0);
		}
		break;

	case LLSD::TypeInteger:
		ostr << "i" << data.asInteger();
		break;

	case LLSD::TypeReal:
		ostr << "r";
		if(mRealFormat.empty())
		{
			ostr << data.asReal();
		}
		else
		{
			formatReal(data.asReal(), ostr);
		}
		break;

	case LLSD::TypeUUID:
		ostr << "u" << data.asUUID();
		break;

	case LLSD::TypeString:
		ostr << '\'';
		serialize_string(data.asString(), ostr);
		ostr << '\'';
		break;

	case LLSD::TypeDate:
		ostr << "d\"" << data.asDate() << "\"";
		break;

	case LLSD::TypeURI:
		ostr << "l\"";
		serialize_string(data.asString(), ostr);
		ostr << "\"";
		break;

	case LLSD::TypeBinary:
	{
		// *FIX: memory inefficient.
		std::vector<U8> buffer = data.asBinary();
		ostr << "b(" << buffer.size() << ")\"";
		if(buffer.size()) ostr.write((const char*)&buffer[0], buffer.size());
		ostr << "\"";
		break;
	}

	default:
		// *NOTE: This should never happen.
		ostr << "!";
		break;
	}
	return format_count;
}
예제 #6
0
// static
std::string LLSDNotationFormatter::escapeString(const std::string& in)
{
	std::ostringstream ostr;
	serialize_string(in, ostr);
	return ostr.str();
}
예제 #7
0
파일: json-builder.c 프로젝트: caryll/otfcc
void json_serialize_ex(json_char *buf, json_value *value, json_serialize_opts opts) {
	json_int_t integer, orig_integer;
	json_object_entry *entry;
	json_char *ptr;
	int indent = 0;
	char indent_char;
	int i;
	int flags;

	flags = get_serialize_flags(opts);

	indent_char = flags & f_tabs ? '\t' : ' ';

	while (value) {
		switch (value->type) {
			case json_array:

				if (((json_builder_value *)value)->length_iterated == 0) {
					if (value->u.array.length == 0) {
						*buf++ = '[';
						*buf++ = ']';

						break;
					}

					PRINT_OPENING_BRACKET('[');

					indent += opts.indent_size;
					PRINT_NEWLINE();
				}

				if (((json_builder_value *)value)->length_iterated == value->u.array.length) {
					indent -= opts.indent_size;
					PRINT_NEWLINE();
					PRINT_CLOSING_BRACKET(']');

					((json_builder_value *)value)->length_iterated = 0;
					break;
				}

				if (((json_builder_value *)value)->length_iterated > 0) {
					*buf++ = ',';

					if (flags & f_spaces_after_commas) *buf++ = ' ';

					PRINT_NEWLINE();
				}

				((json_builder_value *)value)->length_iterated++;
				value = value->u.array.values[((json_builder_value *)value)->length_iterated - 1];
				continue;

			case json_object:

				if (((json_builder_value *)value)->length_iterated == 0) {
					if (value->u.object.length == 0) {
						*buf++ = '{';
						*buf++ = '}';

						break;
					}

					PRINT_OPENING_BRACKET('{');

					indent += opts.indent_size;
					PRINT_NEWLINE();
				}

				if (((json_builder_value *)value)->length_iterated == value->u.object.length) {
					indent -= opts.indent_size;
					PRINT_NEWLINE();
					PRINT_CLOSING_BRACKET('}');

					((json_builder_value *)value)->length_iterated = 0;
					break;
				}

				if (((json_builder_value *)value)->length_iterated > 0) {
					*buf++ = ',';

					if (flags & f_spaces_after_commas) *buf++ = ' ';

					PRINT_NEWLINE();
				}

				entry = value->u.object.values + (((json_builder_value *)value)->length_iterated++);

				*buf++ = '\"';
				buf += serialize_string(buf, entry->name_length, entry->name);
				*buf++ = '\"';
				*buf++ = ':';

				if (flags & f_spaces_after_colons) *buf++ = ' ';

				value = entry->value;
				continue;
#ifdef CARYLL_USE_PRE_SERIALIZED
			case json_pre_serialized:
				memcpy(buf, value->u.string.ptr, value->u.string.length);
				buf += value->u.string.length;
				break;
#endif
			case json_string:

				*buf++ = '\"';
				buf += serialize_string(buf, value->u.string.length, value->u.string.ptr);
				*buf++ = '\"';
				break;

			case json_integer:

				integer = value->u.integer;

				if (integer < 0) {
					*buf++ = '-';
					integer = -integer;
				}

				orig_integer = integer;

				++buf;

				while (integer >= 10) {
					++buf;
					integer /= 10;
				}

				integer = orig_integer;
				ptr = buf;

				do {
					*--ptr = "0123456789"[integer % 10];
				} while ((integer /= 10) > 0);

				break;

			case json_double: {
				char tmp[256];
				emyg_dtoa(value->u.dbl, tmp);
				memcpy(buf, tmp, strlen(tmp));
				buf += strlen(tmp);
				break;
			}

			case json_boolean:

				if (value->u.boolean) {
					memcpy(buf, "true", 4);
					buf += 4;
				} else {
					memcpy(buf, "false", 5);
					buf += 5;
				}

				break;

			case json_null:

				memcpy(buf, "null", 4);
				buf += 4;
				break;

			default:
				break;
		};

		value = value->parent;
	}

	*buf = 0;
}
예제 #8
0
/**
 * Serializes the document model according to the given document type
 * and creates a byte stream from it.
 *
 * @param p0 the destination (Hand over as reference!)
 * @param p1 the destination count
 * @param p2 the destination size
 * @param p3 the source
 * @param p4 the source count
 * @param p5 the type
 * @param p6 the type count
 */
void serialize(void* p0, void* p1, void* p2, const void* p3, const void* p4,
               const void* p5, const void* p6) {

    // The comparison result.
    int r = 0;

    if (r != 1) {

        compare_arrays(p5, p6, (void*) CYBOL_ABSTRACTION, (void*) CYBOL_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_xml(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) OPERATION_ABSTRACTION, (void*) OPERATION_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_string(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) STRING_ABSTRACTION, (void*) STRING_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_string(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) BOOLEAN_ABSTRACTION, (void*) BOOLEAN_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_boolean(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) INTEGER_ABSTRACTION, (void*) INTEGER_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_integer(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) VECTOR_ABSTRACTION, (void*) VECTOR_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_vector(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) DOUBLE_ABSTRACTION, (void*) DOUBLE_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_double(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) FRACTION_ABSTRACTION, (void*) FRACTION_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_fraction(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) COMPLEX_ABSTRACTION, (void*) COMPLEX_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_complex(p0, p1, p2, p3, p4);
        }
    }

    if (r != 1) {

        compare_arrays(p5, p6, (void*) TIME_ABSTRACTION, (void*) TIME_ABSTRACTION_COUNT, (void*) &r, (void*) CHARACTER_ARRAY);

        if (r == 1) {

            serialize_time(p0, p1, p2, p3, p4);
        }
    }

    //
    // CONFIGURATION_ABSTRACTION
    //
    // CAUTION! Parameters of the internals memory MUST NOT be written
    // to the configuration file which was given at command line!
    // The CYBOI configuration file can only be edited MANUALLY.
    //

    //?? Later, distinguish file types according to abstraction,
    //?? for example xml, html, sxi, txt, rtf,
    //?? adl (from OpenEHR), KIF, ODL etc.!
    //?? For now, only the cybol file format is considered.
}
S32 LLSDNotationFormatter::format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const
{
	S32 format_count = 1;
	std::string pre;
	std::string post;

	if (options & LLSDFormatter::OPTIONS_PRETTY)
	{
		for (U32 i = 0; i < level; i++)
		{
			pre += "    ";
		}
		post = "\n";
	}

	switch(data.type())
	{
	case LLSD::TypeMap:
	{
		if (0 != level) ostr << post << pre;
		ostr << "{";
		std::string inner_pre;
		if (options & LLSDFormatter::OPTIONS_PRETTY)
		{
			inner_pre = pre + "    ";
		}

		bool need_comma = false;
		LLSD::map_const_iterator iter = data.beginMap();
		LLSD::map_const_iterator end = data.endMap();
		for(; iter != end; ++iter)
		{
			if(need_comma) ostr << ",";
			need_comma = true;
			ostr << post << inner_pre << '\'';
			serialize_string((*iter).first, ostr);
			ostr << "':";
			format_count += format_impl((*iter).second, ostr, options, level + 2);
		}
		ostr << post << pre << "}";
		break;
	}

	case LLSD::TypeArray:
	{
		ostr << post << pre << "[";
		bool need_comma = false;
		LLSD::array_const_iterator iter = data.beginArray();
		LLSD::array_const_iterator end = data.endArray();
		for(; iter != end; ++iter)
		{
			if(need_comma) ostr << ",";
			need_comma = true;
			format_count += format_impl(*iter, ostr, options, level + 1);
		}
		ostr << "]";
		break;
	}

	case LLSD::TypeUndefined:
		ostr << "!";
		break;

	case LLSD::TypeBoolean:
		if(mBoolAlpha ||
#if( LL_WINDOWS || __GNUC__ > 2)
		   (ostr.flags() & std::ios::boolalpha)
#else
		   (ostr.flags() & 0x0100)
#endif
			)
		{
			ostr << (data.asBoolean()
					 ? NOTATION_TRUE_SERIAL : NOTATION_FALSE_SERIAL);
		}
		else
		{
			ostr << (data.asBoolean() ? 1 : 0);
		}
		break;

	case LLSD::TypeInteger:
		ostr << "i" << data.asInteger();
		break;

	case LLSD::TypeReal:
		ostr << "r";
		if(mRealFormat.empty())
		{
			ostr << data.asReal();
		}
		else
		{
			formatReal(data.asReal(), ostr);
		}
		break;

	case LLSD::TypeUUID:
		ostr << "u" << data.asUUID();
		break;

	case LLSD::TypeString:
		ostr << '\'';
		serialize_string(data.asStringRef(), ostr);
		ostr << '\'';
		break;

	case LLSD::TypeDate:
		ostr << "d\"" << data.asDate() << "\"";
		break;

	case LLSD::TypeURI:
		ostr << "l\"";
		serialize_string(data.asString(), ostr);
		ostr << "\"";
		break;

	case LLSD::TypeBinary:
	{
		// *FIX: memory inefficient.
		const std::vector<U8>& buffer = data.asBinary();
		ostr << "b(" << buffer.size() << ")\"";
		if(buffer.size())
		{
			if (options & LLSDFormatter::OPTIONS_PRETTY_BINARY)
			{
				std::ios_base::fmtflags old_flags = ostr.flags();
				ostr.setf( std::ios::hex, std::ios::basefield );
				ostr << "0x";
				for (size_t i = 0; i < buffer.size(); i++)
				{
					ostr << (int) buffer[i];
				}
				ostr.flags(old_flags);
			}
			else
			{
				ostr.write((const char*)&buffer[0], buffer.size());
			}
		}
		ostr << "\"";
		break;
	}

	default:
		// *NOTE: This should never happen.
		ostr << "!";
		break;
	}
	return format_count;
}