Ejemplo n.º 1
0
// Read the given file into memory and return a pointer to the allocated
// buffer.  The buffer will be null-terminated, like a C string; you can
// also obtain the data length by passing a pointer to an unsigned long
// as the (optional) second parameter.  The buffer should be freed with
// delete[] when no longer needed.
char *readFile(const std::string& path, unsigned long *size_ret)
{
	long fileSize;
#ifdef BBGE_BUILD_VFS
	VFILE *vf = vfs.GetFile(path.c_str());
	if (!vf)
		return NULL;
	char *buffer = (char*)vf->getBuf(NULL, NULL);
	fileSize = vf->size();
	vf->dropBuf(false); // unlink buffer from file
#else
	FILE *f = fopen(path.c_str(), "rb");
	if (!f)
		return NULL;


	if (fseek(f, 0, SEEK_END) != 0
	 || (fileSize = ftell(f)) < 0
	 || fseek(f, 0, SEEK_SET) != 0)
	{
		debugLog(path + ": Failed to get file size");
		fclose(f);
		return NULL;
	}

	char *buffer = new char[fileSize + 1];
	if (!buffer)
	{
		std::ostringstream os;
		os << path << ": Not enough memory for file ("
		   << (fileSize+1) << " bytes)";
		debugLog(os.str());
		fclose(f);
		return NULL;
	}

	long bytesRead = fread(buffer, 1, fileSize, f);
	if (bytesRead != fileSize)
	{
		std::ostringstream os;
		os << path << ": Failed to read file (only got "
		   << bytesRead << " of " << fileSize << " bytes)";
		debugLog(os.str());
		fclose(f);
		return NULL;
	}
	fclose(f);
	buffer[fileSize] = 0;
#endif

	if (size_ret)
		*size_ret = fileSize;
	return buffer;
}
Ejemplo n.º 2
0
VFILE *vfopen(const char *fn, const char *mode)
{
    if (strchr(mode, 'w'))
    {
        fprintf(stderr, "FileAPI.h: File writing via VFS not yet supported!");
        return NULL;
    }

    VFILE *vf = vfs.GetFile(fn);
    if (!vf || !vf->open(mode))
        return NULL;
    ++(vf->ref); // keep the file alive until closed.
    return vf;
}
Ejemplo n.º 3
0
VFILE *vfopen(const char *fn, const char *mode)
{
    if (strchr(mode, 'w'))
    {
        assert(0 && "ttvfs_stdio: File writing via VFS not yet supported!");
        return NULL;
    }

    VFILE *vf = vfs->GetFile(fn);
    if (!vf || !vf->open(mode))
        return NULL;
    vf->incref(); // keep the file alive until closed.
    return vf;
}