-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.c
More file actions
754 lines (642 loc) · 21.5 KB
/
plot.c
File metadata and controls
754 lines (642 loc) · 21.5 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* plot.c
*
* Created on: Jul 16, 2014
* Author: aarias
*/
#include "gnuplot_i.h"
#include <dirent.h>
#include <getopt.h>
#include <ctype.h>
typedef struct event_acumulator {
char* event_name;
unsigned long long count;
}event_acumulator;
typedef struct event_acum_for_action {
char* action_name;
int events_nb;
event_acumulator *acumulator[];
}event_acum_for_action;
typedef struct event_acum_for_actor {
char *proc_file_path;
char *actor_name;
int actions_nb;
event_acum_for_action *actions[];
} event_acum_for_actor;
event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* filename, char* path_to_totals);
int get_nb_of_actors_in_dir(char* path);
unsigned long long get_number(char *line);
void search_and_plot(char *path);
void struct_test(struct event_acum_for_actor *data);
int get_events_nb(char *path);
int get_actions_nb_in_file(char* path_tofile);
int NOLABELS;
unsigned int res_x;
unsigned int res_y;
int tics_font_size;
int labels_font_size;
void printhelp(char** argv){
printf("\tPapiPlot options:\n"
"\t-p [path]"
"\t Set path to the papi-output folder generated with Papify. If not specified, current directory will be used.\n\n"
"\t-x [res_x]"
"\t Change horizontal resolution. If not specified, default is 1680.\n\n"
"\t-y [res_y]"
"\t Change vertical resolution. If not specified, default is 1050.\n\n"
"\t-t [font size]"
"\t Change tics font size. If not specified, default is 8.\n\n"
"\t-l [font size]"
"\t Change labels font size. If not specified, default is 8.\n\n"
"\t-n \t \tNo labels will be printed\n\n"
"\t-h \t \tPrint help\n\n"
"\tPapiPlot requires GNUPLOT. If not installed, assuming you are on Ubuntu, run:\n"
"\tsudo apt-get install gnuplot\n\n");
}
int main(int argc, char **argv) {
int c, len;
res_x = 1680;
res_y = 1050;
tics_font_size = 8;
labels_font_size = 8;
char* path = NULL;
char* temp;
while ((c = getopt (argc, argv, "l:t:nhp:x:y:")) != -1) {
switch (c)
{
case 'h':
printhelp(argv);
exit(0);
case 'x':
res_x = atoi(optarg);
break;
case 'y':
res_y = atoi(optarg);
break;
case 'l':
labels_font_size = atoi(optarg);
break;
case 't':
tics_font_size = atoi(optarg);
break;
case 'p':
temp = optarg;
len = strlen(temp)+2;
path = malloc(len);
strcpy(path, temp);
path[len-2]='/';
path[len-1]='\0';
break;
case 'n':
NOLABELS = 1;
break;
case '?':
if (isprint (optopt)) {
fprintf (stderr, "Unknown option '-%c'.\n", optopt);
}
else {
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
}
return 1;
default:
printhelp(argv);
abort();
}
}
if(path==NULL){
char cwd[3000];
if(getcwd(cwd, sizeof(cwd))==NULL){
perror("Error retrieving working path:");
exit(0);
}
len = strlen(cwd)+2;
path = malloc(len);
strcpy(path, cwd);
path[len-2]='/';
path[len-1]='\0';
}
search_and_plot(path);
printf("All done!\n");
return 0;
}
void configure_handle(gnuplot_ctrl * h, int number_of_events, int number_of_elements){
gnuplot_cmd(h,"reset");
gnuplot_cmd(h, "dx=.7");
gnuplot_cmd(h, "n=%d", number_of_events);
gnuplot_cmd(h,"list = ''");
gnuplot_cmd(h,"index(w) = words(substr(list, 0, strstrt(list, w)-1))");
gnuplot_cmd(h,"add_label(d) = (strstrt(list, d) == 0 ? list=list.' '.d : '')");
gnuplot_cmd(h, "set datafile separator \";\"");
gnuplot_cmd(h, "set xtics rotate by -25");
gnuplot_cmd(h, "set logscale y");
gnuplot_cmd(h, "set term png truecolor");
gnuplot_cmd(h, "set grid");
//
gnuplot_cmd(h, "set style fill transparent solid 0.5 noborder");
gnuplot_cmd(h, "set term png size %d,%d", res_x, res_y);
gnuplot_cmd(h, "set ylabel \"Number of occurrences\"");
if(number_of_elements==1){
float x_top = 0.7+number_of_events/10.0;
float x_low = -0.7;
//printf("x_top = %.2f\nx_low = %.2f\n", x_top, x_low);
gnuplot_cmd(h, "set xrange [%.2f:%.2f]", x_low, x_top);
gnuplot_cmd(h, "total_box_width_relative=0.3");
gnuplot_cmd(h, "gap_width_relative=0.1");
gnuplot_cmd(h, "d_width=(gap_width_relative+total_box_width_relative)*dx/2.");
gnuplot_cmd(h, "set boxwidth total_box_width_relative/n");
}
else {
gnuplot_cmd(h, "total_box_width_relative=0.3");
gnuplot_cmd(h, "gap_width_relative=0.1");
gnuplot_cmd(h, "d_width=(gap_width_relative+total_box_width_relative)*dx/2.");
gnuplot_cmd(h, "set boxwidth total_box_width_relative/n relative");
}
//gnuplot_cmd(h, "set xtics out");
//gnuplot_cmd(h, "set xtics nomirror");
//gnuplot_cmd(h, "set xtics autofreq");
gnuplot_cmd(h, "set xtics font \"monospace,%d\"", tics_font_size);
gnuplot_cmd(h, "set tic scale 0");
gnuplot_cmd(h, "set grid noxtics");
gnuplot_cmd(h, "set label at character 1,1 \"Generated with Papify\"");
gnuplot_cmd(h, "set format y \"%%.0se%%S\"");
}
void plot_overall(char* output_path, char *path, int number_of_actors, int number_of_events) {
int i;
gnuplot_ctrl * h ;
h = gnuplot_init() ;
//int label_rot = 20;
configure_handle(h, number_of_events, number_of_actors);
gnuplot_cmd(h, "set output \"%s/papiplot_overall_.png\"", output_path);
gnuplot_cmd(h, "set title \"Events per actor\"");
gnuplot_cmd(h, "set xlabel \"Actors\"");
//draw lines:
gnuplot_cmd(h, "plot \\");
if(number_of_events==1){
i = 0;
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:xtic(1) w boxes title columnhead,\\", path, i, i+2);
}
else if (number_of_events==0){
printf("nothing to plot\n");
exit(0);
}
else{
for(i=0;i<number_of_events;i++){
if(i == 0){//first
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:xtic(1) w boxes title columnhead,\\", path, i, i+2);
}
else if (i==(number_of_events-1)){//last
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d w boxes title columnhead,\\", path, i, i+2);
}
else{
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d w boxes title columnhead,\\", path, i, i+2);
}
}
}
//draw numbers:
//gnuplot_cmd(h, "plot \\");
if (NOLABELS)
gnuplot_cmd(h, "0 notitle"); //end plot here
else
{
if(number_of_events==1){
i = 0;
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \",5\" notitle", path, i, i+2, i+2);
}
else if (number_of_events==0){
printf("nothing to plot\n");
exit(0);
}
else{
for(i=0;i<number_of_events;i++){
if(i == 0){//first
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \"monospace,%d\" notitle,\\", path, i, i+2, i+2, labels_font_size);
}
else if (i==(number_of_events-1)){//last
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \"monospace,%d\" notitle", path, i, i+2, i+2, labels_font_size);
}
else{
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \"monospace,%d\" notitle,\\", path, i, i+2, i+2, labels_font_size);
}
}
}
}
gnuplot_close(h);
}
void plot(char* output_path, char *path, char* actor_name, int number_of_actions, int number_of_events) {
int i;
gnuplot_ctrl * h ;
h = gnuplot_init() ;
//int label_rot = 20;
configure_handle(h, number_of_events, number_of_actions);
gnuplot_cmd(h, "set output \"/%s/papiplot_%s.png\"", output_path, actor_name);
gnuplot_cmd(h, "set title \"Events in actor \\\"%s\\\" per action\"", actor_name);
gnuplot_cmd(h, "set xlabel \"Actions\"");
//draw lines:
gnuplot_cmd(h, "plot \\");
if(number_of_events==1){
i = 0;
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:xtic(1) w boxes title columnhead,\\", path, i, i+2);
}
else if (number_of_events==0){
printf("nothing to plot\n");
exit(0);
}
else{
for(i=0;i<number_of_events;i++){
if(i == 0){//first
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:xtic(1) w boxes title columnhead,\\", path, i, i+2);
}
else if (i==(number_of_events-1)){//last
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d w boxes title columnhead,\\", path, i, i+2);
//gnuplot_cmd(h, "0 notitle"); //end plot here
}
else{
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d w boxes title columnhead,\\", path, i, i+2);
}
}
}
//draw numbers:
//gnuplot_cmd(h, "plot \\");
if (NOLABELS)
gnuplot_cmd(h, "0 notitle"); //end plot here
else
{
if(number_of_events==1){
i = 0;
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \",5\" notitle", path, i, i+2, i+2);
}
else if (number_of_events==0){
printf("nothing to plot\n");
exit(0);
}
else{
for(i=0;i<number_of_events;i++){
if(i == 0){//first
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \"monospace,%d\" notitle,\\", path, i, i+2, i+2, labels_font_size);
}
else if (i==(number_of_events-1)){//last
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \"monospace,%d\" notitle", path, i, i+2, i+2, labels_font_size);
}
else{
gnuplot_cmd(h, "\"%s\" using ((d='|'.strcol(1).'|', add_label(d),index(d))+(d_width*%d)):%d:(gprintf(\"%%.2se%%S\", $%d)) w labels rotate by 20 font \"monospace,%d\" notitle,\\", path, i, i+2, i+2, labels_font_size);
}
}
}
}
gnuplot_close(h);
}
int get_events_nb(char *path) {
char buf[1500];
char *token;
int nb = 0;
FILE* file = fopen(path,"r");
if(fgets(buf,1500,file)==NULL){
return 0;
}
token = strtok(buf,";");
while(token !=NULL){
nb++;
if(strcmp(token,"\n")==0) nb--;
token = strtok(NULL,";");
}
fclose(file);
return nb-2;//-2 because of the first two columns (not events)
}
const int is_papioutput(const char *filename) {
if (strstr(filename,"papi_output") != NULL) return 1; //si el fichero no tiene extensión..
else return 0;
}
void html_init(FILE* htmlfile){
fprintf(htmlfile, "<html><head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\"><title>PAPI stats</title></head>\n"
"<body><h1>Papify stats processed with PapiPlot</h1><hr style=\"width: 100%%; height: 2px;\">\n");
}
void html_close(FILE* htmlfile, char* path){
fprintf(htmlfile, "<h2>Overall: Events per actor</h2><img alt=\"\""
"src=\"papiplot_overall_.png\">");
int events_nb = get_events_nb(path)+1;
int i, j;
char* token;
char buf[1500];
FILE* ofile = fopen(path, "r");
fprintf(htmlfile,"<table border=\"1\">"
"<td>Actor<br></td>");
if(fgets(buf, 1500, ofile)==NULL){
perror("");
}
token = strtok(buf,";");
for(i=0;i<events_nb;i++){
token = strtok(NULL,";");
fprintf(htmlfile,"<td>%s<br></td>\n", token);
}
fprintf(htmlfile,"</tr>\n");
int actors_nb = get_actions_nb_in_file(path);
for(i=0;i<actors_nb;i++){
fprintf(htmlfile,"<tr>\n");
if(fgets(buf, 1500, ofile)==NULL)
perror("");
token = strtok(buf,";");
for(j=0;j<events_nb+1;j++){
fprintf(htmlfile,"<td>%s<br></td>\n", token);
token = strtok(NULL,";");
}
fprintf(htmlfile,"</tr>\n");
}
fprintf(htmlfile,"</table>\n");
fprintf(htmlfile,"<hr style=\"width: 100%%; height: 2px;\">\n");
fprintf(htmlfile,"<br>\n");
fprintf(htmlfile, "</body></html>");
fclose(ofile);
}
void html_actor(FILE* htmlfile, event_acum_for_actor* data){
fprintf(htmlfile,"<table style=\"text-align: left; width: 100%%;\" border=\"1\" cellpadding=\"2\""
"cellspacing=\"2\">\n");
fprintf(htmlfile,"<table border=\"1\">"
"<td>Action<br></td>");
int i, j;
fprintf(htmlfile, "<h2>Stats for actor \"%s\"</h2>\n",data->actor_name);
fprintf(htmlfile, "<img alt=\"\""
"src=\"papiplot_%s.png\">\n",data->actor_name);
for(i=0; i<data->actions[0]->events_nb;i++){
fprintf(htmlfile,"<td>%s<br></td>\n", data->actions[0]->acumulator[i]->event_name);
}
fprintf(htmlfile,"</tr>\n");
for(i=0; i<data->actions_nb;i++){
fprintf(htmlfile,"<tr>\n");
fprintf(htmlfile,"<td>%s<br></td>\n", data->actions[i]->action_name);
for(j=0; j<data->actions[i]->events_nb;j++){
fprintf(htmlfile,"<td>%llu<br></td>\n", data->actions[i]->acumulator[j]->count);
}
fprintf(htmlfile,"</tr>\n");
}
fprintf(htmlfile,"</table>\n");
fprintf(htmlfile,"<hr style=\"width: 100%%; height: 2px;\">\n");
fprintf(htmlfile,"<br>\n");
}
void search_and_plot(char *path) {
char * path_to_totals = malloc(sizeof(char)*(strlen(path)+strlen("plotdata_papi_totals.csv")+2));
strcpy(path_to_totals, path);
strcat(path_to_totals,"plotdata_papi_totals.csv");
FILE* datafile = fopen(path_to_totals,"w");//reset
fclose(datafile);
char * html = malloc(sizeof(char)*(strlen(path)+strlen("papiplot.html")+2));
strcpy(html, path);
strcat(html,"papiplot.html");
FILE* htmlfile = fopen(html,"w");
html_init(htmlfile);
int actors_nb = get_nb_of_actors_in_dir(path);
event_acum_for_actor* data;
struct dirent *pDirent;
DIR *pDir;
pDir = opendir (path);
if (pDir == NULL) {
perror ("Cannot open directory");
exit(1);
}
char * path_to_csv;
while ((pDirent = readdir(pDir)) != NULL) {
if(is_papioutput(pDirent->d_name)) {
printf("Plotting %s\n", pDirent->d_name);
path_to_csv = malloc(sizeof(char)*(strlen(path)+strlen(pDirent->d_name)+2));
strcpy(path_to_csv, path);
strcat(path_to_csv,"/");
strcat(path_to_csv,pDirent->d_name);
data = process_data(path, path_to_csv, pDirent->d_name, path_to_totals);
if(data->actions_nb == 0)
printf(" no data..\n");
else
plot(path, data->proc_file_path, data->actor_name, data->actions_nb, data->actions[0]->events_nb);
html_actor(htmlfile, data);
remove(data->proc_file_path);
free(data);
}
}
printf("Plotting overalls\n");
plot_overall(path, path_to_totals, actors_nb, get_events_nb(path_to_totals)+1);
html_close(htmlfile, path_to_totals);
remove(path_to_totals);
free(path_to_totals);
free(html);
fclose(htmlfile);
closedir (pDir);
return;
}
event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* filename, char* path_to_totals){
char buf[1500];
char *token;
event_acum_for_actor* actor;
char* path_todatafile = malloc(sizeof(char)*(strlen(path_todir)+strlen("/plotdata_")+strlen(filename)+1));
char* actor_name = malloc(sizeof(char)*(strlen(filename)+1));
strcpy(actor_name,filename);
//memmove(actor_name, actor_name+12, strlen(actor_name));//or
actor_name+=12;
strcpy(path_todatafile, path_todir);
strcat(path_todatafile,"/plotdata_");
char temp2[strlen(actor_name)];
strcpy(temp2, actor_name);
strcat(path_todatafile,actor_name);
//printf("creating %s\n", path_todatafile);
strcpy(actor_name, temp2);
int events_nb = get_events_nb(path_tofile);
//printf("we have %d actions here\n",get_actions_nb_in_file(path_tofile));/////////////////////////////////////////////
//get_actions_nb_in_file(path_tofile);
int actions_nb=get_actions_nb_in_file(path_tofile);
if(actions_nb == 0)
actor = malloc(sizeof(event_acum_for_actor)+sizeof(event_acum_for_action*));//weird memory is weird..
else
actor = malloc(sizeof(event_acum_for_actor)+sizeof(event_acum_for_action*)*(actions_nb));
actor->actor_name = malloc(strlen(actor_name)+1);
strcpy(actor->actor_name,actor_name);
actor->proc_file_path = path_todatafile;
actor->actions_nb = actions_nb;
int i, j;
for(i=0;i<actions_nb;i++){
actor->actions[i] = malloc(sizeof(event_acum_for_action)+sizeof(event_acumulator)*events_nb);//weird memory is weird..
actor->actions[i]->events_nb = events_nb;
for(j=0;j<events_nb;j++){
actor->actions[i]->acumulator[j] = malloc(sizeof(event_acumulator));
}
}
for(j = 0; j <actor->actions_nb; j++){
actor->actions[j]->action_name=NULL;
for(i = 0; i <actor->actions[j]->events_nb; i++){
actor->actions[j]->acumulator[i]->count = 0;
}
}
FILE* ofile = fopen(path_tofile, "r");
if(fgets(buf,1500,ofile)==NULL) perror("");//read first line
token = strtok(buf,";");
token = strtok(NULL,";");//skip action column (actot name)
for(i=0;i<actor->actions[0]->events_nb;i++){
token = strtok(NULL,";");
for(j = 0; j <actor->actions_nb; j++){
if(token[strlen(token)-1]=='\n') token[strlen(token)-1] = '\0';
actor->actions[j]->acumulator[i]->event_name = malloc(strlen(token)+1);
strcpy(actor->actions[j]->acumulator[i]->event_name, token);
}
}
int found = -1;
int found_actions = 0;
int current;
long long unsigned int totals[events_nb];
for (i=0;i<events_nb;i++){
totals[i]=0;
}
//acumuating structures
int result; //borrar
if(actor->actions_nb != 0) {
while(fgets(buf,1500,ofile)!=NULL){
token = strtok(buf,";");
token = strtok(NULL,";");//read action name
for(i=0; i<actor->actions_nb; i++){
if(actor->actions[i]->action_name==NULL)
break;
else {
result = strcmp(actor->actions[i]->action_name,token);
if(result==0){
found = i;
break;
}
else {
}
}
}
if (found == -1){
//printf("not found, adding action %s\n", token);
actor->actions[found_actions]->action_name = malloc(strlen(token)+1);
strcpy(actor->actions[found_actions]->action_name,token);
}
current = (found == -1)? found_actions++ : found;
for(i = 0; i <actor->actions[current]->events_nb; i++){
token = strtok(NULL,";");
actor->actions[current]->acumulator[i]->count += get_number(token);
totals[i] += get_number(token);
//printf("adding %s (%llu).. totals[%d] = %llu\n", token, get_number(token), i, totals[i]);
}
found = -1;
}
}
fclose(ofile);
//writing events per action
FILE* datafile = fopen(path_todatafile,"w");
fprintf(datafile,"Action;");
for(i=0;i< events_nb;i++)
fprintf(datafile,"%s;",actor->actions[0]->acumulator[i]->event_name);
fprintf(datafile,"\n");
for(i = 0; i< actions_nb;i++){
fprintf(datafile,"%s;", actor->actions[i]->action_name);
for(j=0; j < events_nb; j++){
fprintf(datafile,"%llu;", actor->actions[i]->acumulator[j]->count);
}
fprintf(datafile,"\n");
}
fclose(datafile);
//adding to totals per actor
int label_set = 0;
datafile = fopen(path_to_totals,"r");
if(fgets(buf,1500,datafile)==NULL);//nothing wrong here!
if(strstr(buf,"Actor;")!=NULL) label_set = 1;
fclose(datafile);
//appending to totals
datafile = fopen(path_to_totals,"a");
if(label_set == 0){
fprintf(datafile,"Actor;");
for(i=0;i< events_nb;i++)
fprintf(datafile,"%s;",actor->actions[0]->acumulator[i]->event_name);
fprintf(datafile,"\n");
}
fprintf(datafile,"%s;",strtok(actor->actor_name,"."));
for(j=0; j < events_nb; j++){
fprintf(datafile,"%llu;", totals[j]);
}
fprintf(datafile,"\n");
fclose(datafile);
return actor;
}
unsigned long long get_number(char *line){ //removes quotes from string number
int lineLength = strlen(line);
int j = 0;
int i;
for (i = 0; i < lineLength; i++)
{
if (line[i] == '\\')
{
line[j++] = line[i++];
line[j++] = line[i];
if (line[i] == '\0')
break;
}
else if (line[i] != '"')
line[j++] = line[i];
}
line[j] = '\0';
//char *tempptr=NULL;
return strtoul(line,NULL,10);
}
//test structure
void struct_test(struct event_acum_for_actor *data){
int i,j;
printf("Testing structure\n");
printf("Actor name: %s\n", data->actor_name);
printf("Path to processed data file: %s\n", data->proc_file_path);
printf("Actions number: %d\n", data->actions_nb);
for(i = 0; i<data->actions_nb;i++){
printf("\tAction name: %s\n", data->actions[i]->action_name);
printf("\tNumber of events: %d\n", data->actions[i]->events_nb);
for(j=0; j < data->actions[i]->events_nb; j++){
printf("\t\tEvent: %s, total: %llu\n", data->actions[i]->acumulator[j]->event_name, data->actions[i]->acumulator[j]->count);
}
//printf("Event: %s, total: %lu\n",data->, data->acumulator->acum[i]);
}
}
int get_nb_of_actors_in_dir(char* path){
int actors_nb=0;
struct dirent *pDirent;
DIR *pDir;
pDir = opendir (path);
if (pDir == NULL) {
perror ("Cannot open directory\n");
exit(1);
}
while ((pDirent = readdir(pDir)) != NULL) {
if(is_papioutput(pDirent->d_name)) {
actors_nb++;
}
}
closedir (pDir);
return actors_nb;
}
int get_actions_nb_in_file(char* path_tofile){//weird memory allocation... ¿??¿
int actions_nb = 0;
char** theActions;
char* token;
char buf[1500];
int i;
int found = -1;
theActions = malloc(0);
FILE* ofile = fopen(path_tofile, "r");
if(fgets(buf,1500,ofile)==NULL) perror("");//skip labels lines
while(fgets(buf,1500,ofile)!=NULL){
//printf("buf = %d (%d)\n", buf,strlen(buf));
token = strtok(buf,";");
token = strtok(NULL,";");//read action name
for(i=0; i<actions_nb; i++)
if(theActions[i]!=NULL && strcmp(theActions[i],token)==0){
found = i;
break;
}
if (found == -1 && token != NULL){
theActions = realloc(theActions, sizeof(char*)*(actions_nb+2));
theActions[actions_nb] = malloc(strlen(token)+1);
theActions[actions_nb+1] = malloc(strlen(token)+1);
strcpy(theActions[actions_nb],token);
actions_nb++;
}
found = -1;
}
fclose(ofile);
for(i=0; i<actions_nb; i++)
free(theActions[i]);
free(theActions);
return actions_nb;
}