Page MenuHomec4science

main.cpp
No OneTemporary

File Metadata

Created
Sun, Feb 2, 02:51

main.cpp

#include <libsbcp-tool/argstream.h>
#include <libsbcp/utils/Config.h>
#include <vector>
#include <cstdlib>
#include <fstream>
void PrintAllBus(sbcp::Config & config){
typedef sbcp::BusesConfigSection::ListOfSubsection ListOfBus;
const ListOfBus & buses = config.BusDefinitions().SubSections();
std::cout << buses.size() << " bus availables : ";
for(ListOfBus::const_iterator b = buses.begin();
b != buses.end();
++b){
if(b != buses.begin()){
std::cout << " , ";
}
std::cout << "'" << b->first
<< "':{ type := " << b->second->SerialType()
<< " ; ID := " << b->second->MasterID()
<< " ; baudrate := " << b->second->Baudrate()
<< " ; status := ";
bool isWorking(true);
try {
std::tr1::shared_ptr<sbcp::Bus> bus(b->second->OpenBus());
} catch (const std::exception & e){
isWorking = false;
}
if(isWorking){
std::cout << "OK";
} else {
std::cout << "UNFOUND";
}
std::cout << "}";
}
std::cout << std::endl;
}
int main(int argc, char ** argv){
bool systemWide(false),global(false),listAll(false),edit(false);
std::string add;
argstream as(argc,argv);
as >> parameter('a',"add",add,"Add a new definition",false)
>> option("system",systemWide,"System wide configuration")
>> option("global",global,"User wide configuration")
>> option('e',"edit",edit,"Edit the configuration after operation using $EDITOR")
>> option('l',"list-all",listAll,"list all available bus")
>> help();
as.defaultErrorHandling();
if(systemWide && global){
throw std::runtime_error("--system and --global are exclusive");
}
if(listAll && !add.empty()){
throw std::runtime_error("--add and --list-all are exclusive");
}
if(add.empty() && !edit && !listAll){
throw std::runtime_error("Please choose an action, --edit, --list-all, or --add");
}
//--add will call editor
if(!add.empty()){
edit = true;
}
sbcp::Config config;
options::Config::Level level;
std::string filename;
if(systemWide){
level = (options::Config::Level)options::Config::SYSTEM;
filename = config.SystemConfigFilename();
} else if(global){
level = (options::Config::Level)options::Config::GLOBAL;
filename = config.GlobalConfigFilename();
} else {
level = (options::Config::Level)options::Config::DIRECTORY;
filename = config.DirectoryConfigFilename();
}
config.LoadAllFiles(level);
if(listAll){
PrintAllBus(config);
return 0;
}
if(!add.empty()){
config.BusDefinitions().Section().AddSubSection(add,"Automatically generated bus definition");
config.Print(filename);
}
if(edit){
std::ifstream file(filename.c_str());
if(!file){
config.Print(filename);
}
std::ostringstream cmd;
const char * ceditor = getenv("EDITOR");
std::string editor;
if(ceditor != NULL){
editor = ceditor;
}
if(editor.empty()){
editor = "vi";
}
cmd << editor << " " << filename;
return system(cmd.str().c_str());
}
return 0;
}

Event Timeline