diff --git a/src/julia/ClarabelRs/src/interface.jl b/src/julia/ClarabelRs/src/interface.jl index 7c73dd3..f3fec6d 100644 --- a/src/julia/ClarabelRs/src/interface.jl +++ b/src/julia/ClarabelRs/src/interface.jl @@ -45,6 +45,12 @@ function write_to_file(solver::Solver, filename::String) end +function read_from_file(filename::String) + + solver_read_from_file_jlrs(filename::String) + +end + # ------------------------------------- # Wrappers for rust-side interface @@ -117,6 +123,22 @@ function solver_write_to_file_jlrs(solver::Solver, filename::String) end +function solver_read_from_file_jlrs(filename::String) + + ptr = ccall(Libdl.dlsym(librust,:solver_read_from_file_jlrs),Ptr{Cvoid}, + ( + Cstring, + ), + filename, + ) + + if ptr == C_NULL + error("Error reading from file $filename") + end + return Solver{Float64}(ptr) + +end + function solver_drop_jlrs(solver::Solver) ccall(Libdl.dlsym(librust,:solver_drop_jlrs),Cvoid, (Ptr{Cvoid},), solver.ptr) diff --git a/src/julia/interface.rs b/src/julia/interface.rs index 01b9f02..e162d5c 100644 --- a/src/julia/interface.rs +++ b/src/julia/interface.rs @@ -148,6 +148,36 @@ pub(crate) extern "C" fn solver_write_to_file_jlrs( return status; } +// dump problem data to a file +// returns NULL on failure, pointer to solver on success +#[no_mangle] +pub(crate) extern "C" fn solver_read_from_file_jlrs( + filename: *const std::os::raw::c_char, +) -> *const c_void { + let slice = unsafe { CStr::from_ptr(filename) }; + + let filename = match slice.to_str() { + Ok(s) => s, + Err(_) => { + return std::ptr::null(); + } + }; + + let mut file = match File::open(&filename) { + Ok(f) => f, + Err(_) => { + return std::ptr::null(); + } + }; + + let solver = DefaultSolver::read_from_file(&mut file); + + match solver { + Ok(solver) => to_ptr(Box::new(solver)), + Err(_) => std::ptr::null(), + } +} + // safely drop a solver object through its pointer. // called by the Julia side finalizer when a solver // is out of scope diff --git a/src/solver/implementations/default/fileio.rs b/src/solver/implementations/default/json.rs similarity index 100% rename from src/solver/implementations/default/fileio.rs rename to src/solver/implementations/default/json.rs diff --git a/src/solver/implementations/default/mod.rs b/src/solver/implementations/default/mod.rs index b24948b..f0cea8c 100644 --- a/src/solver/implementations/default/mod.rs +++ b/src/solver/implementations/default/mod.rs @@ -30,4 +30,4 @@ pub use solver::*; pub use variables::*; #[cfg(feature = "serde")] -mod fileio; +mod json;