BonColReader.cpp
Go to the documentation of this file.
1 // (C) Copyright Carnegie Mellon University 2005
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, Carnegie Mellon University,
7 //
8 // Date : 26/05/2005
9 #include "BonColReader.hpp"
10 #include <fstream>
11 #include <iostream>
12 
13 namespace Bonmin{
14 
15 NamesReader::NamesReader(const char * file, const char * suffix)
16  :
17  file_(), suffix_(suffix), indices_(), names_()
18 {
19  assert(file!= NULL);
20  file_=file;
21  if (suffix!=NULL)
22  suffix_ = suffix;
23 }
24 NamesReader::NamesReader(const std::string & file, const std::string & suffix)
25  :
26  file_(file), suffix_(suffix), indices_(), names_()
27 {}
28 
30 {
31  std::string colFileName = file_;
32  size_t size = colFileName.size();
33  bool hasNlExtension = (colFileName.size()>4) && (colFileName[size - 1] =='l') && (colFileName[size - 2] =='n') && (colFileName[size - 3] =='.');
34  if(hasNlExtension)
35  colFileName.erase(size-3,3);
36  colFileName+=suffix_;
37  std::ifstream inFile(colFileName.c_str());
38  if(!inFile.is_open()) {
39  return false;
40  }
41  std::string name;
42  int nVar = 0;
43  do {
44  name="";
45  inFile>>name;
46  if(name.size()==0)
47  continue;
48  names_.push_back(name);
49  indices_[names_[nVar].c_str()] = nVar;
50  nVar++;
51  }
52  while(!inFile.eof());
53 
54  // names_ = new std::string[nVar];
55  for(int i = 0 ; i < nVar ; i++) {
56  assert(i==indices_ [ names_ [i].c_str()]);
57  }
58  return true;
59 }
60 
61 void
62 NamesReader::copyNames(OsiSolverInterface::OsiNameVec& names)
63 {
64  names_ = names;
65 }
66 }
void copyNames(OsiSolverInterface::OsiNameVec &Names)
Copy the names to Names.
namesHash indices_
Hash map used to store the indices.
std::string file_
Name of the file to read.
bool readFile()
Reads the .col file.
NamesReader(const char *fileName, const char *suffix)
Constructor with a file name given by a const char *.
std::string suffix_
Suffix of the file (&quot;.col&quot;, &quot;.row&quot;)
std::vector< std::string > names_
Variable names.
const std::string & name(int i)
Access Names of indexed by i.