-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilesystem.h
More file actions
78 lines (65 loc) · 2.42 KB
/
filesystem.h
File metadata and controls
78 lines (65 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#ifdef WIN32
#pragma warning( push )
#pragma warning( disable : 26495) // uninitialized variable
#endif
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <boost/locale.hpp>
#ifdef WIN32
#pragma warning( pop )
#endif
#include <algorithm>
#include <iostream>
#include <fstream>
#include "stringext.h"
#include "result.h"
namespace filesystem
{
inline boost::optional<boost::filesystem::path> FindFile(const boost::filesystem::path& dir, const boost::filesystem::path& file)
{
const boost::filesystem::recursive_directory_iterator end;
const auto it = std::find_if( boost::filesystem::recursive_directory_iterator(dir), end, [&file](const boost::filesystem::directory_entry& e) {
return e.path().filename() == file;
});
return it == end ? boost::optional<boost::filesystem::path>() : it->path();
}
inline boost::optional<boost::filesystem::path> FindFileNoCase(const boost::filesystem::path& dir, const boost::filesystem::path& file)
{
const boost::filesystem::recursive_directory_iterator end;
const auto it = std::find_if( boost::filesystem::recursive_directory_iterator(dir), end, [&file](const boost::filesystem::directory_entry& e) {
return compare_nocase(e.path().filename().string(),file.string());
});
return it == end ? boost::optional<boost::filesystem::path>() : it->path();
}
inline Result ReadTextFileToUTF8(const std::string& path, std::string& utf8)
{
Result result;
std::ifstream is(path, std::ifstream::in | std::ifstream::binary);
if(is.fail() || is.bad())
{
return Result(false, "Could not open %s", path.c_str());
}
is.seekg (0, std::ios::end);
size_t length = is.tellg();
is.seekg (0, std::ios::beg);
char* buffer = new char[length];
is.read (buffer,length);
is.close();
if(length > 2)
{
// UCS-2 BOM
if(buffer[0] == '\377' && buffer[1] == '\376')
{
utf8 = boost::locale::conv::to_utf<char>(buffer, buffer + length, "UCS-2");
}
}
// assume utf8
if(utf8.empty())
{
utf8.insert(utf8.begin(), buffer, buffer + length);
}
delete [] buffer;
return result;
}
}