refact: Process::getStatus

log: as title
This commit is contained in:
black-desk 2023-05-10 15:18:49 +08:00 committed by Tsic
parent fd6622c30c
commit 3932b3637e

View File

@ -100,20 +100,30 @@ std::string Process::getEnv(const std::string &key)
Status Process::getStatus() Status Process::getStatus()
{ {
if (m_status.size() == 0) { if (!m_status.empty()){
std::string statusFile = getFile("status"); return m_status;
FILE *fp = fopen(statusFile.c_str(), "r"); }
if (!fp)
return m_status;
char line[MAX_LINE_LEN] = {0}; std::string statusFile = getFile("status");
while (fgets(line, MAX_LINE_LEN, fp)) {
std::string info(line); std::ifstream fs(statusFile);
std::vector<std::string> parts = DString::splitStr(info, ':'); if (!fs.is_open()) {
if (parts.size() == 2) return m_status;
m_status[parts[0]] = parts[1]; }
std::string tmp = "";
while (std::getline(fs, tmp)) {
auto pos = tmp.find_first_of(':');
if (pos == std::string::npos) {
continue;
} }
fclose(fp);
std::string value;
if (pos + 1 < tmp.length()) {
value = tmp.substr(pos + 1);
}
m_status[tmp.substr(0, pos)] = value;
} }
return m_status; return m_status;