예제 #1
0
// Change the position in the file to the specified offset. If whence if 0, the
// offset is relative to the beggining of the file; if whence is 1, it's
// relative to the current offset; if whence is 2 it's relative to the end of
// the file. Any other value will result in an error. Returns the new offset
// and an error, if any.
func (f *File) Seek(offset int64, whence int) (int64, error) {
	if f.isdir() {
		return 0, syscall.EISDIR
	}

	newoff := offset
	switch whence {
	case 0:
		break
	case 1:
		cp, err := f.Tell()
		if err != nil {
			return newoff + cp, err
		}
		newoff += cp
	case 2:
		eof, err := f.Length()
		if err != nil {
			return eof + newoff, err
		}
		newoff += eof
	default:
		return newoff, errors.New(fmt.Sprintf("Unknown value for whence: %v", whence))
	}

	r := int64(C.PHYSFS_seek(f.cfile, C.PHYSFS_uint64(newoff)))

	if r == 0 {
		return newoff, errors.New(GetLastError())
	}

	return newoff, nil
}
예제 #2
0
// Set up buffering for a PhysicsFS file handle. The following is copied almost
// verbatim from the PhysicsFS API reference:
//
// Define an i/o buffer for a file handle. A memory block of size bytes
// will be allocated and associated with the file. For files opened for reading,
// up to size bytes are read from the file and stored in the internal
// buffer. Calls to File.Read() will pull from this buffer until it is empty,
// and then refill it for more reading. Note that compressed files, like ZIP
// archives, will decompress while buffering, so this can be handy for
// offsetting CPU-intensive operations. The buffer isn't filled until you do
// your next read. For files opened for writing, data will be buffered to memory
// until the buffer is full or the buffer is flushed. Closing a handle
// implicitly causes a flush...check your return values! Seeking, etc
// transparently accounts for buffering. You can resize an existing buffer by
// calling this function more than once on the same file. Setting the buffer
// size to zero will free an existing buffer. PhysicsFS file handles are
// unbuffered by default. Please check the return value of this function!
// Failures can include not being able to seek backwards in a read-only file
// when removing the buffer, not being able to allocate the buffer, and not
// being able to flush the buffer to disk, among other unexpected problems.
func (f *File) SetBuffer(size uint64) error {
	if f.isdir() {
		return syscall.EISDIR
	}

	if int(C.PHYSFS_setBuffer(f.cfile, C.PHYSFS_uint64(size))) != 0 {
		return nil
	}

	return errors.New(GetLastError())
}