From 032d22bb8765ca04d31a6fff93edbc84d4ae163d Mon Sep 17 00:00:00 2001 From: Ian Hinder Date: Fri, 26 Nov 2010 11:19:51 +0100 Subject: [PATCH 1/3] Convert to C++ strings --- src/multipole.cc | 52 ++++++++++++++++++++++++++-------------------------- src/utils.cc | 26 +++++++++++--------------- src/utils.hh | 9 ++++++--- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/multipole.cc b/src/multipole.cc index e0e51e0..975d589 100644 --- a/src/multipole.cc +++ b/src/multipole.cc @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "cctk.h" #include "cctk_Arguments.h" @@ -12,6 +14,8 @@ #include "utils.hh" #include "sphericalharmonic.hh" +using namespace std; + static const int var_name_length = 30; static const int max_vars = 10; @@ -24,7 +28,7 @@ typedef struct int index; int imag_index; int spin_weight; - char name[var_name_length]; + string name; } variable_desc; @@ -51,7 +55,7 @@ static void fill_variable(int idx, const char *optstring, void *callback_arg) // not present v->imag_index = -1; v->spin_weight = 0; - strcpy(v->name, CCTK_VarName(v->index)); + v->name = string(CCTK_VarName(v->index)); if (optstring != 0) { @@ -60,30 +64,31 @@ static void fill_variable(int idx, const char *optstring, void *callback_arg) if (table >= 0) { const int buffer_length = 256; - char imag_name[buffer_length]; + char buffer[buffer_length]; Util_TableGetInt(table, &v->spin_weight , "sw"); ///////////////////////////////////////////////////////////// CCTK_VInfo(CCTK_THORNSTRING,"spinweight %d", v->spin_weight); ///////////////////////////////////////////////////////////// - if (Util_TableGetString(table, buffer_length, imag_name , "cmplx") >= 0) + if (Util_TableGetString(table, buffer_length, buffer , "cmplx") >= 0) { - v->imag_index = CCTK_VarIndex(imag_name); + v->imag_index = CCTK_VarIndex(buffer); } - Util_TableGetString(table, var_name_length, v->name , "name"); + Util_TableGetString(table, buffer_length, buffer , "name"); + v->name = string(buffer); } } vs->n_vars++; } -static void parse_variables_string(const char *var_string, variable_desc v[max_vars], int *n_variables) +static void parse_variables_string(const string &var_string, variable_desc v[max_vars], int *n_variables) { variables_desc vars; vars.n_vars = 0; vars.vars = v; - int ierr = CCTK_TraverseString(var_string, fill_variable, &vars, CCTK_GROUP_OR_VAR); + int ierr = CCTK_TraverseString(var_string.c_str(), fill_variable, &vars, CCTK_GROUP_OR_VAR); assert(ierr > 0); *n_variables = vars.n_vars; @@ -95,12 +100,12 @@ static void output_mode(CCTK_ARGUMENTS, const variable_desc *v, CCTK_REAL rad, DECLARE_CCTK_ARGUMENTS DECLARE_CCTK_PARAMETERS - char name_tmp[1000]; - if (CCTK_MyProc(cctkGH) == 0) { - sprintf(name_tmp, "mp_%s_l%d_m%d_r%1.2f.asc", v->name, l, m, rad); - Multipole_OutputComplexToFile(CCTK_PASS_CTOC, name_tmp, real_lm, imag_lm); + ostringstream name; + name << "mp_" << v->name << "_l" << l << "_m" << m << + "_r" << setiosflags(ios::fixed) << setprecision(2) << rad << ".asc"; + Multipole_OutputComplexToFile(CCTK_PASS_CTOC, name.str(), real_lm, imag_lm); } } @@ -111,26 +116,21 @@ static void output_1D(CCTK_ARGUMENTS, const variable_desc *v, CCTK_REAL rad, DECLARE_CCTK_ARGUMENTS DECLARE_CCTK_PARAMETERS - char name_tmp[1000]; - if (CCTK_MyProc(cctkGH) == 0) { if (out_1d_every != 0 && cctk_iteration % out_1d_every == 0) { - const char *real_name = CCTK_VarName(v->index); - sprintf(name_tmp, "mp_%s_r%1.2f.th.asc", real_name, rad); - Multipole_Output1D(CCTK_PASS_CTOC, name_tmp, array_size, th, ph, mp_theta, real); - sprintf(name_tmp, "mp_%s_r%1.2f.ph.asc", real_name, rad); - Multipole_Output1D(CCTK_PASS_CTOC, name_tmp, array_size, th, ph, mp_phi, real); + ostringstream real_base; + real_base << "mp_" << string(CCTK_VarName(v->index)) << "_r" << setiosflags(ios::fixed) << setprecision(2) << rad; + Multipole_Output1D(CCTK_PASS_CTOC, real_base.str()+string(".th.asc"), array_size, th, ph, mp_theta, real); + Multipole_Output1D(CCTK_PASS_CTOC, real_base.str()+string(".ph.asc"), array_size, th, ph, mp_phi, real); if (v->imag_index != -1) { - const char *imag_name = CCTK_VarName(v->imag_index); - sprintf(name_tmp, "mp_%s_r%1.2f.th.asc", imag_name, rad); - Multipole_Output1D(CCTK_PASS_CTOC, name_tmp, array_size, th, ph, mp_theta, imag); - - sprintf(name_tmp, "mp_%s_r%1.2f.ph.asc", imag_name, rad); - Multipole_Output1D(CCTK_PASS_CTOC, name_tmp, array_size, th, ph, mp_phi, imag); + ostringstream imag_base; + imag_base << "mp_" << string(CCTK_VarName(v->imag_index)) << "_r" << setiosflags(ios::fixed) << setprecision(2) << rad; + Multipole_Output1D(CCTK_PASS_CTOC, imag_base.str()+string(".th.asc"), array_size, th, ph, mp_theta, imag); + Multipole_Output1D(CCTK_PASS_CTOC, imag_base.str()+string(".ph.asc"), array_size, th, ph, mp_phi, imag); } } } @@ -285,7 +285,7 @@ extern "C" void Multipole_Calc(CCTK_ARGUMENTS) yhat = new CCTK_REAL[array_size]; zhat = new CCTK_REAL[array_size]; - parse_variables_string(variables, vars, &n_variables); + parse_variables_string(string(variables), vars, &n_variables); get_spin_weights(vars, n_variables, spin_weights, &n_spin_weights); Multipole_CoordSetup(ntheta, nphi, xhat, yhat, zhat, th, ph); setup_harmonics(spin_weights, n_spin_weights, lmax, th, ph, diff --git a/src/utils.cc b/src/utils.cc index 746c599..01a6b15 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "cctk.h" #include "cctk_Arguments.h" @@ -9,19 +11,19 @@ #include "utils.hh" #include "integrate.hh" +using namespace std; + //////////////////////////////////////////////////////////////////////// // File manipulation //////////////////////////////////////////////////////////////////////// -FILE *Multipole_OpenOutputFile(CCTK_ARGUMENTS, char const *name) +FILE *Multipole_OpenOutputFile(CCTK_ARGUMENTS, const string &name) { DECLARE_CCTK_ARGUMENTS; DECLARE_CCTK_PARAMETERS; - const int buf_size = 1024; bool first_time = cctk_iteration == 0; const char *mode = first_time ? "w" : "a"; - char output_name[buf_size]; CCTK_STRING *out_dir = (CCTK_STRING *) CCTK_ParameterGet("out_dir", "IOUtil", NULL); if (*out_dir == 0) @@ -30,19 +32,13 @@ FILE *Multipole_OpenOutputFile(CCTK_ARGUMENTS, char const *name) return 0; } - if ((int)strlen(*out_dir)+1 > buf_size) - { - CCTK_WARN(1,"Parameter IOUtil::out_dir is too long"); - return 0; - } - - sprintf(output_name, "%s/%s", *out_dir, name); + string output_name(string(*out_dir) + "/" + name); - FILE *fp = fopen(output_name, mode); + FILE *fp = fopen(output_name.c_str(), mode); if (fp == 0) { - CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, "Could not open output file %s", output_name); + CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, (string("Could not open output file ") + output_name).c_str()); } return fp; @@ -72,7 +68,7 @@ void Multipole_OutputArray(CCTK_ARGUMENTS, FILE *f, int array_size, } -void Multipole_OutputArrayToFile(CCTK_ARGUMENTS, char *name, int array_size, +void Multipole_OutputArrayToFile(CCTK_ARGUMENTS, const string &name, int array_size, CCTK_REAL const th[], CCTK_REAL const ph[], CCTK_REAL const xs[], CCTK_REAL const ys[], CCTK_REAL const zs[], CCTK_REAL const data[]) @@ -90,7 +86,7 @@ void Multipole_OutputArrayToFile(CCTK_ARGUMENTS, char *name, int array_size, // Misc //////////////////////////////////////////////////////////////////////// -void Multipole_Output1D(CCTK_ARGUMENTS, char const *name, int array_size, +void Multipole_Output1D(CCTK_ARGUMENTS, const string &name, int array_size, CCTK_REAL const th[], CCTK_REAL const ph[], mp_coord coord, CCTK_REAL const data[]) { @@ -133,7 +129,7 @@ void Multipole_OutputComplex(CCTK_ARGUMENTS, FILE *fp, CCTK_REAL redata, CCTK_RE fprintf(fp, "%f %.19g %.19g\n", cctk_time, redata, imdata); } -void Multipole_OutputComplexToFile(CCTK_ARGUMENTS, char const *name, CCTK_REAL redata, CCTK_REAL imdata) +void Multipole_OutputComplexToFile(CCTK_ARGUMENTS, const string &name, CCTK_REAL redata, CCTK_REAL imdata) { DECLARE_CCTK_ARGUMENTS; diff --git a/src/utils.hh b/src/utils.hh index 2f7d2be..51c76a7 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -3,19 +3,22 @@ #define __utils_h #include "cctk.h" +#include + +using namespace std; enum mp_coord {mp_theta, mp_phi}; -void Multipole_OutputArrayToFile(CCTK_ARGUMENTS, char const *name, int array_size, +void Multipole_OutputArrayToFile(CCTK_ARGUMENTS, const string &name, int array_size, CCTK_REAL const th[], CCTK_REAL const ph[], CCTK_REAL const x[], CCTK_REAL const y[], CCTK_REAL const z[], CCTK_REAL const data[]); -void Multipole_Output1D(CCTK_ARGUMENTS, char const *name, int array_size, +void Multipole_Output1D(CCTK_ARGUMENTS, const string &name, int array_size, CCTK_REAL const th[], CCTK_REAL const ph[], mp_coord coord, CCTK_REAL const data[]); -void Multipole_OutputComplexToFile(CCTK_ARGUMENTS, char const *name, CCTK_REAL redata, CCTK_REAL imdata); +void Multipole_OutputComplexToFile(CCTK_ARGUMENTS, const string &name, CCTK_REAL redata, CCTK_REAL imdata); void Multipole_CoordSetup(int ntheta, int nphi, CCTK_REAL xhat[], CCTK_REAL yhat[], -- 1.7.3.3