2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2008 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 "stream_impl.h"
|
|
|
|
#include "base/logging.h"
|
2010-12-07 02:30:26 +01:00
|
|
|
#include <stdlib.h>
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Static functions
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForFile(
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& fileName)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string fileNameStr = fileName;
|
|
|
|
FILE *file = fopen(fileNameStr.c_str(), "rb");
|
|
|
|
if(file)
|
|
|
|
reader = new CefFileReader(file, true);
|
2010-10-03 23:04:50 +02:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForData(void* data,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
DCHECK(data != NULL);
|
|
|
|
DCHECK(size > 0);
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
|
|
|
if(data && size > 0)
|
|
|
|
reader = new CefBytesReader(data, size, true);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForHandler(
|
|
|
|
CefRefPtr<CefReadHandler> handler)
|
|
|
|
{
|
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
|
|
|
if(handler.get())
|
|
|
|
reader = new CefHandlerReader(handler);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForFile(
|
2010-11-22 18:49:46 +01:00
|
|
|
const CefString& fileName)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
DCHECK(!fileName.empty());
|
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string fileNameStr = fileName;
|
|
|
|
FILE *file = fopen(fileNameStr.c_str(), "wb");
|
2010-10-03 23:04:50 +02:00
|
|
|
if(file)
|
|
|
|
writer = new CefFileWriter(file, true);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForHandler(
|
|
|
|
CefRefPtr<CefWriteHandler> handler)
|
|
|
|
{
|
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
|
|
|
if(handler.get())
|
|
|
|
writer = new CefHandlerWriter(handler);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefFileReader
|
|
|
|
|
|
|
|
CefFileReader::CefFileReader(FILE* file, bool close)
|
2010-10-10 02:16:24 +02:00
|
|
|
: close_(close), file_(file)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileReader::~CefFileReader()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
if(close_)
|
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefFileReader::Read(void* ptr, size_t size, size_t n)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fread(ptr, size, n, file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileReader::Seek(long offset, int whence)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fseek(file_, offset, whence);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long CefFileReader::Tell()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return ftell(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileReader::Eof()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return feof(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefFileWriter
|
|
|
|
|
|
|
|
CefFileWriter::CefFileWriter(FILE* file, bool close)
|
|
|
|
: file_(file), close_(close)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileWriter::~CefFileWriter()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
if(close_)
|
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefFileWriter::Write(const void* ptr, size_t size, size_t n)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return (size_t)fwrite(ptr, size, n, file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileWriter::Seek(long offset, int whence)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fseek(file_, offset, whence);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
long CefFileWriter::Tell()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return ftell(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileWriter::Flush()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fflush(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBytesReader
|
|
|
|
|
|
|
|
CefBytesReader::CefBytesReader(void* data, long datasize, bool copy)
|
|
|
|
: data_(NULL), datasize_(0), copy_(false), offset_(0)
|
|
|
|
{
|
|
|
|
SetData(data, datasize, copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBytesReader::~CefBytesReader()
|
|
|
|
{
|
|
|
|
SetData(NULL, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefBytesReader::Read(void* ptr, size_t size, size_t n)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
size_t s = (datasize_ - offset_) / size;
|
|
|
|
size_t ret = (n < s ? n : s);
|
|
|
|
memcpy(ptr, ((char*)data_) + offset_, ret * size);
|
|
|
|
offset_ += ret * size;
|
2011-05-20 16:42:25 +02:00
|
|
|
return ret;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefBytesReader::Seek(long offset, int whence)
|
|
|
|
{
|
|
|
|
int rv = -1L;
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
switch(whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
if(offset_ + offset > datasize_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
if(offset > (int)datasize_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset_ = datasize_ - offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
if(offset > (int)datasize_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
long CefBytesReader::Tell()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return offset_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefBytesReader::Eof()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return (offset_ >= datasize_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefBytesReader::SetData(void* data, long datasize, bool copy)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
if(copy_)
|
|
|
|
free(data_);
|
|
|
|
|
|
|
|
copy_ = copy;
|
|
|
|
offset_ = 0;
|
|
|
|
datasize_ = datasize;
|
|
|
|
|
|
|
|
if(copy) {
|
|
|
|
data_ = malloc(datasize);
|
|
|
|
DCHECK(data_ != NULL);
|
|
|
|
if(data_)
|
|
|
|
memcpy(data_, data, datasize);
|
|
|
|
} else {
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBytesWriter
|
|
|
|
|
|
|
|
CefBytesWriter::CefBytesWriter(size_t grow)
|
2010-10-10 02:16:24 +02:00
|
|
|
: grow_(grow), datasize_(grow), offset_(0)
|
2010-10-03 23:04:50 +02:00
|
|
|
{
|
|
|
|
DCHECK(grow > 0);
|
|
|
|
data_ = malloc(grow);
|
|
|
|
DCHECK(data_ != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBytesWriter::~CefBytesWriter()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
if(data_)
|
|
|
|
free(data_);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefBytesWriter::Write(const void* ptr, size_t size, size_t n)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
size_t rv;
|
|
|
|
if(offset_ + size * n >= datasize_ && Grow(size * n) == 0) {
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
|
|
|
memcpy(((char*)data_) + offset_, ptr, size * n);
|
|
|
|
offset_ += size * n;
|
|
|
|
rv = n;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefBytesWriter::Seek(long offset, int whence)
|
|
|
|
{
|
|
|
|
int rv = -1L;
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
switch(whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
if(offset_ + offset > datasize_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset_ += offset;
|
|
|
|
rv = offset_;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
if(offset > (int)datasize_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset_ = datasize_ - offset;
|
|
|
|
rv = offset_;
|
|
|
|
case SEEK_SET:
|
|
|
|
if(offset > (int)datasize_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset_ = offset;
|
|
|
|
rv = offset_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
long CefBytesWriter::Tell()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return offset_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefBytesWriter::Flush()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CefBytesWriter::GetDataString()
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
std::string str((char*)data_, offset_);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefBytesWriter::Grow(size_t size)
|
|
|
|
{
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
size_t rv;
|
|
|
|
size_t s = (size > grow_ ? size : grow_);
|
|
|
|
void* tmp = realloc(data_, datasize_ + s);
|
|
|
|
DCHECK(tmp != NULL);
|
|
|
|
if(tmp) {
|
|
|
|
data_ = tmp;
|
|
|
|
datasize_ += s;
|
|
|
|
rv = datasize_;
|
|
|
|
} else {
|
|
|
|
rv = 0;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return rv;
|
|
|
|
}
|