Skip to content

Commit

Permalink
Simplify computation of neuronhome paths (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino authored May 10, 2024
1 parent 4a1e15a commit 0e781ec
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 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,21 @@ 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 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");
char* buf = std::getenv("NEURONHOME");
return (buf != nullptr) ? std::string(buf) : std::string();
#endif
}

Expand Down Expand Up @@ -552,25 +549,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 0e781ec

Please sign in to comment.