-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugins: TcpClientFactory,TcpServerFactory,HttpSessionFactory 可使用 Plugins 機制載入. IoManager: 儲存設定, 檢查Sch.
- Loading branch information
Showing
47 changed files
with
1,950 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// \file fon9/ConfigFileBinder.cpp | ||
// \author [email protected] | ||
#include "fon9/ConfigFileBinder.hpp" | ||
#include "fon9/File.hpp" | ||
#include "fon9/Log.hpp" | ||
|
||
namespace fon9 { | ||
|
||
static std::string MakeErr(StrView logHeader, StrView func, File::Result fres, File& fd) { | ||
std::string errstr = RevPrintTo<std::string>("func=", func, '|', fres, "|file=", fd.GetOpenName()); | ||
if (logHeader.begin() != nullptr) | ||
fon9_LOG_ERROR(logHeader, '|', errstr); | ||
return errstr; | ||
} | ||
static std::string CheckRW(StrView logHeader, StrView func, File::Result fres, File::SizeType expected, File& fd) { | ||
if (!fres) | ||
return MakeErr(logHeader, func, fres, fd); | ||
if (fres.GetResult() == expected) | ||
return std::string{}; | ||
std::string errstr = RevPrintTo<std::string>("func=", func, | ||
"|err.size=", fres.GetResult(), | ||
"|expected=", expected, | ||
"|file=", fd.GetOpenName()); | ||
if (logHeader.begin() != nullptr) | ||
fon9_LOG_ERROR(logHeader, '|', errstr); | ||
return errstr; | ||
} | ||
|
||
std::string ConfigFileBinder::OpenRead(StrView logHeader, std::string cfgfn) { | ||
this->FileName_ = cfgfn; | ||
File fd; | ||
auto fres = fd.Open(std::move(cfgfn), FileMode::Read); | ||
if (!fres) | ||
return MakeErr(logHeader, "Open", fres, fd); | ||
fres = fd.GetFileSize(); | ||
if (!fres) | ||
return MakeErr(logHeader, "GetFileSize", fres, fd); | ||
this->ConfigStr_.resize(fres.GetResult()); | ||
if (this->ConfigStr_.size() <= 0) | ||
return std::string{}; | ||
fres = fd.Read(0, &*this->ConfigStr_.begin(), this->ConfigStr_.size()); | ||
return CheckRW(logHeader, "Read", fres, this->ConfigStr_.size(), fd); | ||
} | ||
std::string ConfigFileBinder::Write(StrView logHeader, std::string cfgstr) { | ||
if (this->ConfigStr_ == cfgstr) | ||
return std::string{}; | ||
if (this->FileName_.empty()) { | ||
this->ConfigStr_ = std::move(cfgstr); | ||
return std::string{}; | ||
} | ||
File fd; | ||
auto fres = fd.Open(this->FileName_, FileMode::Write | FileMode::CreatePath | FileMode::Trunc); | ||
if (!fres) | ||
return MakeErr(logHeader, "Open", fres, fd); | ||
fres = fd.Write(0, &*cfgstr.begin(), cfgstr.size()); | ||
std::string errmsg = CheckRW(logHeader, "Write", fres, cfgstr.size(), fd); | ||
if (errmsg.empty()) | ||
this->ConfigStr_ = std::move(cfgstr); | ||
return errmsg; | ||
} | ||
|
||
} // namespace | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// \file fon9/ConfigFileBinder.cpp | ||
// \author [email protected] | ||
#ifndef __fon9_ConfigFileBinder_hpp__ | ||
#define __fon9_ConfigFileBinder_hpp__ | ||
#include "fon9/StrView.hpp" | ||
|
||
namespace fon9 { | ||
|
||
/// \ingroup Misc | ||
/// - 透過檔案讀/寫設定. | ||
/// - 僅負責讀寫, 不解釋設定內容. | ||
class fon9_API ConfigFileBinder { | ||
std::string FileName_; | ||
std::string ConfigStr_; | ||
public: | ||
bool HasBinding() const { | ||
return !this->FileName_.empty(); | ||
} | ||
|
||
/// 開啟設定檔, 載入內容, 若有失敗則傳回錯誤訊息. | ||
/// - retval.empty() 表示成功, 可透過 GetConfigStr() 取得載入的內容. | ||
/// - 若有失敗, 除了傳回錯誤訊息外, 如果 logHeader != nullptr 則還會透過 log 記錄錯誤訊息. | ||
std::string OpenRead(StrView logHeader, std::string cfgfn); | ||
|
||
/// OpenRead() 成功之後, 透過這裡取得檔案(設定)內容. | ||
/// 僅完整德將檔案內容讀入, 不解釋其內容. | ||
const std::string& GetConfigStr() const { | ||
return this->ConfigStr_; | ||
} | ||
|
||
/// 寫設定有變動時, 將新的設定寫回設定檔. | ||
/// - 設定檔為當初透過 OpenRead() 的 cfgfn; | ||
/// 若 cfgfn.empty() 或沒有呼叫過 OpenRead(); 則只會更新 GetConfigStr(); | ||
/// - 若 cfgstr 與當初讀入的一樣, 則不會有寫入的動作. | ||
/// - 若有失敗, 除了傳回錯誤訊息外, 如果 logHeader != nullptr 則還會透過 log 記錄錯誤訊息. | ||
std::string Write(StrView logHeader, std::string cfgstr); | ||
}; | ||
|
||
} // namespace | ||
|
||
#endif//__fon9_ConfigFileBinder_hpp__ |
Oops, something went wrong.