From 4f7fd9a7fba1ec3a0279c98f18a3c29539e7dfeb Mon Sep 17 00:00:00 2001 From: behemoth Date: Tue, 13 Aug 2019 02:20:43 +0200 Subject: [PATCH] actually add the new/renamed files --- include/fs.hpp | 18 +++++++++ include/utl.hpp | 5 +++ source/fs.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ source/utl.cpp | 40 ++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 include/fs.hpp create mode 100644 include/utl.hpp create mode 100644 source/fs.cpp create mode 100644 source/utl.cpp diff --git a/include/fs.hpp b/include/fs.hpp new file mode 100644 index 0000000..9dcde42 --- /dev/null +++ b/include/fs.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include +#include +#include +#include "model.hpp" +namespace fs{ + u64 getFreeSpace(); + std::string getHumanReadableBytes(u64); + std::vector listDirectory(std::string); + bool writeFile(std::string filename, std::string data); + bool deleteFile(std::string filename); + bool fileExists(std::string filename); + bool mkpath(std::string path); + std::string getSuffix(int fType); +} \ No newline at end of file diff --git a/include/utl.hpp b/include/utl.hpp new file mode 100644 index 0000000..5d2751b --- /dev/null +++ b/include/utl.hpp @@ -0,0 +1,5 @@ +#include +#include +namespace utl { + std::string getRelativeTime(uint64_t timestamp); +} \ No newline at end of file diff --git a/source/fs.cpp b/source/fs.cpp new file mode 100644 index 0000000..c684d40 --- /dev/null +++ b/source/fs.cpp @@ -0,0 +1,98 @@ +#include "fs.hpp" + +namespace fs{ + u64 getFreeSpace() { + u64 space = 0; + fsFsGetFreeSpace(fsdevGetDefaultFileSystem(), "/", &space); + return space; + } + std::vector listDirectory(std::string path) { + std::vector itemList = {}; + DIR* dir; + struct dirent* ent; + dir = opendir(path.c_str()); + while((ent = readdir(dir))){ + itemList.push_back(ent->d_name); + } + closedir(dir); + return itemList; + } + // https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L34 + bool writeFile(std::string filename, std::string data) { + deleteFile(filename); + + FILE * file = fopen(filename.c_str(), "wb"); + if (!file) { + return false; + } + + size_t result = fwrite(data.c_str(), sizeof(char), data.size(), file); + + fflush(file); + fclose(file); + + return (result == data.size()); + } + // https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L50 + bool deleteFile(std::string filename) { + if (fileExists(filename)) { + return remove(filename.c_str()) == 0; + } + + return false; + } + // https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L58 + bool fileExists(std::string filename) { + FILE * file = fopen(filename.c_str(), "r"); + + if (file) { + fflush(file); + fclose(file); + return true; + } + + return false; + } + // http://stackoverflow.com/a/11366985 + bool mkpath(std::string path) { + bool bSuccess = false; + int nRC = ::mkdir( path.c_str(), 0775 ); + if( nRC == -1 ) + { + switch(errno) + { + case ENOENT: + //parent didn't exist, try to create it + if( mkpath( path.substr(0, path.find_last_of('/')) ) ) + //Now, try to create again. + bSuccess = 0 == ::mkdir( path.c_str(), 0775 ); + else + bSuccess = false; + break; + case EEXIST: + //Done! + bSuccess = true; + break; + default: + bSuccess = false; + break; + } + } + else + bSuccess = true; + return bSuccess; + } + + std::string getSuffix(int fType){ + switch(fType) { + case model::FType::JPG: + return ".jpg"; + case model::FType::PNG: + return ".png"; + case model::FType::GIF: + return ".gif"; + default: + return ".unsupported"; + } + } +} \ No newline at end of file diff --git a/source/utl.cpp b/source/utl.cpp new file mode 100644 index 0000000..4d9d60f --- /dev/null +++ b/source/utl.cpp @@ -0,0 +1,40 @@ +#include "utl.hpp" + +using namespace std::chrono; +namespace utl { + std::string getRelativeTime(uint64_t timestamp) { + long currentTimeInSecs = duration_cast(system_clock::now().time_since_epoch()).count(); + long minute = 60L; + long hour = 3600L; + long day = 86400L; + long month = 2592000L; + long year = 31536000L; + + std::string relativeString; + int amount; + long relative = currentTimeInSecs-timestamp; + if(relative>year) { + amount = (int)(relative/year); + relativeString = "year"; + } else if(relative>month) { + amount = (int)(relative/month); + relativeString = "month"; + } else if(relative>day) { + amount = (int)(relative/day); + relativeString = "day"; + } else if(relative>hour) { + amount = (int)(relative/hour); + relativeString = "hour"; + } else if(relative>minute) { + amount = (int)(relative/minute); + relativeString = "minute"; + } else { + amount = (int)relative; + relativeString = "second"; + } + if(amount>1){ + relativeString+="s"; + } + return "Uploaded " + std::to_string(amount) + " " + relativeString + " ago"; + } +} \ No newline at end of file