Skip to content

Commit

Permalink
NLSolver: temp folder #30
Browse files Browse the repository at this point in the history
For auto stub
  • Loading branch information
glebbelov committed Feb 23, 2024
1 parent 3980140 commit 0331c7c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
5 changes: 3 additions & 2 deletions nl-writer2/include/mp/nl-solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,15 @@ class NLSolver {
bool ReadSolution(SOLHandler& solh);

protected:
void Init();
void Destroy();
void InitAutoStub();
void DestroyAutoStub();
mp::NLUtils& Utils() const { return *p_ut_; }

private:
mp::NLUtils utils_;
mp::NLUtils* p_ut_ = nullptr;

std::string pathstr_;
std::string filestub_;
bool filestubCustom_ = false;
NLW2_NLOptionsBasic_C nl_opts_;
Expand Down
2 changes: 1 addition & 1 deletion nl-writer2/include/mp/nl-solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace mp {
template <class NLFeeder>
bool NLSolver::LoadModel(NLFeeder& nlf) {
if (GetFileStub().empty())
return (err_msg_="WriteNL error: provide filestub.", false);
InitAutoStub();
auto result = mp::WriteNLFile(GetFileStub(), nlf, Utils());
if (NLW2_WriteNL_OK != result.first)
return (err_msg_ = "WriteNL error: " + result.second, false);
Expand Down
60 changes: 49 additions & 11 deletions nl-writer2/src/nl-solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
#include <numeric>
#include <vector>
#include <cmath>
#include <random>
#include <filesystem>

#if defined(_WIN32) || defined(_WIN64)
#include <io.h> // _mktemp[_s]
#else
#include <cstdlib> // mkdtemp
#endif

#include "mp/nl-solver.hpp"
#include "mp/nl-opcodes.h"
Expand Down Expand Up @@ -519,24 +527,54 @@ double NLModel::ComputeObjValue(const double *x) const {
}

NLSolver::NLSolver()
: p_ut_(&utils_) { Init(); }
: p_ut_(&utils_) { }

NLSolver::NLSolver(mp::NLUtils* put)
: p_ut_(put ? put : &utils_) { Init(); }
: p_ut_(put ? put : &utils_) { }

NLSolver::~NLSolver() { Destroy(); }
NLSolver::~NLSolver() { DestroyAutoStub(); }

void NLSolver::Init() {
void NLSolver::InitAutoStub() {
// init file stub
char tmpn[L_tmpnam];
tmpnam(tmpn);
filestub_ = tmpn;
std::random_device dev;
std::mt19937 prng(dev());
std::uniform_int_distribution<unsigned long> rand(0);
auto path = std::filesystem::temp_directory_path();
path /= "nlw2_"; // via '/'
char rnds[64] = "rndhex";
std::snprintf(rnds, sizeof(rnds)-1, "%lX", rand(prng));
path += rnds; // no '/'

path += "_XXXXXX";
pathstr_ = path.string();

#if defined(_WIN32) || defined(_WIN64)
auto p1 = _mktemp((char*)pathstr_.c_str());
assert(p1);
if (!std::filesystem::create_directory(pathstr_))
Utils().myexit("Could not create temp dir '"
+ pathstr_ + "'");
#else
if (!mkdtemp((char*)pathstr_.c_str()))
Utils().myexit("Could not create a temp dir\n"
"from pattern '" + pathstr_ + "'");
#endif
path = pathstr_;
// Plus filename
std::snprintf(rnds, sizeof(rnds)-1, "%lX", rand(prng));
path /= rnds;
filestub_ = path.string();
}

void NLSolver::Destroy() {
// try & delete .nl
if (!filestubCustom_)
std::remove((filestub_ + ".nl").c_str());
void NLSolver::DestroyAutoStub() {
// delete temp folder if created
if (pathstr_.size()) {
std::error_code ec;
std::filesystem::remove_all(pathstr_, ec);
if (ec)
Utils().log_warning("Failed to remove temp dir '%s': %s",
pathstr_.c_str(), ec.message().c_str());
}
}

void NLSolver::SetFileStub(std::string stub) {
Expand Down

0 comments on commit 0331c7c

Please sign in to comment.