SHOW:
|
|
- or go back to the newest paste.
| 1 | typedef struct {
| |
| 2 | char simbolo; | |
| 3 | char recurso; | |
| 4 | } __attribute__((__packed__)) personajeYRecursoAsignadoStruct; | |
| 5 | ||
| 6 | typedef struct {
| |
| 7 | int32_t cantidadPersonajes; | |
| 8 | t_queue *cola; | |
| 9 | } __attribute__((__packed__)) colaAsignadosStruct; | |
| 10 | ||
| 11 | typedef struct {
| |
| 12 | int32_t length; | |
| 13 | char *data; | |
| 14 | } __attribute__((__packed__)) t_qasignados; | |
| 15 | ||
| 16 | ||
| 17 | t_qasignados *serializador_recursos(colaAsignadosStruct *self) {
| |
| 18 | char *data = malloc( sizeof( queue_size(self->cola) * sizeof(personajeYRecursoAsignadoStruct) )); | |
| 19 | t_qasignados *stream = malloc( sizeof(t_qasignados) ); | |
| 20 | int32_t offset = 0, tmp_size = 0; | |
| 21 | t_queue *aux=queue_create(); | |
| 22 | - | char nulo= '\0'; |
| 22 | + | |
| 23 | ||
| 24 | memcpy(data, &self->cantidadPersonajes, tmp_size = sizeof(int32_t)); | |
| 25 | offset = tmp_size; | |
| 26 | while (!queue_is_empty(aux)) | |
| 27 | {
| |
| 28 | memcpy(data + offset, (void *) queue_pop(aux), tmp_size = sizeof(personajeYRecursoAsignadoStruct)); | |
| 29 | offset += tmp_size; | |
| 30 | memcpy(data + offset, &nulo, tmp_size = sizeof(char)); | |
| 31 | offset += tmp_size; | |
| 32 | } | |
| 33 | ||
| 34 | stream->length = offset + tmp_size; | |
| 35 | stream->data = data; | |
| 36 | queue_destroy(aux); | |
| 37 | return stream; | |
| 38 | } | |
| 39 | ||
| 40 | colaAsignadosStruct * desserializador_recursos(t_qasignados *stream) | |
| 41 | {
| |
| 42 | colaAsignadosStruct *self = malloc(sizeof(colaAsignadosStruct)); | |
| 43 | personajeYRecursoAsignadoStruct *elem; | |
| 44 | int32_t i, offset = 0, tmp_size = 0; | |
| 45 | ||
| 46 | memcpy(&self->cantidadPersonajes, stream->data, tmp_size = sizeof(int32_t)); | |
| 47 | ||
| 48 | ||
| 49 | for (i = 0; i < self->cantidadPersonajes; i++) | |
| 50 | {
| |
| 51 | offset = tmp_size; | |
| 52 | elem = malloc(sizeof(personajeYRecursoAsignadoStruct)); | |
| 53 | memcpy((personajeYRecursoAsignadoStruct*) elem,(void *) stream->data + offset, 2); | |
| 54 | queue_push(self->cola,elem); | |
| 55 | tmp_size++; | |
| 56 | } | |
| 57 | return self; | |
| 58 | } |