mpvfs

9P file server for controlling mpv playback

git clone https://git.8pit.net/mpvfs.git

 1package playlistfs
 2
 3import (
 4	"bufio"
 5	"bytes"
 6)
 7
 8type Fields []string
 9
10func parseFields(buf []byte, min int, max int) ([]Fields, error) {
11	lineRdr := bytes.NewBuffer(buf)
12	lineScr := bufio.NewScanner(lineRdr)
13
14	var result []Fields
15	for lineScr.Scan() {
16		fieldRdr := bytes.NewBuffer(lineScr.Bytes())
17		fieldScr := bufio.NewScanner(fieldRdr)
18		fieldScr.Split(bufio.ScanWords)
19
20		var fields Fields
21		for fieldScr.Scan() {
22			field := fieldScr.Text()
23			fields = append(fields, field)
24		}
25
26		err := fieldScr.Err()
27		if err != nil {
28			return []Fields{}, err
29		}
30
31		numFields := len(fields)
32		if numFields < min {
33			return []Fields{}, ErrTooFewArgs
34		} else if max != -1 && numFields > max {
35			return []Fields{}, ErrTooManyArgs
36		}
37
38		result = append(result, fields)
39	}
40
41	err := lineScr.Err()
42	if err != nil {
43		return []Fields{}, err
44	}
45
46	return result, nil
47}