Add static function File::ReadFileToString.
Change-Id: Ib50aea743662376c8c7da82bba4002932da4014c
This commit is contained in:
parent
014cde57bd
commit
76269c4706
|
@ -4,13 +4,15 @@
|
||||||
|
|
||||||
#include "media/file/file.h"
|
#include "media/file/file.h"
|
||||||
|
|
||||||
|
#include "base/logging.h"
|
||||||
|
#include "base/memory/scoped_ptr.h"
|
||||||
#include "media/file/local_file.h"
|
#include "media/file/local_file.h"
|
||||||
|
|
||||||
namespace media {
|
namespace media {
|
||||||
|
|
||||||
const char* kLocalFilePrefix = "file://";
|
const char* kLocalFilePrefix = "file://";
|
||||||
|
|
||||||
typedef File* (*FileFactoryFunction)(const char* fname, const char* mode);
|
typedef File* (*FileFactoryFunction)(const char* file_name, const char* mode);
|
||||||
|
|
||||||
struct SupportedTypeInfo {
|
struct SupportedTypeInfo {
|
||||||
const char* type;
|
const char* type;
|
||||||
|
@ -18,30 +20,30 @@ struct SupportedTypeInfo {
|
||||||
const FileFactoryFunction factory_function;
|
const FileFactoryFunction factory_function;
|
||||||
};
|
};
|
||||||
|
|
||||||
static File* CreateLocalFile(const char* fname, const char* mode) {
|
static File* CreateLocalFile(const char* file_name, const char* mode) {
|
||||||
return new LocalFile(fname, mode);
|
return new LocalFile(file_name, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const SupportedTypeInfo kSupportedTypeInfo[] = {
|
static const SupportedTypeInfo kSupportedTypeInfo[] = {
|
||||||
{ kLocalFilePrefix, strlen(kLocalFilePrefix), &CreateLocalFile },
|
{ kLocalFilePrefix, strlen(kLocalFilePrefix), &CreateLocalFile },
|
||||||
};
|
};
|
||||||
|
|
||||||
File* File::Create(const char* fname, const char* mode) {
|
File* File::Create(const char* file_name, const char* mode) {
|
||||||
for (size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
|
for (size_t i = 0; i < arraysize(kSupportedTypeInfo); ++i) {
|
||||||
const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
|
const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
|
||||||
if (strncmp(type_info.type, fname, type_info.type_length) == 0) {
|
if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
|
||||||
return type_info.factory_function(fname + type_info.type_length, mode);
|
return type_info.factory_function(file_name + type_info.type_length,
|
||||||
|
mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Otherwise we assume it is a local file
|
// Otherwise we assume it is a local file
|
||||||
return CreateLocalFile(fname, mode);
|
return CreateLocalFile(file_name, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
File* File::Open(const char* name, const char* mode) {
|
File* File::Open(const char* file_name, const char* mode) {
|
||||||
File* file = File::Create(name, mode);
|
File* file = File::Create(file_name, mode);
|
||||||
if (!file) {
|
if (!file)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
if (!file->Open()) {
|
if (!file->Open()) {
|
||||||
delete file;
|
delete file;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -49,16 +51,31 @@ File* File::Open(const char* name, const char* mode) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the file size or -1 on failure.
|
int64 File::GetFileSize(const char* file_name) {
|
||||||
// Requires opening and closing the file.
|
File* file = File::Open(file_name, "r");
|
||||||
int64 File::GetFileSize(const char* name) {
|
if (!file)
|
||||||
File* f = File::Open(name, "r");
|
|
||||||
if (!f) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
int64 res = file->Size();
|
||||||
int64 res = f->Size();
|
file->Close();
|
||||||
f->Close();
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool File::ReadFileToString(const char* file_name, std::string* contents) {
|
||||||
|
DCHECK(contents);
|
||||||
|
|
||||||
|
File* file = File::Open(file_name, "r");
|
||||||
|
if (!file)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const size_t kBufferSize = 0x40000; // 256KB.
|
||||||
|
scoped_ptr<char[]> buf(new char[kBufferSize]);
|
||||||
|
|
||||||
|
int64 len;
|
||||||
|
while ((len = file->Read(buf.get(), kBufferSize)) > 0)
|
||||||
|
contents->append(buf.get(), len);
|
||||||
|
|
||||||
|
file->Close();
|
||||||
|
return len == 0;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace media
|
} // namespace media
|
||||||
|
|
|
@ -20,7 +20,7 @@ class File {
|
||||||
// Open the specified file, or return NULL on error.
|
// Open the specified file, or return NULL on error.
|
||||||
// This is actually a file factory method, it opens a proper file, e.g.
|
// This is actually a file factory method, it opens a proper file, e.g.
|
||||||
// LocalFile, MemFile automatically based on prefix.
|
// LocalFile, MemFile automatically based on prefix.
|
||||||
static File* Open(const char* name, const char* mode);
|
static File* Open(const char* file_name, const char* mode);
|
||||||
|
|
||||||
// Flush() and de-allocate resources associated with this file, and
|
// Flush() and de-allocate resources associated with this file, and
|
||||||
// delete this File object. THIS IS THE ONE TRUE WAY TO DEALLOCATE
|
// delete this File object. THIS IS THE ONE TRUE WAY TO DEALLOCATE
|
||||||
|
@ -65,8 +65,12 @@ class File {
|
||||||
// ************************************************************
|
// ************************************************************
|
||||||
|
|
||||||
// Returns the size of a file in bytes, and opens and closes the file
|
// Returns the size of a file in bytes, and opens and closes the file
|
||||||
// in the process. Returns -1 on failure.
|
// in the process. Returns a value < 0 on failure.
|
||||||
static int64 GetFileSize(const char* fname);
|
static int64 GetFileSize(const char* file_name);
|
||||||
|
|
||||||
|
// Read the file at |file_name| into |contents|, returning true on success.
|
||||||
|
// |contents| should not be NULL.
|
||||||
|
static bool ReadFileToString(const char* file_name, std::string* contents);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit File(const std::string& file_name) : file_name_(file_name) {}
|
explicit File(const std::string& file_name) : file_name_(file_name) {}
|
||||||
|
@ -80,7 +84,7 @@ class File {
|
||||||
private:
|
private:
|
||||||
// This is a file factory method, it creates a proper file, e.g.
|
// This is a file factory method, it creates a proper file, e.g.
|
||||||
// LocalFile, MemFile based on prefix.
|
// LocalFile, MemFile based on prefix.
|
||||||
static File* Create(const char* fname, const char* mode);
|
static File* Create(const char* file_name, const char* mode);
|
||||||
|
|
||||||
std::string file_name_;
|
std::string file_name_;
|
||||||
DISALLOW_COPY_AND_ASSIGN(File);
|
DISALLOW_COPY_AND_ASSIGN(File);
|
||||||
|
|
Loading…
Reference in New Issue