From 221873029a6166acaba1fef37508f9f1544180cd Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 24 Nov 2017 12:21:55 -0500 Subject: clean up textproto; move types.go into libfastimport --- textproto/io.go | 151 -------------------------------------------------------- 1 file changed, 151 deletions(-) delete mode 100644 textproto/io.go (limited to 'textproto/io.go') diff --git a/textproto/io.go b/textproto/io.go deleted file mode 100644 index 09f36d7..0000000 --- a/textproto/io.go +++ /dev/null @@ -1,151 +0,0 @@ -package textproto - -import ( - "bufio" - "fmt" - "io" - "strconv" - "strings" -) - -type FIReader struct { - r *bufio.Reader - - line *string - err error -} - -func NewFIReader(r io.Reader) *FIReader { - return &FIReader{ - r: bufio.NewReader(r), - } -} - -func (fir *FIReader) ReadLine() (line string, err error) { - for len(line) <= 1 { - line, err = fir.r.ReadString('\n') - if err != nil { - return - } - } - - if strings.HasPrefix(line, "data ") { - if line[5:7] == "<<" { - // Delimited format - delim := line[7 : len(line)-1] - suffix := "\n" + delim + "\n" - - for !strings.HasSuffix(line, suffix) { - var _line string - _line, err = fir.r.ReadString('\n') - line += _line - if err != nil { - return - } - } - } else { - // Exact byte count format - var size int - size, err = strconv.Atoi(line[5 : len(line)-1]) - if err != nil { - return - } - data := make([]byte, size) - _, err = io.ReadFull(fir.r, data) - line += string(data) - } - } - return -} - -type FIWriter struct { - w io.Writer -} - -func NewFIWriter(w io.Writer) *FIWriter { - return &FIWriter{ - w: w, - } -} - -func (fiw *FIWriter) WriteLine(a ...interface{}) error { - _, err := fmt.Fprintln(fiw.w, a...) - return err -} - -func (fiw *FIWriter) WriteData(data string) error { - err := fiw.WriteLine("data", len(data)) - if err != nil { - return err - } - _, err = io.WriteString(fiw.w, data) - return err -} - -type CatBlobReader struct { - r *bufio.Reader -} - -func NewCatBlobReader(r io.Reader) *CatBlobReader { - return &CatBlobReader{ - r: bufio.NewReader(r), - } -} - -func (cbr *CatBlobReader) ReadLine() (line string, err error) { - for len(line) <= 1 { - line, err = cbr.r.ReadString('\n') - if err != nil { - return - } - } - - // get-mark : LF - // cat-blob : SP 'blob' SP LF LF - // ls : SP ('blob' | 'tree' | 'commit') SP HT LF - // ls : 'missing' SP LF - - // decide if we have a cat-blob result - if len(line) <= 46 || line[40:46] != " blob " { - return - } - for _, b := range line[:40] { - if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) { - return - } - } - // we have a cat-blob result - var size int - size, err = strconv.Atoi(line[46 : len(line)-1]) - if err != nil { - return - } - data := make([]byte, size+1) - _, err = io.ReadFull(cbr.r, data) - line += string(data[:size]) - return -} - -type CatBlobWriter struct { - w io.Writer -} - -func NewCatBlobWriter(w io.Writer) *CatBlobWriter { - return &CatBlobWriter{ - w: w, - } -} - -func (cbw *CatBlobWriter) WriteLine(a ...interface{}) error { - _, err := fmt.Fprintln(cbw.w, a...) - return err -} - -func (cbw *CatBlobWriter) WriteBlob(sha1 string, data string) error { - err := cbw.WriteLine(sha1, "blob", len(data)) - if err != nil { - return err - } - _, err = io.WriteString(cbw.w, data) - return err -} -- cgit v1.2.3-54-g00ecf