本文整理汇总了C++中FileMap::create方法的典型用法代码示例。如果您正苦于以下问题:C++ FileMap::create方法的具体用法?C++ FileMap::create怎么用?C++ FileMap::create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileMap
的用法示例。
在下文中一共展示了FileMap::create方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: strerror
FileMap*
FileMapBuffer::createMap(const char* filename)
{
int fd;
size_t length;
fd = ::open(filename, O_RDONLY | O_BINARY);
if (fd < 0) {
LOGW("Unable to open file '%s': %s\n", filename, strerror(errno));
return NULL;
}
length = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
FileMap* map = new FileMap();
if (!map) {
LOGW("Unable to create file map for '%s': %s\n", filename, strerror(errno));
return NULL;
}
if (!map->create(filename, fd, 0, length, true)) {
map->release();
LOGW("Unable to create file map for '%s': %s\n", filename, strerror(errno));
return NULL;
}
close(fd);
return map;
}
示例2: open
status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
*outTokenizer = NULL;
int result = NO_ERROR;
int fd = ::open(filename.string(), O_RDONLY);
if (fd < 0) {
result = -errno;
ALOGE("Error opening file '%s', %s.", filename.string(), strerror(errno));
} else {
struct stat stat;
if (fstat(fd, &stat)) {
result = -errno;
ALOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno));
} else {
size_t length = size_t(stat.st_size);
FileMap* fileMap = new FileMap();
bool ownBuffer = false;
char* buffer;
if (fileMap->create(NULL, fd, 0, length, true)) {
fileMap->advise(FileMap::SEQUENTIAL);
buffer = static_cast<char*>(fileMap->getDataPtr());
} else {
delete fileMap;
fileMap = NULL;
// Fall back to reading into a buffer since we can't mmap files in sysfs.
// The length we obtained from stat is wrong too (it will always be 4096)
// so we must trust that read will read the entire file.
buffer = new char[length];
ownBuffer = true;
ssize_t nrd = read(fd, buffer, length);
if (nrd < 0) {
result = -errno;
ALOGE("Error reading file '%s', %s.", filename.string(), strerror(errno));
delete[] buffer;
buffer = NULL;
} else {
length = size_t(nrd);
}
}
if (!result) {
*outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
}
}
close(fd);
}
return result;
}
示例3: createEntryFileMap
/*
* Create a new FileMap object that spans the data in "entry".
*/
FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
{
const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
const ZipEntry& ze = zipEntry->entry;
int fd = GetFileDescriptor(mHandle);
size_t actualLen = 0;
if (ze.method == kCompressStored) {
actualLen = ze.uncompressed_length;
} else {
actualLen = ze.compressed_length;
}
FileMap* newMap = new FileMap();
if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
newMap->release();
return NULL;
}
return newMap;
}
示例4: getBuffer
/*
* Return a read-only pointer to a buffer.
*
* We can either read the whole thing in or map the relevant piece of
* the source file. Ideally a map would be established at a higher
* level and we'd be using a different object, but we didn't, so we
* deal with it here.
*/
const void* _FileAsset::getBuffer(bool wordAligned)
{
/* subsequent requests just use what we did previously */
if (mBuf != NULL)
return mBuf;
if (mMap != NULL) {
if (!wordAligned) {
return mMap->getDataPtr();
}
return ensureAlignment(mMap);
}
assert(mFp != NULL);
if (mLength < kReadVsMapThreshold) {
unsigned char* buf;
long allocLen;
/* zero-length files are allowed; not sure about zero-len allocs */
/* (works fine with gcc + x86linux) */
allocLen = mLength;
if (mLength == 0)
allocLen = 1;
buf = new unsigned char[allocLen];
if (buf == NULL) {
ALOGE("alloc of %ld bytes failed\n", (long) allocLen);
return NULL;
}
ALOGV("Asset %p allocating buffer size %d (smaller than threshold)", this, (int)allocLen);
if (mLength > 0) {
long oldPosn = ftell(mFp);
fseek(mFp, mStart, SEEK_SET);
if (fread(buf, 1, mLength, mFp) != (size_t) mLength) {
ALOGE("failed reading %ld bytes\n", (long) mLength);
delete[] buf;
return NULL;
}
fseek(mFp, oldPosn, SEEK_SET);
}
ALOGV(" getBuffer: loaded into buffer\n");
mBuf = buf;
return mBuf;
} else {
FileMap* map;
map = new FileMap;
if (!map->create(NULL, fileno(mFp), mStart, mLength, true)) {
delete map;
return NULL;
}
ALOGV(" getBuffer: mapped\n");
mMap = map;
if (!wordAligned) {
return mMap->getDataPtr();
}
return ensureAlignment(mMap);
}
}