Skip to content

Commit 03eeb47

Browse files
smowtonpeterschrammel
authored andcommitted
Style file-util
Also rephrase get-last-path-separator.
1 parent ba470e9 commit 03eeb47

File tree

2 files changed

+122
-139
lines changed

2 files changed

+122
-139
lines changed

src/util/file_util.cpp

Lines changed: 97 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ Date: January 2012
1111
#include <cerrno>
1212
#include <cassert>
1313

14-
#if defined(__linux__) || \
15-
defined(__FreeBSD_kernel__) || \
16-
defined(__GNU__) || \
17-
defined(__unix__) || \
18-
defined(__CYGWIN__) || \
19-
defined(__MACH__)
14+
#include <fstream>
15+
#include <cstdlib>
16+
#include <algorithm>
17+
#include <sstream>
18+
#include <vector>
19+
20+
#if defined(__linux__) || \
21+
defined(__FreeBSD_kernel__) || \
22+
defined(__GNU__) || \
23+
defined(__unix__) || \
24+
defined(__CYGWIN__) || \
25+
defined(__MACH__)
2026
#include <unistd.h>
2127
#include <dirent.h>
22-
#include <cstdlib>
2328
#include <cstdio>
2429
#include <sys/stat.h>
2530
#endif
@@ -89,7 +94,7 @@ Function: delete_directory
8994

9095
void delete_directory(const std::string &path)
9196
{
92-
#ifdef _WIN32
97+
#ifdef _WIN32
9398

9499
std::string pattern=path+"\\*";
95100

@@ -106,7 +111,7 @@ void delete_directory(const std::string &path)
106111
unlink(info.name);
107112
}
108113

109-
#else
114+
#else
110115

111116
DIR *dir=opendir(path.c_str());
112117

@@ -120,7 +125,7 @@ void delete_directory(const std::string &path)
120125
closedir(dir);
121126
}
122127

123-
#endif
128+
#endif
124129

125130
rmdir(path.c_str());
126131
}
@@ -142,137 +147,107 @@ std::string concat_dir_file(
142147
const std::string &directory,
143148
const std::string &file_name)
144149
{
145-
#ifdef _WIN32
146-
return (file_name.size()>1 &&
147-
file_name[0]!='/' &&
148-
file_name[1]!=':') ?
149-
file_name : directory+"\\"+file_name;
150-
#else
150+
#ifdef _WIN32
151+
return (file_name.size()>1 &&
152+
file_name[0]!='/' &&
153+
file_name[1]!=':') ?
154+
file_name : directory+"\\"+file_name;
155+
#else
151156
return (!file_name.empty() && file_name[0]=='/') ?
152-
file_name : directory+"/"+file_name;
153-
#endif
154-
}
155-
156-
157-
158-
159-
160-
#include <fstream>
161-
#include <cstdlib>
162-
#include <algorithm>
163-
#include <sstream>
164-
#include <vector>
165-
166-
#if defined(WIN32)
167-
# include <windows.h>
168-
#elif defined(__linux__) || defined(__APPLE__)
169-
# include <sys/stat.h>
157+
file_name : directory+"/"+file_name;
170158
#endif
159+
}
171160

172-
173-
static std::string::size_type fileutl_parse_last_dir_pos(
174-
std::string const& file_pathname
175-
)
161+
static std::string::size_type fileutl_parse_last_dir_pos(
162+
std::string const &file_pathname)
176163
{
177-
std::string::size_type const last_slash_pos =
178-
file_pathname.find_last_of('/');
179-
std::string::size_type const last_backslash_pos =
180-
file_pathname.find_last_of('\\');
181-
std::string::size_type const last_dir_pos =
182-
last_slash_pos == std::string::npos ?
183-
(last_backslash_pos == std::string::npos ?
184-
0ULL :
185-
last_backslash_pos + 1ULL) :
186-
(last_backslash_pos == std::string::npos ?
187-
last_slash_pos + 1ULL :
188-
(last_slash_pos < last_backslash_pos ?
189-
last_backslash_pos :
190-
last_slash_pos))
191-
;
192-
return last_dir_pos;
164+
auto found = file_pathname.find_last_of("/\\");
165+
if(found == std::string::npos)
166+
return 0;
167+
else
168+
return found + 1;
193169
}
194170

195-
196-
bool fileutl_file_exists(std::string const& pathname)
171+
bool fileutl_file_exists(std::string const &pathname)
197172
{
198-
std::ifstream f{pathname,std::ios::binary};
173+
std::ifstream f{pathname, std::ios::binary};
199174
return f.good();
200175
}
201176

