-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathresource_util_posix.cc
65 lines (48 loc) · 1.34 KB
/
resource_util_posix.cc
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
// Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "examples/shared/resource_util.h"
#include <stdio.h>
namespace shared {
namespace {
bool FileExists(const char* path) {
FILE* f = fopen(path, "rb");
if (f) {
fclose(f);
return true;
}
return false;
}
bool ReadFileToString(const char* path, std::string& data) {
// Implementation adapted from base/file_util.cc
FILE* file = fopen(path, "rb");
if (!file)
return false;
char buf[1 << 16];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), file)) > 0)
data.append(buf, len);
fclose(file);
return true;
}
} // namespace
bool GetResourceString(const std::string& resource_path,
std::string& out_data) {
std::string path;
if (!GetResourceDir(path))
return false;
path.append("/");
path.append(resource_path);
return ReadFileToString(path.c_str(), out_data);
}
CefRefPtr<CefStreamReader> GetResourceReader(const std::string& resource_path) {
std::string path;
if (!GetResourceDir(path))
return nullptr;
path.append("/");
path.append(resource_path);
if (!FileExists(path.c_str()))
return nullptr;
return CefStreamReader::CreateForFile(path);
}
} // namespace shared