refact: Process::readFile
Remove DString::splitVectorChars. Use std::getline and fstream.
This commit is contained in:
		@ -49,28 +49,6 @@ std::vector<std::string> DString::splitStr(const std::string &str, char c)
 | 
			
		||||
    return splitChars(str.c_str(), c);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<std::string> DString::splitVectorChars(const std::vector<char> &content, size_t length, char c)
 | 
			
		||||
{
 | 
			
		||||
    std::vector<std::string> ret;
 | 
			
		||||
    size_t pos = 0;
 | 
			
		||||
    bool hasChar = true;
 | 
			
		||||
    for (size_t i = 0; i < length; i++) {
 | 
			
		||||
        if (content[i] == c && i > pos && hasChar) {
 | 
			
		||||
            std::string str;
 | 
			
		||||
            for (size_t j = pos; j <= i; j++) {
 | 
			
		||||
                str += std::string(1, content[j]);
 | 
			
		||||
            }
 | 
			
		||||
            ret.push_back(str);
 | 
			
		||||
            pos = i + 1;
 | 
			
		||||
            hasChar = false;
 | 
			
		||||
        } else {
 | 
			
		||||
            hasChar = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool DString::startWith(const char *chars, const char *prefix)
 | 
			
		||||
{
 | 
			
		||||
    if (!chars || !prefix) {
 | 
			
		||||
 | 
			
		||||
@ -24,7 +24,6 @@ public:
 | 
			
		||||
    // 字符串拆分
 | 
			
		||||
    static std::vector<std::string> splitChars(const char *cs, char c);
 | 
			
		||||
    static std::vector<std::string> splitStr(const std::string &str, char c);
 | 
			
		||||
    static std::vector<std::string> splitVectorChars(const std::vector<char> &content, size_t length, char c);
 | 
			
		||||
    // 字符串前缀判断
 | 
			
		||||
    static bool startWith(const char *chars, const char *prefix);
 | 
			
		||||
    static bool startWith(const std::string &str, const std::string &prefix);
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@
 | 
			
		||||
#include "dfile.h"
 | 
			
		||||
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <dirent.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
@ -154,18 +155,19 @@ std::string Process::getFile(const std::string &name)
 | 
			
		||||
    return "/proc/" + std::to_string(m_pid) + "/" + name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// /proc is not real file system
 | 
			
		||||
// This funciton only can used to read `environ` and `cmdline`
 | 
			
		||||
std::vector<std::string> Process::readFile(std::string fileName)
 | 
			
		||||
{
 | 
			
		||||
    std::vector<std::string> ret;
 | 
			
		||||
    std::FILE *fp = std::fopen(fileName.c_str(), "r");
 | 
			
		||||
    if (!fp)
 | 
			
		||||
        return ret;
 | 
			
		||||
    std::ifstream fs(fileName);
 | 
			
		||||
    if (!fs.is_open()) {
 | 
			
		||||
            return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    std::vector<char> content(FILECONTENLEN);
 | 
			
		||||
    std::size_t len = std::fread(&content[0], 1, FILECONTENLEN, fp);
 | 
			
		||||
    std::fclose(fp);
 | 
			
		||||
    std::string tmp;
 | 
			
		||||
    while (std::getline(fs, tmp, '\0')) {
 | 
			
		||||
        ret.push_back(tmp);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = DString::splitVectorChars(content, len, '\0');
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user