-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuple.c
313 lines (253 loc) · 9.09 KB
/
tuple.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// tuple.c
// sd15project
//
// Created by Grupo SD015 on 22/09/14.
// Copyright (c) 2014 Grupo SD015. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tuple.h"
#include "message-private.h"
#include <assert.h>
#include "general_utils.h"
#include "inet.h"
/* Função que cria um novo tuplo (isto é, que inicializa
* a estrutura e aloca a memória necessária).
*/
struct tuple_t *tuple_create(int tuple_dim) {
//checks if required dim is valid
if ( tuple_dim <= 0)
return NULL;
//allocs memory
struct tuple_t * newTuple = (struct tuple_t*) malloc (sizeof(struct tuple_t));
if ( newTuple != NULL ) {
newTuple->tuple_dimension = tuple_dim;
newTuple->tuple = (char**) malloc(tuple_dim * sizeof(char*) );
}
return newTuple;
}
/*
* Função idêntica à anterior, mas com uma assinatura diferente.
*/
struct tuple_t *tuple_create2(int tuple_dim, char **tuple) {
if ( tuple_dim <= 0 || tuple == NULL)
return NULL;
struct tuple_t * newTuple = tuple_create(tuple_dim);
//if it was allocated we just need to copy the tuple values
if ( newTuple != NULL ) {
int i;
for ( i=0; i<newTuple->tuple_dimension; i++) {
newTuple->tuple[i] = tuple[i] == NULL ? NULL : strdup(tuple[i]);
}
}
return newTuple;
}
struct tuple_t ** tuple_create_array(int tuples_num) {
if ( tuples_num <= 0 ) return NULL;
return (struct tuple_t **) malloc ( sizeof(struct tuple_t *) * tuples_num);
}
void tuple_array_destroy(struct tuple_t ** tuples, int num ) {
if ( tuples != NULL ) {
int i = 0;
for ( i = 0; i < num; i++) {
tuple_destroy(tuples[i]);
}
free(tuples);
}
}
/*
* Função que destrói um bloco de dados e liberta toda a memoria.
*/
void tuple_destroy(struct tuple_t *tuple) {
if ( tuple != NULL && tuple->tuple != NULL ) {
int i;
for ( i = 0; i < tuple->tuple_dimension; i++ ) {
if (tuple->tuple[i] != NULL ) {
free(tuple->tuple[i]);
}
}
free(tuple->tuple);
free(tuple);
}
}
/*
* Função que duplica um tuplo. Quando se criam duplicados
* é necessário efetuar uma CÓPIA dos dados (e não somente alocar a
* memória necessária).
*/
struct tuple_t *tuple_dup (struct tuple_t *tuple) {
if ( tuple == NULL || tuple->tuple == NULL)
return NULL;
//if tuple is valid
return tuple_create2(tuple->tuple_dimension, tuple->tuple);
}
/********* Implementation of tuple-private.h **********/
/*
* Method that returns the iElement of a given tuple.
*/
char * tuple_element ( struct tuple_t * tuple, int iElement ) {
return tuple->tuple[iElement];
}
char * tuple_elem_str(struct tuple_t * tuple, int i) {
return tuple_element(tuple,i) == NULL ? TUPLE_ELEM_NULL : tuple_element(tuple,i);
}
/*
* Method that returns the key of a given tuple.
*/
char * tuple_key (struct tuple_t * tuple ) {
return tuple_element(tuple,0);
}
/*
* Method that returns the size of a given tuple.
*/
int tuple_size ( struct tuple_t * tuple ) {
return tuple->tuple_dimension;
}
int tuple_size_bytes ( struct tuple_t* tuple) {
int nBytes = 0;
// [dim][size_e1][bytes_e1][size_e2][bytes_e2][size_e3][bytes_e3]
nBytes+= TUPLE_DIMENSION_SIZE;
int i;
for ( i = 0; i < tuple_size(tuple); i++) {
//sums the number of bytes needed to alloc for each element of the tuple
long elementSize = tuple_element(tuple,i) == NULL ? 1 : strlen(tuple_element(tuple,i));
nBytes+= TUPLE_ELEMENTSIZE_SIZE + elementSize;
}
return nBytes;
}
int tuple_size_as_string (struct tuple_t* tuple) {
int size = 0;
int i;
for ( i = 0; i < tuple_size(tuple); i++ ) {
//each elem size is the "" and a space after
size+= ( 1 + strlen(tuple_elem_str(tuple, i)) + 1 + 1);
}
return size;
}
//format has to be:
// [dim][size_e1][bytes_e1][size_e2][bytes_e2][size_e3][bytes_e3]
// 4 4 var 4 var 4 4
// total size is 4*5 + sum lenght of e1,e2,e3
int tuple_serialize(struct tuple_t *tuple, char **buffer) {
if ( tuple == NULL)
return FAILED;
//bytes size needed to be alloc
int buffer_size = tuple_size_bytes(tuple);
//allocs memory
*buffer = (char *) malloc(buffer_size );
//to insert to the buffer
int offset = 0;
//1. insert tuple dimension
int tuple_dim_htonl = htonl(tuple_size(tuple));
//insert to buffer
memcpy((buffer[0]+offset), &tuple_dim_htonl, TUPLE_DIMENSION_SIZE);
//moves offset
offset+=TUPLE_DIMENSION_SIZE;
//serializes each element following the patter [elemSize][elemContent]
int i;
for ( i = 0; i < tuple_size(tuple); i++) {
//gets tuple element information
char* currentElementValue = tuple_element(tuple, i) == NULL ? TUPLE_ELEM_NULL : tuple_element(tuple, i);
// printf("value %d (%s) has size %lu\n",i, currentElementValue, strlen(currentElementValue));
long currentElementSize = currentElementValue == NULL ? 1 : strlen(currentElementValue);
// 1. first inserts element size
int tuple_elementSizeI_htonl = htonl(currentElementSize);
//insert to buffer
memcpy((buffer[0]+offset), &tuple_elementSizeI_htonl, TUPLE_ELEMENTSIZE_SIZE);
//moves offset
offset+=TUPLE_ELEMENTSIZE_SIZE;
//2. then inserts the string itself
// printf("tuple_serialize > element %s has size %ld\n", currentElementValue, currentElementSize);
memcpy((buffer[0]+offset), currentElementValue, currentElementSize);
offset+=currentElementSize;
}
//to make sure its working
assert(buffer_size == offset);
return buffer_size;
}
void tuple_print ( struct tuple_t * tuple ) {
if ( tuple == NULL || tuple->tuple == NULL || tuple->tuple_dimension <= 0)
printf(" <tuplo nulo> ");
else {
printf("<%s,%s,%s>", tuple_element(tuple, 0), tuple_element(tuple, 1),tuple_element(tuple, 2));
}
}
/*
* Gets a buffer with the format:
* [dim][size_e1][bytes_e1][size_e2][bytes_e2][size_e3][bytes_e3]
*/
struct tuple_t *tuple_deserialize(char *buffer, int size) {
if ( buffer == NULL )
return NULL;
//offset to read from the buffer
int offset = 0;
//1. gets the tuple dim (first part of the buffer)
int tupleSize_nl = 0;
memcpy(&tupleSize_nl, buffer+offset, TUPLE_DIMENSION_SIZE );
int tupleSize = ntohl(tupleSize_nl);
//creates a tuple to create with the content from the buffer
struct tuple_t * tuple = tuple_create(tupleSize);
//moves offset
offset+=TUPLE_DIMENSION_SIZE;
//2.gets first element size
int i;
for ( i = 0; i < tupleSize; i++ ) {
//1. gets i element size
int elementSize_nl = 0;
memcpy(&elementSize_nl, buffer+offset, TUPLE_ELEMENTSIZE_SIZE );
offset+= TUPLE_ELEMENTSIZE_SIZE;
int elementSize = ntohl(elementSize_nl);
// printf("tuple_deserialize > element %d has size nl %d and host %d\n", i, elementSize_nl, elementSize);
//memory security check !!!: if elementSize is bigger then space to
// read from buffer operation is canceled
if ( offset + elementSize > size) {
tuple_destroy(tuple);
return NULL;
}
//2. sets the i element value into the tuple
char * elementValue = (char*) malloc(elementSize);
memcpy(elementValue, (buffer+offset), elementSize);
tuple->tuple[i] = strncmp(elementValue, TUPLE_ELEM_NULL, 1) == 0 ? NULL : strndup(elementValue, elementSize);
// printf("tuple[%d] is %s || elementValue is %s and size %d\n",i, tuple->tuple[i], elementValue, elementSize);
free(elementValue);
offset+=elementSize;
}
//returns it
return tuple;
}
char * tuple_to_string( struct tuple_t * tuple ) {
int size = 0;
int i;
for (i = 0; i < tuple_size(tuple); i++)
size+= strlen(tuple_elem_str(tuple, i)) + 3 ; //+3 is the two " " and the space
char * tuple_string = malloc (size);
sprintf(tuple_string, "\"%s\" \"%s\" \"%s\"", tuple_elem_str(tuple,0), tuple_elem_str(tuple,1), tuple_elem_str(tuple,2));
tuple_string[size-1] = '\0';
return tuple_string;
}
//
/*
* Creates a tuple from user input.
*
*/
struct tuple_t* create_tuple_from_input (const char *user_input) {
char *token;
char *search = "\"";
char* tuple_data[3];
struct tuple_t * tuple_to_send;
char * user_input_p = strdup(user_input);
// Avanca para o primeiro elemento (salta o nome da operacao)
strtok(user_input_p, search);
int i;
for (i = 0; i< TUPLE_DIMENSION; i++ ) {
token = strtok(NULL, search);
tuple_data[i] = strcmp(token, TUPLE_ELEM_NULL) == 0 ? NULL : strdup (token);
strtok(NULL, search);
}
//creates new tuple to send
tuple_to_send = tuple_create2(TUPLE_DIMENSION, tuple_data);
free(user_input_p);
return tuple_to_send;
}