mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Implemented config and crypto infrastructure (#2)
Added config file read/write infrastructure with RapidJSON. Added key pair generation and sign/verify helper with libsodium.
This commit is contained in:
61
src/conf.cpp
Normal file
61
src/conf.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <unistd.h>
|
||||
#include "lib/rapidjson/document.h"
|
||||
#include "lib/rapidjson/istreamwrapper.h"
|
||||
#include "lib/rapidjson/ostreamwrapper.h"
|
||||
#include "lib/rapidjson/writer.h"
|
||||
#include "conf.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace rapidjson;
|
||||
|
||||
namespace conf
|
||||
{
|
||||
|
||||
static const char * configPath;
|
||||
|
||||
string get_full_path(const char *filename)
|
||||
{
|
||||
string fullpath = configPath;
|
||||
fullpath += filename;
|
||||
return fullpath;
|
||||
}
|
||||
|
||||
void load(const char *filename, Document &d)
|
||||
{
|
||||
ifstream ifs(get_full_path(filename));
|
||||
IStreamWrapper isw(ifs);
|
||||
|
||||
d.ParseStream(isw);
|
||||
}
|
||||
|
||||
void save(const char *filename, Document &d)
|
||||
{
|
||||
ofstream ofs(get_full_path(filename));
|
||||
OStreamWrapper osw(ofs);
|
||||
|
||||
Writer<OStreamWrapper> writer(osw);
|
||||
d.Accept(writer);
|
||||
}
|
||||
|
||||
string get_exec_path()
|
||||
{
|
||||
char buf[PATH_MAX + 1];
|
||||
if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) == -1)
|
||||
throw string("readlink() failed");
|
||||
string str(buf);
|
||||
return str.substr(0, str.rfind('/') + 1);
|
||||
}
|
||||
|
||||
int init()
|
||||
{
|
||||
string execPath = get_exec_path();
|
||||
char * confPathChar = (char *)malloc(execPath.size() + 1);
|
||||
strcpy(confPathChar, &execPath[0]);
|
||||
configPath = confPathChar;
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace conf
|
||||
Reference in New Issue
Block a user