join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
filesystem.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_FILESYSTEM_HPP
26#define JOIN_CORE_FILESYSTEM_HPP
27
28// C++.
29#include <string>
30
31// C.
32#include <sys/stat.h>
33
34namespace join
35{
41 inline std::string base (const std::string& filepath)
42 {
43 size_t pos = filepath.rfind ("/");
44
45 if (pos == std::string::npos)
46 return {};
47 else
48 return filepath.substr (0, pos + 1);
49 }
50
56 inline std::string filename (const std::string& filepath)
57 {
58 size_t pos = filepath.rfind ("/");
59
60 if (pos == std::string::npos)
61 return filepath;
62 else
63 return filepath.substr (pos + 1);
64 }
65
71 inline std::string extension (const std::string& filepath)
72 {
73 size_t pos = filepath.rfind (".");
74
75 if (pos == std::string::npos)
76 return {};
77 else
78 return filepath.substr (pos + 1);
79 }
80
86 inline std::string mime (const std::string& filepath)
87 {
88 std::string mime, suffix (extension (filepath));
89
90 if (suffix == "htm")
91 mime = "text/html";
92 else if (suffix == "html")
93 mime = "text/html";
94 else if (suffix == "css")
95 mime = "text/css";
96 else if (suffix == "less")
97 mime = "text/css";
98 else if (suffix == "js")
99 mime = "application/javascript";
100 else if (suffix == "xml")
101 mime = "text/xml";
102 else if (suffix == "json")
103 mime = "application/json";
104 else if (suffix == "txt")
105 mime = "text/plain";
106 else if (suffix == "properties")
107 mime = "text/x-java-properties";
108 else if (suffix == "jpg")
109 mime = "image/jpeg";
110 else if (suffix == "jpeg")
111 mime = "image/jpeg";
112 else if (suffix == "png")
113 mime = "image/png";
114 else if (suffix == "bmp")
115 mime = "image/bmp";
116 else if (suffix == "gif")
117 mime = "image/gif";
118 else if (suffix == "jpe")
119 mime = "image/jpg";
120 else if (suffix == "xbm")
121 mime = "image/xbm";
122 else if (suffix == "tiff")
123 mime = "image/tiff";
124 else if (suffix == "tif")
125 mime = "image/tiff";
126 else if (suffix == "ico")
127 mime = "image/x-icon";
128 else if (suffix == "svg")
129 mime = "image/svg+xml";
130 else if (suffix == "pdf")
131 mime = "application/pdf";
132 else if (suffix == "mp3")
133 mime = "audio/mpeg";
134 else if (suffix == "mp4")
135 mime = "audio/mp4";
136 else if (suffix == "zip")
137 mime = "application/zip";
138 else if (suffix == "bz2")
139 mime = "application/x-bzip";
140 else if (suffix == "tbz2")
141 mime = "application/x-bzip";
142 else if (suffix == "tb2")
143 mime = "application/x-bzip";
144 else if (suffix == "gz")
145 mime = "application/x-gzip";
146 else if (suffix == "gzip")
147 mime = "application/x-gzip";
148 else if (suffix == "tar")
149 mime = "application/x-tar";
150 else if (suffix == "rar")
151 mime = "application/x-rar-compressed";
152 else if (suffix == "tpl")
153 mime = "application/vnd.groove-tool-template";
154 else if (suffix == "woff")
155 mime = "application/font-woff";
156 else if (suffix == "woff2")
157 mime = "application/font-woff2";
158 else
159 mime = "application/octet-stream";
160
161 return mime;
162 }
163
169 inline bool exists (const std::string& filepath)
170 {
171 struct stat status;
172 return stat (filepath.c_str (), &status) == 0;
173 }
174}
175
176#endif
Definition acceptor.hpp:32
bool exists(const std::string &filepath)
check if the file specified exists.
Definition filesystem.hpp:169
std::string base(const std::string &filepath)
get base path of the specified file.
Definition filesystem.hpp:41
std::string extension(const std::string &filepath)
get extension of the file specified.
Definition filesystem.hpp:71
std::string filename(const std::string &filepath)
get file name of the specified file.
Definition filesystem.hpp:56
std::string mime(const std::string &filepath)
get mime type of the specified file.
Definition filesystem.hpp:86