77
88#include "gnuplot_i.h"
99#include <dirent.h>
10+ #include <getopt.h>
11+ #include <ctype.h>
12+
1013typedef struct event_acumulator {
1114 char * event_name ;
1215 unsigned long long count ;
@@ -113,7 +116,10 @@ int main(int argc, char **argv) {
113116
114117 if (path == NULL ){
115118 char cwd [3000 ];
116- getcwd (cwd , sizeof (cwd ));
119+ if (getcwd (cwd , sizeof (cwd ))== NULL ){
120+ perror ("Error retrieving working path:" );
121+ exit (0 );
122+ }
117123 len = strlen (cwd )+ 2 ;
118124 path = malloc (len );
119125 strcpy (path , cwd );
@@ -123,6 +129,7 @@ int main(int argc, char **argv) {
123129
124130 search_and_plot (path );
125131
132+ printf ("All done!\n" );
126133 return 0 ;
127134}
128135
@@ -311,7 +318,9 @@ int get_events_nb(char *path) {
311318 int nb = 0 ;
312319 FILE * file = fopen (path ,"r" );
313320
314- fgets (buf ,1500 ,file );
321+ if (fgets (buf ,1500 ,file )== NULL ){
322+ return 0 ;
323+ }
315324
316325 token = strtok (buf ,";" );
317326
@@ -331,8 +340,8 @@ const int is_papioutput(const char *filename) {
331340}
332341
333342void html_init (FILE * htmlfile ){
334- fprintf (htmlfile , "<html><head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\"><title>PAPI stats</title></head>"
335- "<body><h1>Papify stats processed with PapiPlot</h1><hr style=\"width: 100%; height: 2px;\">" );
343+ fprintf (htmlfile , "<html><head><meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\"><title>PAPI stats</title></head>\n "
344+ "<body><h1>Papify stats processed with PapiPlot</h1><hr style=\"width: 100%% ; height: 2px;\">\n " );
336345}
337346void html_close (FILE * htmlfile , char * path ){
338347 fprintf (htmlfile , "<h2>Overall: Events per actor</h2><img alt=\"\""
@@ -349,7 +358,11 @@ void html_close(FILE* htmlfile, char* path){
349358
350359 fprintf (htmlfile ,"<table border=\"1\">"
351360 "<td>Actor<br></td>" );
352- fgets (buf , 1500 , ofile );
361+
362+ if (fgets (buf , 1500 , ofile )== NULL ){
363+ perror ("" );
364+ }
365+
353366 token = strtok (buf ,";" );
354367 for (i = 0 ;i < events_nb ;i ++ ){
355368 token = strtok (NULL ,";" );
@@ -360,7 +373,8 @@ void html_close(FILE* htmlfile, char* path){
360373 int actors_nb = get_actions_nb_in_file (path );
361374 for (i = 0 ;i < actors_nb ;i ++ ){
362375 fprintf (htmlfile ,"<tr>\n" );
363- fgets (buf , 1500 , ofile );
376+ if (fgets (buf , 1500 , ofile )== NULL )
377+ perror ("" );
364378 token = strtok (buf ,";" );
365379 for (j = 0 ;j < events_nb + 1 ;j ++ ){
366380 fprintf (htmlfile ,"<td>%s<br></td>\n" , token );
@@ -370,7 +384,7 @@ void html_close(FILE* htmlfile, char* path){
370384 }
371385
372386 fprintf (htmlfile ,"</table>\n" );
373- fprintf (htmlfile ,"<hr style=\"width: 100%; height: 2px;\">\n" );
387+ fprintf (htmlfile ,"<hr style=\"width: 100%% ; height: 2px;\">\n" );
374388 fprintf (htmlfile ,"<br>\n" );
375389
376390
@@ -379,7 +393,7 @@ void html_close(FILE* htmlfile, char* path){
379393}
380394
381395void html_actor (FILE * htmlfile , event_acum_for_actor * data ){
382- fprintf (htmlfile ,"<table style=\"text-align: left; width: 100%;\" border=\"1\" cellpadding=\"2\""
396+ fprintf (htmlfile ,"<table style=\"text-align: left; width: 100%% ;\" border=\"1\" cellpadding=\"2\""
383397 "cellspacing=\"2\">\n" );
384398
385399 fprintf (htmlfile ,"<table border=\"1\">"
@@ -408,20 +422,20 @@ void html_actor(FILE* htmlfile, event_acum_for_actor* data){
408422 fprintf (htmlfile ,"</tr>\n" );
409423 }
410424 fprintf (htmlfile ,"</table>\n" );
411- fprintf (htmlfile ,"<hr style=\"width: 100%; height: 2px;\">\n" );
425+ fprintf (htmlfile ,"<hr style=\"width: 100%% ; height: 2px;\">\n" );
412426 fprintf (htmlfile ,"<br>\n" );
413427}
414428
415429void search_and_plot (char * path ) {
416430
417- char * path_to_totals = malloc (strlen (path )+ strlen ("plotdata_papi_totals.csv" )+ 2 );
431+ char * path_to_totals = malloc (sizeof ( char ) * ( strlen (path )+ strlen ("plotdata_papi_totals.csv" )+ 2 ) );
418432 strcpy (path_to_totals , path );
419433 strcat (path_to_totals ,"plotdata_papi_totals.csv" );
420434
421435 FILE * datafile = fopen (path_to_totals ,"w" );//reset
422436 fclose (datafile );
423437
424- char * html = malloc (strlen (path )+ strlen ("papiplot.html" )+ 2 );
438+ char * html = malloc (sizeof ( char ) * ( strlen (path )+ strlen ("papiplot.html" )+ 2 ) );
425439 strcpy (html , path );
426440 strcat (html ,"papiplot.html" );
427441 FILE * htmlfile = fopen (html ,"w" );
@@ -434,15 +448,15 @@ void search_and_plot(char *path) {
434448
435449 pDir = opendir (path );
436450 if (pDir == NULL ) {
437- perror ("Cannot open directory\n " );
451+ perror ("Cannot open directory" );
438452 exit (1 );
439453 }
440454 char * path_to_csv ;
441455
442456 while ((pDirent = readdir (pDir )) != NULL ) {
443457 if (is_papioutput (pDirent -> d_name )) {
444- // printf("going for %s\n", pDirent->d_name);
445- path_to_csv = malloc (strlen (path )+ strlen (pDirent -> d_name )+ 2 );
458+ printf ("Plotting %s\n" , pDirent -> d_name );
459+ path_to_csv = malloc (sizeof ( char ) * ( strlen (path )+ strlen (pDirent -> d_name )+ 2 ) );
446460 strcpy (path_to_csv , path );
447461 strcat (path_to_csv ,"/" );
448462 strcat (path_to_csv ,pDirent -> d_name );
@@ -453,12 +467,14 @@ void search_and_plot(char *path) {
453467 free (data );
454468 }
455469 }
456- // printf("going for overalls\n");
470+ printf ("Plotting overalls\n" );
457471 plot_overall (path , path_to_totals , actors_nb , get_events_nb (path_to_totals )+ 1 );
458472
459473 html_close (htmlfile , path_to_totals );
460474 remove (path_to_totals );
461475
476+ free (path_to_totals );
477+ free (html );
462478 fclose (htmlfile );
463479 closedir (pDir );
464480 return ;
@@ -468,47 +484,52 @@ event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* f
468484 char buf [1500 ];
469485 char * token ;
470486 event_acum_for_actor * actor ;
471- char * path_todatafile = malloc (strlen (path_todir )+ strlen ("/papidata.temp" )+ 4 );
472- char * actor_name = & (* filename )+ 12 ;
473- //printf("actor name is %s\n", actor_name);
487+ char * path_todatafile = malloc (sizeof (char )* (strlen (path_todir )+ strlen ("/plotdata_" )+ strlen (filename )+ 1 ));
488+ char * actor_name = malloc (sizeof (char )* (strlen (filename )+ 1 ));
489+ strcpy (actor_name ,filename );
490+ //memmove(actor_name, actor_name+12, strlen(actor_name));//or
491+ actor_name += 12 ;
492+
474493 strcpy (path_todatafile , path_todir );
475- strcat (path_todatafile ,"/" );
476- strcat (path_todatafile ,"plotdata_" );
494+ strcat (path_todatafile ,"/plotdata_" );
495+ char temp2 [strlen (actor_name )];
496+ strcpy (temp2 , actor_name );
477497 strcat (path_todatafile ,actor_name );
478498 //printf("creating %s\n", path_todatafile);
479499
480-
500+ strcpy ( actor_name , temp2 );
481501 int events_nb = get_events_nb (path_tofile );
482502 //printf("we have %d actions here\n",get_actions_nb_in_file(path_tofile));/////////////////////////////////////////////
483503 //get_actions_nb_in_file(path_tofile);
484504 int actions_nb = get_actions_nb_in_file (path_tofile );
485505
486- actor = malloc (sizeof (event_acum_for_actor )* 2 + sizeof (event_acum_for_action * )* (actions_nb )* 3 );//weird memory is weird..
487- actor -> actor_name = malloc (strlen (actor_name ));
506+ actor = malloc (sizeof (event_acum_for_actor )+ sizeof (event_acum_for_action * )* (actions_nb ));//weird memory is weird..
507+ actor -> actor_name = malloc (strlen (actor_name )+ 1 );
488508 strcpy (actor -> actor_name ,actor_name );
509+
489510 actor -> proc_file_path = path_todatafile ;
490511 actor -> actions_nb = actions_nb ;
491512 int i , j ;
492513
493514
494- for (i = 0 ;i < actions_nb * 3 ;i ++ ){
495- actor -> actions [i ] = malloc (sizeof (event_acum_for_action )* 2 + sizeof (event_acumulator )* events_nb * 3 );//weird memory is weird..
515+ for (i = 0 ;i < actions_nb ;i ++ ){
516+ actor -> actions [i ] = malloc (sizeof (event_acum_for_action )+ sizeof (event_acumulator )* events_nb );//weird memory is weird..
496517 actor -> actions [i ]-> events_nb = events_nb ;
497- //actor->actions[i]->action_name
498518 for (j = 0 ;j < events_nb ;j ++ ){
499519 actor -> actions [i ]-> acumulator [j ] = malloc (sizeof (event_acumulator ));
500- //actor->actions[i]->acumulator->event_name
501520 }
502521 }
503522
504523
505- for (j = 0 ; j < actor -> actions_nb ; j ++ )
524+ for (j = 0 ; j < actor -> actions_nb ; j ++ ){
525+ actor -> actions [j ]-> action_name = NULL ;
506526 for (i = 0 ; i < actor -> actions [j ]-> events_nb ; i ++ ){
507527 actor -> actions [j ]-> acumulator [i ]-> count = 0 ;
508528 }
529+ }
509530
510531 FILE * ofile = fopen (path_tofile , "r" );
511- fgets (buf ,1500 ,ofile );//read first line
532+ if ( fgets (buf ,1500 ,ofile ) == NULL ) perror ( "" );//read first line
512533 token = strtok (buf ,";" );
513534 token = strtok (NULL ,";" );//skip action column (actot name)
514535
@@ -518,18 +539,17 @@ event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* f
518539 if (token [strlen (token )- 1 ]== '\n' ) token [strlen (token )- 1 ] = '\0' ;
519540 actor -> actions [j ]-> acumulator [i ]-> event_name = malloc (strlen (token )+ 1 );
520541 strcpy (actor -> actions [j ]-> acumulator [i ]-> event_name , token );
521- //acum->acumulator->event_name[i] = malloc(strlen(token)+1);
522- //strcpy(acum->acumulator->event_name[i], token);
523542 }
524543 }
525544
526545
546+
527547 int found = -1 ;
528548 int found_actions = 0 ;
529549 int current ;
530550
531551
532- long unsigned int totals [events_nb ];
552+ long long unsigned int totals [events_nb ];
533553 for (i = 0 ;i < events_nb ;i ++ ){
534554 totals [i ]= 0 ;
535555 }
@@ -574,6 +594,7 @@ event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* f
574594 }
575595
576596
597+
577598 fclose (ofile );
578599
579600 //writing events per action
@@ -595,7 +616,7 @@ event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* f
595616 //adding to totals per actor
596617 int label_set = 0 ;
597618 datafile = fopen (path_to_totals ,"r" );
598- fgets (buf ,1500 ,datafile );
619+ if ( fgets (buf ,1500 ,datafile ) == NULL ); // perror("" );
599620 if (strstr (buf ,"Actor;" )!= NULL ) label_set = 1 ;
600621 fclose (datafile );
601622
@@ -610,7 +631,7 @@ event_acum_for_actor* process_data (char* path_todir, char* path_tofile, char* f
610631
611632 fprintf (datafile ,"%s;" ,strtok (actor -> actor_name ,"." ));
612633 for (j = 0 ; j < events_nb ; j ++ ){
613- fprintf (datafile ,"%lu ;" , totals [j ]);
634+ fprintf (datafile ,"%llu ;" , totals [j ]);
614635 }
615636 fprintf (datafile ,"\n" );
616637 fclose (datafile );
@@ -694,7 +715,7 @@ int get_actions_nb_in_file(char* path_tofile){//weird memory allocation... ¿??
694715
695716 FILE * ofile = fopen (path_tofile , "r" );
696717
697- fgets (buf ,1500 ,ofile );//skip labels lines
718+ if ( fgets (buf ,1500 ,ofile ) == NULL ) perror ( "" );//skip labels lines
698719 while (fgets (buf ,1500 ,ofile )!= NULL ){
699720 token = strtok (buf ,";" );
700721 token = strtok (NULL ,";" );//read action name
0 commit comments