char *EstEID_mapToJson(EstEID_Map map) {
	size_t rLen = 3u;
	char * result = (char *) malloc(rLen);
	if (!result)
		return NULL;
	memcpy(result, "{", 2u);
	char * entry;
	for (; map; map = map->next) {
		if (map->next) {
			if (SIZE_MAX - rLen < 2u)
				goto EstEID_mapToJson_error_cleanup;
			rLen += 2u;
		}
		if (!(entry = EstEID_mapEntryToJson(*map)))
			goto EstEID_mapToJson_error_cleanup;
		size_t const eLen = strlen(entry);
		if (SIZE_MAX - rLen < eLen)
			goto EstEID_mapToJson_error_cleanup;
		rLen += eLen;
		char * const newResult = (char *) realloc(result, rLen);
		if (!newResult)
			goto EstEID_mapToJson_error_cleanup;
		result = newResult;
		strcat(result, entry);
		free(entry);
		entry = NULL;
		if (map->next) strcat(result, ", ");
	}
	strcat(result, "}");
	return result;
EstEID_mapToJson_error_cleanup:
	free(entry);
	free(result);
	return NULL;
}
예제 #2
0
char *EstEID_mapToJson(EstEID_Map map) {
	char *result = (char *)malloc(3);
	sprintf(result, "{");
	while (map) {
		char *entry = EstEID_mapEntryToJson(*map);
		result = realloc(result, strlen(result) + strlen(entry) + 4);
		strcat(result, entry);
		free(entry);
		if (map->next) strcat(result, ", ");
		map = map->next;
	}
	strcat(result, "}");
	return result;
}