Skip to content

Commit bdd7c9e

Browse files
committed
file_to_string, PATH_SEP, debug log macros, fs->kit namespace
1 parent d98aaf0 commit bdd7c9e

File tree

6 files changed

+202
-15
lines changed

6 files changed

+202
-15
lines changed

kit/fs/fs.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
#include <boost/filesystem/path.hpp>
66
#include "../log/log.h"
77

8-
namespace fs {
9-
108
#ifdef _WIN32
11-
#define SEP "\\"
9+
static const std::string PATH_SEP = "\\";
1210
#else
13-
#define SEP "/"
11+
static const std::string PATH_SEP = "/";
1412
#endif
1513

14+
namespace kit {
15+
1616
inline std::string homedir() {
1717
const char* homedir = getenv("HOME");
1818
if(!homedir)
@@ -42,13 +42,13 @@ namespace fs {
4242
#ifdef _WIN32
4343
r = std::string(cdir);
4444
#else
45-
r = std::string(cdir) + SEP + ".config";
45+
r = std::string(cdir) + PATH_SEP + ".config";
4646
#endif
4747
}
4848
else
4949
r = cdir;
5050
if(not apppath.empty())
51-
return r + SEP + apppath;
51+
return r + PATH_SEP + apppath;
5252
return r;
5353
}
5454
}

kit/kit.h

+26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <atomic>
1313
#include <future>
1414
#include <chrono>
15+
#include <fstream>
1516
#include <boost/algorithm/string.hpp>
1617
#include <kit/local_shared_ptr.hpp>
1718
#include <boost/optional.hpp>
@@ -1176,6 +1177,31 @@ namespace kit
11761177
return a + (b-a)*t;
11771178
}
11781179

1180+
inline std::vector<char> file_to_buffer(const std::string& fn)
1181+
{
1182+
std::ifstream file(fn);
1183+
if(!file)
1184+
return std::vector<char>();
1185+
std::vector<char> data(
1186+
(std::istreambuf_iterator<char>(file)),
1187+
std::istreambuf_iterator<char>()
1188+
);
1189+
data.push_back('\0');
1190+
return data;
1191+
}
1192+
1193+
inline std::string file_to_string(const std::string & fn)
1194+
{
1195+
std::ifstream file(fn);
1196+
if (!file)
1197+
return std::string();
1198+
std::string data(
1199+
(std::istreambuf_iterator<char>(file)),
1200+
std::istreambuf_iterator<char>()
1201+
);
1202+
return data;
1203+
}
1204+
11791205
#define TRY(expr) try{ expr; } catch(...) {}
11801206
#define TRY_OR(expr, alt) [&]{try{ return (expr); } catch(...) {return (alt);}}()
11811207
#define IF_OR(expr, alt) [&]{try{ auto r = (expr);if(r)return r; } catch(...) {}return (alt);}()

kit/log/log.h

+19
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,25 @@ class Log:
408408
throw Error(ErrorCode::CODE, _msg);\
409409
}
410410

411+
#ifdef _DEBUG
412+
#define DEBUG_LOG(X) LOG(X)
413+
#define DEBUG_LOGf(X,Y) LOGf(X,Y)
414+
#define DEBUG_WARNING(X) WARNING(X)
415+
#define DEBUG_WARNINGf(X,Y) WARNINGf(X,Y)
416+
// this will only write the error in debug, otherwise assert(false);
417+
#define DEBUG_ERROR(CODE,X) ERROR(CODE,X)
418+
#define DEBUG_ERRORf(CODE,X,Y) ERRORf(CODE,X,Y)
419+
#else
420+
#define DEBUG_LOG(X)
421+
#define DEBUG_LOGf(X,Y)
422+
#define DEBUG_WARNING(X)
423+
#define DEBUG_WARNINGf(X,Y)
424+
// this will only write the error in debug, otherwise assert(false);
425+
#define DEBUG_ERROR(CODE,X) assert(false);
426+
#define DEBUG_ERRORf(CODE,X,Y) assert(false);
427+
#endif
428+
429+
411430
//#ifdef DEBUG
412431
// #define _()\
413432
// Log::Indent _li(\

