#ifndef FILE_HPP
#define FILE_HPP
#include <fstream>
#include <string>
#include <map>
#include <exception>
#include <stdexcept>
#include <algorithm>
#include <boost/lexical_cast.hpp>
namespace config
{
class file_error:public std::exception
{
public:
file_error(const char* error):_error(error)
{
}
file_error(char* error):_error(error)
{
}
file_error(std::string error):_error(error)
{
}
~file_error()throw()
{
}
const char* what()const throw()
{
return _error.c_str();
}
private:
std::string _error;
};
class file
{
public:
file(const char* file_name)throw(file_error)
{
try
{
_input_file.open(file_name);
load();
}
catch(const std::exception& e)
{
throw file_error(e.what());
}
catch(...)
{
throw file_error("file open error");
}
}
virtual ~file()
{
}
void load(void)throw(file_error)
{
try
{
std::string line;
std::string::iterator result;
while (!_input_file.eof())
{
std::getline(_input_file, line);
result = std::remove(line.begin(), line.end(), ' ');
line.erase(result, line.end());
result = std::find(line.begin(), line.end(), '=');
if (result != line.end())
{
int port = boost::lexical_cast<int>(std::string(line.begin(), result) );
_port_name_map[port] = std::string(result+1, line.end());
}
}
}
catch(const std::exception& e)
{
throw file_error(e.what());
}
catch(...)
{
throw file_error("read file error");
}
}
void reload(void)throw(file_error)
{
try
{
_port_name_map.clear();
load();
}
catch(...)
{
throw;
}
}
bool get_name_by(int port, std::string& name)
{
std::map<int, std::string>::const_iterator it = _port_name_map.find(port);
if (it != _port_name_map.end())
{
name = _port_name_map[port];
return true;
}
else
{
return false;
}
}
const std::map<int, std::string>& get_map(void)
{
return _port_name_map;
}
private:
std::ifstream _input_file;
std::map<int, std::string> _port_name_map;
};
}
#endif
#define FILE_HPP
#include <fstream>
#include <string>
#include <map>
#include <exception>
#include <stdexcept>
#include <algorithm>
#include <boost/lexical_cast.hpp>
namespace config
{
class file_error:public std::exception
{
public:
file_error(const char* error):_error(error)
{
}
file_error(char* error):_error(error)
{
}
file_error(std::string error):_error(error)
{
}
~file_error()throw()
{
}
const char* what()const throw()
{
return _error.c_str();
}
private:
std::string _error;
};
class file
{
public:
file(const char* file_name)throw(file_error)
{
try
{
_input_file.open(file_name);
load();
}
catch(const std::exception& e)
{
throw file_error(e.what());
}
catch(...)
{
throw file_error("file open error");
}
}
virtual ~file()
{
}
void load(void)throw(file_error)
{
try
{
std::string line;
std::string::iterator result;
while (!_input_file.eof())
{
std::getline(_input_file, line);
result = std::remove(line.begin(), line.end(), ' ');
line.erase(result, line.end());
result = std::find(line.begin(), line.end(), '=');
if (result != line.end())
{
int port = boost::lexical_cast<int>(std::string(line.begin(), result) );
_port_name_map[port] = std::string(result+1, line.end());
}
}
}
catch(const std::exception& e)
{
throw file_error(e.what());
}
catch(...)
{
throw file_error("read file error");
}
}
void reload(void)throw(file_error)
{
try
{
_port_name_map.clear();
load();
}
catch(...)
{
throw;
}
}
bool get_name_by(int port, std::string& name)
{
std::map<int, std::string>::const_iterator it = _port_name_map.find(port);
if (it != _port_name_map.end())
{
name = _port_name_map[port];
return true;
}
else
{
return false;
}
}
const std::map<int, std::string>& get_map(void)
{
return _port_name_map;
}
private:
std::ifstream _input_file;
std::map<int, std::string> _port_name_map;
};
}
#endif