diff options
Diffstat (limited to 'node/Utils.cpp')
-rw-r--r-- | node/Utils.cpp | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/node/Utils.cpp b/node/Utils.cpp index b9db07b9..7a4d51ba 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -28,14 +28,14 @@ #include <stdio.h> #include <string.h> #include <stdlib.h> -#include "Utils.hpp" -#include "Mutex.hpp" +#include <stdarg.h> #if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux) #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> +#include <dirent.h> #endif #ifdef _WIN32 @@ -45,6 +45,9 @@ #include <sys/stat.h> #include <openssl/rand.h> +#include "Utils.hpp" +#include "Mutex.hpp" + namespace ZeroTier { const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; @@ -213,6 +216,29 @@ const char Utils::base64DecMap[128] = { static const char *DAY_NAMES[7] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" }; static const char *MONTH_NAMES[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" }; +std::map<std::string,bool> Utils::listDirectory(const char *path) +{ + struct dirent de; + struct dirent *dptr; + std::map<std::string,bool> r; + + DIR *d = opendir(path); + if (!d) + return r; + + dptr = (struct dirent *)0; + for(;;) { + if (readdir_r(d,&de,&dptr)) + break; + if (dptr) { + if ((!strcmp(dptr->d_name,"."))&&(!strcmp(dptr->d_name,".."))) + r[std::string(dptr->d_name)] = (dptr->d_type == DT_DIR); + } else break; + } + + return r; +} + std::string Utils::base64Encode(const void *data,unsigned int len) { if (!len) @@ -530,4 +556,20 @@ std::string Utils::trim(const std::string &s) return s.substr(start,end - start); } +void Utils::stdsprintf(std::string &s,const char *fmt,...) + throw(std::bad_alloc,std::length_error) +{ + char buf[65536]; + va_list ap; + + va_start(ap,fmt); + int n = vsnprintf(buf,sizeof(buf),fmt,ap); + va_end(ap); + + if ((n >= (int)sizeof(buf))||(n < 0)) + throw std::length_error("printf result too large"); + + s.append(buf); +} + } // namespace ZeroTier |