startgit

Static page generator for git repositories
git clone git://git.dimitrijedobrota.com/startgit.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

utils.cpp (2254B)


0 #include <chrono> 1 #include <format> 2 3 #include "utils.hpp" 4 5 #include <sys/stat.h> 6 7 namespace startgit 8 { 9 10 auto sec_since_epoch(std::int64_t sec) 11 { 12 return std::chrono::time_point_cast<std::chrono::seconds>( 13 std::chrono::system_clock::from_time_t(time_t {0}) 14 + std::chrono::seconds(sec) 15 ); 16 } 17 18 std::string time_short(std::int64_t date) 19 { 20 return std::format("{:%Y-%m-%d %H:%M}", sec_since_epoch(date)); 21 } 22 23 std::string time_long(const git2wrap::time& time) 24 { 25 return std::format( 26 "{:%a, %e %b %Y %H:%M:%S} {}{:02}{:02}", 27 sec_since_epoch(time.time), 28 time.offset < 0 ? '-' : '+', 29 time.offset / 60, // NOLINT 30 time.offset % 60 // NOLINT 31 ); 32 } 33 // NOLINTBEGIN 34 // clang-format off 35 36 std::string xmlencode(const std::string& str) 37 { 38 std::string res; 39 40 res.reserve(str.size()); 41 for (const char c: str) { 42 switch(c) { 43 case '<': res += "&lt;"; continue; 44 case '>': res += "&gt;"; continue; 45 case '\'': res += "&#39;"; continue; 46 case '&': res += "&amp;"; continue; 47 case '"': res += "&quot;"; continue; 48 case '\n': res += "<br>"; continue; 49 } 50 res += c; 51 } 52 53 return res; 54 } 55 56 std::string filemode(git2wrap::filemode_t filemode) 57 { 58 std::string mode(10, '-'); 59 60 if (S_ISREG(filemode)) mode[0] = '-'; 61 else if (S_ISBLK(filemode)) mode[0] = 'b'; 62 else if (S_ISCHR(filemode)) mode[0] = 'c'; 63 else if (S_ISDIR(filemode)) mode[0] = 'd'; 64 else if (S_ISFIFO(filemode)) mode[0] = 'p'; 65 else if (S_ISLNK(filemode)) mode[0] = 'l'; 66 else if (S_ISSOCK(filemode)) mode[0] = 's'; 67 else mode[0] = '?'; 68 69 if (filemode & S_IRUSR) mode[1] = 'r'; 70 if (filemode & S_IWUSR) mode[2] = 'w'; 71 if (filemode & S_IXUSR) mode[3] = 'x'; 72 if (filemode & S_IRGRP) mode[4] = 'r'; 73 if (filemode & S_IWGRP) mode[5] = 'w'; 74 if (filemode & S_IXGRP) mode[6] = 'x'; 75 if (filemode & S_IROTH) mode[7] = 'r'; 76 if (filemode & S_IWOTH) mode[8] = 'w'; 77 if (filemode & S_IXOTH) mode[9] = 'x'; 78 79 if (filemode & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S'; 80 if (filemode & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S'; 81 if (filemode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T'; 82 83 return mode; 84 } 85 // clang-format on 86 // NOLINTEND 87 88 } // namespace startgit