2011-10-24 22:20:52 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2011 The Chromium 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 "resource_util.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "base/file_util.h"
|
|
|
|
|
|
|
|
bool GetResourceDir(std::string& dir) {
|
|
|
|
char buff[1024];
|
2011-11-12 05:47:00 +01:00
|
|
|
|
|
|
|
// Retrieve the executable path.
|
2011-10-24 22:20:52 +02:00
|
|
|
ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
|
2011-11-12 05:47:00 +01:00
|
|
|
if (len == -1)
|
|
|
|
return false;
|
2011-10-24 22:20:52 +02:00
|
|
|
|
2011-11-12 05:47:00 +01:00
|
|
|
buff[len] = 0;
|
|
|
|
|
|
|
|
// Remove the executable name from the path.
|
|
|
|
char* pos = strrchr(buff, '/');
|
|
|
|
if (!pos)
|
2011-10-24 22:20:52 +02:00
|
|
|
return false;
|
2011-11-12 05:47:00 +01:00
|
|
|
|
|
|
|
// Add "files" to the path.
|
|
|
|
strcpy(pos+1, "files");
|
|
|
|
dir = std::string(buff);
|
|
|
|
return true;
|
2011-10-24 22:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadBinaryResource(const char* resource_name, std::string& resource_data) {
|
2011-11-12 05:47:00 +01:00
|
|
|
std::string path;
|
|
|
|
if (!GetResourceDir(path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
path.append("/");
|
|
|
|
path.append(resource_name);
|
|
|
|
|
|
|
|
FILE* f = fopen(path.c_str(), "rb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t bytes_read;
|
|
|
|
char buff[1024*8];
|
|
|
|
|
|
|
|
do {
|
|
|
|
bytes_read = fread(buff, 1, sizeof(buff)-1, f);
|
|
|
|
if (bytes_read > 0)
|
|
|
|
resource_data.append(buff, bytes_read);
|
|
|
|
} while(bytes_read > 0);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
2011-10-24 22:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> GetBinaryResourceReader(const char* resource_name) {
|
|
|
|
std::string path;
|
|
|
|
if (!GetResourceDir(path))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
path.append("/");
|
|
|
|
path.append(resource_name);
|
|
|
|
|
|
|
|
return CefStreamReader::CreateForFile(path);
|
|
|
|
}
|