示例#1
0
文件: job.go 项目: callistoaz/3
// return file content as string, no errors
func httpfsRead(fname string) string {
	data, err := httpfs.Read(fname)
	if err != nil {
		return ""
	}
	return string(data)
}
示例#2
0
文件: script.go 项目: jsampaio/3
func CompileFile(fname string) (*script.BlockStmt, error) {
	bytes, err := httpfs.Read(fname)
	if err != nil {
		return nil, err
	}
	return World.Compile(string(bytes))
}
示例#3
0
文件: plot.go 项目: kyeongdong/3
func (g *guistate) servePlot(w http.ResponseWriter, r *http.Request) {

	out := []byte{}

	// handle error and return wheter err != nil.
	handle := func(err error) bool {
		if err != nil {
			w.Write(emptyIMG())
			g.Set("plotErr", err.Error()+string(out))
			return true
		} else {
			return false
		}
	}

	// limit max processes
	atomic.AddInt32(&nPlots, 1)
	defer atomic.AddInt32(&nPlots, -1)
	if atomic.LoadInt32(&nPlots) > MAX_GNUPLOTS {
		handle(errors.New("too many gnuplot processes"))
		return
	}

	a := g.StringValue("usingx")
	b := g.StringValue("usingy")

	cmd := "gnuplot"
	args := []string{"-e", fmt.Sprintf(`set format x "%%g"; set key off; set format y "%%g"; set term svg size 480,320 fsize 10; plot "-" u %v:%v w li; set output;exit;`, a, b)}
	excmd := exec.Command(cmd, args...)

	stdin, err := excmd.StdinPipe()
	if handle(err) {
		return
	}

	stdout, err := excmd.StdoutPipe()
	if handle(err) {
		return
	}

	data, err := httpfs.Read(fmt.Sprintf(`%vtable.txt`, OD()))
	if handle(err) {
		return
	}

	err = excmd.Start()
	if handle(err) {
		return
	}
	defer excmd.Wait()

	_, err = stdin.Write(data)
	if handle(err) {
		return
	}
	err = stdin.Close()
	if handle(err) {
		return
	}

	out, err = ioutil.ReadAll(stdout)
	if handle(err) {
		return
	}

	w.Header().Set("Content-Type", "image/svg+xml")
	w.Write(out)
	g.Set("plotErr", "")
	return

}