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 "base/logging.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "media/file/local_file.h"
|
||||
|
||||
namespace media {
|
||||
|
||||
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 {
|
||||
const char* type;
|
||||
|
@ -18,30 +20,30 @@ struct SupportedTypeInfo {
|
|||
const FileFactoryFunction factory_function;
|
||||
};
|
||||
|
||||
static File* CreateLocalFile(const char* fname, const char* mode) {
|
||||
return new LocalFile(fname, mode);
|
||||
static File* CreateLocalFile(const char* file_name, const char* mode) {
|
||||
return new LocalFile(file_name, mode);
|
||||
}
|
||||
|
||||
static const SupportedTypeInfo kSupportedTypeInfo[] = {
|
||||
{ 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) {
|
||||
const SupportedTypeInfo& type_info = kSupportedTypeInfo[i];
|
||||
if (strncmp(type_info.type, fname, type_info.type_length) == 0) {
|
||||
return type_info.factory_function(fname + type_info.type_length, mode);
|
||||
if (strncmp(type_info.type, file_name, type_info.type_length) == 0) {
|
||||
return type_info.factory_function(file_name + type_info.type_length,
|
||||
mode);
|
||||
}
|
||||
}
|
||||
// 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 = File::Create(name, mode);
|
||||
if (!file) {
|
||||
File* File::Open(const char* file_name, const char* mode) {
|
||||
File* file = File::Create(file_name, mode);
|
||||
if (!file)
|
||||
return NULL;
|
||||
}
|
||||
if (!file->Open()) {
|
||||
delete file;
|
||||
return NULL;
|
||||
|
@ -49,16 +51,31 @@ File* File::Open(const char* name, const char* mode) {
|
|||
return file;
|
||||
}
|
||||
|
||||
// Return the file size or -1 on failure.
|
||||
// Requires opening and closing the file.
|
||||
int64 File::GetFileSize(const char* name) {
|
||||
File* f = File::Open(name, "r");
|
||||
if (!f) {
|
||||
int64 File::GetFileSize(const char* file_name) {
|
||||
File* file = File::Open(file_name, "r");
|
||||
if (!file)
|
||||
return -1;
|
||||
}
|
||||
int64 res = f->Size();
|
||||
f->Close();
|
||||
int64 res = file->Size();
|
||||
file->Close();
|
||||
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
|
||||
|
|
|
@ -20,7 +20,7 @@ class File {
|
|||
// Open the specified file, or return NULL on error.
|
||||
// This is actually a file factory method, it opens a proper file, e.g.
|
||||
// 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
|
||||
// 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
|
||||
// in the process. Returns -1 on failure.
|
||||
static int64 GetFileSize(const char* fname);
|
||||
// in the process. Returns a value < 0 on failure.
|
||||
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:
|
||||
explicit File(const std::string& file_name) : file_name_(file_name) {}
|
||||
|
@ -80,7 +84,7 @@ class File {
|
|||
private:
|
||||
// This is a file factory method, it creates a proper file, e.g.
|
||||
// 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_;
|
||||
DISALLOW_COPY_AND_ASSIGN(File);
|
||||
|
|
Loading…
Reference in New Issue