blc_array.cpp 9.38 KB
Newer Older
Arnaud Blanchard's avatar
Arnaud Blanchard committed
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
/* Basic Library for C/C++ (blclib)
 Copyright  ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
 
 Author: A. Blanchard
 
 This software is a computer program whose purpose is to simulate neural networks and control robots or simulations.
 This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
 You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
 As a counterpart to the access to the source code and  rights to copy, modify and redistribute granted by the license,
  users are provided only with a limited warranty and the software's author, the holder of the economic rights,  and the successive licensors have only limited liability.
  In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
  that may mean  that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
 Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
  and, more generally, to use and operate it in the same conditions as regards security.
  The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms. */

//
//  Created by Arnaud Blanchard on 17/06/2014.
//
//

#include "blc_array.h"
#include "blc_text.h"

#include <signal.h>

blc_array::blc_array():dims(NULL), dims_nb(0){};

29
30
31
32
33
void blc_array::init(char const *properties){
    sscan_properties(properties);
}


Arnaud Blanchard's avatar
Arnaud Blanchard committed
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
void blc_array::vdef_array(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){
    blc_dim *dim;
    

        this->dims_nb=dims_nb;
        this->type=type;
        this->format=format;
        dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
        
        size=get_type_size();
        
        FOR_EACH(dim, dims, dims_nb)
        {
            dim->step=size;
            size*=length;
            dim->length=length;
            length=va_arg(arguments, int);
        }
}

void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int length, ...){
    va_list arguments;
    
    va_start(arguments, length);
    vdef_array(type, format, dims_nb, length, arguments);
    va_end(arguments);
    
}

void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){

     this->type=type;
     this->format=format;
     this->dims_nb=dims_nb;

     size=get_type_size();
     this->dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
     memcpy(this->dims, dims, sizeof(blc_dim)*dims_nb);

     size =dims[dims_nb-1].length*dims[dims_nb-1].step;
}

76
77
78
79
80
81
void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_string){
    this->type=type;
    this->format=format;
    sscan_dims(dims_string);
}

