#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;
        }
    }
}
}

CommStringLib的更多相关文章

随机推荐

  1. CommonJS和AMD/CMD

    JS中的模块规范(CommonJS,AMD,CMD) 一,CommonJS NodeJS是CommonJS规范的实现,webpack也是以CommonJS的形式来书写. 在浏览器环境下,没有模块也不是 ...

  2. hdu2732 (Leapin' Lizards)

    题目链接:传送门 题目大意:给你 n,m  n:有几行图,m是一个人最多跳m个曼哈顿距离. 给你两张图,第一张图数字为0表示没有柱子,否则有柱子且只能跳出去 x 次(x为当前字符代表的数字) 第二张图 ...

  3. Jenkins publish over ssh 路劲配置问题 记录

    每次通过jenkins 实现  maven项目编辑后 自动通过 ssh发布到 服务器的功能时,对配置的路劲有疑问,特整理出来 前提:服务器路径   /home/ubuntu/aps 目标: 构建后的j ...

  4. iOS企业开发In House ipa发布流程

    这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号.在网上查了一下资料,感觉没有一个比较完善的流程,于是决定把整个流程写下来,供大家参考. 首先详细说明一下我们的目标,我们需要发布 ...

  5. C#操作AD及Exchange Server总结(二)

    上一节C#操作AD及Exchange Server总结(一)写了对AD的操作,新建AD用户后,通常都需要为此用户开启Exchange邮箱,接下来写如何远程操作Exchange. 三.对Exchange ...

  6. SpringBoot专题2----springboot与schedule的激情相拥

    Schedule:计划,任务.就是我们常说的定时任务.这在我们做一些系统的时候,是经常需要用到的.比如:定时更新一些数据,定时更新索引,都需要这样的一个功能. 第一:创建一个名为springboot- ...

  7. 深度扫盲O2O

    http://www.ftchinese.com/interactive/5038?i=3 http://www.ftchinese.com/interactive/5038?i=3

  8. Powershell Get File/Disk Size

    知识点: 1.获取路径中的文件夹:Get-ChildItem $startFolder  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Obje ...

  9. 手机APP卸载原因 不会卸载

  10. Taylor's theorem

    w https://en.wikipedia.org/wiki/Taylor_series