23 #include "modem_id_convert.h" 24 #include "goby/util/as.h" 25 #include <boost/algorithm/string.hpp> 32 std::string goby::moos::ModemIdConvert::read_lookup_file(
string path)
35 ss <<
"***********************************" << endl;
36 ss <<
"modem_id_convert trying to read modem id lookup file" << endl;
40 fin.open(path.c_str(), ifstream::in);
44 string message =
"cannot open " + path +
" for reading!";
45 ss << message << endl;
49 ss <<
"reading in modem id lookup table:" << endl;
57 string::size_type pos = sline.find(
' ');
58 while (pos != string::npos)
61 pos = sline.find(
' ');
63 pos = sline.find(
'#');
64 while (pos != string::npos)
67 pos = sline.find(
'#');
69 pos = sline.find(
"//");
70 while (pos != string::npos)
73 pos = sline.find(
"//");
79 vector<string> line_parsed;
80 boost::algorithm::split(line_parsed, sline, boost::is_any_of(
","));
82 if (line_parsed.size() < 3)
83 ss <<
"invalid line: " << sline << endl;
87 if (line_parsed.size() < 4)
88 location_name = line_parsed[1];
90 location_name = line_parsed[3];
92 ss <<
"modem id [" << line_parsed[0] <<
"], name [" << line_parsed[1] <<
"], type [" 93 << line_parsed[2] <<
"]" 94 <<
", location name [" << location_name <<
"]" << endl;
96 int id = atoi(line_parsed[0].c_str());
100 names_[id] = line_parsed[1];
102 max_name_length_ = max(names_[
id].length(), max_name_length_);
103 max_id_ = max(
id, max_id_);
105 types_[id] = line_parsed[2];
106 locations_[id] = location_name;
113 ss <<
"***********************************" << endl;
117 string goby::moos::ModemIdConvert::get_name_from_id(
int id)
119 if (names_.count(
id))
122 return as<string>(id);
125 string goby::moos::ModemIdConvert::get_type_from_id(
int id)
127 if (types_.count(
id))
130 return "unknown_type";
133 string goby::moos::ModemIdConvert::get_location_from_id(
int id)
135 if (locations_.count(
id))
136 return locations_[
id];
138 return as<string>(id);
141 int goby::moos::ModemIdConvert::get_id_from_name(
string name)
143 for (map<int, string>::iterator it = names_.begin(); it != names_.end(); ++it)
145 if (boost::iequals(it->second, name))
149 return int(as<double>(name));