Skip to content

Commit e981de8

Browse files
author
Roberto Di Remigio
committed
Convert to Fortran strings already in C++
1 parent e6773d3 commit e981de8

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

src/cavity/GePolCavity.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,21 @@ extern "C" void generatecavity_cpp(int * maxts, int * maxsph, int * maxvert,
8585
double * xe, double * ye, double * ze, double * rin, double * masses,
8686
double * avgArea, double * rsolv, double * ret,
8787
int * nr_gen, int * gen1, int * gen2, int * gen3,
88-
int * nvert, double * vert, double * centr, const char * pedra, const char * off);
88+
int * nvert, double * vert, double * centr, char * pedra, char * off);
8989

90+
void convertToFortran(char * fstring, size_t fstring_len, const char * cstring)
91+
{
92+
size_t inlen = std::strlen(cstring);
93+
size_t cpylen = std::min(inlen, fstring_len);
94+
95+
if (inlen > fstring_len)
96+
{
97+
// TODO: truncation error or warning
98+
}
99+
100+
std::copy(cstring, cstring + cpylen, fstring);
101+
std::fill(fstring + cpylen, fstring + fstring_len, ' ');
102+
}
90103

91104
void GePolCavity::build(const std::string & suffix, int maxts, int maxsph, int maxvert)
92105
{
@@ -169,8 +182,12 @@ void GePolCavity::build(const std::string & suffix, int maxts, int maxsph, int m
169182

170183
std::stringstream pedra;
171184
pedra << "PEDRA.OUT_" << suffix << "_" << ::getpid();
185+
char f_pedra[std::strlen(pedra.str().c_str()) -1];
186+
convertToFortran(f_pedra, std::strlen(pedra.str().c_str()) -1, pedra.str().c_str());
172187
std::stringstream off;
173188
off << "cavity.off_" << suffix << "_" << ::getpid();
189+
char f_off[std::strlen(off.str().c_str()) -1];
190+
convertToFortran(f_off, std::strlen(off.str().c_str()) -1, off.str().c_str());
174191
// Go PEDRA, Go!
175192
TIMER_ON("GePolCavity::generatecavity_cpp");
176193
generatecavity_cpp(&maxts, &maxsph, &maxvert,
@@ -179,7 +196,7 @@ void GePolCavity::build(const std::string & suffix, int maxts, int maxsph, int m
179196
xe, ye, ze, rin, mass,
180197
&averageArea, &probeRadius, &minimalRadius,
181198
&nr_gen, &gen1, &gen2, &gen3,
182-
nvert, vert, centr, pedra.str().c_str(), off.str().c_str());
199+
nvert, vert, centr, f_pedra, f_off);
183200
TIMER_OFF("GePolCavity::generatecavity_cpp");
184201

185202
// The "intensive" part of updating the spheres related class data members will be of course

src/pedra/pedra_cavity_interface.F90

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,50 +57,29 @@ subroutine generatecavity_cpp(maxts_, maxsph_, maxvert_, &
5757
integer(c_int) :: nr_gen_, gen1_, gen2_, gen3_
5858
integer(c_int) :: nvert_(maxts_)
5959
real(c_double) :: vert_(maxts_ * 30), centr_(maxts_ * 30)
60-
character(kind=c_char, len=1), intent(in) :: pedra_(*)
61-
character(kind=c_char, len=1), intent(in) :: off_(*)
60+
character(kind=c_char) :: pedra_
61+
character(kind=c_char) :: off_
6262

6363
integer(c_int) :: i, j, k, offset
6464
integer(c_int) :: error_code
6565
integer(kind=regint_k) :: lvpri
6666
logical :: pedra_file_exists
6767
real(c_double), allocatable :: vert(:, :, :), centr(:, :, :)
68-
character(kind=c_char, len=:), allocatable :: pedra
69-
character(kind=c_char, len=:), allocatable :: off
70-
integer :: l, nchars
7168
type(point_group) :: pgroup
7269

73-
l = 1
74-
do
75-
if (pedra_(l) == c_null_char) exit
76-
l = l + 1
77-
end do
78-
nchars = l - 1 ! Exclude null character from Fortran string
79-
allocate(character(len=nchars) :: pedra)
80-
pedra = transfer(pedra_(1:nchars), pedra)
81-
82-
l = 1
83-
do
84-
if (off_(l) == c_null_char) exit
85-
l = l + 1
86-
end do
87-
nchars = l - 1 ! Exclude null character from Fortran string
88-
allocate(character(len=nchars) :: off)
89-
off = transfer(off_(1:nchars), off)
90-
9170
lvpri = 121201_regint_k
9271
pedra_file_exists = .false.
93-
inquire(file = pedra, exist = pedra_file_exists)
72+
inquire(file = pedra_, exist = pedra_file_exists)
9473
if (pedra_file_exists) then
9574
open(lvpri, &
96-
file = pedra, &
75+
file = pedra_, &
9776
status = 'unknown', &
9877
form = 'formatted', &
9978
access = 'sequential')
10079
close(lvpri, status = 'delete')
10180
end if
10281
open(lvpri, &
103-
file = pedra, &
82+
file = pedra_, &
10483
status = 'new', &
10584
form = 'formatted', &
10685
access = 'sequential')
@@ -135,7 +114,7 @@ subroutine generatecavity_cpp(maxts_, maxsph_, maxvert_, &
135114

136115
nesf = nesfp
137116

138-
call polyhedra_driver(pgroup, vert, centr, masses_, lvpri, off, error_code)
117+
call polyhedra_driver(pgroup, vert, centr, masses_, lvpri, off_, error_code)
139118

140119
! Common block dark magic, it will disappear one day...
141120
nts_ = nts

0 commit comments

Comments
 (0)