BonSolReader.cpp
Go to the documentation of this file.
1 // (C) Copyright CNRS 2011
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, LIF, CNRS,
7 //
8 // Date : 03/01/2011
9 #include "BonSolReader.hpp"
10 #include <fstream>
11 #include <iostream>
12 
13 namespace Bonmin{
14 
15 SolReader::SolReader(const char * file, const char * suffix)
16  :
17  file_(), suffix_(suffix), x_()
18 {
19  assert(file!= NULL);
20  file_=file;
21  if (suffix!=NULL)
22  suffix_ = suffix;
23 }
24 SolReader::SolReader(const std::string & file, const std::string & suffix)
25  :
26  file_(file), suffix_(suffix), x_()
27 {}
28 
30 {
31  std::string fileName = file_;
32  size_t size = fileName.size();
33  bool hasNlExtension = (fileName.size()>4) && (fileName[size - 1] =='l') && (fileName[size - 2] =='n') && (fileName[size - 3] =='.');
34  if(hasNlExtension)
35  fileName.erase(size-3,3);
36  fileName+=suffix_;
37  std::ifstream inFile(fileName.c_str());
38  if(!inFile.is_open()) {
39  return false;
40  }
41  std::string token;
42  inFile>>token;
43  assert(token == "bonmin:");
44 
45  std::string status;
46  inFile>>status;
47  inFile>>token;
48  if(token == "Options"){
49  for(int i = 0 ; i < 6 ; i++){
50  inFile>>token;
51  }
52  int n_cols, n_cols_2;
53  inFile>>n_cols_2>>n_cols;
54  if(n_cols != static_cast<int>(x_.size())){
55  fprintf(stderr, "Number of columns different %d\n", n_cols);
56  x_.resize(n_cols);
57  }
58  }
59 for(size_t i = 0 ; i < x_.size() ; i++){
60  inFile>>x_[i];
61  }
62  return true;
63 }
64 
65 void
67 {
68  std::copy(x_.begin(), x_.end(), x);
69 }
70 }
const double * x()
std::string suffix_
Suffix of the file (&quot;.col&quot;, &quot;.row&quot;)
void copySol(double *x)
Copy the names to Names.
std::string file_
Name of the file to read.
bool readFile()
Reads the .sol file.
void fint fint fint real fint real * x
SolReader(const char *fileName, const char *suffix)
Constructor with a file name given by a const char *.
vector< double > x_
Sol values.