entmemberfunktioned

master
blastmaster 9 years ago
parent 1ab8812486
commit b181c1906b

@ -19,7 +19,7 @@ class DynamicObjects
private:
std::string _cleanpath (const char * const path);
std::vector<Sicccobject> m_themap;
std::vector<Sicccobject> m_thevec;
const Routerin * m_merouterin;
};

@ -8,7 +8,7 @@ std::string DynamicObjects::render_object_list(){
<ul id="down">
)FOO";
for (auto& it : this->m_themap){
for (auto& it : this->m_thevec){
output += u8"<li>\n";
output += u8"<a href=\"" + m_merouterin->get_baseurl() + it.get_path() + u8"\">"; //we could include baseurl here?
output += it.get_filename();
@ -30,8 +30,7 @@ std::string DynamicObjects::render_object_list(){
DynamicObjects::DynamicObjects(Routerin * merouterin) :
m_merouterin(merouterin)
{
m_thevec = SicccPersister::read_sicccdir(*merouterin);
}

@ -11,118 +11,142 @@
#include <iostream>
#include <vector>
static json_t * load_json(const std::string &s)
class SicccPersister
{
assert(!s.empty());
std::cout << "[DEBUG] joad_json: " << s << std::endl;
json_t *root = nullptr;
json_error_t error;
root = json_load_file(s.c_str(), JSON_DECODE_ANY, &error);
if (!root) {
/* std::cerr << "[read_json] " << error << std::endl; */
return nullptr;
}
return root;
}
public:
static std::string get_string_from_json(const json_t *j)
{
assert(j);
assert(json_is_string(j));
const char *tmp = json_string_value(j);
assert(tmp);
return std::string(tmp);
}
static json_t *get_json_from_string(const std::string &s)
{
json_t* ret = json_string(s.c_str());
assert(ret);
return ret;
}
static json_t * load_json(const std::string &s)
{
assert(!s.empty());
std::cout << "[DEBUG] joad_json: " << s << std::endl;
json_t *root = nullptr;
json_error_t error;
root = json_load_file(s.c_str(), JSON_DECODE_ANY, &error);
if (!root) {
/* std::cerr << "[read_json] " << error << std::endl; */
return nullptr;
}
return root;
}
static unsigned int get_integer_from_json(const json_t *j)
{
assert(j);
assert(json_is_integer(j));
return json_integer_value(j);
}
static std::string get_string_from_json(const json_t *j)
{
assert(j);
assert(json_is_string(j));
const char *tmp = json_string_value(j);
assert(tmp);
return std::string(tmp);
}
static json_t *get_json_from_integer(const unsigned int value)
{
assert(value);
json_t* ret = json_integer(value);
assert(ret);
return ret;
}
static std::chrono::time_point<std::chrono::system_clock>
get_time_from_json(const json_t *j)
{
assert(j);
auto tmp = get_string_from_json(j);
time_t t = stol(tmp);
return std::chrono::system_clock::from_time_t(t);
}
static json_t *get_json_from_string(const std::string &s)
{
json_t* ret = json_string(s.c_str());
assert(ret);
return ret;
}
static std::string time_to_string(const std::chrono::time_point<std::chrono::system_clock>& t)
{
time_t time = std::chrono::system_clock::to_time_t(t);
std::string ret = std::to_string(time);
return ret;
}
/** TODO
* implement bool operator to take advantages of the use of assert
*/
static json_t *get_json_from_sicccobject(const Sicccobject& so)
{
json_t *root = json_object();
json_object_set_new(root, "filename", get_json_from_string(so.get_filename()));
json_object_set_new(root, "filetype", get_json_from_string(so.get_filetype()));
json_object_set_new(root, "path", get_json_from_string(so.get_path()));
json_object_set_new(root, "size", get_json_from_integer(so.get_size()));
std::string tmp = time_to_string(so.get_uploadtime());
json_object_set_new(root, "upload_time", get_json_from_string(tmp));
assert(root);
return root;
}
static std::string get_json_string_representation(const json_t *j)
{
assert(j);
assert(json_is_object(j));
const char *tmp = json_dumps(j, JSON_INDENT(4));
assert(tmp);
std::string ret(tmp);
free((void*) tmp);
return ret;
}
static unsigned int get_integer_from_json(const json_t *j)
{
assert(j);
assert(json_is_integer(j));
return json_integer_value(j);
}
class SicccPersister
{
public:
SicccPersister(const Routerin & merouter = *Routerin::get_instance()) :
merouter(merouter)
static json_t *get_json_from_integer(const unsigned int value)
{
root = json_object();
json_file_name = "foo.json"; // not the best default
};
assert(value);
json_t* ret = json_integer(value);
assert(ret);
return ret;
}
SicccPersister(const Sicccobject& so,const Routerin &merouter = * Routerin::get_instance()):
merouter(merouter)
static std::chrono::time_point<std::chrono::system_clock>
get_time_from_json(const json_t *j)
{
assert(j);
auto tmp = get_string_from_json(j);
time_t t = stol(tmp);
return std::chrono::system_clock::from_time_t(t);
}
static std::string time_to_string(const std::chrono::time_point<std::chrono::system_clock>& t)
{
root = get_json_from_sicccobject(so);
json_file_name = so.get_filename();
time_t time = std::chrono::system_clock::to_time_t(t);
std::string ret = std::to_string(time);
return ret;
}
SicccPersister(json_t *j, const Routerin & merouter =* Routerin::get_instance()):
merouter(merouter)
/** TODO
* implement bool operator to take advantages of the use of assert
*/
static json_t *get_json_from_sicccobject(const Sicccobject& so)
{
json_t *root = json_object();
json_object_set_new(root, "filename", get_json_from_string(so.get_filename()));
json_object_set_new(root, "filetype", get_json_from_string(so.get_filetype()));
json_object_set_new(root, "path", get_json_from_string(so.get_path()));
json_object_set_new(root, "size", get_json_from_integer(so.get_size()));
std::string tmp = time_to_string(so.get_uploadtime());
json_object_set_new(root, "upload_time", get_json_from_string(tmp));
assert(root);
return root;
}
static std::string get_json_string_representation(const json_t *j)
{
root = json_object();
json_object_update(root, j);
assert(j);
assert(json_is_object(j));
const char *tmp = json_dumps(j, JSON_INDENT(4));
assert(tmp);
std::string ret(tmp);
free((void*) tmp);
return ret;
}
static std::vector<Sicccobject>
read_sicccdir(const Routerin &merouter = *Routerin::get_instance())
{
DIR *dir = nullptr;
struct dirent *entry = nullptr;
std::vector<Sicccobject> v;
json_error_t *error = nullptr;
if ((dir = opendir(merouter.get_persist_path().c_str())) == NULL) {
std::cerr << "Error could not open directory" << std::endl;
return v; // should throw exception instea1d
}
while ((entry = readdir(dir)) != NULL) {
if (std::strstr(entry->d_name, ".json") == NULL)
continue;
json_t *tmp = json_load_file(entry->d_name, JSON_DECODE_ANY, error);
assert(tmp);
Sicccobject so(tmp);
v.push_back(so);
}
return v;
}
public:
SicccPersister(const Routerin & merouter = *Routerin::get_instance()) :
merouter(merouter)
{
root = json_object();
json_file_name = "foo.json"; // not the best default
};
SicccPersister(const Sicccobject& so,const Routerin &merouter = * Routerin::get_instance()):
merouter(merouter)
{
root = get_json_from_sicccobject(so);
json_file_name = so.get_filename();
}
SicccPersister(json_t *j, const Routerin & merouter =* Routerin::get_instance()):
merouter(merouter)
{
root = json_object();
json_object_update(root, j);
}
~SicccPersister()
{
if (root)
@ -131,7 +155,7 @@ class SicccPersister
int write_sicccfile(const Sicccobject&);
int write_sicccfile();
std::vector<Sicccobject> read_sicccdir();
/* std::vector<Sicccobject> read_sicccdir(); */
Sicccobject read_json(const std::string&);
Sicccobject read_json() { return read_json(json_file_name); }
@ -149,7 +173,7 @@ class SicccPersister
}
private:
const Routerin & merouter;
const Routerin & merouter;
std::string json_file_name;
json_t *root;

@ -30,24 +30,3 @@ SicccPersister::read_json(const std::string &s)
return Sicccobject(root);
}
std::vector<Sicccobject>
SicccPersister::read_sicccdir()
{
DIR *dir = nullptr;
struct dirent *entry = nullptr;
std::vector<Sicccobject> v;
json_error_t *error = nullptr;
if ((dir = opendir(merouter.get_persist_path().c_str())) == NULL) {
std::cerr << "Error could not open directory" << std::endl;
return v; // should throw exception instea1d
}
while ((entry = readdir(dir)) != NULL) {
if (std::strstr(entry->d_name, ".json") == NULL)
continue;
json_t *tmp = json_load_file(entry->d_name, JSON_DECODE_ANY, error);
assert(tmp);
Sicccobject so(tmp);
v.push_back(so);
}
return v;
}

@ -9,15 +9,15 @@ Sicccobject::Sicccobject(const json_t *j)
assert(j);
json_t *val;
val = json_object_get(j, "filename");
filename = get_string_from_json(val);
filename = SicccPersister::get_string_from_json(val);
val = json_object_get(j, "filetype");
filetype = get_string_from_json(val);
filetype = SicccPersister::get_string_from_json(val);
val = json_object_get(j, "path");
path = get_string_from_json(val);
path = SicccPersister::get_string_from_json(val);
val = json_object_get(j, "size");
size = get_integer_from_json(val);
size = SicccPersister::get_integer_from_json(val);
val = json_object_get(j, "upload_time");
upload_date = get_time_from_json(val);
upload_date = SicccPersister::get_time_from_json(val);
json_decref(val);
}

Loading…
Cancel
Save