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 (2235B)


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