Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions reader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2013 Blake Smith <blakesmith0@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -29,47 +29,47 @@ import (
"time"
)

// Provides read access to an ar archive.
// Call next to skip files
//
// Reader provides read access to an ar archive.
// Call next to skip files.
//
// Example:
// reader := NewReader(f)
// var buf bytes.Buffer
// for {
// _, err := reader.Next()
// if err == io.EOF {
// break
// }
// if err != nil {
// t.Errorf(err.Error())
// }
// io.Copy(&buf, reader)
// }

//
// reader := NewReader(f)
// var buf bytes.Buffer
// for {
// _, err := reader.Next()
// if err == io.EOF {
// break
// }
// if err != nil {
// t.Errorf(err.Error())
// }
// io.Copy(&buf, reader)
// }
type Reader struct {
r io.Reader
nb int64
r io.Reader
nb int64
pad int64
}

// Copies read data to r. Strips the global ar header.
// NewReader creates a new reader reading from r. It strips the global ar header.
func NewReader(r io.Reader) *Reader {
io.CopyN(ioutil.Discard, r, 8) // Discard global header

return &Reader{r: r}
}

func (rd *Reader) string(b []byte) string {
i := len(b)-1
i := len(b) - 1
for i > 0 && b[i] == 32 {
i--
}

return string(b[0:i+1])
return string(b[0 : i+1])
}

func (rd *Reader) numeric(b []byte) int64 {
i := len(b)-1
i := len(b) - 1
for i > 0 && b[i] == 32 {
i--
}
Expand All @@ -80,7 +80,7 @@ func (rd *Reader) numeric(b []byte) int64 {
}

func (rd *Reader) octal(b []byte) int64 {
i := len(b)-1
i := len(b) - 1
for i > 0 && b[i] == 32 {
i--
}
Expand Down Expand Up @@ -128,19 +128,19 @@ func (rd *Reader) readHeader() (*Header, error) {
return header, nil
}

// Call Next() to skip to the next file in the archive file.
// Returns a Header which contains the metadata about the
// file in the archive.
// Next skips to the next file in the archive file.
// Returns a Header which contains the metadata about the
// file in the archive. io.EOF is returned at the end of the input.
func (rd *Reader) Next() (*Header, error) {
err := rd.skipUnread()
if err != nil {
return nil, err
}

return rd.readHeader()
}

// Read data from the current entry in the archive.
// Read reads data from the current entry in the archive.
func (rd *Reader) Read(b []byte) (n int, err error) {
if rd.nb == 0 {
return 0, io.EOF
Expand Down