Exemplo n.º 1
0
char *getXmlFromDictionary(Dictionary * myself, int tabsCount)
{
	char tabs[100];
	char buffer[4096];
	char *toReturn;
	char *ret;
	size_t toReturnSize;
	int i;
	DictValue *curValue;

	tabs[0] = '\0';
	for (i = 0; i < tabsCount; i++) {
		strcat(tabs, "\t");
	}

	sprintf(buffer, "%s<dict>\n", tabs);
	toReturnSize = sizeof(char) * (strlen(buffer) + 1);
	toReturn = malloc(toReturnSize);
	toReturn = strcpy(toReturn, buffer);

	curValue = myself->values;
	while (curValue != NULL) {
		sprintf(buffer, "%s\t<key>%s</key>\n", tabs, curValue->key);
		toReturnSize += sizeof(char) * (strlen(buffer) + 1);
		toReturn = realloc(toReturn, toReturnSize);
		toReturn = strcat(toReturn, buffer);

		if (curValue->type == DictionaryType) {
			ret =
			    getXmlFromDictionary((Dictionary *) curValue,
						 tabsCount + 1);
			toReturnSize += sizeof(char) * (strlen(ret) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, ret);
			free(ret);

		} else if (curValue->type == StringType) {
			sprintf(buffer, "%s\t<string>%s</string>\n", tabs,
				((StringValue *) curValue)->value);
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);

		} else if (curValue->type == DataType) {
			sprintf(buffer, "%s\t<data>%s</data>\n", tabs,
				((DataValue *) curValue)->value);
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);

		} else if (curValue->type == IntegerType) {
			sprintf(buffer, "%s\t<integer>%d</integer>\n", tabs,
				((IntegerValue *) curValue)->value);
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);

		} else if (curValue->type == ArrayType) {
			ret =
			    getXmlFromArrayValue((ArrayValue *) curValue,
						 tabsCount + 1);
			toReturnSize += sizeof(char) * (strlen(ret) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, ret);
			free(ret);
		} else if (curValue->type == BoolType) {
			if (((BoolValue *) curValue)->value) {
				sprintf(buffer, "%s\t<true/>\n", tabs);
			} else {
				sprintf(buffer, "%s\t<false/>\n", tabs);
			}

			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);
		}

		curValue = curValue->next;
	}

	sprintf(buffer, "%s</dict>\n", tabs);
	toReturnSize += sizeof(char) * (strlen(buffer) + 1);
	toReturn = realloc(toReturn, toReturnSize);
	toReturn = strcat(toReturn, buffer);

	return toReturn;
}
Exemplo n.º 2
0
char* getXmlFromArrayValue(ArrayValue* myself, int tabsCount) {
	char tabs[100];
	char buffer[4096];
	char* toReturn;
	char* ret;
	size_t toReturnSize;
	int i;
	DictValue* curValue;
	
	tabs[0] = '\0';
	for(i = 0; i < tabsCount; i++) {
		strcat(tabs, "\t");
	}

	
	sprintf(buffer, "%s<array>\n", tabs);
	toReturnSize = sizeof(char) * (strlen(buffer) + 1);
	toReturn = malloc(toReturnSize);
	toReturn = strcpy(toReturn, buffer);
	
	for(i = 0; i < myself->size; i++) {
		curValue = myself->values[i];

		if(curValue->type == DictionaryType) {
			ret = getXmlFromDictionary((Dictionary*)curValue, tabsCount + 1);
			toReturnSize += sizeof(char) * (strlen(ret) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, ret);
			free(ret);
		} else if(curValue->type == StringType) {
			sprintf(buffer, "%s\t<string>%s</string>\n", tabs, ((StringValue*)curValue)->value);
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);
		} else if(curValue->type == DataType) {
			char* base64 = convertBase64(((DataValue*)curValue)->value, ((DataValue*)curValue)->len, 0, -1);
			sprintf(buffer, "%s\t<data>", tabs);
			toReturnSize += sizeof(char) * (strlen(buffer) + strlen(base64) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);
			toReturn = strcat(toReturn, base64);
			sprintf(buffer, "</data>\n", tabs);
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);
			free(base64);
		} else if(curValue->type == IntegerType) {
			sprintf(buffer, "%s\t<integer>%d</integer>\n", tabs, ((IntegerValue*)curValue)->value);
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);
		} else if(curValue->type == ArrayType) {
			ret = getXmlFromArrayValue((ArrayValue*)curValue, tabsCount + 1);
			toReturnSize += sizeof(char) * (strlen(ret) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, ret);
			free(ret);
		} else if(curValue->type == BoolType) {
			if(((BoolValue*)curValue)->value) {
				sprintf(buffer, "%s\t<true/>\n", tabs);
			} else {
				sprintf(buffer, "%s\t<false/>\n", tabs);
			}
			toReturnSize += sizeof(char) * (strlen(buffer) + 1);
			toReturn = realloc(toReturn, toReturnSize);
			toReturn = strcat(toReturn, buffer);
		}
	}
	
	sprintf(buffer, "%s</array>\n", tabs);
	toReturnSize += sizeof(char) * (strlen(buffer) + 1);
	toReturn = realloc(toReturn, toReturnSize);
	toReturn = strcat(toReturn, buffer);
	
	return toReturn;
}