Goby v2
modem_id_convert.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #include "modem_id_convert.h"
24 #include "goby/util/as.h"
25 #include <boost/algorithm/string.hpp>
26 #include <string>
27 #include <vector>
28 
29 using namespace std;
30 using goby::util::as;
31 
32 std::string goby::moos::ModemIdConvert::read_lookup_file(string path)
33 {
34  stringstream ss;
35  ss << "***********************************" << endl;
36  ss << "modem_id_convert trying to read modem id lookup file" << endl;
37 
38  ifstream fin;
39 
40  fin.open(path.c_str(), ifstream::in);
41 
42  if (fin.fail())
43  {
44  string message = "cannot open " + path + " for reading!";
45  ss << message << endl;
46  return ss.str();
47  }
48 
49  ss << "reading in modem id lookup table:" << endl;
50 
51  while (fin)
52  {
53  string sline;
54  getline(fin, sline);
55 
56  // strip the spaces and comments out
57  string::size_type pos = sline.find(' ');
58  while (pos != string::npos)
59  {
60  sline.erase(pos, 1);
61  pos = sline.find(' ');
62  }
63  pos = sline.find('#');
64  while (pos != string::npos)
65  {
66  sline.erase(pos);
67  pos = sline.find('#');
68  }
69  pos = sline.find("//");
70  while (pos != string::npos)
71  {
72  sline.erase(pos);
73  pos = sline.find("//");
74  }
75 
76  // ignore blank lines
77  if (sline != "")
78  {
79  vector<string> line_parsed;
80  boost::algorithm::split(line_parsed, sline, boost::is_any_of(","));
81 
82  if (line_parsed.size() < 3)
83  ss << "invalid line: " << sline << endl;
84  else
85  {
86  string location_name;
87  if (line_parsed.size() < 4)
88  location_name = line_parsed[1];
89  else
90  location_name = line_parsed[3];
91 
92  ss << "modem id [" << line_parsed[0] << "], name [" << line_parsed[1] << "], type ["
93  << line_parsed[2] << "]"
94  << ", location name [" << location_name << "]" << endl;
95 
96  int id = atoi(line_parsed[0].c_str());
97 
98  //add the entry to our lookup vectors
99 
100  names_[id] = line_parsed[1];
101 
102  max_name_length_ = max(names_[id].length(), max_name_length_);
103  max_id_ = max(id, max_id_);
104 
105  types_[id] = line_parsed[2];
106  locations_[id] = location_name;
107  }
108  }
109  }
110 
111  fin.close();
112 
113  ss << "***********************************" << endl;
114  return ss.str();
115 }
116 
117 string goby::moos::ModemIdConvert::get_name_from_id(int id)
118 {
119  if (names_.count(id))
120  return names_[id]; // return the found name
121  else
122  return as<string>(id); // if not in the lookup table, just give the number back as a string
123 }
124 
125 string goby::moos::ModemIdConvert::get_type_from_id(int id)
126 {
127  if (types_.count(id))
128  return types_[id];
129  else
130  return "unknown_type";
131 }
132 
133 string goby::moos::ModemIdConvert::get_location_from_id(int id)
134 {
135  if (locations_.count(id))
136  return locations_[id];
137  else
138  return as<string>(id);
139 }
140 
141 int goby::moos::ModemIdConvert::get_id_from_name(string name)
142 {
143  for (map<int, string>::iterator it = names_.begin(); it != names_.end(); ++it)
144  {
145  if (boost::iequals(it->second, name))
146  return it->first;
147  }
148 
149  return int(as<double>(name));
150 }
STL namespace.