func openFile(filename string, read_write bool) (*FileInfo, error) { file_mode := os.O_RDONLY mmap_mode := mmap.RDONLY if read_write { file_mode = os.O_RDWR mmap_mode = mmap.RDWR } file, err := os.OpenFile(filename, file_mode, 0) if err != nil { return nil, errors.New(fmt.Sprintf("Error opening file: %q\n", err.Error())) } fi, err := file.Stat() if err != nil { return nil, errors.New(fmt.Sprintf("Error stat'ing file: %q\n", err.Error())) } if fi.Size() < 8 { return nil, errors.New(fmt.Sprintf("File %s is too short to be edited\n", filename)) } mm, err := mmap.Map(file, mmap_mode, 0) if err != nil { return nil, errors.New(fmt.Sprintf("Error mmap'ing file: %q\n", err.Error())) } return &FileInfo{filename: filename, bytes: mm, rw: read_write}, nil }
func main() { var err error if len(os.Args) < 2 { fmt.Printf("Usage: %s <filename> [...]\n", PROGRAM_NAME) os.Exit(1) } var files []FileInfo for i := 1; i < len(os.Args); i++ { file_path := os.Args[i] file, err := os.Open(file_path) if err != nil { fmt.Printf("Error opening file: %q\n", err.Error()) os.Exit(1) } fi, err := file.Stat() if err != nil { fmt.Printf("Error stat'ing file: %q\n", err.Error()) os.Exit(1) } if fi.Size() < 8 { fmt.Printf("File %s is too short to be edited\n", file_path) os.Exit(1) } mm, err := mmap.Map(file, mmap.RDONLY, 0) if err != nil { fmt.Printf("Error mmap'ing file: %q\n", err.Error()) os.Exit(1) } files = append(files, FileInfo{filename: path.Base(file_path), bytes: mm}) } err = termbox.Init() if err != nil { panic(err) } defer termbox.Close() style := defaultStyle() termbox.SetOutputMode(outputMode) mainLoop(files, style) }
func RunI16(filename string, ncols int, nrows int, fun func([]int16, int, int) error) error { f, e := os.Open(filename) if e != nil { return e } defer f.Close() m, e := mmap.Map(f, mmap.RDONLY, 0) if e != nil { return e } defer m.Unmap() fs := unsafeBytesAsInt16([]byte(m)) if len(fs) != ncols*nrows { return errors.New("Dimensions don't match to data size") } return fun(fs, ncols, nrows) }
func (ms *mmapStorage) Open() (err error) { ms.f, err = os.OpenFile(ms.name, os.O_RDWR, 0644) if err != nil { return err } fs, err := ms.f.Stat() if err != nil { return err } ms.length = int(fs.Size()) mm, err := mmap.Map(ms.f, mmap.RDWR, 0) if err != nil { return err } ms.mm = &mm ms.mStor = adapters.NewMemoryStorage(mm) //ms.fStor = adapters.NewFileStorage(ms.name) return nil }
func openFile(filename string) (*FileInfo, error) { file, err := os.Open(filename) if err != nil { return nil, errors.New(fmt.Sprintf("Error opening file: %q\n", err.Error())) } fi, err := file.Stat() if err != nil { return nil, errors.New(fmt.Sprintf("Error stat'ing file: %q\n", err.Error())) } if fi.Size() < 8 { return nil, errors.New(fmt.Sprintf("File %s is too short to be edited\n", filename)) } mm, err := mmap.Map(file, mmap.RDONLY, 0) if err != nil { return nil, errors.New(fmt.Sprintf("Error mmap'ing file: %q\n", err.Error())) } return &FileInfo{filename: path.Base(filename), bytes: mm}, nil }
func getMmap(f *os.File) (memmap.MMap, error) { return memmap.Map(f, memmap.RDONLY, 0) }