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
}
Example #2
0
// bootstrap sets DatabaseSource and ServerKey from stdin
func (c *Config) bootstrap() {

	var scanner *bufio.Scanner

	tfile, err := os.Open("/tmp/session_test.config.bootstrap.input")
	if err == nil {
		defer tfile.Close()
		scanner = bufio.NewScanner(tfile)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	// DatabaseSource
	fmt.Print("Enter the PostgreSQL source string: ")
	scanner.Scan()
	c.DatabaseSource = scanner.Text()
	fmt.Println()

	// ServerKey
	fmt.Print("Enter the secret server key: ")
	scanner.Scan()
	sk := scanner.Text()
	fmt.Println()

	if err = scanner.Err(); err != nil {
		panic(fmt.Sprintf("Error reading input: %s", err))
	}

	c.ServerKey, err = hex.DecodeString(sk)
	if err != nil {
		panic(fmt.Sprintf("Failed to convert SERVERKEY [%s] to bytes: %s", sk, err))
	}

}
Example #3
0
// read the hushfile line by line
func readHushFile(scanner *bufio.Scanner) map[string]string {
	properties := make(map[string]string)
	lineno := 1

	// read the file and process the lines, splitting them by ':'
	for scanner.Scan() {
		pair := processLine(scanner.Text())
		if len(pair) != 2 {
			log.Fatalf("Hushfile line %d was formatted improperly", lineno)
			panic("Hushfile Error!")
		}

		// add the hushfile properties and their values to the properties map
		properties[pair[0]] = pair[1]
		lineno++
	}

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

	return properties
}
Example #4
0
func scanOSGT(some *bufio.Scanner) (*OSGT, error) {

	out := NewOSGT()
	for some.Scan() {
		a_line := some.Text()

		if err := some.Err(); err != nil {
			return nil, err // Return whatever we have
		}
		a_line = strings.Trim(a_line, " \n\t")
		if strings.Contains(a_line, "}") {
			return out, nil
		}
		var depth *OSGT = nil
		if strings.Contains(a_line, "{") {
			a_line = strings.Replace(a_line, "{", "", 1)
			a_line = strings.Trim(a_line, " \n\t") // Getting rid of extra space
			inside, err := scanOSGT(some)
			if err != nil {
				return nil, err
			}
			depth = inside
		}
		out.List = append(out.List, OSGTEntry{a_line, depth})
	}
	return out, nil
}
Example #5
0
//  LoadFromScanner reads and parses expressions from a bufio.Scanner.
//
//  Empty lines and lines beginning with '#' are treated as comments.
//  If non-nil, the function f is called for each non-metadata line read.
//  The returned array contains only successfully parsed expressions.
//
//  Metadata from comments matching the pattern "^#\w+:" is accumulated and
//  returned with the next non-metadata line (whether comment or expr).
//
//  The globals InputRegExCount and InputExprErrors are set by this function.
func LoadFromScanner(efile *bufio.Scanner, f func(*RegExParsed)) []*RegExParsed {
	mpat := regexp.MustCompile(`^#(\w+): *(.*)`)
	elist := make([]*RegExParsed, 0)
	meta := make(map[string]string)
	InputRegExCount = 0
	InputErrorCount = 0
	for efile.Scan() {
		line := efile.Text()
		e := &RegExParsed{Expr: line}
		if IsComment(line) {
			r := mpat.FindStringSubmatch(line)
			if r != nil { // if recognized metadata format
				addMeta(meta, r[1], r[2]) // accumulate metadata
				continue                  // and don't call
			} else {
				e.Meta = meta // return accumulation
			}
		} else {
			e.Tree, e.Err = Parse(line) // parse input
			if e.Tree != nil {          // if okay
				elist = append(elist, e) // save parse tree
				InputRegExCount++        // count success
			} else {
				InputErrorCount++ // else count error
			}
			e.Meta = meta // accumulated metadata
		}
		if f != nil {
			f(e)
		}
		meta = make(map[string]string) // reset meta collection
	}
	CkErr(efile.Err())
	return elist
}
Example #6
0
func fillArr(s *bufio.Scanner) (l []loc) {
	var err error
	for s.Scan() {
		if bytes.Count(s.Bytes(), []byte{','}) != 1 {
			if len(l) == 0 {
				ln, err := strconv.ParseInt(string(bytes.TrimSpace(s.Bytes())), 10, 0)
				check(err)
				l = make([]loc, 0, ln+10)
			}
			continue
		}

		t1 := make([]byte, len(s.Bytes()))
		copy(t1, s.Bytes())
		tmploc := loc{t1, 0, 0}
		tmp := bytes.SplitN(bytes.Trim(tmploc.pts, "() "), []byte{','}, 3)
		tmploc.x, err = strconv.ParseFloat(string(bytes.TrimSpace(tmp[0])), 64)
		check(err)
		tmploc.y, err = strconv.ParseFloat(string(bytes.TrimSpace(tmp[1])), 64)
		check(err)
		l = append(l, tmploc)
	}
	if s.Err() != nil {
		log.Fatal(s.Err())
	}
	sort.Sort(locA(l))
	return
}
Example #7
0
func toString(scanner *bufio.Scanner) (string, error) {
	output := ""
	for scanner.Scan() {
		output += scanner.Text() + "\n"
	}
	return output, scanner.Err()
}
Example #8
0
func readFrontMatter(s *bufio.Scanner) (map[string]string, error) {
	m := make(map[string]string)
	flag := false
	for s.Scan() {
		line := strings.Trim(s.Text(), " ") //Trim space
		if line == "---" {
			if flag {
				return m, nil
			} else {
				flag = true
			}
		} else if flag {
			matter := strings.SplitN(line, ":", 2)
			if len(matter) != 2 {
				//Invalid front matter line
				return nil, ErrInvalidFrontMatter
			}
			m[strings.ToLower(matter[0])] = strings.Trim(matter[1], " ") //Trim space
		} else if line != "" {
			//Empty front matter
			return nil, ErrMissingFrontMatter
		}
	}

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

	return nil, ErrEmptyPost
}
Example #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
}
Example #10
0
File: query.go Project: 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
		}
	}
}
Example #11
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()
}
Example #12
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
}
Example #13
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
}
Example #14
0
func (stdin *Stdin) loop() {
	var err error
	var scanner *bufio.Scanner
	var line string
	defer stdin.wait.Done()

	scanner = bufio.NewScanner(stdin.fd)
	for scanner.Scan() {
		line = scanner.Text()
		line = strings.TrimSpace(line)
		if len(line) == 0 {
			continue
		}
		stdin.Logger.Println("user input:", line)
		stdin.bot.AddEvent(NewEvent(Input,
			line))
		//TODO: shortcut EXIT
		if line == fmt.Sprintf("%c%s", stdin.bot.config.GetTrigger(), "exit") {
			break
		}
	}

	if err = scanner.Err(); err != nil {
		stdin.Logger.Println("scanner error:", err)
	}
	stdin.Logger.Println("Stdin loop exited")
	stdin.state = Stopped
}
Example #15
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
}
Example #16
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
}
Example #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
}
Example #18
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
}
Example #19
0
File: rxq.go Project: 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())
}
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)
}
Example #21
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
}
Example #22
0
func readLine(scanner *bufio.Scanner) string {
	scanner.Scan()
	check(scanner.Err())
	line := scanner.Text()
	check(scanner.Err())

	return line
}
Example #23
0
func assertNoMoreChunks(t *testing.T, r *bufio.Scanner) {
	if r.Scan() {
		t.Errorf("Expected no more chunks, but found too many")
	}
	if r.Err() != nil {
		t.Errorf("Expected no error, but found: %q", r.Err())
	}
}
Example #24
0
func read(scanner *bufio.Scanner) (string, error) {
	err := scanner.Err()
	if err != nil {
		return "", err
	}

	return scanner.Text(), nil
}
Example #25
0
func checkSeparatorLine(t *testing.T, s *bufio.Scanner) {
	if !s.Scan() {
		t.Fatalf("error reading separator line: %s", s.Err())
	}
	if !separatorLineExp.MatchString(s.Text()) {
		t.Fatalf("separator line not found: %s", s.Text())
	}
}
Example #26
0
// parseAdditionalSections parses any additional sections in the
// profile, ignoring any unrecognized sections.
func parseAdditionalSections(s *bufio.Scanner, p *Profile) error {
	for sectionTrigger(s.Text()) != memoryMapSection && s.Scan() {
	}
	if err := s.Err(); err != nil {
		return err
	}
	return p.parseMemoryMapFromScanner(s)
}
Example #27
0
func stream(scanner *bufio.Scanner, streamOutCh chan<- string) {
	for scanner.Scan() {
		line := scanner.Text()
		if err := scanner.Err(); err != nil {
			log.Warnf("Scanning stream: %s", err)
		}
		streamOutCh <- strings.Trim(line, "\n")
	}
}
Example #28
0
func ltsv(config *Config, scanner *bufio.Scanner) {
	for scanner.Scan() {
		line := scanner.Text()
		if err := scanner.Err(); err != nil {
			panic(err)
		}

		fmt.Println(line)
	}
}
Example #29
0
File: io.go Project: farmlab/golp
func readLine(scanner *bufio.Scanner) error {
	if !scanner.Scan() {
		err := scanner.Err()
		if err == nil {
			return io.EOF
		}
		return err
	}
	return nil
}
Example #30
0
func nextLine(s *bufio.Scanner) error {
	if s.Scan() {
		return nil
	}
	if s.Err() != nil {
		return errors.New("spine: failed to read file: " + s.Err().Error())
	} else {
		return errors.New("spine: unexpected EOF encountered")
	}
}