feat: 实现StartManager功能
实现StartManager功能, 曝露在ApplicationManager服务上 Log: 实现StartManager功能 Task: https://pms.uniontech.com/task-view-130337.html Influence: 无 Change-Id: I8e83a66f8c25b09e0f4be1e4be9defac95b02b80
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2022 ~ 2023 Deepin Technology Co., Ltd.
|
||||
* Copyright (C) 2021 ~ 2022 Deepin Technology Co., Ltd.
|
||||
*
|
||||
* Author: weizhixiang <weizhixiang@uniontech.com>
|
||||
*
|
||||
@ -29,26 +29,26 @@
|
||||
#include <iostream>
|
||||
|
||||
KeyFile::KeyFile(char separtor)
|
||||
: fp(nullptr)
|
||||
, modified(false)
|
||||
, listSeparator(separtor)
|
||||
: m_fp(nullptr)
|
||||
, m_modified(false)
|
||||
, m_listSeparator(separtor)
|
||||
{
|
||||
}
|
||||
|
||||
KeyFile::~KeyFile()
|
||||
{
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
fp = nullptr;
|
||||
if (m_fp) {
|
||||
fclose(m_fp);
|
||||
m_fp = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool KeyFile::getBool(const std::string §ion, const std::string &key, bool defaultValue)
|
||||
{
|
||||
if (mainKeyMap.find(section) == mainKeyMap.end())
|
||||
if (m_mainKeyMap.find(section) == m_mainKeyMap.end())
|
||||
return false;
|
||||
|
||||
std::string valueStr = mainKeyMap[section][key];
|
||||
std::string valueStr = m_mainKeyMap[section][key];
|
||||
bool value = defaultValue;
|
||||
if (valueStr == "true")
|
||||
value = true;
|
||||
@ -58,6 +58,14 @@ bool KeyFile::getBool(const std::string §ion, const std::string &key, bool d
|
||||
return value;
|
||||
}
|
||||
|
||||
void KeyFile::setBool(const std::string §ion, const std::string &key, const std::string &defaultValue)
|
||||
{
|
||||
if (m_mainKeyMap.find(section) == m_mainKeyMap.end())
|
||||
m_mainKeyMap.insert({section, KeyMap()});
|
||||
|
||||
m_mainKeyMap[section][key] = defaultValue;
|
||||
}
|
||||
|
||||
// TODO
|
||||
std::vector<bool> KeyFile::getBoolList(const std::string §ion, const std::string &key, bool defaultValue)
|
||||
{
|
||||
@ -67,10 +75,10 @@ std::vector<bool> KeyFile::getBoolList(const std::string §ion, const std::st
|
||||
|
||||
int KeyFile::getInt(const std::string §ion, const std::string &key, int defaultValue)
|
||||
{
|
||||
if (mainKeyMap.find(section) == mainKeyMap.end())
|
||||
if (m_mainKeyMap.find(section) == m_mainKeyMap.end())
|
||||
return defaultValue;
|
||||
|
||||
std::string valueStr = mainKeyMap[section][key];
|
||||
std::string valueStr = m_mainKeyMap[section][key];
|
||||
int value;
|
||||
try {
|
||||
value = std::stoi(valueStr);
|
||||
@ -94,6 +102,12 @@ int64_t KeyFile::getInt64(const std::string §ion, const std::string &key, in
|
||||
return int64_t(0);
|
||||
}
|
||||
|
||||
// TODO
|
||||
uint64_t KeyFile::getUint64(const std::string §ion, const std::string &key, int64_t defaultValue)
|
||||
{
|
||||
return uint64_t(0);
|
||||
}
|
||||
|
||||
// TODO
|
||||
float KeyFile::getFloat(const std::string §ion, const std::string &key, float defaultValue)
|
||||
{
|
||||
@ -102,10 +116,10 @@ float KeyFile::getFloat(const std::string §ion, const std::string &key, floa
|
||||
|
||||
std::string KeyFile::getStr(const std::string §ion, const std::string &key, std::string defaultValue)
|
||||
{
|
||||
if (mainKeyMap.find(section) == mainKeyMap.end())
|
||||
if (m_mainKeyMap.find(section) == m_mainKeyMap.end())
|
||||
return defaultValue;
|
||||
|
||||
std::string valueStr = mainKeyMap[section][key];
|
||||
std::string valueStr = m_mainKeyMap[section][key];
|
||||
if (valueStr.empty())
|
||||
valueStr = defaultValue;
|
||||
|
||||
@ -114,10 +128,10 @@ std::string KeyFile::getStr(const std::string §ion, const std::string &key,
|
||||
|
||||
bool KeyFile::containKey(const std::string §ion, const std::string &key)
|
||||
{
|
||||
if (mainKeyMap.find(section) == mainKeyMap.end())
|
||||
if (m_mainKeyMap.find(section) == m_mainKeyMap.end())
|
||||
return false;
|
||||
|
||||
return mainKeyMap[section].find(key) != mainKeyMap[section].end();
|
||||
return m_mainKeyMap[section].find(key) != m_mainKeyMap[section].end();
|
||||
}
|
||||
|
||||
std::string KeyFile::getLocaleStr(const std::string §ion, const std::string &key, std::string defaultLocale)
|
||||
@ -141,7 +155,7 @@ std::string KeyFile::getLocaleStr(const std::string §ion, const std::string
|
||||
std::vector<std::string> KeyFile::getStrList(const std::string §ion, const std::string &key)
|
||||
{
|
||||
std::string value = getStr(section, key);
|
||||
return DString::splitStr(value, listSeparator);
|
||||
return DString::splitStr(value, m_listSeparator);
|
||||
}
|
||||
|
||||
std::vector<std::string> KeyFile::getLocaleStrList(const std::string §ion, const std::string &key, std::string defaultLocale)
|
||||
@ -164,10 +178,10 @@ std::vector<std::string> KeyFile::getLocaleStrList(const std::string §ion, c
|
||||
// 修改keyfile内容
|
||||
void KeyFile::setKey(const std::string §ion, const std::string &key, const std::string &value)
|
||||
{
|
||||
if (mainKeyMap.find(section) == mainKeyMap.end())
|
||||
mainKeyMap.insert({section, KeyMap()});
|
||||
if (m_mainKeyMap.find(section) == m_mainKeyMap.end())
|
||||
m_mainKeyMap.insert({section, KeyMap()});
|
||||
|
||||
mainKeyMap[section].insert({key, value});
|
||||
m_mainKeyMap[section][key] = value;
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
@ -178,7 +192,7 @@ bool KeyFile::saveToFile(const std::string &filePath)
|
||||
return false;
|
||||
|
||||
|
||||
for (const auto &im : mainKeyMap) {
|
||||
for (const auto &im : m_mainKeyMap) {
|
||||
const auto &keyMap = im.second;
|
||||
std::string section = "[" + im.first + "]\n";
|
||||
fputs(section.c_str(), sfp);
|
||||
@ -194,19 +208,19 @@ bool KeyFile::saveToFile(const std::string &filePath)
|
||||
|
||||
bool KeyFile::loadFile(const std::string &filePath)
|
||||
{
|
||||
mainKeyMap.clear();
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
fp = nullptr;
|
||||
m_mainKeyMap.clear();
|
||||
if (m_fp) {
|
||||
fclose(m_fp);
|
||||
m_fp = nullptr;
|
||||
}
|
||||
|
||||
std::string lastSection;
|
||||
fp = fopen(filePath.data(), "r");
|
||||
if (!fp)
|
||||
m_fp = fopen(filePath.data(), "r");
|
||||
if (!m_fp)
|
||||
return false;
|
||||
|
||||
char line[MAX_LINE_LEN] = {0};
|
||||
while (fgets(line, MAX_LINE_LEN, fp)) {
|
||||
while (fgets(line, MAX_LINE_LEN, m_fp)) {
|
||||
char *start = &line[0];
|
||||
char *end = start;
|
||||
while (!strneq(end, "\0", 1))
|
||||
@ -232,7 +246,7 @@ bool KeyFile::loadFile(const std::string &filePath)
|
||||
if (lPos && rPos && rPos - lPos > 0 && lPos == start && rPos == end) {
|
||||
// 主键
|
||||
std::string section(lPos + 1, size_t(rPos - lPos - 1));
|
||||
mainKeyMap.insert({section, KeyMap()});
|
||||
m_mainKeyMap.insert({section, KeyMap()});
|
||||
lastSection = section;
|
||||
} else {
|
||||
char *equal = strchr(start, '=');
|
||||
@ -248,7 +262,7 @@ bool KeyFile::loadFile(const std::string &filePath)
|
||||
// 子键
|
||||
std::string key(start, size_t(equal - start));
|
||||
std::string value(equal + 1, size_t(end - equal));
|
||||
for (auto &iter : mainKeyMap) {
|
||||
for (auto &iter : m_mainKeyMap) {
|
||||
if (iter.first != lastSection)
|
||||
continue;
|
||||
|
||||
@ -256,8 +270,9 @@ bool KeyFile::loadFile(const std::string &filePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
fp = nullptr;
|
||||
fclose(m_fp);
|
||||
m_fp = nullptr;
|
||||
m_filePath = filePath;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -265,7 +280,7 @@ bool KeyFile::loadFile(const std::string &filePath)
|
||||
std::vector<std::string> KeyFile::getMainKeys()
|
||||
{
|
||||
std::vector<std::string> mainKeys;
|
||||
for (const auto &iter : mainKeyMap)
|
||||
for (const auto &iter : m_mainKeyMap)
|
||||
mainKeys.push_back(iter.first);
|
||||
|
||||
return mainKeys;
|
||||
@ -274,7 +289,7 @@ std::vector<std::string> KeyFile::getMainKeys()
|
||||
void KeyFile::print()
|
||||
{
|
||||
std::cout << "sectionMap: " << std::endl;
|
||||
for (auto sectionMap : mainKeyMap) {
|
||||
for (auto sectionMap : m_mainKeyMap) {
|
||||
std::cout << "section=" << sectionMap.first << std::endl;
|
||||
KeyMap keyMap = sectionMap.second;
|
||||
|
||||
|
Reference in New Issue
Block a user