Beispiel #1
0
int64 File::getSize()
{
	// If the file is closed, open it to
	// check the size.
	if (file == 0)
	{
		open(READ);
		int64 size = (int64)PHYSFS_fileLength(file);
		close();
		return size;
	}

	return (int64)PHYSFS_fileLength(file);
}
Beispiel #2
0
	unsigned int File::getSize()
	{
		// If the file is closed, open it to
		// check the size.
		if(file == 0)
		{
			open(READ);
			unsigned int size = (unsigned int)PHYSFS_fileLength(file);
			close();
			return size;
		}

		return (unsigned int)PHYSFS_fileLength(file);
	}
Beispiel #3
0
	int File::getSize()
	{
		// If the file is closed, open it to
		// check the size.
		if(file == 0)
		{
			if(!open())
				return 0;
			int size = (int)PHYSFS_fileLength(file);
			close();
			return size;
		}

		return (int)PHYSFS_fileLength(file);
	}
natU8* FileManager::Read(const natChar* _filename, size_t* _size)
{
//#if defined(WINDOWS_TARGET)
	PHYSFS_file* file = PHYSFS_openRead(_filename);
	assert(file);

	*_size = PHYSFS_fileLength(file);
	assert(*_size != 0);

	natU8* buffer = new natU8[*_size];
	PHYSFS_read(file, buffer, 1, static_cast<PHYSFS_uint32>(*_size));
	PHYSFS_close(file);

	return buffer;
/*#elif defined(EMSCRIPTEN_TARGET)
	std::ifstream file(_filename, std::ios::in|std::ios::binary|std::ios::ate);
	assert(file.is_open());

	*_size = file.tellg();
	assert(*_size != 0);

	natU8* buffer = new natU8[*_size];
	file.seekg (0, std::ios::beg);
	file.read ((char*)buffer, *_size);
	file.close();

	return buffer;
#else

#endif*/
}
Beispiel #5
0
IFileStreambuf::pos_type
IFileStreambuf::seekoff(off_type off, std::ios_base::seekdir dir,
                        std::ios_base::openmode mode)
{
  off_type pos = off;
  PHYSFS_sint64 ptell = PHYSFS_tell(file);

  switch(dir) {
    case std::ios_base::beg:
      break;
    case std::ios_base::cur:
      if(off == 0)
        return static_cast<pos_type> (ptell) - static_cast<pos_type> (egptr() - gptr());
      pos += static_cast<off_type> (ptell) - static_cast<off_type> (egptr() - gptr());
      break;
    case std::ios_base::end:
      pos += static_cast<off_type> (PHYSFS_fileLength(file));
      break;
    default:
      assert(false);
      return pos_type(off_type(-1));
  }

  return seekpos(static_cast<pos_type> (pos), mode);
}
Beispiel #6
0
static int l_filesystem_require(lua_State* state)
{
  if(!lua_isstring(state, 1))
    return luaL_error(state, "The argument must be a string.");

  const char* filename = lua_tostring(state, 1);

  char myBuf[2048] = {0};

  PHYSFS_file* myfile = PHYSFS_openRead(filename);
  if(!myfile)
    printf("%s %s \n", "No .lua file named in directory ", filename);
  PHYSFS_sint64 fileLngth = PHYSFS_fileLength(myfile);
  PHYSFS_read (myfile, myBuf, 1, fileLngth);
  int status = 0;

  status = luaL_loadbuffer(state, (const char *)myBuf, fileLngth, filename) || lua_pcall(state, 0,0,0);
  if(status != 0) luaL_error(state,lua_tostring(state,-1));

  PHYSFS_close(myfile);

  switch (status)
    {
    case LUA_ERRMEM:
      return luaL_error(state, "Memory allocation error: %s\n", lua_tostring(state, -1));
    case LUA_ERRSYNTAX:
      return luaL_error(state, "Syntax error: %s\n", lua_tostring(state, -1));
    default: // success
      return 1;
    }

  return 1;
}
Beispiel #7
0
int fs_seek(fs_file fh, long offset, int whence)
{
    PHYSFS_uint64 pos = 0;
    PHYSFS_sint64 cur = PHYSFS_tell(fh->handle);
    PHYSFS_sint64 len = PHYSFS_fileLength(fh->handle);

    switch (whence)
    {
    case SEEK_SET:
        pos = offset;
        break;

    case SEEK_CUR:
        if (cur < 0)
            return -1;
        pos = cur + offset;
        break;

    case SEEK_END:
        if (len < 0)
            return -1;
        pos = len + offset;
        break;
    }

    return PHYSFS_seek(fh->handle, pos);
}
Beispiel #8
0
void*
ResourceManager::loadFile(const std::string &fileName, int &fileSize)
{
    // Attempt to open the specified file using PhysicsFS
    PHYSFS_file *file = PHYSFS_openRead(fileName.c_str());

    // If the handler is an invalid pointer indicate failure
    if (file == NULL) {
        logger->log("Warning: Failed to load %s: %s",
                fileName.c_str(), PHYSFS_getLastError());
        return NULL;
    }

    // Log the real dir of the file
    logger->log("Loaded %s/%s", PHYSFS_getRealDir(fileName.c_str()),
            fileName.c_str());

    // Get the size of the file
    fileSize = PHYSFS_fileLength(file);

    // Allocate memory and load the file
    void *buffer = malloc(fileSize);
    PHYSFS_read(file, buffer, 1, fileSize);

    // Close the file and let the user deallocate the memory
    PHYSFS_close(file);

    return buffer;
}
Beispiel #9
0
int OSBasics::seek(OSFILE * stream, long int offset, int origin ) {
	switch(stream->fileType) {
		case OSFILE::TYPE_FILE:
			return fseek(stream->file, offset, origin);
			break;
		case OSFILE::TYPE_ARCHIVE_FILE:
			switch(origin) {
				case SEEK_SET:
					return PHYSFS_seek(stream->physFSFile, offset);
				break;
				case SEEK_CUR: {
					PHYSFS_sint64 curoffset = PHYSFS_tell(stream->physFSFile);					
					return PHYSFS_seek(stream->physFSFile, curoffset+offset);				
				}
				break;
				case SEEK_END: {
					PHYSFS_sint64 fileLength =  PHYSFS_fileLength(stream->physFSFile);
					return PHYSFS_seek(stream->physFSFile, fileLength-offset);
				}
				break;
			}
			break;			
	}
	return 0;	
}
Beispiel #10
0
// Read a whole file and put it in a new buffer
unsigned int j1FileSystem::Load(const char* file, char** buffer) const
{
	unsigned int ret = 0;

	PHYSFS_file* fs_file = PHYSFS_openRead(file);

	if(fs_file != NULL)
	{
		PHYSFS_sint32 size = PHYSFS_fileLength(fs_file);

		if(size > 0)
		{
			*buffer = new char[size];
			int readed = PHYSFS_read(fs_file, *buffer, 1, size);
			if(readed != size)
			{
				LOG("File System error while reading from file %s: %s\n", file, PHYSFS_getLastError());
				RELEASE(buffer);
			}
			else
				ret = readed;
		}

		if(PHYSFS_close(fs_file) == 0)
			LOG("File System error while closing file %s: %s\n", file, PHYSFS_getLastError());
	}
	else
		LOG("File System error while opening file %s: %s\n", file, PHYSFS_getLastError());

	return ret;
}
Beispiel #11
0
static Sint64 SDL_RWopsSeek(SDL_RWops *ops, int64_t offset, int whence)
{
	PHYSFS_File *f = sdlPHYS(ops);

	if (!f)
		return -1;

	int64_t base;

	switch (whence)
	{
	default:
	case RW_SEEK_SET :
		base = 0;
		break;
	case RW_SEEK_CUR :
		base = PHYSFS_tell(f);
		break;
	case RW_SEEK_END :
		base = PHYSFS_fileLength(f);
		break;
	}

	int result = PHYSFS_seek(f, base + offset);

	return (result != 0) ? PHYSFS_tell(f) : -1;
}
Beispiel #12
0
bool j1Physfs::OpenFile(const char* file)
{
	if (0 == PHYSFS_exists(file))
	{
		return false; //file doesn't exist
	}

	PHYSFS_file* myfile = PHYSFS_openRead(file);

	// Get the lenght of the file
	file_lenght = PHYSFS_fileLength(myfile);

	// Get the file data.
	file_data = new Uint32[file_lenght];

	int length_read = PHYSFS_read(myfile, file_data, 1, file_lenght);

	if (length_read != (int)file_lenght)
	{
		delete[] file_data;
		file_data = 0;
		return false;
	}

	PHYSFS_close(myfile);


	return true;
}
Beispiel #13
0
	bool FileManager::loadFile(const std::string & path, std::vector<u8>& out)
	{
		auto file = PHYSFS_openRead(path.c_str());

		if (file == nullptr)
		{
			Log::write(Log::Error, fmt::format("Error: Unable to open file for reading. {}", path));
			return false;
		}

		auto length = PHYSFS_fileLength(file);

		if (length == -1)
		{
			Log::write(Log::Error, fmt::format("Error: Unable to get file length. {}", path));
			return false;
		}

		out.resize(length + 1);
		auto num = PHYSFS_read(file, out.data(), sizeof(out[0]), length);

		if (num < length / sizeof(out[0]))
		{
			Log::write(Log::Error, fmt::format("Error: Unable to read file contents. {}", path));
			return false;
		}

		return true;
	}
