2525 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2626 THE POSSIBILITY OF SUCH DAMAGE.
2727*/
28+ #include < algorithm>
2829#include < cstdio>
2930#include < cstdlib>
3031#include < cstring>
32+ #include < fstream>
3133#include < iostream>
34+ #include < sstream>
3235#include < string>
3336#include < vector>
3437
35- #include " coreneuron/utils/nrn_assert.h"
3638#include " coreneuron/io/reports/nrnreport.hpp"
37- #include " coreneuron/sim/fast_imem.hpp"
3839#include " coreneuron/mechanism/mech_mapping.hpp"
40+ #include " coreneuron/sim/fast_imem.hpp"
41+ #include " coreneuron/utils/nrn_assert.h"
3942
4043namespace coreneuron {
4144
@@ -44,8 +47,7 @@ namespace coreneuron {
4447 * 0=Compartment, 1=Cell/Soma, Section { 2=Axon, 3=Dendrite, 4=Apical }
4548 * The "Comp" variations are compartment-based (all segments, not middle only)
4649 */
47- enum class TargetType
48- {
50+ enum class TargetType {
4951 Compartment = 0 ,
5052 Soma = 1 ,
5153 Axon = 2 ,
@@ -59,62 +61,44 @@ enum class TargetType
5961/*
6062 * Split filter string ("mech.var_name") into mech_id and var_name
6163 */
62- void parse_filter_string (char * filter, ReportConfiguration& config) {
63- char * token = strtok (filter, " ." );
64- if (!token) {
65- std::cerr << " Error : Invalid report variable, should be mch_name.var_name" << std::endl;
66- nrn_abort (1 );
67- }
68- strcpy (config.mech_name , token);
69- token = strtok (nullptr , " \n " );
70- if (!token) {
71- std::cerr << " Error : Invalid report variable, should be mch_name.var_name" << std::endl;
72- nrn_abort (1 );
73- }
74- strcpy (config.var_name , token);
64+ void parse_filter_string (const std::string &filter, ReportConfiguration &config) {
65+ std::istringstream iss (filter);
66+ std::string token;
67+ std::getline (iss, config.mech_name , ' .' );
68+ std::getline (iss, config.var_name , ' .' );
7569}
7670
77- std::vector<ReportConfiguration> create_report_configurations (const char * conf_file,
78- const char * output_dir,
79- std::string& spikes_population_name) {
71+ std::vector<ReportConfiguration> create_report_configurations (const std::string & conf_file,
72+ const std::string & output_dir,
73+ std::string & spikes_population_name) {
8074 std::vector<ReportConfiguration> reports;
81- int num_reports = 0 ;
82- char report_on[REPORT_MAX_NAME_LEN] = " " ;
83- char raw_line[REPORT_MAX_FILEPATH_LEN] = " " ;
84- char spikes_population[REPORT_MAX_NAME_LEN] = " " ;
85- TargetType target_type;
86- int * gids;
87-
88- FILE* fp = fopen (conf_file, " r" );
89- if (!fp) {
90- std::cerr << " Cannot open configuration file: " << conf_file << " , aborting execution"
91- << std::endl;
92- nrn_abort (1 );
93- }
75+ std::string report_on;
76+ int target_type;
77+ std::ifstream report_conf (conf_file);
9478
95- fgets (raw_line, REPORT_MAX_FILEPATH_LEN, fp) ;
96- sscanf (raw_line, " %d \n " , & num_reports) ;
79+ int num_reports = 0 ;
80+ report_conf >> num_reports;
9781 for (int i = 0 ; i < num_reports; i++) {
98- reports.push_back (ReportConfiguration ());
99- ReportConfiguration& report = reports[reports.size () - 1 ];
82+ ReportConfiguration report;
10083 // mechansim id registered in coreneuron
10184 report.mech_id = -1 ;
102- report.buffer_size = 4 ; // default size to 4 Mb
103- fgets (raw_line, REPORT_MAX_FILEPATH_LEN, fp);
104- sscanf (raw_line, " \n %s %s %s %s %s %s %d %lf %lf %lf %d %d %s\n " , report.name ,
105- report.target_name , report.type_str , report_on, report.unit , report.format , &target_type,
106- &report.report_dt , &report.start , &report.stop , &report.num_gids ,
107- &report.buffer_size , report.population_name );
108- for (int i = 0 ; i < REPORT_MAX_NAME_LEN; i++) {
109- report.type_str [i] = tolower (report.type_str [i]);
110- }
111- sprintf (report.output_path , " %s/%s" , output_dir, report.name );
112- if (strcmp (report.type_str , " compartment" ) == 0 ) {
113- if (strcmp (report_on, " i_membrane" ) == 0 ) {
85+ report.buffer_size = 4 ; // default size to 4 Mb
86+
87+ report_conf >> report.name >> report.target_name >> report.type_str >> report_on >>
88+ report.unit >> report.format >> target_type >> report.report_dt >>
89+ report.start >> report.stop >> report.num_gids >> report.buffer_size >>
90+ report.population_name ;
91+
92+ std::transform (report.type_str .begin (), report.type_str .end (),
93+ report.type_str .begin (),
94+ [](unsigned char c) { return std::tolower (c); });
95+ report.output_path = output_dir + " /" + report.name ;
96+ if (report.type_str == " compartment" ) {
97+ if (report_on == " i_membrane" ) {
11498 nrn_use_fast_imem = true ;
11599 report.type = IMembraneReport;
116100 } else {
117- switch (target_type) {
101+ switch (static_cast <TargetType>( target_type) ) {
118102 case TargetType::Soma:
119103 report.type = SomaReport;
120104 break ;
@@ -156,31 +140,27 @@ std::vector<ReportConfiguration> create_report_configurations(const char* conf_f
156140 nrn_abort (1 );
157141 }
158142 }
159- } else if (strcmp ( report.type_str , " synapse " ) == 0 ) {
143+ } else if (report.type_str == " synapse " ) {
160144 report.type = SynapseReport;
161145 } else {
162146 std::cerr << " Report error: unsupported type " << report.type_str << std::endl;
163147 nrn_abort (1 );
164148 }
165-
166- if (report.type == SynapseReport)
149+ if (report.type == SynapseReport) {
167150 parse_filter_string (report_on, report);
168-
151+ }
169152 if (report.num_gids ) {
170- gids = (int *)calloc (report.num_gids , sizeof (int ));
171- fread (gids, sizeof (int ), report.num_gids , fp);
172- // extra new line
173- fgets (raw_line, REPORT_MAX_FILEPATH_LEN, fp);
174- for (int i = 0 ; i < report.num_gids ; i++) {
175- report.target .insert (gids[i]);
176- }
177- free (gids);
153+ std::vector<int > new_gids (report.num_gids );
154+ report_conf.ignore (std::numeric_limits<std::streamsize>::max (), ' \n ' );
155+ report_conf.read (reinterpret_cast <char *>(new_gids.data ()), report.num_gids * sizeof (int ));
156+ report.target = std::set<int >(new_gids.begin (), new_gids.end ());
157+ // extra new line: skip
158+ report_conf.ignore (std::numeric_limits<std::streamsize>::max (), ' \n ' );
178159 }
160+ reports.push_back (report);
179161 }
180- fgets (raw_line, REPORT_MAX_NAME_LEN, fp);
181- sscanf (raw_line, " %s\n " , spikes_population);
182- spikes_population_name = spikes_population;
183- fclose (fp);
162+
163+ report_conf >> spikes_population_name;
184164 return reports;
185165}
186166} // namespace coreneuron
0 commit comments