2013-04-03 20:20:59 +02:00
|
|
|
// Copyright (c) 2013 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.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/resource_util.h"
|
2015-01-31 01:55:56 +01:00
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-23 20:09:34 +01:00
|
|
|
namespace client {
|
|
|
|
|
2013-04-03 20:20:59 +02:00
|
|
|
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");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!file) {
|
2013-04-03 20:20:59 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
|
|
|
|
char buf[1 << 16];
|
|
|
|
size_t len;
|
2023-01-02 23:59:03 +01:00
|
|
|
while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
|
2013-04-03 20:20:59 +02:00
|
|
|
data.append(buf, len);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool LoadBinaryResource(const char* resource_name, std::string& resource_data) {
|
|
|
|
std::string path;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!GetResourceDir(path)) {
|
2013-04-03 20:20:59 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
|
|
|
|
path.append("/");
|
|
|
|
path.append(resource_name);
|
|
|
|
|
|
|
|
return ReadFileToString(path.c_str(), resource_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> GetBinaryResourceReader(const char* resource_name) {
|
|
|
|
std::string path;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!GetResourceDir(path)) {
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
|
|
|
|
path.append("/");
|
|
|
|
path.append(resource_name);
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!FileExists(path.c_str())) {
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
|
|
|
|
return CefStreamReader::CreateForFile(path);
|
|
|
|
}
|
2015-01-23 20:09:34 +01:00
|
|
|
|
|
|
|
} // namespace client
|