-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
479 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CPP=g++ -Wall -g | ||
PWD=$(shell pwd) | ||
PROG=net_dev_list | ||
|
||
LDFLAGS = -lboost_system -lboost_filesystem -lboost_thread -lpthread | ||
OBJS = main.o network_device.o file.o task.o task_manager.o | ||
|
||
vpath %.cpp ${PWD} ../runner | ||
|
||
build:${PROG} | ||
|
||
${PROG}:${OBJS} | ||
${CPP} -o $@ $^ ${LDFLAGS} | ||
|
||
%.o:%.cpp | ||
${CPP} -c $< | ||
|
||
clean: | ||
rm -rf *.o ${PROG} | ||
|
||
rebuild: clean build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include "file.h" | ||
|
||
#include <exception> | ||
#include <boost/algorithm/string.hpp> | ||
|
||
#include <iostream> | ||
|
||
namespace jouyouyun | ||
{ | ||
namespace network | ||
{ | ||
namespace device | ||
{ | ||
using namespace std; | ||
|
||
KeyValueFile::KeyValueFile(const string &filepath, | ||
const string &sep, ios::openmode mode) : line_sep(sep) | ||
{ | ||
try { | ||
this->core = unique_ptr<fstream>(new fstream); | ||
this->core->open(filepath, mode); | ||
} catch (exception &e) { | ||
throw string("Failed to open file:") + e.what(); | ||
return; | ||
} | ||
} | ||
|
||
KeyValueFile::~KeyValueFile() | ||
{ | ||
} | ||
|
||
string KeyValueFile::LoadContent() | ||
{ | ||
string content; | ||
try { | ||
istreambuf_iterator<char> begin(*(this->core)); | ||
istreambuf_iterator<char> end; | ||
content = string(begin, end); | ||
} catch (exception &e) { | ||
cout << "Failed to load file content:" << e.what() << endl; | ||
} | ||
return content; | ||
} | ||
|
||
string KeyValueFile::GetKey(const string &key) | ||
{ | ||
try { | ||
// must exec 'clear' before 'seekg'. | ||
// because of 'core' if reached the end of file, will set the eofbit, seekg will not work | ||
this->core->clear(); | ||
this->core->seekg(ios::beg); | ||
} catch (exception &e) { | ||
cout << "Failed to seek file:" << e.what() << endl; | ||
return string(""); | ||
} | ||
|
||
string result; | ||
string line; | ||
while (getline(*(this->core), line)) { | ||
vector<string> list = SplitString(line, this->line_sep); | ||
if (list.size() != 2) { | ||
continue; | ||
} | ||
boost::algorithm::trim(list[0]); | ||
if (list[0] != key) { | ||
continue; | ||
} | ||
result = list[1]; | ||
break; | ||
} | ||
// TODO(jouyouyun): remove | ||
boost::algorithm::trim_left_if(result, | ||
boost::algorithm::is_any_of("\t")); | ||
return result; | ||
} | ||
|
||
vector<string> SplitString(const string &str, const string &sep) | ||
{ | ||
vector<string> items; | ||
if (str.empty()) { | ||
return items; | ||
} | ||
if (sep.empty()) { | ||
items.push_back(str); | ||
return items; | ||
} | ||
|
||
string::size_type pos = 0; | ||
pos = str.find(sep, 0); | ||
if (pos == string::npos) { | ||
items.push_back(str); | ||
return items; | ||
} | ||
|
||
items.push_back(str.substr(0, pos)); | ||
items.push_back(str.substr(pos + sep.size())); | ||
return items; | ||
} | ||
} // namespace device | ||
} // namespace network | ||
} // namesapce jouyouyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <ios> | ||
#include <memory> | ||
#include <vector> | ||
#include <fstream> | ||
|
||
namespace jouyouyun | ||
{ | ||
namespace network | ||
{ | ||
namespace device | ||
{ | ||
class KeyValueFile | ||
{ | ||
public: | ||
KeyValueFile(const std::string &filepath, | ||
const std::string &sep = "=", | ||
std::ios::openmode mode = std::ios::in); | ||
~KeyValueFile(); | ||
|
||
std::string LoadContent(); | ||
std::string GetKey(const std::string &key); | ||
private: | ||
std::string line_sep; | ||
std::unique_ptr<std::fstream> core; | ||
}; | ||
|
||
std::vector<std::string> SplitString(const std::string &str, | ||
const std::string &sep); | ||
} // namespace device | ||
} // namespace network | ||
} // namesapce jouyouyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "network_device.h" | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
using namespace jouyouyun::network::device; | ||
|
||
int | ||
main() | ||
{ | ||
NetworkDevice dev; | ||
vector<DeviceInfo> infos = dev.List(); | ||
vector<DeviceInfo>::iterator it = infos.begin(); | ||
for (; it != infos.end(); it++) { | ||
cout<<"Name: "<<it->name<<endl; | ||
cout<<"\tInterface: "<<it->interface<<endl; | ||
cout<<"\tMacaddress: "<<it->macaddress<<endl; | ||
cout<<"\tIP: "<<it->ip<<endl; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.