kit/meta/meta.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ class Meta_:
697697
*
698698
* TODO: timeout callback if we hit a lock
699699
* TODO: incremental behavior (merge only what you can without locking)
700-
* so you can loop this until `t` is empty)
700+
* so you can loop this until `t` is empty) -- warning: destructive
701701
*
702702
* For throw-on-conflict behavior, throw inside of `which`
703703
*/
@@ -733,7 +733,7 @@ class Meta_:
733733
const std::string& fn,
734734
unsigned flags = (unsigned)MergeFlags::DEFAULTS // MergeFlags
735735
);
736-
736+
737737
//void merge(
738738
// const Meta_& t,
739739
// unsigned flags = (unsigned)MergeFlags::DEFAULTS,
@@ -1511,8 +1511,8 @@ class Meta_:
15111511
// Use these instead of defines
15121512
using Meta = Meta_<kit::dummy_mutex, META_STORAGE, META_THIS>;
15131513
using MetaS = Meta_<kit::dummy_mutex, std::shared_ptr, std::enable_shared_from_this>;
1514-
using MetaL = Meta_<kit::dummy_mutex, kit::local_shared_ptr>;
1515-
using MetaMT = Meta_<std::recursive_mutex, std::shared_ptr>;
1514+
using MetaL = Meta_<kit::dummy_mutex, kit::local_shared_ptr, kit::enable_shared_from_this>;
1515+
using MetaMT = Meta_<std::recursive_mutex, std::shared_ptr, std::enable_shared_from_this>;
15161516

15171517
#include "meta.inl"
15181518

kit/meta/meta.inl

+137-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ Meta_<Mutex,Ptr,This> :: Meta_(const std::string& fn, unsigned flags):
1616
template<class Mutex, template <class> class Ptr, template <typename> class This>
1717
Meta_<Mutex,Ptr,This> :: Meta_(const Ptr<Meta_<Mutex,Ptr,This>>& rhs)
1818
{
19-
clear();
20-
merge(rhs);
19+
//clear();
20+
//auto tmp = make_shared<Meta>();
21+
//tmp->merge(rhs);
22+
//merge(rhs);
2123
}
2224

2325
template<class Mutex, template <class> class Ptr, template <typename> class This>
@@ -1079,3 +1081,136 @@ template<class Mutex, template <class> class Ptr, template <typename> class This
10791081
return MetaFormat::UNKNOWN;
10801082
}
10811083

