4 #include "util_const.h"
5 #include "util_splitter.h"
11 bool isPlainFile (const ustring& name) {
14 if (name.size () > 0 && stat (name.c_str (), &sb) == 0 && S_ISREG (sb.st_mode)) {
21 bool isExecutableFile (const ustring& name) {
24 if (name.size () > 0 && stat (name.c_str (), &sb) == 0 && (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
31 bool isDirectory (const ustring& name) {
34 if (name.size () > 0 && stat (name.c_str (), &sb) == 0 && S_ISDIR (sb.st_mode)) {
41 bool fileSize (const ustring& name, off_t& size) {
44 if (name.size () > 0 && stat (name.c_str (), &sb) == 0 && S_ISREG (sb.st_mode)) {
52 bool readFile (const ustring& filename, ustring& ans, size_t max) {
53 if (filename.size () > 0) {
57 if (f.openRead (filename.c_str ())) {
59 if (max > 0 && s > max)
60 throw (filename + uErrorFileSize);
63 f.read (&ans.at (0), s);
74 void writeFile (const ustring& filename, ustring& data) {
75 if (filename.size () > 0) {
77 if (f.openWrite (filename.c_str ())) {
79 f.write (&data.at (0), data.size ());
85 top must end with slash.
87 void makeSubdir (ustring& top, const ustring& sub) {
88 SplitterCh sp (sub, '/');
91 top.append (sp.b, sp.u);
92 mkdir (top.c_str (), 0777);
94 std::cerr << "mkdir:" << top << "\n";
99 void shapePath (ustring& path) {
100 static uregex re1 ("//+");
101 static uregex re2 ("[^/]+/\\.\\.(/|$)");
102 static uregex re3 ("(/|^)\\.(/|$)");
103 static uregex re4 ("^/\\.\\./");
107 while (usearch (path, m, re1)) {
110 path = ustring (b, m[0].first + 1).append (m[0].second, e);
112 while (usearch (path, m, re2)) {
113 if (m[1].first == m[1].second) { // '$'
114 path.resize (m[0].first - path.begin ());
118 path = ustring (b, m[0].first).append (m[0].second, e);
121 while (usearch (path, m, re3)) {
122 switch (m[0].second - m[0].first) {
128 path = ustring (b, m[0].first).append (m[0].second, e);
133 path = ustring (b, m[0].first).append (m[2].first, e);
139 while (usearch (path, m, re4)) {
141 path = ustring (m[0].second - 1, e);
146 bool isAbsolutePath (const ustring& path) {
147 return (path.length () > 0 && path[0] == '/');