gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

calculate_seek.go (1115B)


      1 package rifs
      2 
      3 import (
      4 	"io"
      5 	"os"
      6 
      7 	"github.com/dsoprea/go-logging"
      8 )
      9 
     10 // SeekType is a convenience type to associate the different seek-types with
     11 // printable descriptions.
     12 type SeekType int
     13 
     14 // String returns a descriptive string.
     15 func (n SeekType) String() string {
     16 	if n == io.SeekCurrent {
     17 		return "SEEK-CURRENT"
     18 	} else if n == io.SeekEnd {
     19 		return "SEEK-END"
     20 	} else if n == io.SeekStart {
     21 		return "SEEK-START"
     22 	}
     23 
     24 	log.Panicf("unknown seek-type: (%d)", n)
     25 	return ""
     26 }
     27 
     28 // CalculateSeek calculates an offset in a file-stream given the parameters.
     29 func CalculateSeek(currentOffset int64, delta int64, whence int, fileSize int64) (finalOffset int64, err error) {
     30 	defer func() {
     31 		if state := recover(); state != nil {
     32 			err = log.Wrap(state.(error))
     33 			finalOffset = 0
     34 		}
     35 	}()
     36 
     37 	if whence == os.SEEK_SET {
     38 		finalOffset = delta
     39 	} else if whence == os.SEEK_CUR {
     40 		finalOffset = currentOffset + delta
     41 	} else if whence == os.SEEK_END {
     42 		finalOffset = fileSize + delta
     43 	} else {
     44 		log.Panicf("whence not valid: (%d)", whence)
     45 	}
     46 
     47 	if finalOffset < 0 {
     48 		finalOffset = 0
     49 	}
     50 
     51 	return finalOffset, nil
     52 }