#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;
}
}
}
}
随机推荐
- 【BZOJ4380】[POI2015]Myjnie 区间DP
[BZOJ4380][POI2015]Myjnie Description 有n家洗车店从左往右排成一排,每家店都有一个正整数价格p[i].有m个人要来消费,第i个人会驶过第a[i]个开始一直到第b[ ...
- java的static final和final的区别
转自:https://www.cnblogs.com/EasonJim/p/7841990.html 说明:不一定准确,但是最快理解. final: final可以修饰:属性,方法,类,局部变量(方法 ...
- [LintCode] 尾部的零
class Solution { public: // param n : description of n // return: description of return long long tr ...
- css 横线中间添加文字
demoline01.02选一个用足够了 <style> .demo_line_01 { width: 200px;/*这指的是文字的宽度*/ padding: 0 20px 0; m ...
- Oracle是如何工作的?实例是如何响应用户请求?一条SQL的执行过程~
Oracle 是如何工作的? Select id,name from t order by id ; – SQL 解析(查看语法是否错误,如果没有错误,分析语意,执行此语句的权限) – 执行计划(OR ...
- MIS货物拆包销售的问题
就是不能拆包装销售.比如一箱香烟要一包包的卖,一箱里面有50条,一条里面有10包,而是,要一包一包的卖. 解决方案:入库的时候,记录下包装总量(自动改成数量×50),再附加2条说明字段,第一条说明是一 ...
- git 添加远程仓库后无法push
push的时候提示fatal: refusing to merge unrelated histories 假如我们的源是origin,分支是master,那么我们 需要这样写git pull o ...
- pandas 如何判断指定列是否(全部)为NaN(空值)
判断某列是否有NaN df['$open'].isnull().any() # 判断open这一列列是否有 NaN 判断某列是否全部为NaN df['$open'].isnull().all() # ...
- 为什么JSP的内置对象不需要声明
本文将通过对一个JSP运行过程的剖析,深入JSP运行的内幕,并从全新的视角阐述一些JSP中的技术要点. HelloWorld.jsp 我们以Tomcat 4.1.17服务器为例,来看看最简单的Hell ...
- 企业内部安全宣贯:乌云网停摆事件的思考与评论——By Me
2016年7月20日,“自由平等开放的漏洞报告平台”乌云网[1] 被迫停摆,包括乌云网创始人方小顿[2] 在内的多名高管突然被捕.乌云的存在可以说是为了修复人们长期缺失的安全意识和堪忧的安全生态,但是 ...