delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
parser.cpp
1
6#include "delpi/parser/parser.h"
7
8#include <fstream>
9#include <iostream>
10#include <memory>
11#include <string>
12
13#include "delpi/parser/lp/Driver.h"
14#include "delpi/parser/mps/Driver.h"
15#include "delpi/util/error.h"
16
17namespace delpi {
18
19bool LpSolver::Parse() { return config_.read_from_stdin() ? ParseStream(std::cin) : ParseFile(config_.filename()); }
20bool LpSolver::ParseFile(const std::string& filename) {
21 std::ifstream in(filename.c_str());
22 if (!in.good()) return false;
23 return ParseStream(in, filename);
24}
25bool LpSolver::ParseStream(std::istream& stream, const std::string& stream_name) {
26 const std::unique_ptr parser{GetDriverInstance(*this)};
27 DELPI_ASSERT(parser, "Parser not found");
28 const bool res = parser->ParseStream(stream, stream_name);
29 stats_.parser_stats = parser->stats();
30 return res;
31}
32bool LpSolver::ParseString(const std::string& string) { return GetDriverInstance(*this)->ParseString(string); }
33
34std::unique_ptr<Driver> GetDriverInstance(LpSolver& lp_solver) {
35 switch (lp_solver.config().actual_format()) {
37 return std::make_unique<mps::MpsDriver>(lp_solver);
39 return std::make_unique<lp::LpDriver>(lp_solver);
40 default:
41 DELPI_UNREACHABLE();
42 }
43}
44
45} // namespace delpi
@ LP
LP format.
Definition Config.h:48
@ MPS
MPS format.
Definition Config.h:47
Facade class that hides the underlying LP solver used by delpi.
Definition LpSolver.h:60
Config config_
Configuration to use.
Definition LpSolver.h:533
bool Parse()
Parse the input file or stdin based on the Config parameters.
Definition parser.cpp:19
bool ParseStream(std::istream &stream, const std::string &stream_name="(stdin)")
Parse the given stream as input.
Definition parser.cpp:25
bool ParseFile(const std::string &filename)
Parse the file with the given filename.
Definition parser.cpp:20
LpStats stats_
Statistics of the solver.
Definition LpSolver.h:534
bool ParseString(const std::string &string)
Parse the given string as input.
Definition parser.cpp:32
Global namespace for the delpi library.
std::unique_ptr< Driver > GetDriverInstance(LpSolver &lp_solver)
Get the correct driver instance based on the actual format set in the Config.
Definition parser.cpp:34