func populateGridFromStdin() [GRID_ROW_SIZE][GRID_COL_SIZE]int { var grid [GRID_ROW_SIZE][GRID_COL_SIZE]int i := 0 inputScanner := bufio.NewScanner(os.Stdin) for inputScanner.Scan() { j := 0 lineScanner := bufio.NewScanner(strings.NewReader(inputScanner.Text())) lineScanner.Split(bufio.ScanWords) for lineScanner.Scan() { grid[i][j], _ = strconv.Atoi(lineScanner.Text()) j++ } if j == 0 { break } i++ } return grid }
// RecordResponse writes response's status, headers and body. func (r *APIB) RecordResponse(resp *http.Response, status, headers, body []byte, wc io.WriteCloser) (err error) { indent := strings.Repeat(" ", 12) // indent headers var headersS []string s := bufio.NewScanner(bytes.NewReader(headers)) for s.Scan() { headersS = append(headersS, indent+s.Text()) } if err = s.Err(); err != nil { return } // indent body var bodyS []string s = bufio.NewScanner(bytes.NewReader(body)) for s.Scan() { bodyS = append(bodyS, indent+s.Text()) } if err = s.Err(); err != nil { return } err = apibResponseTemplate.Execute(wc, map[string]interface{}{ "StatusCode": resp.StatusCode, "Headers": strings.Join(headersS, "\n"), "Body": strings.Join(bodyS, "\n"), }) if err == nil { err = wc.Close() } return }
func MergeColumns() { f1, err := os.Open(COL1_FILENAME) if err != nil { log.Fatalln("open col1.txt error:", err) } defer f1.Close() s1 := bufio.NewScanner(f1) f2, err := os.Open(COL2_FILENAME) if err != nil { log.Fatalln("open col2.txt error:", err) } defer f2.Close() s2 := bufio.NewScanner(f2) mf, err := os.Create(MERGED_FILENAME) if err != nil { log.Fatalln("create merged.txt error:", err) } defer mf.Close() for s1.Scan() && s2.Scan() { c1 := s1.Text() c2 := s2.Text() fmt.Fprintf(mf, "%s\t%s\n", c1, c2) } }
func LogCommand(cmd *exec.Cmd, asDebug bool) { logrus.WithField("command", cmd.Args).Debugf("running command") stdout, err := cmd.StdoutPipe() if err != nil { return } stderr, err := cmd.StderrPipe() if err != nil { return } go func() { in := bufio.NewScanner(stdout) for in.Scan() { if asDebug { logrus.Debugf(in.Text()) } else { logrus.Infof(in.Text()) } } }() go func() { in := bufio.NewScanner(stderr) for in.Scan() { logrus.Debugf(in.Text()) } }() }
func runClient(addr string) { conn, err := net.Dial("tcp", addr) if err != nil { log.Fatal(err) } defer conn.Close() // Messages from the conn to stdout. // The scanner will exit when the conn is closed. // We close the conn via the defer. go func() { s := bufio.NewScanner(conn) for s.Scan() { fmt.Fprintf(os.Stdout, s.Text()+"\n") } }() // Messages from stdin to the conn. // The scanner will exit when stdin is closed. // We close stdin via ctrl-D. s := bufio.NewScanner(os.Stdin) for s.Scan() { fmt.Fprintf(conn, s.Text()+"\n") } }
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 }
func (c *Client) listen() { r := bufio.NewScanner(c) res := time.Now() for { select { case <-c.quit: c.Close() return default: c.SetReadDeadline(time.Now().Add(120 * time.Second)) if !r.Scan() { if ne, ok := r.Err().(net.Error); ok { if ne.Temporary() && ne.Timeout() && time.Since(res) < 300*time.Second { r = bufio.NewScanner(c) c.sout("PING %s", c.Host) continue } } c.err <- r.Err() return } c.psrv(r.Text()) res = time.Now() } } }
// I actually wanted to have a test containing all the words used in the // original paper but Martin Porter (author of the original algorithm) // has provided a massive collection of ~23000 words with the expected // output of the algorithm so we can test our implementation against // the original paper implementation func TestCorpus(t *testing.T) { input, errIn := os.Open("corpus/test_input.txt") output, errOut := os.Open("corpus/test_output.txt") defer input.Close() defer output.Close() if errIn != nil || errOut != nil { t.Fatalf("Could not read input or output test files [%s, %s]", errIn, errOut) } inScan := bufio.NewScanner(input) outScan := bufio.NewScanner(output) for inScan.Scan() && outScan.Scan() { in := inScan.Text() out := outScan.Text() stemmed := Stem(in) t.Logf("[PASS] Input: %s → Expected: %s, Stemmed: %s\n", in, out, stemmed) if !strings.EqualFold(out, stemmed) { t.Errorf("[FAIL] Expected %s but got %s for input %s\n", out, stemmed, in) } } if inScan.Err() != nil || outScan.Err() != nil { t.Fatalf("Could not open scanner for input or output test files [%s, %s]", inScan.Err(), outScan.Err()) } }
// Secure chat client func ExampleSecureConn_client() error { // connect to remote server c, err := net.Dial("tcp", "localhost:8080") if err != nil { return err } defer c.Close() // wrap insecure client, perform key exchange and retrieve // a secured connection sc, err := secureconn.New(c) if err != nil { return err } defer sc.Close() // read messages from the server line by line go func() { scanner := bufio.NewScanner(sc) for scanner.Scan() { fmt.Println(scanner.Text()) } }() // read messages from command line to send to server scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { _, err := sc.Write([]byte(scanner.Text() + "\n")) if err != nil { return err } } return nil }
func (tm *TestKeeper) Start() error { if tm.cmd != nil { panic(fmt.Errorf("tm: %s, cmd not cleanly stopped", tm.id)) } tm.cmd = exec.Command(tm.keeperBin, tm.args...) stdoutPipe, err := tm.cmd.StdoutPipe() if err != nil { return err } go func() { scanner := bufio.NewScanner(stdoutPipe) for scanner.Scan() { fmt.Printf("[%s]: %s\n", tm.id, scanner.Text()) } }() stderrPipe, err := tm.cmd.StderrPipe() if err != nil { return err } go func() { scanner := bufio.NewScanner(stderrPipe) for scanner.Scan() { fmt.Printf("[%s]: %s\n", tm.id, scanner.Text()) } }() err = tm.cmd.Start() if err != nil { return err } return nil }
// Creates new Trie struct loaded from filename func Load(filename string, maxLength int) (*Trie, error) { file, err := os.Open(filename) if err != nil { return nil, err } defer file.Close() cfs := make(map[rune]int) scanner := bufio.NewScanner(file) for scanner.Scan() { w := scanner.Text() if maxLength < 1 || len(w) < maxLength { CharFreqs(&cfs, w) } } t := CustomNew(cfs) if _, err := file.Seek(0, 0); err != nil { return nil, err } scanner = bufio.NewScanner(file) for scanner.Scan() { w := scanner.Text() if maxLength < 1 || len(w) < maxLength { t.AddWord(w) } } return t, nil }
func (ts *TestSentinel) Start() error { if ts.cmd != nil { panic(fmt.Errorf("ts: %s, cmd not cleanly stopped", ts.id)) } ts.cmd = exec.Command(ts.sentinelBin, ts.args...) stdoutPipe, err := ts.cmd.StdoutPipe() if err != nil { return err } go func() { scanner := bufio.NewScanner(stdoutPipe) for scanner.Scan() { fmt.Printf("[%s]: %s\n", ts.id, scanner.Text()) } }() stderrPipe, err := ts.cmd.StderrPipe() if err != nil { return err } go func() { scanner := bufio.NewScanner(stderrPipe) for scanner.Scan() { fmt.Printf("[%s]: %s\n", ts.id, scanner.Text()) } }() err = ts.cmd.Start() if err != nil { return err } return nil }
func gatherCmdOutput(taskName string, cmd *exec.Cmd, eventsChan chan *types.Event) error { stdoutReader, err := cmd.StdoutPipe() if err != nil { return err } stderrReader, err := cmd.StderrPipe() if err != nil { return err } stdoutScanner := bufio.NewScanner(stdoutReader) stderrScanner := bufio.NewScanner(stderrReader) go func() { for stdoutScanner.Scan() { eventsChan <- types.NewOutputEvent(taskName, stdoutScanner.Bytes()) } }() go func() { for stderrScanner.Scan() { eventsChan <- types.NewOutputEvent(taskName, stderrScanner.Bytes()) } }() return nil }
func main() { // EXAMPLE 1 reader := StringPump("Test") scanner := bufio.NewScanner(reader) for scanner.Scan() { fmt.Printf("Received: %s \n", scanner.Text()) } // EXAMPLE 2 reader = Capitalize(StringPump("Test")) scanner = bufio.NewScanner(reader) for scanner.Scan() { fmt.Printf("Received: %s \n", scanner.Text()) } // EXAMPLE 3 ToLower := WrapStringWordFilter(strings.ToLower) reader = ToLower(Capitalize(StringPump("Test THIS bit"))) scanner = bufio.NewScanner(reader) for scanner.Scan() { fmt.Printf("Received: %s \n", scanner.Text()) } fmt.Printf("Finished.\n") }
func (t *task) logCmd(cmd *exec.Cmd) { stderr, _ := cmd.StderrPipe() stdout, _ := cmd.StdoutPipe() go t.logPipe(bufio.NewScanner(stderr)) go t.logPipe(bufio.NewScanner(stdout)) }
func scannerFromFile(reader io.Reader) (*bufio.Scanner, error) { var scanner *bufio.Scanner //create a bufio.Reader so we can 'peek' at the first few bytes bReader := bufio.NewReader(reader) testBytes, err := bReader.Peek(64) //read a few bytes without consuming if err != nil { return nil, err } //Detect if the content is gzipped contentType := http.DetectContentType(testBytes) fmt.Printf("Content Type:%s\n", contentType) //If we detect gzip, then make a gzip reader, then wrap it in a scanner if strings.Contains(contentType, "x-gzip") { gzipReader, err := gzip.NewReader(bReader) if err != nil { return nil, err } scanner = bufio.NewScanner(gzipReader) } else { //Not gzipped, just make a scanner based on the reader scanner = bufio.NewScanner(bReader) } return scanner, nil }
func ensureSlot(slots, item string) (err error) { fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666) defer fi.Close() if err != nil { return } // ensure the slot is not already written into the capemanager // (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190) scanner := bufio.NewScanner(fi) for scanner.Scan() { line := scanner.Text() if strings.Index(line, item) > 0 { return } } _, err = fi.WriteString(item) if err != nil { return err } fi.Sync() scanner = bufio.NewScanner(fi) for scanner.Scan() { line := scanner.Text() if strings.Index(line, item) > 0 { return } } return }
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") }
func main() { d := make(map[string]string) for _, a := range os.Args[1:] { ww := strings.Split(a, ",") tag, fname := ww[0], ww[1] println(a) r, err := os.Open(fname) if err != nil { panic(fname) } sc := bufio.NewScanner(r) for sc.Scan() { line := sc.Text() GrokLine(line, tag, d) } r.Close() } r := os.Stdin sc := bufio.NewScanner(r) for sc.Scan() { line := sc.Text() tail := "" ww := strings.Split(line, " ") if len(ww) > 0 && len(ww[0]) > 0 { k := strings.ToUpper(ww[0]) if v, ok := d[k]; ok { tail = ReplaceWhite(v, " ") } } Println(line + " ;; " + tail) } }
func (s Shell) executeCommand() (err error) { outReader, err := s.Command.StdoutPipe() if err != nil { return } errReader, err := s.Command.StderrPipe() if err != nil { return } var bufout, buferr bytes.Buffer outReader2 := io.TeeReader(outReader, &bufout) errReader2 := io.TeeReader(errReader, &buferr) s.Command.Start() outScanner := bufio.NewScanner(outReader2) for outScanner.Scan() { fmt.Println(outScanner.Text()) } errScanner := bufio.NewScanner(errReader2) for errScanner.Scan() { fmt.Println("[stderr]" + errScanner.Text()) } s.Command.Wait() return }
func main() { exb := make([]byte, 0, 128*128) buf := bytes.NewBuffer(exb) f, err := os.Open("tmp.txt") if err != nil { panic(err) } s := bufio.NewScanner(f) start := make(chan int) go func() { <-start for s.Scan() { if s.Err() != nil { panic(err) } _, err := buf.WriteString(s.Text() + "\n") if err != nil { panic(err) } } }() start <- 1 s2 := bufio.NewScanner(buf) for s2.Scan() { if err := s2.Err(); err != nil { panic(err) } fmt.Println("Received line:", s2.Text()) } }
func (lbe *Executor) Start() (*bufio.Scanner, *bufio.Scanner, error) { var err error log.Debugf("Launching plugin server for driver %s", lbe.DriverName) cmd := exec.Command(lbe.binaryPath) lbe.pluginStdout, err = cmd.StdoutPipe() if err != nil { return nil, nil, fmt.Errorf("Error getting cmd stdout pipe: %s", err) } lbe.pluginStderr, err = cmd.StderrPipe() if err != nil { return nil, nil, fmt.Errorf("Error getting cmd stderr pipe: %s", err) } outScanner := bufio.NewScanner(lbe.pluginStdout) errScanner := bufio.NewScanner(lbe.pluginStderr) os.Setenv(PluginEnvKey, PluginEnvVal) os.Setenv(PluginEnvDriverName, lbe.DriverName) if err := cmd.Start(); err != nil { return nil, nil, fmt.Errorf("Error starting plugin binary: %s", err) } return outScanner, errScanner, nil }
// read lime from file and Scan func Prepare(iplist, user_dict, pass_dict string) (slice_iplist, slice_user, slice_pass []string) { iplistFile, _ := os.Open(iplist) defer iplistFile.Close() scanner := bufio.NewScanner(iplistFile) scanner.Split(bufio.ScanLines) for scanner.Scan() { slice_iplist = append(slice_iplist, scanner.Text()) } user_dictFile, _ := os.Open(user_dict) defer user_dictFile.Close() scanner_u := bufio.NewScanner(user_dictFile) scanner_u.Split(bufio.ScanLines) for scanner_u.Scan() { slice_user = append(slice_user, scanner_u.Text()) } pass_dictFile, _ := os.Open(pass_dict) defer pass_dictFile.Close() scanner_p := bufio.NewScanner(pass_dictFile) scanner_p.Split(bufio.ScanLines) for scanner_p.Scan() { slice_pass = append(slice_pass, scanner_p.Text()) } return slice_iplist, slice_user, slice_pass }
func RenderGraph(dot string) bytes.Buffer { var out bytes.Buffer unflatten := exec.Command("unflatten", "-l 10") graphviz := exec.Command("dot", "-Tsvg") reader, writer := io.Pipe() unflatten.Stdin = strings.NewReader(dot) unflatten.Stdout = writer graphviz.Stdin = reader graphviz.Stdout = &out var stderr io.ReadCloser stderr, _ = unflatten.StderrPipe() go func(stderr io.ReadCloser) { in := bufio.NewScanner(stderr) for in.Scan() { log.Print(in.Text()) } }(stderr) stderr, _ = graphviz.StderrPipe() go func(stderr io.ReadCloser) { in := bufio.NewScanner(stderr) for in.Scan() { log.Print(in.Text()) } }(stderr) unflatten.Start() graphviz.Start() unflatten.Wait() writer.Close() graphviz.Wait() return out }
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) }
func main() { lineScanner := bufio.NewScanner(os.Stdin) for lineScanner.Scan() { line := lineScanner.Text() scanner := bufio.NewScanner(strings.NewReader(line)) scanner.Split(bufio.ScanWords) var words []string for scanner.Scan() { word := scanner.Text() words = append(words, word) if len(words) == 2 { break } } if len(words) != 2 { continue } fmt.Printf("%q: ", words) if anagram(words[0], words[1]) { fmt.Println("anagrams") } else { fmt.Println("not anagrams") } } }
func Publish() { command := globalConfig.Build.Publish // Prepare exec command var shell, flag string if runtime.GOOS == "windows" { shell = "cmd" flag = "/C" } else { shell = "/bin/sh" flag = "-c" } cmd := exec.Command(shell, flag, command) cmd.Dir = filepath.Join(rootPath, "public") // Start print stdout and stderr of process stdout, _ := cmd.StdoutPipe() stderr, _ := cmd.StderrPipe() out := bufio.NewScanner(stdout) err := bufio.NewScanner(stderr) // Print stdout go func() { for out.Scan() { Log(out.Text()) } }() // Print stdin go func() { for err.Scan() { Log(err.Text()) } }() // Exec command cmd.Run() }
func init() { // Redirect the standard output and error to logcat oldStdout, oldStderr := os.Stdout, os.Stderr outRead, outWrite, _ := os.Pipe() errRead, errWrite, _ := os.Pipe() os.Stdout = outWrite os.Stderr = errWrite go func() { scanner := bufio.NewScanner(outRead) for scanner.Scan() { line := scanner.Text() C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stdout"), C.CString(line)) oldStdout.WriteString(line + "\n") } }() go func() { scanner := bufio.NewScanner(errRead) for scanner.Scan() { line := scanner.Text() C.__android_log_write(C.ANDROID_LOG_INFO, C.CString("Stderr"), C.CString(line)) oldStderr.WriteString(line + "\n") } }() }
func (lbe *LocalBinaryExecutor) Start() (*bufio.Scanner, *bufio.Scanner, error) { log.Debugf("Launching plugin server for driver %s", lbe.DriverName) binaryPath, err := exec.LookPath(fmt.Sprintf("docker-machine-driver-%s", lbe.DriverName)) if err != nil { return nil, nil, fmt.Errorf("Driver %q not found. Do you have the plugin binary accessible in your PATH?", lbe.DriverName) } log.Debugf("Found binary path at %s", binaryPath) cmd := exec.Command(binaryPath) lbe.pluginStdout, err = cmd.StdoutPipe() if err != nil { return nil, nil, fmt.Errorf("Error getting cmd stdout pipe: %s", err) } lbe.pluginStderr, err = cmd.StderrPipe() if err != nil { return nil, nil, fmt.Errorf("Error getting cmd stderr pipe: %s", err) } outScanner := bufio.NewScanner(lbe.pluginStdout) errScanner := bufio.NewScanner(lbe.pluginStderr) os.Setenv(PluginEnvKey, PluginEnvVal) if err := cmd.Start(); err != nil { return nil, nil, fmt.Errorf("Error starting plugin binary: %s", err) } return outScanner, errScanner, nil }
func init() { file, err := os.Open("/usr/share/dict/web2") if err != nil { fmt.Println("Cannot open /usr/share/dict/web2 - " + err.Error()) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { web2 = append(web2, scanner.Text()) } if err = scanner.Err(); err != nil { fmt.Println("Error reading file - " + err.Error()) } file2, err2 := os.Open("/usr/share/dict/web2a") if err2 != nil { fmt.Println("Cannot open /usr/share/dict/web2a - " + err2.Error()) } defer file2.Close() scanner = bufio.NewScanner(file2) for scanner.Scan() { web2a = append(web2a, scanner.Text()) } if err2 = scanner.Err(); err2 != nil { fmt.Println("Error reading file - " + err2.Error()) } }