1084+
//template<class Mutex, template <class> class Ptr, template <typename> class This>
1085+
//static std::shared_ptr<Meta> Meta_<Mutex,Ptr,This> :: copy()
1086+
//{
1087+
// assert(t);
1088+
1089+
// auto l = lock();
1090+
1091+
// if(!timeout)
1092+
// std::lock(al, bl);
1093+
// else
1094+
// {
1095+
// if(std::try_lock(al, bl) != -1) // failed to this->lock
1096+
// {
1097+
// // incremental merges only try subtrees once and only if they have
1098+
// // timeouts
1099+
// auto spthis = this->shared_from_this();
1100+
// if(flags & (unsigned)MergeFlags::INCREMENTAL)
1101+
// {
1102+
// // bail if no timeout function or if timeout returns false
1103+
// if(!timeout || !timeout(spthis))
1104+
// return;
1105+
// // timeout told us to retry once more
1106+
// if(std::try_lock(al, bl) != -1)
1107+
// return;
1108+
// }
1109+
// else
1110+
// {
1111+
// do {
1112+
// if(!timeout(spthis))
1113+
// return; // give up
1114+
// }while(std::try_lock(al, bl) == -1);
1115+
// }
1116+
// }
1117+
// }
1118+
1119+
// for(auto&& e: t->elements_ref())
1120+
// {
1121+
// if(visit)
1122+
// visit(t, e);
1123+
1124+
// if(e.key.empty())
1125+
// m_Elements.push_back(e);
1126+
// else if(m_Keys.find(e.key)==m_Keys.end())
1127+
// {
1128+
// m_Elements.push_back(e);
1129+
// m_Keys[e.key] = m_Elements.size()-1;
1130+
// }
1131+
// else
1132+
// {
1133+
// // already there, merge?
1134+
// unsigned this_id = m_Keys[e.key];
1135+
// auto&& this_e = m_Elements[this_id];
1136+
// if(this_e.type.id == MetaType::ID::META &&
1137+
// e.type.id == MetaType::ID::META// &&
1138+
// //this_e.type.storage == MetaType::Ptr::SHARED &&
1139+
// // e.type.storage == MetaType::Ptr::SHARED
1140+
// ){
1141+
// if(flags & (unsigned)MergeFlags::RECURSIVE)
1142+
// {
1143+
// //try{
1144+
// at<Ptr<Meta_<Mutex,Ptr,This>>>(this_id)->merge(
1145+
// t->template at<TPtr<Meta_<TMutex,TPtr>>>(e.key),
1146+
// which,
1147+
// flags,
1148+
// timeout
1149+
// );
1150+
// //}catch(const kit::null_ptr_exception&){}
1151+
// }
1152+
// else
1153+
// {
1154+
// auto r = which(this_e, e);
1155+
// Meta_::Which* w = boost::get<Meta_::Which>(&r);
1156+
// if(!w)
1157+
// {
1158+
// MetaElement* a = boost::get<MetaElement>(&r);
1159+
// auto preserved_key = this_e.key;
1160+
// if(flags & (unsigned)MergeFlags::INCREMENTAL)
1161+
// this_e = std::move(*a);
1162+
// else
1163+
// this_e = *a;
1164+
// this_e.key = preserved_key;
1165+
// }
1166+
// else if(*w == Which::RECURSE)
1167+
// {
1168+
// auto m = at<Ptr<Meta_<Mutex,Ptr,This>>>(this_id);
1169+
// assert(m);
1170+
// m->merge(
1171+
// t->template at<Ptr<Meta_<Mutex,Ptr,This>>>(e.key),
1172+
// which,
1173+
// flags,
1174+
// timeout
1175+
// );
1176+
// // delete entire null trees in destructive merges
1177+
// if(flags & (unsigned)MergeFlags::INCREMENTAL)
1178+
// {
1179+
// if(m->all_empty())
1180+
// {
1181+
// m->clear();
1182+
// e.nullify();
1183+
// }
1184+
// }
1185+
// }
1186+
// else if(*w == Which::OTHER)
1187+
// {
1188+
// replace(this_id, e);
1189+
// if(flags & (unsigned)MergeFlags::INCREMENTAL)
1190+
// e.nullify();
1191+
// }
1192+
// else if(*w == Which::NEITHER)
1193+
// {
1194+
// remove(this_id);
1195+
// if(flags & (unsigned)MergeFlags::INCREMENTAL)
1196+
// e.nullify();
1197+
// }
1198+
1199+
// // TODO: other Meta_::Which values (except Which::SELF,
1200+
// // that can be treated simply by continue; below)
1201+
// // Meta_::Which values have preference over flags below
1202+
// // so we continue; here
1203+
1204+
// continue;
1205+
// }
1206+
// }
1207+
1208+
// if(flags & (unsigned)MergeFlags::REPLACE) {
1209+
// replace(this_id, e);
1210+
// e.nullify();
1211+
// }
1212+
// }
1213+
// }
1214+
1215+
//}
1216+

tests/fs.test.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ using namespace std;
55

66
TEST_CASE("fs","[fs]") {
77
SECTION("usage") {
8-
cout << fs::homedir() << endl;
9-
cout << fs::configdir() << endl;
10-
cout << fs::configdir("test") << endl;
8+
// TODO: write OS-specific tests
9+
10+
// $HOME or $HOMEPATH
11+
cout << kit::homedir() << endl;
12+
13+
// $XDG_CONFIG_HOME or $HOME/.config on Linux
14+
cout << kit::configdir() << endl;
15+
16+
// $XDG_CONFIG_HOME/test or $HOME/.config/test on Linux
17+
cout << kit::configdir("test") << endl;
1118
}
1219
}
1320

0 commit comments

Comments
 (0)