Skip to content

Commit

Permalink
Simplify computation of neuronhome paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed May 8, 2024
1 parent 4a1e15a commit 85f602d
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions src/modlunit/units.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include <../../nrnconf.h>

#include <algorithm>
#include <string>

/* /local/src/master/nrn/src/modlunit/units.c,v 1.5 1997/11/24 16:19:13 hines Exp */
/* Mostly from Berkeley */
#include "model.h"
Expand Down Expand Up @@ -109,28 +113,25 @@ static int Getc(FILE* inp) {
#define UNIT_STK_SIZE 20
static struct unit unit_stack[UNIT_STK_SIZE], *usp{nullptr};

static char* neuronhome() {
static std::string hoc_dos2unixpath(std::string d) {
std::replace(d.begin(), d.end(), '\\', '/');
return d;

Check warning on line 118 in src/modlunit/units.cpp

View check run for this annotation

Codecov / codecov/patch

src/modlunit/units.cpp#L116-L118

Added lines #L116 - L118 were not covered by tests
}

static std::string neuronhome() {
#if defined(MINGW)
int i;
static char buf[256];
GetModuleFileName(NULL, buf, 256);
for (i = strlen(buf); i >= 0 && buf[i] != '\\'; --i) {
;
}
buf[i] = '\0'; // /neuron.exe gone
// printf("setneuronhome |%s|\n", buf);
for (i = strlen(buf); i >= 0 && buf[i] != '\\'; --i) {
;
}
buf[i] = '\0'; // /bin gone
{
char* u = hoc_dos2unixpath(buf);
strcpy(buf, hoc_dos2unixpath(u));
free(u);
}
std::string buf(256, '\0');
GetModuleFileName(nullptr, buf.data(), 256);
// Remove neuron.exe
auto pos = buf.find_last_of('\\');
buf.resize(pos);
// Remove bin
pos = buf.find_last_of('\\');
buf.resize(pos);
std::replace(buf.begin(), buf.end(), '\\', '/');
return buf;
#else
return getenv("NEURONHOME");
return std::getenv("NEURONHOME");
#endif
}

Expand Down Expand Up @@ -552,25 +553,19 @@ void unit_init() {
}
#if defined(__MINGW32__)
if (!inpfile) {
s = strdup(neuronhome());
if (s) {
if (strncmp(s, "/cygdrive/", 10) == 0) {
/* /cygdrive/x/... to c:/... */
Sprintf(buf, "%c:%s/lib/nrnunits.lib", s[10], s + 11);
} else {
Sprintf(buf, "%s/lib/nrnunits.lib", s);
}
inpfile = fopen(buf, "r");
free(s);
auto s = neuronhome();
if (!s.empty()) {
std::string filename = s + "/lib/nrnunits.lib";
inpfile = fopen(filename.c_str(), "r");
}
}
#else
if (!inpfile && (inpfile = fopen(dfile, "r")) == (FILE*) 0) {
if ((inpfile = fopen(dfilealt, "r")) == (FILE*) 0) {
s = neuronhome();
if (s) {
Sprintf(buf, "%s/lib/nrnunits.lib", s);
inpfile = fopen(buf, "r");
if (!inpfile && (inpfile = fopen(dfile, "r")) == nullptr) {
if ((inpfile = fopen(dfilealt, "r")) == nullptr) {
auto s = neuronhome();
if (!s.empty()) {
std::string filename = s + "/lib/nrnunits.lib";
inpfile = fopen(filename.c_str(), "r");
}
}
}
Expand Down

0 comments on commit 85f602d

Please sign in to comment.