CommStringLib

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

posted on 2017-11-14 16:04  小小鸟儿!  阅读(277)  评论(0编辑  收藏  举报