[Commits] [svn:einsteintoolkit] Multipole/trunk/src/ (Rev. 72)

ian.hinder at aei.mpg.de ian.hinder at aei.mpg.de
Mon Jan 3 12:50:18 CST 2011


User: hinder
Date: 2011/01/03 12:50 PM

Modified:
 /trunk/src/
  multipole.cc, utils.cc, utils.hh

Log:
 Convert to C++ strings

File Changes:

Directory: /trunk/src/
======================

File [modified]: multipole.cc
Delta lines: +26 -26
===================================================================
--- trunk/src/multipole.cc	2010-11-21 03:29:03 UTC (rev 71)
+++ trunk/src/multipole.cc	2011-01-03 18:50:17 UTC (rev 72)
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <string>
+#include <iomanip>
 
 #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 @@
   int index;
   int imag_index;
   int spin_weight;
-  char name[var_name_length];
+  string name;
 }
 variable_desc;
 
@@ -51,7 +55,7 @@
   // 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 @@
     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 @@
   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 @@
   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 @@
     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,

File [modified]: utils.cc
Delta lines: +11 -15
===================================================================
--- trunk/src/utils.cc	2010-11-21 03:29:03 UTC (rev 71)
+++ trunk/src/utils.cc	2011-01-03 18:50:17 UTC (rev 72)
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
+#include <string>
+#include <iostream>
 
 #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 @@
     return 0;
   }
 
-  if ((int)strlen(*out_dir)+1 > buf_size)
-  {
-    CCTK_WARN(1,"Parameter IOUtil::out_dir is too long");
-    return 0;
-  }
+  string output_name(string(*out_dir) + "/" + name);
 
-  sprintf(output_name, "%s/%s", *out_dir, name);
+  FILE *fp = fopen(output_name.c_str(), mode);
 
-  FILE *fp = fopen(output_name, 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_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 @@
 // 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 @@
   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;
 

File [modified]: utils.hh
Delta lines: +6 -3
===================================================================
--- trunk/src/utils.hh	2010-11-21 03:29:03 UTC (rev 71)
+++ trunk/src/utils.hh	2011-01-03 18:50:17 UTC (rev 72)
@@ -3,19 +3,22 @@
 #define __utils_h
 
 #include "cctk.h"
+#include <string>
 
+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[], 



More information about the Commits mailing list