changed namepsaces, added doy
This commit is contained in:
parent
fe6e8e27c6
commit
4967f73ca1
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace mrbesen::doy {
|
||||
|
||||
unsigned int getDOY(bool timezone = false, time_t time = 0); //get the doy of today or specific time, timezone = false -> dont compensate for timezone, true -> compensate for timezone, returns 0 on error
|
||||
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
namespace mrbesen::Files {
|
||||
namespace mrbesen::files {
|
||||
|
||||
void parent(const std::string& child, std::string& out); //get the parent directory of a file or directory path
|
||||
void file(const std::string& path, std::string& out); //get the filename without the path
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include "doy.h"
|
||||
#include "files.h"
|
||||
#include "util.h"
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
namespace mrbesen::Util {
|
||||
namespace mrbesen::util {
|
||||
|
||||
unsigned int count(const std::string& str, char c); //count occurances of c in str
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
#include "doy.h"
|
||||
|
||||
unsigned int mrbesen::doy::getDOY(bool timezone, time_t ttime) {
|
||||
if(ttime == 0) {
|
||||
ttime = time(nullptr);
|
||||
}
|
||||
|
||||
struct tm* timestruct = timezone ? localtime(&ttime) : gmtime(&ttime);
|
||||
|
||||
if(timestruct) {
|
||||
return timestruct->tm_yday +1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -5,7 +5,7 @@
|
|||
#include <set>
|
||||
#include "util.h"
|
||||
|
||||
void mrbesen::Files::parent(const std::string& child, std::string& out) {
|
||||
void mrbesen::files::parent(const std::string& child, std::string& out) {
|
||||
//letzten path trenner finden
|
||||
size_t pos = child.rfind('/', child.length() -2); //das erste Zeichen überspringen (könnte ein / sein)
|
||||
if(pos == std::string::npos)
|
||||
|
@ -16,7 +16,7 @@ void mrbesen::Files::parent(const std::string& child, std::string& out) {
|
|||
out = "";
|
||||
}
|
||||
|
||||
void mrbesen::Files::file(const std::string& path, std::string& out) {
|
||||
void mrbesen::files::file(const std::string& path, std::string& out) {
|
||||
//letzten path trenner finden
|
||||
size_t pos = path.rfind('/', path.length() -2); //das erste Zeichen überspringen (könnte ein / sein)
|
||||
if(pos == std::string::npos)
|
||||
|
@ -27,7 +27,7 @@ void mrbesen::Files::file(const std::string& path, std::string& out) {
|
|||
out = path;
|
||||
}
|
||||
|
||||
void mrbesen::Files::extention(const std::string& path, std::string& ext) {
|
||||
void mrbesen::files::extention(const std::string& path, std::string& ext) {
|
||||
std::string filestr;
|
||||
file(path, filestr);
|
||||
size_t pos = filestr.rfind('.');
|
||||
|
@ -41,18 +41,18 @@ void mrbesen::Files::extention(const std::string& path, std::string& ext) {
|
|||
}
|
||||
|
||||
template<class Container>
|
||||
bool mrbesen::Files::scan(const std::string& path, std::insert_iterator<Container> it, bool prefixdir, fileNameFilter fnf) {
|
||||
bool mrbesen::files::scan(const std::string& path, std::insert_iterator<Container> it, bool prefixdir, fileNameFilter fnf) {
|
||||
return scan(path, [&](const std::string& p, FileType t){ it = p; }, prefixdir, fnf);
|
||||
}
|
||||
|
||||
//curently only these are supported, because iterators may break on others upon insertion
|
||||
template bool mrbesen::Files::scan<std::list<std::string>>(const std::string&, std::insert_iterator<std::list<std::string>>, bool, fileNameFilter);
|
||||
template bool mrbesen::Files::scan<std::set<std::string>>(const std::string&, std::insert_iterator<std::set<std::string>>, bool, fileNameFilter);
|
||||
template bool mrbesen::Files::scan<std::multiset<std::string>>(const std::string&, std::insert_iterator<std::multiset<std::string>>, bool, fileNameFilter);
|
||||
template bool mrbesen::files::scan<std::list<std::string>>(const std::string&, std::insert_iterator<std::list<std::string>>, bool, fileNameFilter);
|
||||
template bool mrbesen::files::scan<std::set<std::string>>(const std::string&, std::insert_iterator<std::set<std::string>>, bool, fileNameFilter);
|
||||
template bool mrbesen::files::scan<std::multiset<std::string>>(const std::string&, std::insert_iterator<std::multiset<std::string>>, bool, fileNameFilter);
|
||||
|
||||
bool mrbesen::Files::scan(const std::string& path, fileCallback clb, bool prefixdir, fileNameFilter fnf) {
|
||||
bool mrbesen::files::scan(const std::string& path, fileCallback clb, bool prefixdir, fileNameFilter fnf) {
|
||||
std::string path_ = path;
|
||||
mrbesen::Util::insertEnd(path_, "/");
|
||||
mrbesen::util::insertEnd(path_, "/");
|
||||
|
||||
::DIR* dir = opendir(path_.c_str());
|
||||
if(!dir) {
|
||||
|
|
46
src/util.cpp
46
src/util.cpp
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <algorithm>
|
||||
|
||||
unsigned int mrbesen::Util::count(const std::string& str, char c) {
|
||||
unsigned int mrbesen::util::count(const std::string& str, char c) {
|
||||
size_t pos = 0;
|
||||
long count = -1;
|
||||
do {
|
||||
|
@ -16,7 +16,7 @@ bool icompare_pred(unsigned char a, unsigned char b) {
|
|||
return std::tolower(a) == std::tolower(b);
|
||||
}
|
||||
|
||||
bool mrbesen::Util::equalsIgnoreCase(const std::string& a, const std::string& b, size_t max) {
|
||||
bool mrbesen::util::equalsIgnoreCase(const std::string& a, const std::string& b, size_t max) {
|
||||
size_t al = a.size(), bl = b.size();
|
||||
if((al == 0 && bl == 0) || max == 0) return true;
|
||||
|
||||
|
@ -34,7 +34,7 @@ bool mrbesen::Util::equalsIgnoreCase(const std::string& a, const std::string& b,
|
|||
}
|
||||
}
|
||||
|
||||
void mrbesen::Util::toLower(const std::string& in, std::string& out) {
|
||||
void mrbesen::util::toLower(const std::string& in, std::string& out) {
|
||||
|
||||
if(&in == &out) {
|
||||
//wenn beide strings identisch sind, inplace funktion verwenden;
|
||||
|
@ -49,13 +49,13 @@ void mrbesen::Util::toLower(const std::string& in, std::string& out) {
|
|||
}
|
||||
}
|
||||
|
||||
void mrbesen::Util::toLower(std::string& in) {
|
||||
void mrbesen::util::toLower(std::string& in) {
|
||||
for(size_t p = 0; p < in.size(); p++) {
|
||||
in[p] = std::tolower(in[p]);
|
||||
}
|
||||
}
|
||||
|
||||
bool mrbesen::Util::endsWith(const std::string& str, const std::string& ending) {
|
||||
bool mrbesen::util::endsWith(const std::string& str, const std::string& ending) {
|
||||
size_t es = ending.size();
|
||||
size_t ss = str.size();
|
||||
if(es > ss) return false;
|
||||
|
@ -64,14 +64,14 @@ bool mrbesen::Util::endsWith(const std::string& str, const std::string& ending)
|
|||
return str.rfind(ending) == (ss - es);
|
||||
}
|
||||
|
||||
bool mrbesen::Util::startsWith(const std::string& str, const std::string& start) {
|
||||
bool mrbesen::util::startsWith(const std::string& str, const std::string& start) {
|
||||
if(start.size() > str.size()) return false;
|
||||
if(start.size() == 0) return true;
|
||||
|
||||
return str.find(start) == 0;
|
||||
}
|
||||
|
||||
bool mrbesen::Util::removeEnd(std::string& str, const std::string& ending) {
|
||||
bool mrbesen::util::removeEnd(std::string& str, const std::string& ending) {
|
||||
if(ending.empty()) return false;
|
||||
|
||||
if(endsWith(str, ending)) {
|
||||
|
@ -81,7 +81,7 @@ bool mrbesen::Util::removeEnd(std::string& str, const std::string& ending) {
|
|||
return false;
|
||||
}
|
||||
|
||||
bool mrbesen::Util::removeStart(std::string& str, const std::string& start) {
|
||||
bool mrbesen::util::removeStart(std::string& str, const std::string& start) {
|
||||
if(start.empty()) return false;
|
||||
|
||||
if(startsWith(str, start)) {
|
||||
|
@ -91,7 +91,7 @@ bool mrbesen::Util::removeStart(std::string& str, const std::string& start) {
|
|||
return false;
|
||||
}
|
||||
|
||||
bool mrbesen::Util::insertEnd(std::string& str, const std::string& ending) {
|
||||
bool mrbesen::util::insertEnd(std::string& str, const std::string& ending) {
|
||||
if(ending.empty()) return false;
|
||||
|
||||
if(endsWith(str, ending)) return false;
|
||||
|
@ -100,7 +100,7 @@ bool mrbesen::Util::insertEnd(std::string& str, const std::string& ending) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool mrbesen::Util::insertStart(std::string& str, const std::string& start) {
|
||||
bool mrbesen::util::insertStart(std::string& str, const std::string& start) {
|
||||
if(start.empty()) return false;
|
||||
|
||||
if(startsWith(str, start)) return false;
|
||||
|
@ -109,7 +109,7 @@ bool mrbesen::Util::insertStart(std::string& str, const std::string& start) {
|
|||
return true;
|
||||
}
|
||||
|
||||
unsigned int mrbesen::Util::split(const std::string& str, const std::string& token, std::string* out, unsigned int count) {
|
||||
unsigned int mrbesen::util::split(const std::string& str, const std::string& token, std::string* out, unsigned int count) {
|
||||
StringSpliterator spl(str, token);
|
||||
for(unsigned int i = 0; i < count; ++i) {
|
||||
out[i] = *spl;
|
||||
|
@ -119,7 +119,7 @@ unsigned int mrbesen::Util::split(const std::string& str, const std::string& tok
|
|||
return count;
|
||||
}
|
||||
|
||||
void mrbesen::Util::trim(std::string& str, char c) {
|
||||
void mrbesen::util::trim(std::string& str, char c) {
|
||||
size_t first = str.find_first_not_of(c);
|
||||
if(first == std::string::npos) {
|
||||
str = "";
|
||||
|
@ -129,7 +129,7 @@ void mrbesen::Util::trim(std::string& str, char c) {
|
|||
str = str.substr(first, last-first);
|
||||
}
|
||||
|
||||
std::string mrbesen::Util::bytesToBase16(char* buffer, unsigned int len) {
|
||||
std::string mrbesen::util::bytesToBase16(char* buffer, unsigned int len) {
|
||||
static const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
std::string s(len << 1, ' ');
|
||||
for (unsigned int i = 0; i < len; ++i) {
|
||||
|
@ -139,7 +139,7 @@ std::string mrbesen::Util::bytesToBase16(char* buffer, unsigned int len) {
|
|||
return s;
|
||||
}
|
||||
|
||||
std::string mrbesen::Util::bytesToBase64(char* buffer, unsigned int len) {
|
||||
std::string mrbesen::util::bytesToBase64(char* buffer, unsigned int len) {
|
||||
//from: https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp/index#cpp-base64-cpp
|
||||
|
||||
if(len == 0) return "";
|
||||
|
@ -174,32 +174,32 @@ std::string mrbesen::Util::bytesToBase64(char* buffer, unsigned int len) {
|
|||
}
|
||||
|
||||
|
||||
mrbesen::Util::StringSpliterator::StringSpliterator(const std::string& d, const std::string& token) : data(d), token(token) {
|
||||
mrbesen::util::StringSpliterator::StringSpliterator(const std::string& d, const std::string& token) : data(d), token(token) {
|
||||
//trim
|
||||
while(mrbesen::Util::removeStart(data, token));
|
||||
while(mrbesen::Util::removeEnd(data, token));
|
||||
while(mrbesen::util::removeStart(data, token));
|
||||
while(mrbesen::util::removeEnd(data, token));
|
||||
|
||||
findNext();
|
||||
}
|
||||
|
||||
mrbesen::Util::StringSpliterator::~StringSpliterator() {}
|
||||
mrbesen::util::StringSpliterator::~StringSpliterator() {}
|
||||
|
||||
std::string mrbesen::Util::StringSpliterator::operator*() const {
|
||||
std::string mrbesen::util::StringSpliterator::operator*() const {
|
||||
return data.substr(lasttok, (nexttok - lasttok)-token.size());
|
||||
}
|
||||
|
||||
mrbesen::Util::StringSpliterator& mrbesen::Util::StringSpliterator::operator++() {
|
||||
mrbesen::util::StringSpliterator& mrbesen::util::StringSpliterator::operator++() {
|
||||
findNext();
|
||||
return *this;
|
||||
}
|
||||
|
||||
mrbesen::Util::StringSpliterator mrbesen::Util::StringSpliterator::operator++(int) {
|
||||
mrbesen::util::StringSpliterator mrbesen::util::StringSpliterator::operator++(int) {
|
||||
StringSpliterator ret = *this;
|
||||
findNext();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void mrbesen::Util::StringSpliterator::findNext() {
|
||||
void mrbesen::util::StringSpliterator::findNext() {
|
||||
if(!operator bool()) return;
|
||||
|
||||
lasttok = nexttok;
|
||||
|
@ -214,6 +214,6 @@ void mrbesen::Util::StringSpliterator::findNext() {
|
|||
nexttok += token.size();
|
||||
}
|
||||
|
||||
mrbesen::Util::StringSpliterator::operator bool() const {
|
||||
mrbesen::util::StringSpliterator::operator bool() const {
|
||||
return lasttok < data.length() && nexttok >= lasttok;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
#include "test.h"
|
||||
#include "doy.h"
|
||||
|
||||
using namespace mrbesen::doy;
|
||||
|
||||
int testGetDoy() {
|
||||
|
||||
//wird nur ohne timezone getestet!
|
||||
|
||||
ASSERT(getDOY(false, 1) == 1, ""); //01.01.1970
|
||||
ASSERT(getDOY(false, 31449600) == 365, ""); //31.12.1970 00:00:00
|
||||
ASSERT(getDOY(false, 31535999) == 365, ""); //31.12.1970 23:59:59
|
||||
|
||||
ASSERT(getDOY(false, 31536000) == 1, ""); //01.01.1971 00:00:00
|
||||
|
||||
ASSERT(getDOY(false, 131284800) == 59, ""); //28.02.1974 12:00:00
|
||||
|
||||
return TESTGOOD;
|
||||
}
|
|
@ -12,20 +12,20 @@ int testFiles_parent() {
|
|||
std::string out, out2;
|
||||
std::string testpath1 = "/asdf1/asdf2/test.png";
|
||||
|
||||
Files::parent(testpath1, out);
|
||||
files::parent(testpath1, out);
|
||||
ASSERT(out == "/asdf1/asdf2/", out);
|
||||
|
||||
Files::parent(out, out2);
|
||||
files::parent(out, out2);
|
||||
ASSERT(out2 == "/asdf1/", out2);
|
||||
|
||||
Files::parent(out2, out);
|
||||
files::parent(out2, out);
|
||||
ASSERT(out == "/", out);
|
||||
|
||||
testpath1 = "assets/tex/img1.png";
|
||||
Files::parent(testpath1, out);
|
||||
files::parent(testpath1, out);
|
||||
ASSERT(out == "assets/tex/", out);
|
||||
|
||||
Files::parent("asd", out);
|
||||
files::parent("asd", out);
|
||||
ASSERT(out == "", out);
|
||||
|
||||
return TESTGOOD;
|
||||
|
@ -34,22 +34,22 @@ int testFiles_parent() {
|
|||
int testFiles_file() {
|
||||
std::string a;
|
||||
|
||||
Files::file("/abc/def/123.txt", a);
|
||||
files::file("/abc/def/123.txt", a);
|
||||
ASSERT(a == "123.txt", "normalfile" << a);
|
||||
|
||||
Files::file("/abc/def/.gitignore", a);
|
||||
files::file("/abc/def/.gitignore", a);
|
||||
ASSERT(a == ".gitignore", "dotfiles" << a);
|
||||
|
||||
Files::file("/abc/def/executeable", a);
|
||||
files::file("/abc/def/executeable", a);
|
||||
ASSERT(a == "executeable", "no extention" << a);
|
||||
|
||||
Files::file("123.txt", a);
|
||||
files::file("123.txt", a);
|
||||
ASSERT(a == "123.txt", "normalfile - nopath" << a);
|
||||
|
||||
Files::file(".gitignore", a);
|
||||
files::file(".gitignore", a);
|
||||
ASSERT(a == ".gitignore", "dotfiles - nopath " << a);
|
||||
|
||||
Files::file("executeable", a);
|
||||
files::file("executeable", a);
|
||||
ASSERT(a == "executeable", "no extention - nopath " << a);
|
||||
|
||||
return TESTGOOD;
|
||||
|
@ -58,46 +58,46 @@ int testFiles_file() {
|
|||
int testFiles_extention() {
|
||||
std::string a;
|
||||
|
||||
Files::extention("abc/asd/1.txt", a);
|
||||
files::extention("abc/asd/1.txt", a);
|
||||
ASSERT(a == "txt", a);
|
||||
|
||||
Files::extention("1.txt", a);
|
||||
files::extention("1.txt", a);
|
||||
ASSERT(a == "txt", a);
|
||||
|
||||
Files::extention("/1.txt", a);
|
||||
files::extention("/1.txt", a);
|
||||
ASSERT(a == "txt", "");
|
||||
|
||||
Files::extention("/a.b.c/ad./1.txt", a);
|
||||
files::extention("/a.b.c/ad./1.txt", a);
|
||||
ASSERT(a == "txt", "");
|
||||
|
||||
Files::extention("a.b.c/ad./1.txt", a);
|
||||
files::extention("a.b.c/ad./1.txt", a);
|
||||
ASSERT(a == "txt", "");
|
||||
|
||||
Files::extention("./1.txt", a);
|
||||
files::extention("./1.txt", a);
|
||||
ASSERT(a == "txt", "");
|
||||
|
||||
Files::extention("/a/ad/1", a);
|
||||
files::extention("/a/ad/1", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
Files::extention("/a/ad/", a);
|
||||
files::extention("/a/ad/", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
Files::extention("/", a);
|
||||
files::extention("/", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
Files::extention("/a", a);
|
||||
files::extention("/a", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
Files::extention("/a.", a);
|
||||
files::extention("/a.", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
Files::extention("/a.b", a);
|
||||
files::extention("/a.b", a);
|
||||
ASSERT(a == "b", "");
|
||||
|
||||
Files::extention("/sad.asd/ab", a);
|
||||
files::extention("/sad.asd/ab", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
Files::extention("sad.asd/ab", a);
|
||||
files::extention("sad.asd/ab", a);
|
||||
ASSERT(a == "", "");
|
||||
|
||||
return TESTGOOD;
|
||||
|
@ -108,13 +108,13 @@ int testFiles_scan() {
|
|||
std::string path = "./tests";
|
||||
std::list<std::string> out, out2;
|
||||
|
||||
ASSERT(Files::scan(path, std::inserter(out, out.begin()), false), "");
|
||||
ASSERT(Files::scan(path, std::inserter(out2, out2.begin()), true), "");
|
||||
ASSERT(files::scan(path, std::inserter(out, out.begin()), false), "");
|
||||
ASSERT(files::scan(path, std::inserter(out2, out2.begin()), true), "");
|
||||
|
||||
ASSERT(out.size() == out2.size(), out.size() << " " << out2.size());
|
||||
|
||||
std::set<std::string> out3;
|
||||
ASSERT(Files::scan(path, std::inserter(out3, out3.begin()), false, [](const std::string& str, Files::FileType t){ return mrbesen::Util::startsWith(str, "."); }), "");
|
||||
ASSERT(files::scan(path, std::inserter(out3, out3.begin()), false, [](const std::string& str, files::FileType t){ return mrbesen::util::startsWith(str, "."); }), "");
|
||||
ASSERT(out3.size() == 2, out3.size());
|
||||
ASSERT(out3.find(".") != out3.end(), "");
|
||||
ASSERT(out3.find("..") != out3.end(), "");
|
||||
|
|
|
@ -19,9 +19,12 @@ int testUtil_base();
|
|||
int testStringSpliterator();
|
||||
int testUtilSplit();
|
||||
|
||||
int testGetDoy();
|
||||
|
||||
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan,
|
||||
testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_base,
|
||||
testStringSpliterator, testUtilSplit,
|
||||
testGetDoy,
|
||||
NULL};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "util.h"
|
||||
|
||||
using namespace mrbesen::Util;
|
||||
using namespace mrbesen::util;
|
||||
|
||||
|
||||
int testStringSpliterator() {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "util.h"
|
||||
#include "test.h"
|
||||
|
||||
using namespace mrbesen::Util;
|
||||
using namespace mrbesen::util;
|
||||
|
||||
int testUtil_Count() {
|
||||
std::string test1 = "ababababa";
|
||||
|
|
Loading…
Reference in New Issue