Beispiel #14
0
static QImage loadQImage(char const *fileName, char const *format = nullptr)
{
	PHYSFS_file *fileHandle = PHYSFS_openRead(fileName);
	if (fileHandle == nullptr)
	{
		return {};
	}
	int64_t fileSizeGuess = PHYSFS_fileLength(fileHandle);  // PHYSFS_fileLength may return -1.
	int64_t lengthRead = 0;
	std::vector<unsigned char> data(fileSizeGuess != -1? fileSizeGuess : 16384);
	while (true)
	{
		int64_t moreRead = PHYSFS_read(fileHandle, &data[lengthRead], 1, data.size() - lengthRead);
		lengthRead += std::max<int64_t>(moreRead, 0);
		if (lengthRead < data.size())
		{
			PHYSFS_close(fileHandle);
			data.resize(lengthRead);
			QImage image;
			image.loadFromData(&data[0], data.size(), format);
			return std::move(image);
		}
		data.resize(data.size() + 16384);
	}
}
Beispiel #15
0
const char* kp::file::read(const char* filename)
{
    PHYSFS_File* handle = PHYSFS_openRead(filename);

    if(handle == NULL)
    {
        //kp::debug::error("PHYSFS_openRead(%s): %s", filename, PHYSFS_getLastError());
        
        return NULL;
    }

    // Create a buffer big enough for the file
    PHYSFS_sint64 size = PHYSFS_fileLength(handle);

    // Append an extra byte to the string so we can null terminate it
    char* buffer = new char[size+1];

    // Read the bytes
    if(PHYSFS_readBytes(handle, buffer, size) != size)
    {
        //kp::debug::error("PHYSFS_read: %s", PHYSFS_getLastError());
        
        return NULL;
    }

    // Null terminate the buffer
    buffer[size] = '\0';

    // Close the file handle
    PHYSFS_close(handle);

    return buffer;
}
Beispiel #16
0
unsigned int Audio::File::length()
{
    if (!mFile) return 0u;
    auto status = PHYSFS_fileLength(mFile);
    if (status < 0) return 0u;
    return static_cast<unsigned int>(status);
}
Beispiel #17
0
boost::shared_ptr<ResourceInfo> Resource::LoadResource(const std::string& name)
{
    boost::shared_ptr<ResourceInfo> resource_info = m_resources.LoadResource(name);

    if (resource_info.get() == NULL)
    {
        const char* name_cstr = name.c_str();

        // Check if resource exists
        if (PHYSFS_exists(name_cstr) && !PHYSFS_isDirectory(name_cstr))
        {
            // Load the resource
            PHYSFS_File* fp = PHYSFS_openRead(name_cstr);

            // Load file data into ResourceInfo class
            Sint64 length = (Sint64)PHYSFS_fileLength(fp);
            char* data = new char[length];
            PHYSFS_read(fp, (void*)data, (PHYSFS_uint32)length, 1);
            resource_info = m_resources.Construct(name, data, length);

            // Close PHYSFS_File
            PHYSFS_close(fp);
            fp = NULL;
        }
        else
        {
            BOOST_THROW_EXCEPTION(ResourceNotFoundException()
                                  << StringErrorInfo("The requested resource could not be found: " + name));
        }
    }

    return resource_info;
}
Beispiel #18
0
int
OggSoundFile::cb_seek(void* source, ogg_int64_t offset, int whence)
{
  PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source);

  switch(whence) {
    case SEEK_SET:
      if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (offset)) == 0)
        return -1;
      break;
    case SEEK_CUR:
      if(PHYSFS_seek(file, PHYSFS_tell(file) + offset) == 0)
        return -1;
      break;
    case SEEK_END:
      if(PHYSFS_seek(file, PHYSFS_fileLength(file) + offset) == 0)
        return -1;
      break;
    default:
