1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "bs_oswrap.h"
10 #include "bs_tracing.h"
11 
12 /**
13  * If the results folder for this sim_id does not exist create it
14  * return a pointer to the path string
15  * Exists with an ERROR if unable to complete
16  */
bs_create_result_folder(const char * s_id)17 char* bs_create_result_folder(const char* s_id){
18   char* results_path;
19   //we progressively check & build the directory structure up to the FIFOs (check if it is there and if not create)
20   results_path = (char*)bs_calloc(15 + strlen(s_id), sizeof(char));
21   sprintf(results_path,"../results");
22 
23   if (bs_createfolder(results_path) != 0) {
24     bs_trace_error_line("Couldn't create results folder %s\n", results_path);
25     free(results_path);
26   }
27   sprintf(results_path,"../results/%s",s_id);
28   if (bs_createfolder(results_path) != 0) {
29     bs_trace_error_line("Couldn't create results folder %s\n", results_path);
30     free(results_path);
31   }
32   return results_path;
33 }
34 
35 /**
36  * Create a results file in the results folder
37  * (if the results folder does not exist yet, it will be created)
38  * and return a pointer to the file
39  *
40  * Mode can be either "w" "r" "a" (or "w+" or "r+" "a+")
41  *
42  * The file will be:
43  *   <resultsfolderpath>/d_<device_number>.<postfix>.csv
44  *
45  *   <resultsfolderpath> = results/<s_id>
46  */
bs_create_result_file(const char * s_id,const uint dev_nbr,const char * postfix,char * mode)47 FILE* bs_create_result_file(const char* s_id, const uint dev_nbr, const char* postfix, char* mode){
48   char* results_path;
49   FILE *file_ptr;
50 
51   //Ensure folder is there, if not create:
52   results_path = bs_create_result_folder(s_id);
53 
54   //Create file in mode requested
55   char filename[18 + strlen(results_path) + strlen(postfix)];
56 
57   sprintf(filename,"%s/d_%i.%s.csv",results_path, dev_nbr, postfix);
58   free(results_path);
59 
60   file_ptr = bs_fopen(filename, mode);
61 
62   return file_ptr;
63 }
64