Created
October 19, 2017 02:57
-
-
Save anonymous/6d1af24d7a0ba48d8cd381e356c9a581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char * substr(char * string, int ini, int len){ | |
char * ret = malloc(len + 1); | |
ret[len] = 0; | |
int i = ini; | |
int j = 0; | |
while (i < strlen(string) && j < len){ | |
//strcpy(ret[i], string[i]); | |
ret[j] = string[i]; | |
i++; | |
j++; | |
} | |
return ret; | |
} | |
int strsearch(char * string, char * substring, int ini){ | |
char * aux = malloc(strlen(substring) + 1); | |
aux[strlen(substring)] = 0; | |
int i = ini; | |
int j = 0; | |
int k = 0; | |
while (i < strlen(string)){ | |
j = 0; | |
k = i; | |
while (j < strlen(substring) && ( k < strlen(string) )){ | |
aux[j] = string[k]; | |
j++; | |
k++; | |
} | |
if (strcmp(substring, aux) == 0) | |
return i; | |
i++; | |
} | |
return -1; | |
} | |
char * strreplace(char * str, char * oldstr, char * newstr){ | |
char * ret = malloc(strlen(str)); | |
char * aux; | |
int pos = 0; | |
int i = 0; | |
strcpy(ret, str); | |
pos = strsearch(ret, oldstr, 0); | |
while (pos >= 0){ | |
aux = malloc(strlen(ret) -strlen(oldstr) + (strcmp(newstr, "") != 0? 0: strlen(newstr))); | |
i = 0; | |
strcpy(aux, ""); | |
while (i < pos){ | |
strncat(aux, &ret[i], 1); | |
i++; | |
} | |
if (strcmp(newstr, "") != 0) | |
strcat(aux, newstr); | |
i = pos + strlen(oldstr); | |
while (i < strlen(ret)){ | |
strncat(aux, &ret[i], 1); | |
i ++; | |
} | |
free(ret); | |
ret = aux; | |
aux = NULL; | |
pos = strsearch(ret, oldstr, 0); | |
} | |
return ret; | |
} | |
char* getAttr(char* attr, char* json, int inicio) { | |
char* retorno; | |
int auxint = 0; | |
int auxlen = 0; | |
int achoustring = 0; | |
retorno = malloc(strlen(attr) + 3); | |
strcpy(retorno, "\""); | |
strcat(retorno, attr); | |
strcat(retorno, "\""); | |
retorno[strlen(attr) + 2] = 0; | |
auxint = strsearch(json, retorno, inicio); | |
free(retorno); | |
retorno = NULL; | |
if (auxint >= 0) | |
{ | |
auxint = strsearch(json, ":", auxint + 1); | |
if (auxint >= 0) | |
{ | |
auxlen = strsearch(json, "\"", auxint + 1); | |
if (auxlen >= 0){ | |
achoustring = 1; | |
} | |
if (auxlen < 0 || strsearch(json, "\"", auxint + 1) >= 0 && strsearch(json, "\"", auxint + 1) < auxlen){ | |
auxlen = strsearch(json, ",", auxint + 1); | |
achoustring = 0; | |
} | |
if (auxlen < 0 || strsearch(json, "}", auxint + 1) >= 0 && strsearch(json, "}", auxint + 1) < auxlen){ | |
auxlen = strsearch(json,"}", auxint + 1); | |
achoustring = 0; | |
} | |
if (auxlen < 0){ | |
auxlen = strsearch(json, "\n", auxint + 1); | |
achoustring = 0; | |
} | |
if (achoustring == 1) // pega o fim da string | |
auxlen = strsearch(json, "\"", auxlen + 1); | |
if (auxlen - (auxint + 1) > 0) | |
{ | |
retorno = malloc(auxlen); | |
retorno = substr(json, auxint + 1, (auxlen - (auxint + 1))); | |
retorno = strreplace(retorno, "\"", ""); | |
} | |
} | |
} | |
return retorno; | |
} | |
struct PassoConfig { | |
int pino; | |
int tipo; | |
int valor; | |
struct PassoConfig *prox; | |
}; | |
typedef struct PassoConfig PassoConfig; | |
struct Passo { | |
int acao; | |
int pino; | |
int tipo; | |
char * valor; | |
struct Passo *prox; | |
}; | |
typedef struct Passo Passo; | |
void insere(PassoConfig *lista, int pino, int tipo, int valor){ | |
PassoConfig *p = (PassoConfig *) malloc(sizeof(PassoConfig)); | |
p->pino=pino; | |
p->tipo = tipo; | |
p->valor = valor; | |
p->prox=NULL; | |
PassoConfig *aux; | |
aux = lista; | |
while (aux->prox!=NULL){ | |
aux=aux->prox; | |
} | |
aux->prox=p; | |
} | |
PassoConfig * fromJsonPassoConfig(char * json){ | |
PassoConfig *lista = NULL; | |
int pino = 0, tipo = 0, valor = 0; | |
char * aux; | |
int idx_passo = strsearch(json, "{", 0); | |
while (idx_passo >= 0){ | |
aux = getAttr("pino", json, idx_passo); | |
if (aux == NULL || strcmp(aux, "") == 0){ | |
if (aux == NULL) | |
aux = malloc(1); | |
strcpy(aux, "0"); | |
} | |
pino = atoi(aux); | |
free(aux); | |
aux = NULL; | |
aux = getAttr("tipo", json, idx_passo); | |
if (aux == NULL || aux == ""){ | |
if (aux == NULL) | |
aux = malloc(1); | |
aux = "0"; | |
} | |
tipo = atoi(aux); | |
free(aux); | |
aux = NULL; | |
aux = getAttr("valor", json, idx_passo); | |
if (aux == NULL || aux == ""){ | |
if (aux == NULL) | |
aux = malloc(1); | |
aux = "0"; | |
} | |
valor = atoi(aux); | |
free(aux); | |
aux = NULL; | |
if (lista){ | |
insere(lista, pino, tipo, valor); | |
}else { | |
lista = (PassoConfig *) malloc(sizeof(PassoConfig)); | |
lista->pino=pino; | |
lista->tipo=tipo; | |
lista->valor = valor; | |
lista->prox=NULL; | |
} | |
idx_passo = strsearch(json, "{", idx_passo + 1); | |
} | |
return lista; | |
} | |
void limpaLista(PassoConfig *lista){ | |
PassoConfig *aux; | |
while (lista){ | |
aux=lista; | |
lista=lista->prox; | |
free(aux); | |
aux = NULL; | |
} | |
} | |
int main() | |
{ | |
//printf("%s", substr("Hello world!", 1, 3)); | |
//printf("\n%d", strsearch("Hello world!", "o", 0)); | |
//printf("\n%s", strreplace("Hello world!", "H", "")); | |
PassoConfig *listConfig = fromJsonPassoConfig("[{\"pino\":\"2\",\"tipo\":\"10\",\"valor\":\"3\"},{\"pino\":\"2\",\"tipo\":\"40\",\"valor\":\"3\"},{\"pino\":\"2\",\"tipo\":\"80\",\"valor\":\"3\"}]"); | |
PassoConfig *aux; | |
// | |
aux=listConfig; | |
while (aux){ | |
printf("\n%d", aux->pino); | |
printf("\n%d", aux->tipo); | |
printf("\n%d", aux->valor); | |
aux=aux->prox; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment