1
0
This commit is contained in:
Scott E. Graves
2017-03-15 16:43:30 -05:00
parent 46136b5c1f
commit 14349a082a
3 changed files with 316 additions and 11 deletions

View File

@@ -0,0 +1,68 @@
#include <siaapi.h>
#include <regex>
#include <siadriveconfig.h>
using namespace Sia::Api;
CSiaApi::CSiaApi(const SiaHostConfig& hostConfig, CSiaDriveConfig* siaDriveConfig) :
_hostConfig(hostConfig),
_siaCurl(hostConfig),
_siaDriveConfig(siaDriveConfig),
_wallet(new CSiaWallet(_siaCurl, siaDriveConfig)),
_renter(new CSiaRenter(_siaCurl, siaDriveConfig)),
_consensus(new CSiaConsensus(_siaCurl, siaDriveConfig))
{
}
CSiaApi::~CSiaApi()
{
//TODO Make this an option to lock on exit
//_wallet->Lock();
}
SString CSiaApi::FormatToSiaPath(SString path)
{
if (path.Length())
{
std::replace(path.begin(), path.end(), '\\', '/');
std::wregex r(L"/+");
path = std::regex_replace(path.str(), r, L"/");
while (path[0] == '/')
{
path = path.SubString(1);
}
}
else
{
path = L"/";
}
return path;
}
SString CSiaApi::GetServerVersion() const
{
return _siaCurl.GetServerVersion();
}
CSiaWalletPtr CSiaApi::GetWallet() const
{
return _wallet;
}
CSiaRenterPtr CSiaApi::GetRenter() const
{
return _renter;
}
CSiaConsensusPtr CSiaApi::GetConsensus() const
{
return _consensus;
}
SiaHostConfig CSiaApi::GetHostConfig() const
{
return _hostConfig;
}

View File

@@ -1,5 +1,9 @@
#include <siadriveconfig.h>
#include <fstream>
#ifdef _WIN32
#include <Shlobj.h>
#endif
using namespace Sia::Api;
CSiaDriveConfig::CSiaDriveConfig() :
@@ -10,8 +14,10 @@ CSiaDriveConfig::CSiaDriveConfig() :
CSiaDriveConfig::CSiaDriveConfig(const SString& filePath) :
_FilePath(filePath)
{
#ifdef _WIN32
#ifdef _DEBUG
::DeleteFile(filePath.c_str());
::DeleteFile(filePath.str().c_str());
#endif
#endif
Load();
}
@@ -23,6 +29,7 @@ CSiaDriveConfig::~CSiaDriveConfig()
void CSiaDriveConfig::LoadDefaults()
{
#ifdef _WIN32
SetUI_Main_TabIndex(0);
SetRenter_UploadDbFilePath("./config/renter_upload.db3");
@@ -44,21 +51,21 @@ void CSiaDriveConfig::LoadDefaults()
::PathCombine(&cacheFolder[0], &sdFolder[0], L"Cache");
::CreateDirectory(cacheFolder.str().c_str(), nullptr);
SetCacheFolder(cacheFolder);
#else
#endif
}
void CSiaDriveConfig::Load( )
{
CFile f;
if (f.Open(GetFilePath().c_str(), CFile::modeRead))
std::ifstream myfile(GetFilePath().str().c_str());
if (myfile.is_open())
{
SString s;
s.Resize(static_cast<std::size_t>(f.GetLength()));
f.Read(&s[0], static_cast<UINT>(s.Length()));
f.Close();
_configDocument = json::parse(s.begin(), s.end());
std::stringstream ss;
ss << myfile.rdbuf();
std::string jsonTxt = ss.str();
_configDocument = json::parse(jsonTxt.begin(), jsonTxt.end());
myfile.close();
}
else
{
@@ -69,6 +76,7 @@ void CSiaDriveConfig::Load( )
void CSiaDriveConfig::Save() const
{
#ifdef _WIN32
SString folder = GetFilePath();
::PathRemoveFileSpec(&folder[0]);
@@ -76,6 +84,8 @@ void CSiaDriveConfig::Save() const
{
::CreateDirectory(folder.str().c_str(), nullptr);
}
#else
#endif
std::ofstream(SString::ToUtf8(GetFilePath()).c_str()) << std::setw(2) << _configDocument << std::endl;
}