#ifdef DEBUG
      assert(false);
#else
      return -1;
#endif
  }
  return 0;
}
static int cmd_filelength(char *args)
{
    PHYSFS_File *f;

    if (*args == '\"')
    {
        args++;
        args[strlen(args) - 1] = '\0';
    } /* if */

    f = PHYSFS_openRead(args);
    if (f == NULL)
        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
    else
    {
        PHYSFS_sint64 len = PHYSFS_fileLength(f);
        if (len == -1)
            printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
        else
            printf(" (cast to int) %d bytes.\n", (int) len);

        PHYSFS_close(f);
    } /* else */

    return(1);
} /* cmd_filelength */
void SetWindowIcon(photon_window &window, const std::string &filename){
    if(PHYSFS_exists(filename.c_str())){
        auto fp = PHYSFS_openRead(filename.c_str());
        intmax_t length = PHYSFS_fileLength(fp);
        if(length > 0){
            uint8_t *buffer = new uint8_t[length];

            PHYSFS_read(fp, buffer, 1, length);

            PHYSFS_close(fp);

            SDL_RWops *rw = SDL_RWFromMem(buffer, length);
            SDL_Surface *icon = IMG_Load_RW(rw, 1);

            if(icon == nullptr){
                PrintToLog("ERROR: icon loading failed! %s", IMG_GetError());
            }

            SDL_SetWindowIcon(window.window_SDL, icon);

            SDL_FreeSurface(icon);
            delete[] buffer;
        }else{
            PrintToLog("ERROR: Unable to open image file \"%s\"!");
        }
    }else{
        PrintToLog("ERROR: Image file \"%s\" does not exist!", filename.c_str());
    }
}
Beispiel #21
0
bool ResourceManager::copyFile(const std::string &src, const std::string &dst)
{
    PHYSFS_file *srcFile = PHYSFS_openRead(src.c_str());
    if (!srcFile)
    {
        logger->log("Read error: %s", PHYSFS_getLastError());
        return false;
    }
    PHYSFS_file *dstFile = PHYSFS_openWrite(dst.c_str());
    if (!dstFile)
    {
        logger->log("Write error: %s", PHYSFS_getLastError());
        PHYSFS_close(srcFile);
        return false;
    }

    int fileSize = PHYSFS_fileLength(srcFile);
    void *buf = malloc(fileSize);
    PHYSFS_read(srcFile, buf, 1, fileSize);
    PHYSFS_write(dstFile, buf, 1, fileSize);

    PHYSFS_close(srcFile);
    PHYSFS_close(dstFile);
    free(buf);
    return true;
}
Beispiel #22
0
char *ResourceManager::loadFile(const std::string &fileName, int &fileSize)
{
    // Attempt to open the specified file using PhysicsFS
    PHYSFS_file *file = PHYSFS_openRead(fileName.c_str());

    // If the handler is an invalid pointer indicate failure
    if (file == nullptr)
    {
        LOG_WARN("Failed to load '" << fileName << "': "
                 << PHYSFS_getLastError());
        return nullptr;
    }

    // Get the size of the file
    fileSize = PHYSFS_fileLength(file);

    // Allocate memory and load the file
    char *buffer = (char *) malloc(fileSize + 1);
    if (PHYSFS_read(file, buffer, 1, fileSize) != fileSize)
    {
        free(buffer);
        LOG_WARN("Failed to load '" << fileName << "': "
                 << PHYSFS_getLastError());
        return nullptr;
    }

    // Close the file and let the user deallocate the memory
    PHYSFS_close(file);

    // Add a trailing null character, so that the file can be used as a string
    buffer[fileSize] = 0;
    return buffer;
}
Beispiel #23
0
int FS_loadFile(lua_State *L, const char *filename) {
	char *buffer;		/* buffer for the file */
	char *entryPoint;	/* entry point of file (differs from buffer, if "#!" in the first line is skipped */
	int err;
	PHYSFS_file *Hndfile = NULL;
	PHYSFS_sint64 fileLength, size;

	if (PHYSFS_exists(filename) == 0) {
		lua_pushfstring(L, "Error loading '%s': file not found.", filename);
		return FILEIO_ERROR;
	}

	Hndfile = PHYSFS_openRead(filename); /* open file to read! */
	if (Hndfile == NULL) {
		lua_pushfstring(L, "Error while reading from '%s': %s", filename, PHYSFS_getLastError());
		return FILEIO_ERROR;
	}

	size = PHYSFS_fileLength(Hndfile);
	if (size == -1) {
		lua_pushfstring(L, "Error while determining the size of %s.", filename);
		return FILEIO_ERROR;
	}

	buffer = (char *)malloc((unsigned int)size);
	if (buffer == NULL) {
		lua_pushfstring(L, "Error loading %s: Insufficient memory available.", filename);
		return FILEIO_ERROR;
	}

	fileLength = PHYSFS_read(Hndfile, buffer, 1, (unsigned int)size);
	if (fileLength < size) {
		free(buffer);
		lua_pushfstring(L, "Error while reading from %s: %s", filename, PHYSFS_getLastError());
		return FILEIO_ERROR;
	}
	/* close the file */
	err = PHYSFS_close(Hndfile);
	if (err == 0) {
		free(buffer);
		lua_pushfstring(L, "Error closing %s: %s", filename, PHYSFS_getLastError());
		return FILEIO_ERROR;
	}
	/* skip #! if nescessary */
	entryPoint = buffer;
	if (buffer[0] == '#') {
		while ((*entryPoint != 0x0D) && (*entryPoint != 0x0A) && (*entryPoint != EOF)) {
			entryPoint++;
			fileLength--;
		}
	}
	err = luaL_loadbuffer(L, entryPoint, (size_t)fileLength, filename);
	free(buffer);
	if (err != 0) {
		/* error message is on the stack */
		return FILEIO_ERROR;
	}

	return FILEIO_SUCCESS;
}
Beispiel #24
0
//-----------------------------------------------------------------------------
//	Load Descent briefing text.
static int load_screen_text(const d_fname &filename, std::unique_ptr<char[]> &buf)
{
	int len, have_binary = 0;
	auto e = end(filename);
	auto ext = std::find(begin(filename), e, '.');
	if (ext == e)
		return (0);
	if (!d_stricmp(&*ext, ".txb"))
		have_binary = 1;
	
	auto tfile = PHYSFSX_openReadBuffered(filename);
	if (!tfile)
		return (0);

	len = PHYSFS_fileLength(tfile);
	buf = make_unique<char[]>(len + 1);
#if defined(DXX_BUILD_DESCENT_I)
	PHYSFS_read(tfile, buf.get(), 1, len);
#elif defined(DXX_BUILD_DESCENT_II)
	for (int x=0, i=0; i < len; i++, x++) {
		PHYSFS_read(tfile, &buf[x], 1, 1);
		if (buf[x] == 13)
			x--;
	}
#endif

	if (have_binary)
		decode_text(buf.get(), len);

	buf[len] = '\0';

	return (1);
}
Beispiel #25
0
/* The hackishness level is quite low, but to get perfect, here is my personal wishlist for PHYSFS:
 - mounting zip files at arbitrary locations (already in CVS, I think)
 - rename support
 - a better API for stat() infos
 - more stdio-like API for seek, open and truncate
 - perhaps a ramdisk as write dir?
*/
PHYSFS_sint64 PHYSFS_fileLength(const char *name) {
	PHYSFS_file *f = PHYSFS_openRead(name);
	if (f == NULL) return 0;
	PHYSFS_sint64 size = PHYSFS_fileLength(f);
	PHYSFS_close(f);
	return size;
}
Beispiel #26
0
int FS_ReadFile(const char *path, void **buffer) {
	auto f = PHYSFS_openRead(path);

	if (f == nullptr) {
		return -1;
	}

	auto sz = PHYSFS_fileLength(f);

	if (buffer == nullptr) {
		return (int)sz;
	}
	
	*buffer = malloc((size_t)sz+1);
	memset(*buffer, 0, (size_t)sz + 1);

	auto read_sz = PHYSFS_read(f, *buffer, (PHYSFS_uint32)1, (PHYSFS_uint32)sz);

	if (read_sz == -1) {
		auto lastErr = PHYSFS_getLastError();
		Con_Printf("FS err: %s", lastErr);
	}

	PHYSFS_close(f);

	return (int)read_sz;
}
Beispiel #27
0
uint FileStream::size()
{
    if(!m_caching)
        return PHYSFS_fileLength(m_fileHandle);
    else
        return m_data.size();
}
static int funcSeek(struct SDL_RWops* context, int offset, int whence)
{
    PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
    int res;
    switch(whence) {
        case SEEK_SET:
            res = PHYSFS_seek(file, offset);
            break;
        case SEEK_CUR:
            res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
            break;
        case SEEK_END:
            res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
            break;
        default:
            res = 0;
            assert(false);
            break;
    }
    if(res == 0) {
        std::cerr << "Error seeking in file: " << PHYSFS_getLastError() << "\n";
        return -1;
    }

    return (int) PHYSFS_tell(file);
}
Beispiel #29
0
//Open a file for reading, set up a buffer
PHYSFS_file *PHYSFSX_openReadBuffered(const char *filename)
{
	PHYSFS_file *fp;
	PHYSFS_uint64 bufSize;
	char filename2[PATH_MAX];
	
	if (filename[0] == '\x01')
	{
		//FIXME: don't look in dir, only in hogfile
		filename++;
	}
	
	snprintf(filename2, sizeof(filename2), "%s", filename);
	PHYSFSEXT_locateCorrectCase(filename2);
	
	fp = PHYSFS_openRead(filename2);
	if (!fp)
		return NULL;
	
	bufSize = PHYSFS_fileLength(fp);
	while (!PHYSFS_setBuffer(fp, bufSize) && bufSize)
		bufSize /= 2;	// even if the error isn't memory full, for a 20MB file it'll only do this 8 times
	
	return fp;
}
Beispiel #30
0
//load all the text strings for Descent
void load_text()
{
	PHYSFS_file  *tfile;
	PHYSFS_file *ifile;
	int len,i, have_binary = 0;
	char *tptr;
	char *filename="descent.tex";

	if (GameArg.DbgAltTex)
		filename = GameArg.DbgAltTex;

	if ((tfile = PHYSFSX_openReadBuffered(filename)) == NULL) {
		filename="descent.txb";
		if ((ifile = PHYSFSX_openReadBuffered(filename)) == NULL) {
			Error("Cannot open file DESCENT.TEX or DESCENT.TXB");
			return;
		}
		have_binary = 1;

		len = PHYSFS_fileLength(ifile);

//edited 05/17/99 Matt Mueller - malloc an extra byte, and null terminate.
		MALLOC(text,char,len+1);

		PHYSFS_read(ifile,text,1,len);
		text[len]=0;
//end edit -MM
		PHYSFS_close(ifile);

	} else {