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