Skip to content

Commit

Permalink
mkdir_p: purge from codebase, use std::filesystem. (#2571)
Browse files Browse the repository at this point in the history
This should remove some of our code smells, too, while simplifying
things.
  • Loading branch information
matz-e authored Oct 6, 2023
1 parent 66a7c0e commit 21c4afd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 127 deletions.
8 changes: 5 additions & 3 deletions src/coreneuron/apps/main1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstring>
#include <climits>
#include <dlfcn.h>
#include <filesystem>
#include <memory>
#include <vector>

Expand All @@ -40,11 +41,12 @@
#include "coreneuron/network/partrans.hpp"
#include "coreneuron/network/multisend.hpp"
#include "coreneuron/io/nrn_setup.hpp"
#include "coreneuron/io/file_utils.hpp"
#include "coreneuron/io/nrn2core_direct.h"
#include "coreneuron/io/core2nrn_data_return.hpp"
#include "coreneuron/utils/utils.hpp"

namespace fs = std::filesystem;

extern "C" {
const char* corenrn_version() {
return coreneuron::bbcore_write_version;
Expand Down Expand Up @@ -501,7 +503,7 @@ extern "C" int run_solve_core(int argc, char** argv) {

// Create outpath if it does not exist
if (nrnmpi_myid == 0) {
mkdir_p(corenrn_param.outpath.c_str());
fs::create_directories(corenrn_param.outpath);
}

if (!corenrn_param.reportfilepath.empty()) {
Expand All @@ -522,7 +524,7 @@ extern "C" int run_solve_core(int argc, char** argv) {
std::string output_dir = corenrn_param.outpath;

if (nrnmpi_myid == 0) {
mkdir_p(output_dir.c_str());
fs::create_directories(output_dir);
}
#if NRNMPI
if (corenrn_param.mpi_enable) {
Expand Down
53 changes: 0 additions & 53 deletions src/coreneuron/io/file_utils.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions src/coreneuron/io/file_utils.hpp

This file was deleted.

6 changes: 4 additions & 2 deletions src/coreneuron/io/nrn_checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# See top-level LICENSE file for details.
# =============================================================================.
*/
#include <filesystem>
#include <iostream>
#include <sstream>
#include <cassert>
Expand All @@ -19,13 +20,14 @@
#include "coreneuron/network/netpar.hpp"
#include "coreneuron/utils/vrecitem.h"
#include "coreneuron/mechanism/mech/mod2c_core_thread.hpp"
#include "coreneuron/io/file_utils.hpp"
#include "coreneuron/permute/data_layout.hpp"
#include "coreneuron/permute/node_permute.h"
#include "coreneuron/coreneuron.hpp"
#include "coreneuron/utils/nrnoc_aux.hpp"
#include "coreneuron/apps/corenrn_parameters.hpp"

namespace fs = std::filesystem;

namespace coreneuron {
// Those functions comes from mod file directly
extern int checkpoint_save_patternstim(_threadargsproto_);
Expand All @@ -37,7 +39,7 @@ CheckPoints::CheckPoints(const std::string& save, const std::string& restore)
, restored(false) {
if (!save.empty()) {
if (nrnmpi_myid == 0) {
mkdir_p(save.c_str());
fs::create_directories(save);
}
}
}
Expand Down
46 changes: 0 additions & 46 deletions src/nmodl/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,49 +445,3 @@ static int file_stack_empty() {
}
return (filestack->next == filestack);
}

/* adapted from : gist@jonathonreinhart/mkdir_p.c */
int mkdir_p(const char* path) {
const size_t len = strlen(path);
char mypath[PATH_MAX];
char* p;

errno = 0;

/* copy string so its mutable */
if (len > sizeof(mypath) - 1) {
fprintf(stderr, "Output directory path too long\n");
return -1;
}

strcpy(mypath, path);

/* iterate the string */
for (p = mypath + 1; *p; p++) {
if (*p == '/') {
/* temporarily truncate */
*p = '\0';

#if defined(_WIN32)
if (_mkdir(mypath) != 0) {
#else
if (mkdir(mypath, S_IRWXU) != 0) {
#endif
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}

#if defined(_WIN32)
if (_mkdir(mypath) != 0) {
#else
if (mkdir(mypath, S_IRWXU) != 0) {
#endif
if (errno != EEXIST)
return -1;
}

return 0;
}
8 changes: 6 additions & 2 deletions src/nmodl/modl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include "modl.h"

#include <cstring>
#include <filesystem>

namespace fs = std::filesystem;

FILE *fin, /* input file descriptor for filename.mod */
/* or file2 from the second argument */
Expand All @@ -55,7 +58,6 @@ int nmodl_text = 1;
List* filetxtlist;

extern int yyparse();
extern int mkdir_p(const char*);

extern int vectorize;
extern int numlist;
Expand Down Expand Up @@ -276,7 +278,9 @@ static void openfiles(char* given_filename, char* output_dir) {
}
}
if (output_dir) {
if (mkdir_p(output_dir) != 0) {
try {
fs::create_directories(output_dir);
} catch (...) {
fprintf(stderr, "Can't create output directory %s\n", output_dir);
exit(1);
}
Expand Down

0 comments on commit 21c4afd

Please sign in to comment.