Arnaud Blanchard's avatar
Arnaud Blanchard committed
82
void blc_array::destroy(){
83
84
    free();
    ::free(dims);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

void blc_array::add_dim(int length, int step){
    blc_dim dim;
    
    dim.length=length;
    dim.step=step;
    
    APPEND_ITEM(&dims, &dims_nb, &dim);
    size=step*length; //Check if it always true ??
}

void blc_array::add_dim(int length){
    if (dims_nb==0) add_dim(length, get_type_size());
    else add_dim(length, dims[dims_nb-1].length*dims[dims_nb-1].step);
}

void blc_array::set_dims(int dims_nb, int length, ...){
    blc_dim *dim;
    va_list args;

106
    ::free(dims);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

    this->dims_nb=dims_nb;
    dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
    
    size=get_type_size();
    va_start(args, length);
    FOR_EACH(dim, dims,  dims_nb)
    {
        size*=length;
        dim->step=1;
        dim->length=length;
        length=va_arg(args, int);
    }
    va_end(args);
}

int blc_array::get_type_size(){
    switch (type){
125
        case 'INT8': case 'UIN8':  return 1;
Arnaud Blanchard's avatar
Arnaud Blanchard committed
126
127
128
        case 'UI16':case 'IN16':  return 2;
        case 'FL32':case 'UI32':case 'IN32': return 4;
        case 'FL64': case'UI64':case 'IN64': return 8;
129
        case 'CHAR': case 'TEXT': EXIT_ON_ARRAY_ERROR(this, "The type is unknown. You probably mean 'INT8'");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
130
131
132
133
134
        default: EXIT_ON_ARRAY_ERROR(this, "The type is unknown.");
    }
    return -1;
}

135

Arnaud Blanchard's avatar
Arnaud Blanchard committed
136
137
138
int blc_array::sprint_dims(char *string, int string_size){
    int i, width=0;
    
139
    if (dims_nb==0)  width=snprintf(string, string_size,  "0");
Arnaud Blanchard's avatar
Arnaud Blanchard committed
140
    else {
141
142
        width=snprintf(string, string_size, "%lu", dims[0].length);
        for(i=1; i<dims_nb; i++) width+=snprintf(string+width, string_size-width, "x%lu", dims[i].length);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
143
144
145
146
147
148
149
150
151
152
153
    }
    if (width >= string_size) EXIT_ON_ERROR("The reserved size %d is too small to store the %d dims.",size, dims_nb);
    return width;
}


int blc_array::fprint_dims(FILE *file)const {
    int i, width=0;
    
    if (dims_nb==0)  width+=fprintf(file, "0");
    else {
Arnaud Blanchard's avatar
Arnaud Blanchard committed
154
155
        width+=fprintf(file, "%lu", dims[0].length);
        for(i=1; i<dims_nb; i++) width+=fprintf(file, "x%lu", dims[i].length);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    }
    return width;
}


void blc_array::fscan_dims(FILE *file){
    blc_dim *dim;
    int length;
    
    size=get_type_size();
    
    FSCANF(1, file, "%d", &length);
    dims=NULL;
    dims_nb=0;
    if (length != 0){
        do{
           dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
           dim->length=length;
           dim->step=size;
           size*=dim->length;
        }while(fscanf(file, "x%d", &length)==1);
    }
}

180
int blc_array::sscan_dims(char const *string){
Arnaud Blanchard's avatar
Arnaud Blanchard committed
181
    blc_dim *dim;
182
    int pos, total_pos;
Arnaud Blanchard's avatar
Arnaud Blanchard committed
183
    int length;
184
    
Arnaud Blanchard's avatar
Arnaud Blanchard committed
185
186
187
    size=get_type_size();
    
    SSCANF(1, string, "%d%n", &length, &pos);
188
    total_pos=pos;
Arnaud Blanchard's avatar
Arnaud Blanchard committed
189
190
191
    dims_nb=0;
    if (length != 0){
        do{
192
            total_pos+=pos;
Arnaud Blanchard's avatar
Arnaud Blanchard committed
193
194
195
196
            dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
            dim->length=length;
            dim->step=size;
            size*=dim->length;
197
        }while(sscanf(string+total_pos, "x%lu%n", &dim->length, &pos)==1);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
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
    return total_pos;
}


//Should use code in common with fprint_info
void blc_array::sprint_properties(char *buffer, size_t buffer_size){
    int width;
    uint32_t str_type;
    uint32_t str_format;
    
    if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default.");
    if (format==0) EXIT_ON_ERROR("The format should not be NULL. Use 'NDEF' by default.");
    
    width=snprintf(buffer, buffer_size, "%.4s %.4s ", UINT32_TO_STRING(str_type, type),  UINT32_TO_STRING(str_format, format));
    width+=sprint_dims(buffer+width,buffer_size-width);
}

void blc_array::fprint_properties(FILE *file){
    int width;
    uint32_t net_type = htonl(type);
    uint32_t net_format = htonl(format);
    
    if (type==0) EXIT_ON_ERROR("The type should not be NULL. Use 'NDEF' by default.");
    if (format==0) EXIT_ON_ERROR("The format should not be NULL. Use 'NDEF' by default.");
    

    fprintf(file, "%.4s %.4s ", (char*)&net_type, (char*)&net_format);
    width=fprint_dims(file);
}

//Should use code in common with fscan_info
void blc_array::sscan_properties(char const *string){
    int ret, pos;
    
    dims_nb=0;
    data=NULL;
    size=0;
    
    ret = sscanf(string, "%4c %4c%n", (char*)&type, (char*)&format, &pos);
    if (ret == EOF) EXIT_ON_ERROR("End of FILE");
    else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2 in '%s'.", ret,  string);
    
    string+=pos;
    
    NTOHL(type);
    NTOHL(format);
    
    string+=sscan_dims(string);
}

void blc_array::fscan_properties(FILE *file){
    int ret;
    dims_nb=0;
    data=NULL;
    size=0;
    
    ret = fscanf(file, "%4c %4c ", (char*)&type, (char*)&format);
    if (ret == EOF) EXIT_ON_ERROR("End of FILE");
    else if (ret!=2) EXIT_ON_ERROR("%d parameters have been read instead of 2", ret);
    
    NTOHL(type);
    NTOHL(format);
    
    fscan_dims(file);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
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
}

//usefull ??
size_t blc_array::get_minimum_size(){
    blc_dim *dim;
    size_t size = get_type_size();
    
    FOR_EACH(dim, dims, dims_nb) {
        if (dim->length==0) EXIT_ON_ERROR("Length must not be 0 on dim '%d'");
        size*=dim->length;
    }
    return size;
}

void blc_array::fprint_debug (FILE *file) const{
    uint32_t type_str, format_str;

    fprintf(file, "\nblc_array:\n type:%.4s, format:%.4s, dims_nb:%d, size:%ld \n", UINT32_TO_STRING(type_str, type), UINT32_TO_STRING(format_str, format),  dims_nb, size);
    fprint_dims(file);
    fprintf(file, "\n");
    if (data==NULL) fprintf(file, "data is null\n");
}


/* C wrapper */
extern "C" {
// Envoie un message d'erreur avec name_of_file, name_of_function, number_of_line et affiche le message formate avec les parametres variables. Puis exit le programme avec le parametre EXIT_FAILURE. To be used with EXIT_ON_ERROR.
void blc_array_fatal_error(blc_array const *array, const char *name_of_file, const char* name_of_function, int numero_of_line, const char *message, ...){
    va_list arguments;
    va_start(arguments, message);
    fprintf(stderr, "\n%s: %s \t %s \t %i :\nError: ", blc_program_name, name_of_file, name_of_function, numero_of_line);
    color_vfprintf(BLC_BRIGHT_RED, stderr, message, arguments);
    va_end(arguments);
    array->fprint_debug(stderr);
    fprintf(stderr, "\n\n");
    fflush(stderr);
    raise(SIGABRT);
    exit(EXIT_FAILURE);
}

void blc_array_destroy(blc_array *array){
  array->destroy();
}
}