Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify computation of neuronhome paths #2874

Merged
merged 3 commits into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
alkino marked this conversation as resolved.
Show resolved Hide resolved
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