Files
c/kubernetes/model/int_or_string.c

62 lines
1.3 KiB
C
Raw Permalink Normal View History

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "int_or_string.h"
2021-12-20 17:27:39 +08:00
int_or_string_t *int_or_string_create()
{
int_or_string_t *ios = calloc(sizeof(int_or_string_t), 1);
if (!ios) {
return NULL;
}
ios->type = IOS_DATA_TYPE_INT;
2021-12-21 19:47:08 +08:00
ios->s = NULL;
return ios;
}
2021-12-20 17:27:39 +08:00
void int_or_string_free(int_or_string_t * ios)
{
if (!ios) {
2021-12-20 17:27:39 +08:00
return;
}
if (IOS_DATA_TYPE_STRING == ios->type && ios->s) {
free(ios->s);
ios->s = NULL;
}
free(ios);
}
2021-12-20 17:27:39 +08:00
cJSON *int_or_string_convertToJSON(int_or_string_t * ios)
{
if (IOS_DATA_TYPE_INT == ios->type) {
return cJSON_CreateNumber(ios->i);
2021-12-20 17:27:39 +08:00
}
if (IOS_DATA_TYPE_STRING == ios->type && ios->s) {
return cJSON_CreateString(ios->s);
}
return NULL;
}
2021-12-20 17:27:39 +08:00
int_or_string_t *int_or_string_parseFromJSON(cJSON * cjson)
{
int_or_string_t *ios = int_or_string_create();
if (!ios) {
return NULL;
}
2021-12-20 17:27:39 +08:00
if (cJSON_IsNumber(cjson)) {
ios->i = cjson->valuedouble;
ios->type = IOS_DATA_TYPE_INT;
return ios;
}
if (cJSON_IsString(cjson)) {
ios->s = strdup(cjson->valuestring);
ios->type = IOS_DATA_TYPE_STRING;
return ios;
}
if (ios) {
int_or_string_free(ios);
ios = NULL;
}
return NULL;
2021-12-20 17:27:39 +08:00
}