Skip to content

Commit

Permalink
feat(cpp): add 'NetworkDevice'
Browse files Browse the repository at this point in the history
  • Loading branch information
jouyouyun committed May 13, 2019
1 parent 715024f commit 8eae1c8
Show file tree
Hide file tree
Showing 6 changed files with 479 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cpp/NetworkDevice/Makefile
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
101 changes: 101 additions & 0 deletions cpp/NetworkDevice/file.cpp
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
33 changes: 33 additions & 0 deletions cpp/NetworkDevice/file.h
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
22 changes: 22 additions & 0 deletions cpp/NetworkDevice/main.cpp
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;
}
Loading

0 comments on commit 8eae1c8

Please sign in to comment.