#include "syswatcher/CommStringLib.h"
//#include "String16.h"
#undef LOG_TAG
#define LOG_TAG "SysWatcher"
namespace yunos {
using namespace std;
using namespace android;
string readproc(const char* path) {
ifstream infile(path);
string message;
if (infile.is_open()) {
message = string((std::istreambuf_iterator<char>(infile)),
(std::istreambuf_iterator<char>()));
infile.close();
} else {
printf("readproc error!");
}
return message;
}
bool starts_with(string src, string key) {
if (src.substr(0, key.size()) == key) {
return true;
} else {
return false;
}
}
std::string& trim(std::string &s) {
if (s.empty()) {
return s;
}
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
s.erase(0, s.find_first_not_of("\t"));
s.erase(s.find_last_not_of("\t") + 1);
return s;
}
string replace(const string& str, const string& src, const string& dest) {
string ret;
string::size_type pos_begin = 0;
string::size_type pos = str.find(src);
while (pos != string::npos) {
ret.append(str.data() + pos_begin, pos - pos_begin);
ret += dest;
pos_begin = pos + src.size();
pos = str.find(src, pos_begin);
}
if (pos_begin < str.length()) {
ret.append(str.begin() + pos_begin, str.end());
}
return ret;
}
String16 toString16(const string &ret) {
return String16(ret.c_str());
}
String16 toString16(const char* p) {
return String16(p);
}
string toString(String16 ret) {
printf("toString String16=%s\n", String8(ret.string()).string());
return string(String8(ret.string()).string());
}
//注意:当字符串为空时,也会返回一个空字符串
void split(std::string& s, std::string const &delim,
std::vector<std::string>* ret) {
size_t last = 0;
size_t index = s.find_first_of(delim, last);
while (index != std::string::npos) {
ret->push_back(s.substr(last, index - last));
last = index + 1;
index = s.find_first_of(delim, last);
}
if (index - last > 0) {
ret->push_back(s.substr(last, index - last));
}
}
string transValue(long size) {
char str[32];
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
float f;
if (size >= gb) {
sprintf(str, "%.1f GB", (float) size / gb);
} else if (size >= mb) {
f = (float) size / mb;
sprintf(str, (f > 100 ? "%.0f MB" : "%.1f MB"), f);
} else if (size >= kb) {
f = (float) size / kb;
sprintf(str, (f > 100 ? "%.0f KB" : "%.1f KB"), f);
}
return string(str);
}
long mykbtoi(string v) { //str= "123 KB"
int len = v.size() - 3 + 1;
char * tmp = new char[len];
string sv = v.substr(0, len - 1);
memset(tmp, 0, len);
strcpy(tmp, sv.c_str());
long size = atoi(tmp);
delete tmp;
return size * 1024;
}
string lines2json(std::string& lines, std::string const &delim1, std::vector<
std::string>const &keys, bool trans, string interface) {
string delim = string("\n");
std::vector<std::string> * ret = new vector<string> ();
Json::Value root;
split(lines, delim, ret);
for (std::vector<std::string>::iterator iter = ret->begin(); iter
!= ret->end(); ++iter) {
*iter = trim(*iter);
string line = *iter;
if (keys.size() == 0) {
std::vector<std::string> * _value0 = new vector<string> ();
split(line, delim1, _value0);
if (_value0->size() >= 2) {
string k = trim(_value0->at(0));
string v = trim(_value0->at(1));
if (trans) {
long num = mykbtoi(v);
v = transValue(num);
}
root[k] = v;
}
delete _value0;
continue;
}
for (u_int i = 0; i < keys.size(); ++i) {
if (starts_with(line, keys[i])) {
std::vector<std::string> * _value = new vector<string> ();
split(line, delim1, _value);
if (_value->size() >= 2) {
string k = trim(_value->at(0));
string v = trim(_value->at(1));
if (trans) {
long num = mykbtoi(v);
v = transValue(num);
}
root[k] = v;
}
delete _value;
}
}
}
delete ret;
if (interface != "") {
root["interface"] = interface;
}
string str_json = root.toStyledString();
return str_json;
}
status_t checkSystem(pid_t status) {
if (-1 == status) {
printf("system error!");
return false;
} else {
printf("exit status value=[0x%x]\n", status);
if (WIFEXITED(status)) {
if (0 == WEXITSTATUS(status)) {
printf("run shell script successfully!\n");
return true;
} else {
printf("run shell script fail, script exit code:%d\n",
WEXITSTATUS(status));
return false;
}
} else {
printf("exit status=[%d]\n", WEXITSTATUS(status));
return false;
}
}
}
}
随机推荐
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- python macos scrapy ,gevent module
easy_install pip pip install scrapy pip install ipython ImportError: No module named items https://g ...
- 【我的Android进阶之旅】如何在浏览器上使用Octotree插件树形地展示Github项目代码?
前言 最近有个同事看到我打开Github项目时,浏览器上的展示效果是树形的,于是他问我这个是什么浏览器插件,我告诉他是Octotree插件.现在我就来介绍介绍这款Octotree插件. 效果对比 1. ...
- UTF-8具体解释
UTF-8是一种变长字节的编码方式.它以8位(1字节)为单位对Unicode进行编码. UTF-8理论上最多能够达到6字节长.但眼下全世界的字符仅仅须要4字节就能够表示完. UTF-8规定,对于某一字 ...
- 基于docker 搭建Elasticsearch5.6.4 分布式集群
说明: 准备2台机器,我这里有192. 和 192.168.0.164 192.168.0.164 作为master 192.168.0.107 作为普通node 一.环境 .docker 环境 .E ...
- ionic3使用echart插件
安装 看官方文档可以知道ECharts可以在webpack中使用看这里,故我们可以使用npm下载安装到项目中 npm install echarts --save //下载ECharts npm in ...
- Linux学习笔记之文件权限
前言: 说起文件权限,大家在windows下应该很熟悉就对文件右键属性,然后配置一点什么读写之类的权限,然后可以分配到每个的人. 对于linux 我先为大家介绍一个使用者和组和其他的概念说明一下 文件 ...
- matplotlib作图——plot() 线图
线图 #定义 matplotlib.pyplot.plot() plot([x], y, [fmt], data=None, **kwargs) plot([x], y, [fmt], [x2], y ...
- NHibernate & INotifyPropertyChanged
One of the things that make NHibernate easy to use is that it fully support the POCO model. But one ...
- Educational Codeforces Round 11C. Hard Process two pointer
地址:http://codeforces.com/contest/660/problem/C 题目: You are given an array a with n elements. Each el ...