202-
bool fileutl_is_directory(std::string const& pathname)
177+
bool fileutl_is_directory(std::string const &pathname)
203178
{
204-
if (!fileutl_file_exists(pathname))
205-
return false;
179+
if(!fileutl_file_exists(pathname))
180+
return false;
206181
# if defined(WIN32)
207-
return PathIsDirectory(pathname.c_str());
182+
return PathIsDirectory(pathname.c_str());
208183
# elif defined(__linux__) || defined(__APPLE__)
209184
struct stat buf;
210185
stat(pathname.c_str(), &buf);
211186
return S_ISDIR(buf.st_mode);
212-
//return S_ISREG(buf.st_mode); // is file ?
213187
# else
214188
# error "Unsuported platform."
215189
# endif
216190
}
217191

218-
uint64_t fileutl_file_size(std::string const& file_pathname)
192+
uint64_t fileutl_file_size(std::string const &file_pathname)
219193
{
220-
std::ifstream f{file_pathname,std::ios::binary};
221-
std::streampos const begin = f.tellg();
222-
f.seekg(0ULL,std::ios::end);
223-
std::streampos const end = f.tellg();
194+
std::ifstream f{file_pathname, std::ios::binary};
195+
std::streampos const begin = f.tellg();
196+
f.seekg(0ULL, std::ios::end);
197+
std::streampos const end = f.tellg();
224198
return end - begin;
225199
}
226200

227-
std::string fileutl_parse_name_in_pathname(std::string const& file_pathname)
201+
std::string fileutl_parse_name_in_pathname(std::string const &file_pathname)
228202
{
229203
return file_pathname.substr(fileutl_parse_last_dir_pos(file_pathname));
230204
}
231205

232-
std::string fileutl_parse_path_in_pathname(std::string const& file_pathname)
206+
std::string fileutl_parse_path_in_pathname(std::string const &file_pathname)
233207
{
234-
return file_pathname.substr(0U,fileutl_parse_last_dir_pos(file_pathname));
208+
return file_pathname.substr(0U, fileutl_parse_last_dir_pos(file_pathname));
235209
}
236210

237-
std::string fileutl_remove_extension(std::string const& filename)
211+
std::string fileutl_remove_extension(std::string const &filename)
238212
{
239-
return filename.substr(0U,filename.find_last_of('.'));
213+
return filename.substr(0U, filename.find_last_of('.'));
240214
}
241215

242-
void fileutl_create_directory(std::string const& pathname)
216+
void fileutl_create_directory(std::string const &pathname)
243217
{
244218
# if defined(WIN32)
245-
std::system((std::string("mkdir \"") + pathname + "\"").c_str());
219+
std::system((std::string("mkdir \"") + pathname + "\"").c_str());
246220
# elif defined(__linux__) || defined(__APPLE__)
247221
#ifdef USE_BOOST
248222
boost::filesystem::create_directories(pathname);
249223
#else
250224
auto ignore = std::system(
251-
(std::string("mkdir -p \"") + pathname + "\"").c_str()
252-
);
225+
(std::string("mkdir -p \"") + pathname + "\"").c_str());
253226
(void)ignore;
254227
#endif
255228
# else
256229
# error "Unsuported platform."
257230
# endif
258231
}
259232

260-
std::string fileutl_concatenate_file_paths(std::string const& left_path,
261-
std::string const& right_path)
233+
std::string fileutl_concatenate_file_paths(
234+
std::string const &left_path,
235+
std::string const &right_path)
262236
{
263-
if (left_path.empty())
237+
if(left_path.empty())
264238
return right_path;
265-
if (right_path.empty())
239+
if(right_path.empty())
266240
return left_path;
267241
return left_path + "/" + right_path;
268242
}
269243

270-
std::string fileutl_absolute_path(std::string const& path)
244+
std::string fileutl_absolute_path(std::string const &path)
271245
{
272246
#if defined(WIN32)
273247
DWORD buffer_length = GetFullPathName(path.c_str(), 0, nullptr, nullptr);
274248
std::vector<char> buffer(buffer_length);
275-
DWORD actual_length = GetFullPathName(path.c_str(), buffer_length, &(buffer[0]), nullptr);
249+
DWORD actual_length =
250+
GetFullPathName(path.c_str(), buffer_length, &(buffer[0]), nullptr);
276251
if(actual_length != buffer_length - 1)
277252
throw "fileutl_absolute_path: GetFullPathName failed";
278253
return std::string(&(buffer[0]));
@@ -284,85 +259,88 @@ std::string fileutl_absolute_path(std::string const& path)
284259
#endif
285260
}
286261

