Pass values to a struct

0

Good afternoon.

I am in doubt if it is possible to pass values to a struct in C using JSON, hibernate or some other known method?

    
asked by anonymous 01.03.2016 / 20:00

1 answer

2

Okay, I've been able to solve the problem, I'll leave the code in case anyone needs it any day

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct __attribute__((__packed__)){
    char *s;
    int *x;
}my_struct;

void *chr2struct(const char *text){
    char c;
    my_struct *mst = (my_struct *) malloc(sizeof(my_struct));
    void **swap = (void **) malloc(sizeof(my_struct));
    void **res = swap;

    if(!text)
        return 0;

    int i;
    while(*text){
        c = *text;
        text++;
        char str[32];
        int x, len;
        char *data;
        switch(c){
            case 's':
            case 'c':
                sscanf(text,"%[^7]",str);
                text += strlen(str)+1;
                len = atoi(str);
                data = (void *) malloc(sizeof(char)*len);
                strcpy(data,text);
                text+= len+1;
                swap[i] = data;
                break;
            case 'i':
                sscanf(text,"%[^7]",str);
                text += strlen(str)+1;
                len = atoi(str);
                data = (void *) malloc(sizeof(int)*len);
                for(x=0; x<len; x++){
                    sscanf(text,"%i",&data[x]);
                    sprintf(str,"%i",data[x]);
                    text+=strlen(str)+1;
                    swap[i] = *data;
                }
                break;
            default:
                return 0;
        }
        i++;
    }

    return res;
}

int main(int argc, char *argv[]){
    my_struct *mst;
    mst = (my_struct *) chr2struct("s167qwertyuiopasdfgh
#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct __attribute__((__packed__)){
    char *s;
    int *x;
}my_struct;

void *chr2struct(const char *text){
    char c;
    my_struct *mst = (my_struct *) malloc(sizeof(my_struct));
    void **swap = (void **) malloc(sizeof(my_struct));
    void **res = swap;

    if(!text)
        return 0;

    int i;
    while(*text){
        c = *text;
        text++;
        char str[32];
        int x, len;
        char *data;
        switch(c){
            case 's':
            case 'c':
                sscanf(text,"%[^7]",str);
                text += strlen(str)+1;
                len = atoi(str);
                data = (void *) malloc(sizeof(char)*len);
                strcpy(data,text);
                text+= len+1;
                swap[i] = data;
                break;
            case 'i':
                sscanf(text,"%[^7]",str);
                text += strlen(str)+1;
                len = atoi(str);
                data = (void *) malloc(sizeof(int)*len);
                for(x=0; x<len; x++){
                    sscanf(text,"%i",&data[x]);
                    sprintf(str,"%i",data[x]);
                    text+=strlen(str)+1;
                    swap[i] = *data;
                }
                break;
            default:
                return 0;
        }
        i++;
    }

    return res;
}

int main(int argc, char *argv[]){
    my_struct *mst;
    mst = (my_struct *) chr2struct("s167qwertyuiopasdfgh%pre%i1716%pre%");
    printf("%s | %i\n", mst->s, mst->x);
}
i1716%pre%"); printf("%s | %i\n", mst->s, mst->x); }
    
02.03.2016 / 06:28