-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.cc
More file actions
390 lines (334 loc) · 10.1 KB
/
Copy pathutils.cc
File metadata and controls
390 lines (334 loc) · 10.1 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "nc.h"
#include "utils.h"
/*
gf_t gf_complete_init(int w){
gf_t gfm;
if(w == 16){
printf("w = 16\n");
if (!gf_init_easy(&gfm, w)) {
printf("Bad gf spec\n");
exit(1);
}
}
#if 0
if (w == 16 || w == 32) {
if (!gf_init_hard(&gfm, w, GF_MULT_SPLIT_TABLE, GF_REGION_ALTMAP | GF_REGION_SSE, GF_DIVIDE_DEFAULT, 0, 4, w, NULL, NULL)) {
printf("Bad gf spec\n");
exit(1);
}
} else if (w == 8 || w == 64 || w == 128 || w == 4) {
if (!gf_init_easy(&gfm, w)) {
printf("Bad gf spec\n");
exit(1);
}
} else {
printf("Not supporting w = %d\n", w);
}
#endif
return gfm;
}
*/
DLLEXPORT int NC_random(int max){
return rand()%max;
}
int NC_random(int min, int max){
return rand()%(max-min)+min;
}
/* Return second level time interval */
double dt_s(struct timeval end,struct timeval start){
double ts;
ts=(end.tv_sec-start.tv_sec)+((end.tv_usec-start.tv_usec)/1000)/1000;
return ts;
}
/* Return millisecond level time interval */
double dt_ms(struct timeval end,struct timeval start){
double tm;
tm=((end.tv_sec-start.tv_sec)*1000.0)+((end.tv_usec-start.tv_usec)*1.0)/1000;
return tm;
}
/* Return microsecond level time interval */
double dt_us(struct timeval end,struct timeval start){
double tu;
tu=((end.tv_sec-start.tv_sec)*1000.0)*1000.0+(end.tv_usec-start.tv_usec);
return tu;
}
void mem_print_(unsigned char* pc, int len, int breaklen){
for(int i = 0; i < len; i++){
printf("%d ", *(pc+i));
if(((i+1)%breaklen)== 0){
printf("\n");
}
}
printf("\n");
}
void mem_print(unsigned char* pc, int len, int breaklen){
for(int i = 0; i < len; i++){
printf("%03d ", *(pc+i));
if(((i+1)%breaklen)== 0){
printf("\n");
}
}
printf("\n");
}
int Get_file_len(const string& filename){
struct stat file_info;
char filename_f[128];
FILE *fp;
sprintf(filename_f , "%s" , filename.c_str());
if((fp = fopen(filename_f , "r"))){
fclose(fp);
stat(filename_f, &file_info);
return file_info.st_size;
}else{
throw File_not_found(filename.c_str());
}
}
DLLEXPORT int NC_code_py(char *mat_c_p, int r, int c, int w, char *p_des, char *p_src, int length){
int *mat_i_p;
GMatrix mat;
mat_i_p = (int *)mat_c_p;
mat.Make_from_list(mat_i_p, r, c, w);
matrix_coding(mat, (unsigned char *)p_des, (unsigned char *)p_src, length);
return 1;
}
/* Encode a file using mat_en matrix *
* fp_src points to the file to be encoded *
* fpp_des point to the files where encoded data store */
int NC_encode_file(GMatrix mat_en, FILE *fp_src, FILE **fpp_des){
if(mat_en.rr == 0 || fp_src == NULL || fpp_des == NULL){
return 1;
}
return 1;
}
/* Decode files to a file *
* fpp_src point to the files to be decoded *
* fp_des point to the file where decoded data store */
int NC_decode_file(GMatrix mat_de, FILE **fpp_src, FILE *fp_des){
if(mat_de.rr == 0 || fpp_src == NULL || fp_des == NULL){
return 1;
}
return 1;
}
string _Trim(string ss){
static const char whitespace[] = " \n\t\v\r\f";
ss.erase(0, ss.find_first_not_of(whitespace));
ss.erase(ss.find_last_not_of(whitespace)+1);
return ss;
}
// Not consider memory overflow in buff
int Bat_Write( const string& filename,
const int& filenum,
unsigned char* out_buff,
const unsigned long& size_each_file,
const string& filepath
){
FILE * fp;
int i;
char c_filename[128];
string filename_full;
unsigned char* p_f_buff;
filename_full = filepath+filename;
for(i = 0; i < filenum; i++){
sprintf(c_filename, "%s%c%02d", filename_full.c_str(), '_', i);
if(NULL == (fp = fopen(c_filename, "w"))){
throw File_can_not_create(c_filename);
}
p_f_buff = out_buff;
p_f_buff = p_f_buff + i*size_each_file;
fwrite(p_f_buff, size_each_file, 1, fp);
fclose(fp);
}
return filenum;
}
int Bat_Write( const string& filename,
const vector<int>& files_idx_list,
unsigned char * out_buff,
const unsigned long& size_each_file,
const string& filepath
){
FILE *fp;
int amount_files = files_idx_list.size();
char c_filename[128];
unsigned char* p_f_buff = out_buff;
//printf("NOTE1\n");
for(int i = 0 ; i < amount_files ; ++i){
sprintf(c_filename , "%s%c%02d" , (filepath+filename).c_str() , '_' , files_idx_list.at(i));
//printf("%s\n", c_filename);
if(NULL == (fp = fopen(c_filename , "w"))){
throw File_not_found(c_filename);
}
p_f_buff = out_buff;
p_f_buff = p_f_buff + i*size_each_file;
fwrite(p_f_buff, size_each_file, 1, fp);
fclose(fp);
}
return amount_files;
}
int Bat_Read( const string& filename,
const vector<int>& files_idx_list,
unsigned char * in_buff,
const unsigned long& size_each_file
){
FILE *fp;
int amount_files = files_idx_list.size();
char c_filename[128];
for(int i = 0 ; i < amount_files ; ++i){
sprintf(c_filename , "%s%c%02d" , filename.c_str() , '_' , files_idx_list.at(i));
if(NULL == (fp = fopen(c_filename , "r"))){
throw File_not_found(c_filename);
}
fread(in_buff + i*size_each_file , size_each_file , 1 , fp);
fclose(fp);
}
return amount_files;
}
// Deprecated
// Not consider memory overflow in buff
int Bat_Read( string filename,
string files_index,
unsigned char* in_buff,
unsigned long size_each_file
){
stringstream ss_index(files_index);
int file_i;
int ins = 0;
char c_filename[128];
FILE* fp;
files_index = _Trim(files_index);
while(!ss_index.eof()){
ss_index >> file_i;
sprintf(c_filename, "%s%c%02d", filename.c_str(), '_', file_i);
if(NULL == (fp = fopen(c_filename, "r"))){
throw File_not_found(c_filename);
}
fread(in_buff+ins*size_each_file, size_each_file, 1, fp);
ins++;
fclose(fp);
}
return ins;
}
int Bat_Delete( string filename,
string files_index
){
stringstream ss_index(files_index);
int file_i;
int ins = 0;
char c_filename[128];
files_index = _Trim(files_index);
while(!ss_index.eof()){
ss_index >> file_i;
sprintf(c_filename, "%s%c%02d", filename.c_str(), '_', file_i);
if(remove(c_filename)!=0){
printf("Error deleting file");
}
ins++;
}
return ins;
}
int Bat_Delete( const string& filename,
const vector<int>& files_idx_list
){
int amount_files = files_idx_list.size();
char c_filename[128];
for(int i = 0 ; i < amount_files ; ++i){
sprintf(c_filename, "%s%c%02d", filename.c_str(), '_', files_idx_list.at(i));
if(remove(c_filename)!=0){
printf("Error deleting file");
}
}
return amount_files;
}
void fsread_buff(const string& filename, unsigned char* p_buff, const unsigned long& len){
FILE *fp;
unsigned char * pb = p_buff;
char filename_[128];
sprintf(filename_ , "%s" , filename.c_str());
if(NULL != (fp = fopen(filename_ , "r"))){
fread(pb , len , 1 , fp);
fclose(fp);
}else{
throw File_not_found(filename_);
}
}
void fswrite_buff(const string& filename, unsigned char* p_buff, const unsigned long& len){
FILE *fp;
unsigned char * pb = p_buff;
char filename_[128];
sprintf(filename_ , "%s" , filename.c_str());
if(NULL != (fp = fopen(filename_ , "w"))){
fwrite(pb , len , 1 , fp);
fclose(fp);
}else{
throw File_not_found(filename_);
}
}
void fswrite_buff(const string& filename, const int num , unsigned char *p_buff , const long& len){
FILE *fp;
unsigned char *pb = p_buff;
char filename_[128];
sprintf(filename_ , "%s%c%02d" , filename.c_str() , '_' , num);
if(NULL != (fp = fopen(filename_ , "w"))){
fwrite(pb , len , 1 , fp);
fclose(fp);
}else{
throw File_not_found(filename_);
}
}
void sort_v_i(vector<int> &v){
//size of vector is small, unnecessary to use recursive quick sort algorithm
int size_v = v.size();
int i,j;
int temp;
for(i = 0; i < size_v; ++i){
for(j = i; j < size_v; ++j){
if(v.at(i) > v.at(j)){
temp = v.at(i);
v.at(i) = v.at(j);
v.at(j) = temp;
}
}
}
}
// from 0,1,...,inall-1 , combination of a.size() numbers
bool next_combination(vector<int> &a, int inall){
int size;
int i;
int value;
size = a.size();
// 0 1 2 3 4
if(a.at(0) >= (inall-size)){
return false;
}
for(i = size-1; i > 0; --i){
if(a.at(i) < inall-(size-i)){
break;
}
}
value = a.at(i);
for(;i < size; ++i){
++value;
a.at(i) = value;
}
return true;
}
bool next_permutation(vector<int> &v){
int size;
int i, j;
int temp;
size = v.size();
//stop at the first place where the latter element is greater than the previous element
for(i = size-1; i > 0&&v.at(i)<v.at(i-1); --i);
if(0 == i) return false; //at the end of the permutation
//find first element after v.at(i-1) greater than it
for(j = size-1; j > i&&v.at(j)<v.at(i-1); --j);
temp = v.at(j);
v.at(j) = v.at(i-1);
v.at(i-1) = temp;
//inverse from v.at(i) to v.at(size-1)
for(j = size-1; i < j; --j, ++i){
temp = v.at(i);
v.at(i) = v.at(j);
v.at(j) = temp;
}
return true;
}