287-
std::string fileutl_normalise_path(std::string const& path)
262+
std::string fileutl_normalise_path(std::string const &path)
288263
{
289-
std::string result = path;
290-
std::replace(result.begin(),result.end(),'\\','/');
291-
std::string::size_type pos = 0ULL;
292-
while((pos = result.find("//",0)) != std::string::npos)
293-
result.replace(pos,2,"/");
294-
while((pos = result.find("/./",0)) != std::string::npos)
295-
result.replace(pos,3,"/");
296-
while((pos = result.find("/../",0)) != std::string::npos)
264+
std::string result = path;
265+
std::replace(result.begin(), result.end(), '\\', '/');
266+
std::string::size_type pos = 0ULL;
267+
while((pos = result.find("//", 0)) != std::string::npos)
268+
result.replace(pos, 2, "/");
269+
while((pos = result.find("/./", 0)) != std::string::npos)
270+
result.replace(pos, 3, "/");
271+
while((pos = result.find("/../", 0)) != std::string::npos)
297272
{
298273
assert(pos != 0ULL);
299-
const std::string::size_type prev_pos = result.rfind("/",pos-1ULL);
274+
const std::string::size_type prev_pos = result.rfind("/", pos-1ULL);
300275
if(prev_pos == std::string::npos)
301276
break;
302-
result.replace(prev_pos,pos - prev_pos + 4ULL,"/");
277+
result.replace(prev_pos, pos - prev_pos + 4ULL, "/");
303278
}
304279
return result;
305280
}
306281

307-
void fileutl_split_pathname(std::string const& pathname,
308-
std::vector<std::string>& output)
282+
void fileutl_split_pathname(
283+
std::string const &pathname,
284+
std::vector<std::string> &output)
309285
{
310-
std::istringstream istr(fileutl_normalise_path(pathname));
311-
std::string token;
312-
while (std::getline(istr,token,'/'))
286+
std::istringstream istr(fileutl_normalise_path(pathname));
287+
std::string token;
288+
while(std::getline(istr, token, '/'))
313289
output.push_back(token);
314290
}
315291

316-
std::string fileutl_join_path_parts(std::vector<std::string> const& parts)
292+
std::string fileutl_join_path_parts(std::vector<std::string> const &parts)
317293
{
318-
if (parts.empty())
294+
if(parts.empty())
319295
return "";
320-
std::string result = parts.at(0);
321-
for (uint64_t i = 1ULL; i < parts.size(); ++i)
296+
std::string result = parts.at(0);
297+
for(uint64_t i = 1ULL; i < parts.size(); ++i)
322298
{
323299
result.push_back('/');
324300
result.append(parts.at(i));
325301
}
326302
return result;
327303
}
328304

329-
std::string fileutl_get_common_preffix(std::string const& pathname1,
330-
std::string const& pathname2)
305+
std::string fileutl_get_common_prefix(
306+
std::string const &pathname1,
307+
std::string const &pathname2)
331308
{
332309
std::vector<std::string> split1;
333-
fileutl_split_pathname(pathname1,split1);
310+
fileutl_split_pathname(pathname1, split1);
334311

335312
std::vector<std::string> split2;
336-
fileutl_split_pathname(pathname2,split2);
313+
fileutl_split_pathname(pathname2, split2);
337314

338315
std::vector<std::string> common_split;
339-
for (uint64_t i = 0ULL, size = std::min(split1.size(),split2.size());
340-
i < size && split1.at(i) == split2.at(i);
341-
++i)
316+
for(uint64_t i = 0ULL, size = std::min(split1.size(), split2.size());
317+
i < size && split1.at(i) == split2.at(i);
318+
++i)
342319
common_split.push_back(split1.at(i));
343320

344-
std::string const result = fileutl_join_path_parts(common_split);
321+
std::string const result = fileutl_join_path_parts(common_split);
345322
return result;
346323
}
347324

348-
std::string fileutl_get_relative_path(std::string const& pathname,
349-
std::string const& directory)
325+
std::string fileutl_get_relative_path(
326+
std::string const &pathname,
327+
std::string const &directory)
350328
{
351329
std::vector<std::string> split1;
352-
fileutl_split_pathname(pathname,split1);
353-
std::reverse(split1.begin(),split1.end());
330+
fileutl_split_pathname(pathname, split1);
331+
std::reverse(split1.begin(), split1.end());
354332

355333
std::vector<std::string> split2;
356-
fileutl_split_pathname(directory,split2);
357-
std::reverse(split2.begin(),split2.end());
334+
fileutl_split_pathname(directory, split2);
335+
std::reverse(split2.begin(), split2.end());
358336

359-
while (!split1.empty() && !split2.empty() && split1.back() == split2.back())
337+
while(!split1.empty() && !split2.empty() && split1.back() == split2.back())
360338
{
361339
split1.pop_back();
362340
split2.pop_back();
363341
}
364342

365-
std::reverse(split1.begin(),split1.end());
366-
std::string const result = fileutl_join_path_parts(split1);
343+
std::reverse(split1.begin(), split1.end());
344+
std::string const result = fileutl_join_path_parts(split1);
367345
return result;
368346
}

0 commit comments

Comments
 (0)