Goby Underwater Autonomy Project
Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
|
00001 // t. schneider tes@mit.edu 12.22.08 00002 // ocean engineering graudate student - mit / whoi joint program 00003 // massachusetts institute of technology (mit) 00004 // laboratory for autonomous marine sensing systems (lamss) 00005 // 00006 // this is modem_id_convert.h 00007 // 00008 // this does lookups from modem to vehicle name and vice versa 00009 // 00010 // This program is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // This software is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU General Public License 00021 // along with this software. If not, see <http://www.gnu.org/licenses/>. 00022 00023 00024 #include "modem_id_convert.h" 00025 #include <string> 00026 #include <vector> 00027 #include "goby/util/string.h" 00028 #include <boost/algorithm/string.hpp> 00029 00030 using namespace std; 00031 using goby::util::as; 00032 00033 namespace tes 00034 { 00035 std::string ModemIdConvert::read_lookup_file(string path) 00036 { 00037 00038 stringstream ss; 00039 ss << "***********************************" << endl; 00040 ss << "modem_id_convert trying to read modem id lookup file" << endl; 00041 00042 00043 ifstream fin; 00044 00045 fin.open(path.c_str(), ifstream::in); 00046 00047 if (fin.fail()) 00048 { 00049 string message = "cannot open " + path + " for reading!"; 00050 ss << message << endl; 00051 return ss.str(); 00052 } 00053 00054 ss << "reading in modem id lookup table:" << endl; 00055 00056 while(fin) 00057 { 00058 string sline; 00059 getline(fin, sline); 00060 00061 // strip the spaces and comments out 00062 string::size_type pos = sline.find(' '); 00063 while(pos != string::npos) 00064 { 00065 sline.erase(pos, 1); 00066 pos = sline.find(' '); 00067 } 00068 pos = sline.find('#'); 00069 while(pos != string::npos) 00070 { 00071 sline.erase(pos); 00072 pos = sline.find('#'); 00073 } 00074 pos = sline.find("//"); 00075 while(pos != string::npos) 00076 { 00077 sline.erase(pos); 00078 pos = sline.find("//"); 00079 } 00080 00081 // ignore blank lines 00082 if(sline != "") 00083 { 00084 vector<string> line_parsed; 00085 boost::algorithm::split(line_parsed, sline, boost::is_any_of(",")); 00086 00087 if(line_parsed.size() < 3) 00088 ss << "invalid line: " << sline << endl; 00089 else 00090 { 00091 string location_name; 00092 if(line_parsed.size() < 4) 00093 location_name = line_parsed[1]; 00094 else 00095 location_name = line_parsed[3]; 00096 00097 00098 ss << "modem id [" << line_parsed[0] << "], name [" << line_parsed[1] << "], type [" << line_parsed[2] << "]" << ", location name [" << location_name << "]" << endl; 00099 00100 int id = atoi(line_parsed[0].c_str()); 00101 00102 //add the entry to our lookup vectors 00103 00104 names[id] = line_parsed[1]; 00105 00106 max_name_length_ = max(names[id].length(), max_name_length_); 00107 max_id_ = max(id, max_id_); 00108 00109 00110 types[id] = line_parsed[2]; 00111 locations[id] = location_name; 00112 } 00113 } 00114 } 00115 00116 fin.close(); 00117 00118 ss << "***********************************" << endl; 00119 return ss.str(); 00120 } 00121 00122 string ModemIdConvert::get_name_from_id(int id) 00123 { 00124 if(names.count(id)) 00125 return names[id]; // return the found name 00126 else 00127 return as<string>(id); // if not in the lookup table, just give the number back as a string 00128 } 00129 00130 00131 string ModemIdConvert::get_type_from_id(int id) 00132 { 00133 if(types.count(id)) 00134 return types[id]; 00135 else 00136 return "unknown_type"; 00137 } 00138 00139 string ModemIdConvert::get_location_from_id(int id) 00140 { 00141 if(locations.count(id)) 00142 return locations[id]; 00143 else 00144 return as<string>(id); 00145 } 00146 00147 int ModemIdConvert::get_id_from_name(string name) 00148 { 00149 for (map<int,string>::iterator it = names.begin(); it != names.end(); ++it) 00150 { 00151 if(boost::iequals(it->second, name)) 00152 return it->first; 00153 } 00154 00155 00156 return int(as<double>(name)); 00157 } 00158 }