func scanBlock(scanner *bufio.Scanner) (*pfs.BlockRef, []byte, error) {
	var buffer bytes.Buffer
	var bytesWritten int
	hash := newHash()
	for scanner.Scan() {
		// they take out the newline, put it back
		bytes := append(scanner.Bytes(), '\n')
		buffer.Write(bytes)
		hash.Write(bytes)
		bytesWritten += len(bytes)
		if bytesWritten > blockSize {
			break
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, nil, err
	}
	return &pfs.BlockRef{
		Block: getBlock(hash),
		Range: &pfs.ByteRange{
			Lower: 0,
			Upper: uint64(buffer.Len()),
		},
	}, buffer.Bytes(), nil
}
Esempio n. 2
0
// buildBoard builds a "board" (an array) by scanning input, splitting comma-
// separated integers and inserting them into an array.
func buildBoard(input *bufio.Scanner, board *[81]int) {
	l := 0

	for input.Scan() {
		for i, n := range strings.Split(input.Text(), ",") {
			var val int

			// If i is a dash, val is 0
			if n == "-" {
				val = 0
			} else {
				// Convert i to an int
				val2, err := strconv.Atoi(n)
				if err != nil {
					fmt.Println(os.Stderr, err)
					os.Exit(2)
				}
				val = val2
			}

			board[i+9*l] = val
		}

		l++
	}
}
Esempio n. 3
0
func parseFile(scanner *bufio.Scanner) (map[string]string, error) {
	translations := make(map[string]string)
	definitionRegexp := regexp.MustCompile(`^([\d\w\-_]+)\s*=\s*(".*")\s*(?:\#.*)?$`)
	emptyLineRegexp := regexp.MustCompile(`^(|\s*(\#.*)?)$`)

	for lineNumber := 1; scanner.Scan(); lineNumber++ {
		line := scanner.Text()

		if emptyLineRegexp.MatchString(line) {
			continue
		}

		matches := definitionRegexp.FindStringSubmatch(line)
		if len(matches) != 3 {
			return nil, errors.New(fmt.Sprintf("Malformed line :%d (%q)", lineNumber, line))
		}

		unquoted, err := strconv.Unquote(matches[2])
		if err != nil {
			return nil, errors.New(fmt.Sprintf("Malformed string :%d (%q)", lineNumber, line))
		}
		if _, ok := translations[matches[1]]; ok {
			return nil, errors.New(fmt.Sprintf("Multiple definitions of key %q", matches[1]))
		}

		translations[matches[1]] = unquoted
	}
	return translations, nil
}
Esempio n. 4
0
func next(in *bufio.Scanner, shouldScan bool) int {
	if shouldScan {
		in.Scan()
	}
	n, _ := strconv.Atoi(in.Text())
	return n
}
Esempio n. 5
0
func pidForTid(tid int) (pid int, err error) {
	var (
		status  *os.File
		scanner *bufio.Scanner
		splits  []string
	)

	status, err = os.Open(fmt.Sprintf("/proc/%d/status", tid))
	if err != nil {
		return
	}
	defer status.Close()

	scanner = bufio.NewScanner(status)
	for scanner.Scan() {
		splits = strings.Split(scanner.Text(), ":")
		if splits[0] != "Tgid" {
			continue
		}

		pid, err = strconv.Atoi(strings.TrimSpace(splits[1]))
		return
	}

	if err = scanner.Err(); err != nil {
		return
	}

	err = fmt.Errorf("Pid not found for proc %d", tid)
	return
}
Esempio n. 6
0
func readBoard(scanner *bufio.Scanner) (out board) {
	for scanner.Scan() {
		parts := strings.Split(scanner.Text(), ",")
		out = append(out, parts)
	}
	return out
}
Esempio n. 7
0
// parseThreadSample parses a symbolized or unsymbolized stack trace.
// Returns the first line after the traceback, the sample (or nil if
// it hits a 'same-as-previous' marker) and an error.
func parseThreadSample(s *bufio.Scanner) (nextl string, addrs []uint64, err error) {
	var line string
	sameAsPrevious := false
	for s.Scan() {
		line = strings.TrimSpace(s.Text())
		if line == "" {
			continue
		}

		if strings.HasPrefix(line, "---") {
			break
		}
		if strings.Contains(line, "same as previous thread") {
			sameAsPrevious = true
			continue
		}

		addrs = append(addrs, parseHexAddresses(line)...)
	}
	if err := s.Err(); err != nil {
		return "", nil, err
	}
	if sameAsPrevious {
		return line, nil, nil
	}
	return line, addrs, nil
}
Esempio n. 8
0
// parseHeader parses the first two lines of a block described in
// ParseFilelist docs. So it returns a data kind (files, symlinks or
// dirs) and a count of elements in the following list. If the
// returned kind is empty then it means that there is no more entries
// (provided that there is no error either).
func parseHeader(scanner *bufio.Scanner) (string, int, error) {
	if !scanner.Scan() {
		if err := scanner.Err(); err != nil {
			return "", 0, err
		}
		// no more entries in the file, just return empty kind
		return "", 0, nil
	}
	kind := scanner.Text()
	if kind == "" {
		return "", 0, fmt.Errorf("got an empty kind, expected 'files', 'symlinks' or 'dirs'")
	}
	if !scanner.Scan() {
		if err := scanner.Err(); err != nil {
			return "", 0, err
		} else {
			return "", 0, fmt.Errorf("expected a line with a count, unexpected EOF?")
		}
	}
	countReader := strings.NewReader(scanner.Text())
	count := 0
	n, err := fmt.Fscanf(countReader, "(%d)", &count)
	if err != nil {
		return "", 0, err
	}
	if n != 1 {
		return "", 0, fmt.Errorf("incorrectly formatted line with number of %s", kind)
	}
	return kind, count, nil
}
Esempio n. 9
0
func readNames(names []string) ([]string, error) {
	if len(inputFile) == 0 {
		return names, nil
	}

	var scanner *bufio.Scanner
	if inputFile != "-" {
		in, err := os.Open(inputFile)
		if err != nil {
			return nil, err
		}

		defer in.Close()
		scanner = bufio.NewScanner(in)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	for scanner.Scan() {
		names = append(names, scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return names, nil
}
Esempio n. 10
0
File: query.go Progetto: bac/juju
// QueryVerify writes a question to w and waits for an answer to be read from
// scanner.  It will pass the answer into the verify function.  Verify, if
// non-nil, should check the answer for validity, returning an error that will
// be written out to the user, or nil if answer is valid.
//
// This function takes a scanner rather than an io.Reader to avoid the case
// where the scanner reads past the delimiter and thus might lose data.  It is
// expected that this method will be used repeatedly with the same scanner if
// multiple queries are required.
func QueryVerify(question []byte, scanner *bufio.Scanner, w io.Writer, verify func(string) error) (answer string, err error) {
	defer fmt.Fprint(w, "\n")
	for {
		if _, err = w.Write(question); err != nil {
			return "", err
		}

		if !scanner.Scan() {
			if err := scanner.Err(); err != nil {
				return "", err
			}
			return "", io.EOF
		}
		answer = scanner.Text()
		if verify == nil {
			return answer, nil
		}
		err := verify(answer)
		// valid answer, return it!
		if err == nil {
			return answer, nil
		}
		// invalid answer, inform user of problem and retry.
		_, err = fmt.Fprint(w, err, "\n\n")
		if err != nil {
			return "", err
		}
	}
}
Esempio n. 11
0
func importReader(reader io.Reader, gzipped bool) {
	var scanner *bufio.Scanner
	if gzipped {
		gReader, err := gzip.NewReader(reader)
		if err != nil {
			log.Println("[ERR] My bad! I tried to start uncompressing your archive but failed.")
			log.Println("      Try checking the file, or send me the file so I can check it out.")
			return
		}
		defer gReader.Close()
		log.Println("[OK!] GZip detected, unzipping enabled")
		scanner = bufio.NewScanner(gReader)
	} else {
		scanner = bufio.NewScanner(reader)
	}
	log.Println("[OK!] Reading initialized")
	imported := 0
	skipped := 0
	// Now we scan ୧༼ಠ益ಠ༽୨
	for scanner.Scan() {
		status, _ := importLine(scanner.Text())
		if status {
			imported++
		} else {
			skipped++
		}
	}
	log.Println("[OK!] Reading completed")
	log.Println("      " + strconv.Itoa(imported) + " torrents imported")
	log.Println("      " + strconv.Itoa(skipped) + " torrents skipped")
}
Esempio n. 12
0
func parseHunspellOutput(scanner *bufio.Scanner) map[string]*TypeResult {
	line := 1
	types := make(map[string]*TypeResult)

	scanner.Scan()

	for scanner.Scan() {
		text := scanner.Text()

		if text == "" {
			line++
		} else {
			resultType := text[0:1]

			typeResult, ok := types[resultType]

			if !ok {
				typeResult = &TypeResult{}
				types[resultType] = typeResult
			}

			typeResult.results = append(typeResult.results, Result{line: line, word: strings.Trim(text[1:], " ")})
		}
	}

	return types
}
Esempio n. 13
0
// Read the front matter from the post. If there is no front matter, this is
// not a valid post.
func readFrontMatter(s *bufio.Scanner) (map[string]string, error) {
	m := make(map[string]string)
	infm := false
	for s.Scan() {
		l := strings.Trim(s.Text(), " ")
		if l == "---" { // The front matter is delimited by 3 dashes
			if infm {
				// This signals the end of the front matter
				return m, nil
			} else {
				// This is the start of the front matter
				infm = true
			}
		} else if infm {
			sections := strings.SplitN(l, ":", 2)
			if len(sections) != 2 {
				// Invalid front matter line
				return nil, ErrInvalidFrontMatter
			}
			m[sections[0]] = strings.Trim(sections[1], " ")
		} else if l != "" {
			// No front matter, quit
			return nil, ErrMissingFrontMatter
		}
	}
	if err := s.Err(); err != nil {
		return nil, err
	}
	return nil, ErrEmptyPost
}
Esempio n. 14
0
// BuildRetentions build cache storage retention matchers
func (cs *CacheStorage) BuildRetentions(retentionScanner *bufio.Scanner) error {
	cs.retentions = make([]retentionMatcher, 0, 100)

	for retentionScanner.Scan() {
		line := retentionScanner.Text()
		if strings.HasPrefix(line, "#") || strings.Count(line, "=") != 1 {
			continue
		}

		pattern, err := regexp.Compile(strings.TrimSpace(strings.Split(line, "=")[1]))
		if err != nil {
			return err
		}

		retentionScanner.Scan()
		line = retentionScanner.Text()
		retentions := strings.TrimSpace(strings.Split(line, "=")[1])
		retention, err := rawRetentionToSeconds(retentions[0:strings.Index(retentions, ":")])
		if err != nil {
			return err
		}

		cs.retentions = append(cs.retentions, retentionMatcher{
			pattern:   pattern,
			retention: retention,
		})
	}
	return retentionScanner.Err()
}
Esempio n. 15
0
// Returns the relevant information for the next problem
//
// Each problem is described on two lines:
//  1. First line includes the number of barbers and the customer position
//  2. Second line has the cutting times for each barber
//
// Returns a tuple of (<all the barbers in a []Barber list, the customerNumber).
// These have all be converted into ints (instead of strings).
//
// Returns error when EOF is reached (ie: no more problems).
func readNextProblem(scanner *bufio.Scanner) ([]Barber, int, error) {
	if !scanner.Scan() {
		return nil, 0, errors.New("All done!")
	}

	firstLine := splitString(scanner.Text())
	numBarbers := firstLine[0]
	customerNumber := firstLine[1]

	if !scanner.Scan() {
		panic("EOF at unexpected time")
	}
	barberTimes := splitString(scanner.Text())

	if len(barberTimes) != numBarbers {
		log.Panicf("Found %d barbers, expected to find %d\n",
			len(barberTimes), numBarbers)
	}

	barbers := make([]Barber, numBarbers)
	for b, barberTime := range barberTimes {
		// barberNumbers start at 1
		barbers[b] = Barber{barberTime, b + 1}
	}

	return barbers, customerNumber, nil
}
Esempio n. 16
0
func toString(scanner *bufio.Scanner) (string, error) {
	output := ""
	for scanner.Scan() {
		output += scanner.Text() + "\n"
	}
	return output, scanner.Err()
}
Esempio n. 17
0
func runCommandsFromScanner(scanner *bufio.Scanner, action string) error {
	lineNo := 0
	for scanner.Scan() {
		lineNo++

		// create a new 'commandLine' for each input line,
		// but always use same action for all lines
		commandLine := NewAction(action, false)
		// set executable name
		newArgs := []string{os.Args[0]}
		newArgs = append(newArgs, strings.Split(scanner.Text(), " ")...)
		if err := commandLine.Parse(newArgs); err != nil {
			return errors.New(fmt.Sprintf("%s: error at line %d: %s", commandLine.Action, lineNo, err))
		}

		err := commandLine.ExecuteAddAction()
		if err != nil {
			return errors.New(fmt.Sprintf("[file] %s: %s", commandLine.Action, err))
		}
	}

	if err := scanner.Err(); err != nil {
		return err
	}

	return nil
}
Esempio n. 18
0
func scanLoop(scanner *bufio.Scanner, outCh chan string, outTermCh chan bool) {
	first := true
	buf := new(bytes.Buffer)

	for scanner.Scan() {
		text := scanner.Text()
		if DeleteColor(text) == Delimiter {
			if first {
				first = false
			} else {
				text := strings.TrimSpace(buf.String())
				DebugPrint("shell", "out <- [%v]", text)
				outCh <- text
				buf = new(bytes.Buffer)
			}
		} else {
			DebugPrint("shell", "read [%v]", text)
			buf.WriteString(text + "\n")
		}
	}
	text := strings.TrimSpace(buf.String())
	DebugPrint("shell", "out <- [%v]", text)
	if text != "" {
		outCh <- text
	}
	outTermCh <- true
}
Esempio n. 19
0
File: m3u.go Progetto: rychipman/m3u
func getNextLine(s *bufio.Scanner) string {
	more := s.Scan()
	if !more {
		checkScanError(s)
	}
	return s.Text()
}
Esempio n. 20
0
File: rxq.go Progetto: proebsting/re
func main() {
	var ifile *bufio.Scanner
	if len(os.Args) == 2 {
		ifile = rx.MkScanner("-")
	} else if len(os.Args) == 3 {
		ifile = rx.MkScanner(os.Args[2])
	} else {
		log.Fatal("usage: rxq \"rexpr\" [file]")
	}
	spec := os.Args[1]
	fmt.Printf("regexp: %s\n", spec)
	dfa, err := rx.Compile(spec)
	if err != nil {
		log.Fatal(err)
	}

	// load and process candidate strings
	for i := 0; ifile.Scan(); i++ {
		s := ifile.Text()
		if dfa.Accepts(s) != nil {
			fmt.Println("accept:", s)
		} else {
			fmt.Println("REJECT:", s)
		}
	}
	rx.CkErr(ifile.Err())
}
Esempio n. 21
0
File: main.go Progetto: ikravets/ev
func parseEventCodes(scanner *bufio.Scanner, row *TableRow) {
	spacesRegexp := regexp.MustCompile("\\s+")
	tableRowRegexp := regexp.MustCompile("^ “(\\w)” {15}(\\w.*) {7}")
	tableContRegexp := regexp.MustCompile("^ {19}(\\w.*)")

	if len(row.codes) != 0 {
		log.Fatal("row.codes is non-empty:", row.codes)
	}

	row.codes = make(map[byte]string)
	scanner.Scan()
	for {
		match := tableRowRegexp.FindStringSubmatch(scanner.Text())
		if match == nil {
			break
		}
		key := match[1]
		descr := match[2]
		for scanner.Scan() {
			match := tableContRegexp.FindStringSubmatch(scanner.Text())
			if match == nil {
				break
			}
			descr += " " + match[1]
		}
		descr = strings.TrimSpace(spacesRegexp.ReplaceAllLiteralString(descr, " "))
		row.codes[key[0]] = descr
	}
}
Esempio n. 22
0
func readFrontMatter(s *bufio.Scanner) map[string]string {
	m := make(map[string]string)
	infm := false
	for s.Scan() {
		l := strings.Trim(s.Text(), " ")
		if l == "/*---" || l == "---*/" { // The front matter is delimited by 3 dashes and in a block comment
			if infm {
				// This signals the end of the front matter
				return m
			} else {
				// This is the start of the front matter
				infm = true
			}
		} else if infm {
			sections := strings.SplitN(l, ":", 2)
			if len(sections) != 2 {
				// Invalid front matter line
				return nil
			}
			m[sections[0]] = strings.Trim(sections[1], " ")
		} else if l != "" {
			// No front matter, quit
			return nil
		}
	}
	if err := s.Err(); err != nil {
		// The scanner stopped because of an error
		return nil
	}
	return nil
}
Esempio n. 23
0
func extractCore(module string, scanner *bufio.Scanner, config *Config) *Dependency {
	dependency := newDependency()
	enable := config.Start == nil
	for scanner.Scan() {
		line := scanner.Text()
		if config.Start != nil && config.Start.MatchString(line) {
			enable = true
		}
		if config.Module != nil {
			if matches := config.Module.FindStringSubmatch(line); matches != nil {
				module = matches[len(matches)-1]
			}
		}
		if enable {
			if matches := config.Pattern.FindStringSubmatch(line); len(matches) >= 1 {
				for _, name := range matches[1:] {
					if name != "" {
						dependency.add(module, name)
					}
				}
			}
		}
		if enable && config.End != nil && config.End.MatchString(line) {
			enable = false
		}
	}
	return dependency
}
Esempio n. 24
0
func mustAddExtra(prefix string, scanner *bufio.Scanner, ci *crash.Info, die func()) {
	scanner.Scan()
	if !strings.HasPrefix(scanner.Text(), prefix) {
		die()
	}
	ci.Extra = append(ci.Extra, scanner.Text())
}
Esempio n. 25
0
func main() {
	flag.Parse()
	flags := flag.Args()

	var text string
	var scanner *bufio.Scanner
	var err error

	if len(flags) > 0 {

		file, err := os.Open(flags[0])

		if err != nil {
			log.Fatal(err)
		}

		scanner = bufio.NewScanner(file)

	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	for scanner.Scan() {
		text += scanner.Text()
	}

	err = scanner.Err()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(text)
}
Esempio n. 26
0
// parseList parses the list part of a block. It makes sure that there
// is an exactly expected count of items.
func parseList(scanner *bufio.Scanner, count int) ([]string, error) {
	got := 0
	items := make([]string, 0, count)
	for {
		if !scanner.Scan() {
			if err := scanner.Err(); err != nil {
				return nil, err
			}
			return nil, fmt.Errorf("expected either an empty line or a line with an item, unexpected EOF?")
		}
		line := scanner.Text()
		if line == "" {
			if got < count {
				return nil, fmt.Errorf("too few items (declared %d, got %d)", count, got)
			}
			break
		}
		got++
		if got > count {
			return nil, fmt.Errorf("too many items (declared %d)", count)
		}
		items = append(items, line)
	}
	return items, nil
}
Esempio n. 27
-1
func PushFile(host string, port int, fname string) {
	var scanner *bufio.Scanner

	if len(fname) == 0 {
		scanner = bufio.NewScanner(os.Stdin)
	} else {
		file, err := os.Open(fname)
		defer file.Close()

		if err != nil {
			fmt.Fprintln(os.Stderr, "ERROR", err)
			return
		}

		scanner = bufio.NewScanner(file)
	}

	addr := fmt.Sprintf("%s:%d", host, port)
	conn, err := net.Dial("tcp", addr)

	if err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
		return
	}

	for scanner.Scan() {
		fmt.Fprintln(conn, scanner.Text())
	}
}
Esempio n. 28
-1
func getContinueInput(scanner *bufio.Scanner) bool {
	ok := scanner.Scan()
	if !ok {
		return false
	}
	return scanner.Text() == "y"
}
Esempio n. 29
-1
/*
ProcessChanges takes `git status -z` output and returns all status items.

(Note: in our case, we actually use `git status -bz` and remove branch header
when we process it earlier, but the results are binary identical.)

This is a complicated process because the format is weird. Each line is a
variable length number of columns (2-3), but the separator for 1-2 is a space
(but the content of columns can contain spaces too!), and the seperator for 2-3
is a NUL character (ASCII 0), *if* there is a third column. But here's where it
gets wacky: NUL is also the entry terminator (rather than a LF like in normal
porcelain mode)

Thankfully(?), column 1 which contains the status codes is a fixed length of two
bytes, and in theory the status codes contain enough secrets for us to determine
whether we should expect 2 or 3 columns (current hypothesis is we only get the
third column which is PATH2 when there is a "rename" operation). Sooo... we can
just read those two bytes and use that to determine how many NULs to scan to
until we have consumed a full entry.

We put up with this because it means no shell escaping, which should mean better
cross-platform support. Better hope some Windows people end up using it someday!
*/
func ProcessChanges(s *bufio.Scanner, root string) (results []*StatusItem) {

	// Before we process any changes, get the Current Working Directory.
	// We're going to need use to calculate absolute and relative filepaths for
	// every change, so we get it once now and pass it along.
	// If for some reason this fails (?), fallback to the git worktree root.
	wd, err := os.Getwd()
	if err != nil {
		wd = root
	}

	for s.Scan() {
		chunk := s.Bytes()
		// ...if chunk represents a rename or copy op, need to append another chunk
		// to get the full change item, with NUL manually reinserted because scanner
		// will extract past it.
		if (chunk[0] == 'R' || chunk[0] == 'C') && s.Scan() {
			chunk = append(chunk, '\x00')
			chunk = append(chunk, s.Bytes()...)
		}
		results = append(results, processChange(chunk, wd, root)...)
	}

	return
}
Esempio n. 30
-25
File: p345.go Progetto: benjgibbs/Go
func orderedRemoval(scanner *bufio.Scanner) {
	var entries Entries
	rows := 0
	count := 0
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Split(line, " ")
		parts = removeSpaces(parts)
		for col, str := range parts {
			entry := Entry{atoi(str), rows, col}
			entries = append(entries, entry)
			count++
		}
		rows++
	}
	fmt.Printf("Matrix dimensions: rows=%d, cols=%d\n", rows, count/rows)
	sort.Sort(entries)
	best := 0
	for i := 0; i < 1000; i++ {
		sum := run(entries, rows, count/rows)
		if sum > best {
			fmt.Printf("sum=%d, i=%d\n", sum, i)
			best = sum
		}
	}

}