Example #1
0
func (e *Error) writeTo(w *bufio.Writer) error {
	if _, err := w.WriteString(e.ID.String()); err != nil {
		return err
	}

	if err := w.WriteByte(' '); err != nil {
		return err
	}

	if _, err := w.Write(ProtoResError); err != nil {
		return err
	}

	if err := w.WriteByte(' '); err != nil {
		return err
	}

	if _, err := w.WriteString(e.Message); err != nil {
		return err
	}

	if err := w.WriteByte('\n'); err != nil {
		return err
	}

	return nil
}
Example #2
0
func (e Enqued) writeTo(w *bufio.Writer) (err error) {
	if _, err = w.WriteString(e.ID.String()); err != nil {
		return err
	}

	if err = w.WriteByte(' '); err != nil {
		return err
	}

	if _, err = w.Write(ProtoResEnQed); err != nil {
		return err
	}

	if err = w.WriteByte(' '); err != nil {
		return err
	}

	if _, err = w.Write([]byte(e.Queue)); err != nil {
		return err
	}

	if err = w.WriteByte(' '); err != nil {
		return err
	}

	if _, err = w.WriteString(e.TaskID.String()); err != nil {
		return err
	}

	if err = w.WriteByte('\n'); err != nil {
		return err
	}

	return nil
}
Example #3
0
func (a Acked) writeTo(w *bufio.Writer) (err error) {
	if _, err = w.WriteString(a.ID.String()); err != nil {
		return err
	}

	if err = w.WriteByte(' '); err != nil {
		return err
	}

	if _, err = w.Write(ProtoResAcked); err != nil {
		return err
	}

	if err = w.WriteByte(' '); err != nil {
		return err
	}

	if _, err = w.Write([]byte(a.Queue)); err != nil {
		return err
	}

	if err = w.WriteByte(' '); err != nil {
		return err
	}

	if _, err = w.WriteString(a.TaskID.String()); err != nil {
		return err
	}

	if err = w.WriteByte('\n'); err != nil {
		return err
	}

	return nil
}
Example #4
0
/*
	Writes all incoming contents from [source] to a file at the given [schemaPath],
	returning all found errors to [resultError], and returning only once a value is
	received on [exit], or if the file was unable to be opened.
*/
func writeSingleFile(schemaPath string, source chan string, resultError chan error, wg *sync.WaitGroup) {

	var contents string
	var outFile *os.File
	var writer *bufio.Writer
	var err error
	var ok bool

	defer wg.Done()

	outFile, err = os.Create(schemaPath)
	if err != nil {
		resultError <- err
		return
	}

	writer = bufio.NewWriter(outFile)

	for {
		contents, ok = <-source
		if !ok {
			break
		}

		_, err = writer.Write([]byte(contents))
		if err != nil {
			resultError <- err
		}
	}

	writer.Flush()
}
Example #5
0
func (self *writePublish) write(wr *bufio.Writer) error {
	var err error
	var protocol string

	protocol = fmt.Sprintf("PUB %s", self.Subject)

	if len(self.ReplyTo) > 0 {
		protocol += fmt.Sprintf(" %s", self.ReplyTo)
	}

	protocol += fmt.Sprintf(" %d\r\n", len(self.Message))

	_, err = wr.WriteString(protocol)
	if err != nil {
		return err
	}

	_, err = wr.Write(self.Message)
	if err != nil {
		return err
	}

	_, err = wr.WriteString("\r\n")
	if err != nil {
		return err
	}

	return nil
}
Example #6
0
func writeReqToBuf(buf *bufio.Writer, req *Request) {
	//写入参数个数
	argsCnt := len(req.args) + 1
	buf.WriteByte(array_byte)
	buf.WriteString(strconv.Itoa(argsCnt))
	buf.Write(cr_lf)

	//写入命令
	buf.WriteByte(size_byte)
	buf.WriteString(strconv.Itoa(len(req.cmd)))
	buf.Write(cr_lf)
	buf.WriteString(req.cmd)
	buf.Write(cr_lf)

	//写入参数
	for _, arg := range req.args {
		str := fmt.Sprint(arg)
		buf.WriteByte(size_byte)
		buf.WriteString(strconv.Itoa(len(str)))
		buf.Write(cr_lf)

		buf.WriteString(str)
		buf.Write(cr_lf)
	}
}
Example #7
0
func writeLines(lines []*lineInfo, out *bufio.Writer) (err error) {
	var size int
	var line []byte

	for _, item := range lines {
		// prepare the line
		size = item.indent + len(item.body)
		line = make([]byte, size+len(crlf))

		for i := 0; i < item.indent; i++ {
			line[i] = ' ' // proper indent
		}
		copy(line[item.indent:], item.body)
		copy(line[size:], crlf)

		// write to buffer
		_, err = out.Write(line)
		if err != nil {
			return err
		}
	}

	err = out.Flush()
	return
}
Example #8
0
func WriteBytes(b *bufio.Writer, v []byte) error {
	err := b.WriteByte('$')
	_, err = b.WriteString(strconv.Itoa(len(v)))
	err = b.WriteByte(' ')
	_, err = b.Write(v)
	return err
}
Example #9
0
// sendResponse send resp to client, sendResponse must be goroutine safe.
func (server *Server) writeTCPResponse(wr *bufio.Writer, proto *Proto) (err error) {
	if Conf.Debug {
		log.Debug("write proto: %v", proto)
	}
	if err = ioutil.WriteBigEndianInt32(wr, int32(rawHeaderLen)+int32(len(proto.Body))); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt16(wr, rawHeaderLen); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt16(wr, proto.Ver); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt32(wr, proto.Operation); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt32(wr, proto.SeqId); err != nil {
		return
	}
	if proto.Body != nil {
		if _, err = wr.Write(proto.Body); err != nil {
			return
		}
	}
	if err = wr.Flush(); err != nil {
		log.Error("tcp wr.Flush() error(%v)", err)
	}
	proto.Reset()
	return
}
Example #10
0
func SendHandshakeResponse(writer *bufio.Writer, status byte) (err error) {
	_, err = writer.Write([]byte{0x05, status})
	if err != nil {
		return
	}
	return writer.Flush()
}
Example #11
0
// Write Optional param
func (op *pduOptParam) write(w *bufio.Writer) (err os.Error) {
	// Create byte array
	p := make([]byte, 4+op.length)
	copy(p[0:2], packUint(uint64(op.tag), 2))
	copy(p[2:4], packUint(uint64(op.length), 2))
	// Determine data type of value
	v := reflect.NewValue(op.value)
	switch t := v.(type) {
	case *reflect.StringValue:
		copy(p[4:op.length], []byte(op.value.(string)))
	case *reflect.Uint8Value:
		p[4] = byte(op.value.(uint8))
	case *reflect.Uint16Value:
		copy(p[4:6], packUint(uint64(op.value.(uint16)), 2))
	case *reflect.Uint32Value:
		copy(p[4:8], packUint(uint64(op.value.(uint32)), 4))
	}
	// Write to buffer
	_, err = w.Write(p)
	if err != nil {
		return
	}
	// Flush write buffer
	err = w.Flush()
	return
}
Example #12
0
func HandshakeSample(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	// Send C0+C1
	err = bw.WriteByte(0x03)
	c1 := CreateRandomBlock(RTMP_SIG_SIZE)
	for i := 0; i < 8; i++ {
		c1[i] = 0
	}
	bw.Write(c1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C0+C1")
	// Read S0+S1+S2
	s0, err := br.ReadByte()
	CheckError(err, "Handshake() Read S0")
	if s0 != 0x03 {
		return errors.New(fmt.Sprintf("Handshake() Got S0: %x", s0))
	}
	s1 := make([]byte, RTMP_SIG_SIZE)
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S1")
	bw.Write(s1)
	err = bw.Flush()
	CheckError(err, "Handshake() Flush C2")
	_, err = io.ReadAtLeast(br, s1, RTMP_SIG_SIZE)
	CheckError(err, "Handshake() Read S2")
	return
}
Example #13
0
// Write writes request to w.
//
// Write doesn't flush request to w for performance reasons.
func (req *Request) Write(w *bufio.Writer) error {
	if len(req.Header.Host()) == 0 {
		uri := req.URI()
		host := uri.Host()
		if len(host) == 0 {
			return errRequestHostRequired
		}
		req.Header.SetHostBytes(host)
		req.Header.SetRequestURIBytes(uri.RequestURI())
	}

	body := req.body
	var err error
	if req.multipartForm != nil && len(req.body) == 0 {
		body, err = marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)
		if err != nil {
			return fmt.Errorf("error when marshaling multipart form: %s", err)
		}
		req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
	}

	hasBody := !req.Header.noBody()
	if hasBody {
		req.Header.SetContentLength(len(body))
	}
	if err = req.Header.Write(w); err != nil {
		return err
	}
	if hasBody {
		_, err = w.Write(body)
	} else if len(body) > 0 {
		return fmt.Errorf("Non-zero body for non-POST request. body=%q", body)
	}
	return err
}
Example #14
0
// Write Bind Response PDU
func (pdu *PDUBindResp) write(w *bufio.Writer) (err os.Error) {
	// Write Header
	err = pdu.Header.write(w)
	if err != nil {
		err = os.NewError("Bind Response: Error writing Header")
		return
	}
	// Create byte array the size of the PDU
	p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
	pos := 0
	// Copy system id
	if len(pdu.SystemId) > 0 {
		copy(p[pos:len(pdu.SystemId)], []byte(pdu.SystemId))
		pos += len(pdu.SystemId)
	}
	pos++ // Null terminator
	// Write to buffer
	_, err = w.Write(p)
	if err != nil {
		err = os.NewError("Bind Response: Error writing to buffer")
		return
	}
	// Flush write buffer
	err = w.Flush()
	if err != nil {
		err = os.NewError("Bind Response: Error flushing write buffer")
	}
	// Optional params
	err = pdu.writeOptional(w)
	if err != nil {
		err = os.NewError("Bind Response: Error writing optional params")
	}
	return
}
Example #15
0
// Write writes response to w.
//
// Write doesn't flush response to w for performance reasons.
func (resp *Response) Write(w *bufio.Writer) error {
	var err error
	if resp.bodyStream != nil {
		contentLength := resp.Header.ContentLength()
		if contentLength >= 0 {
			if err = resp.Header.Write(w); err != nil {
				return err
			}
			if err = writeBodyFixedSize(w, resp.bodyStream, contentLength); err != nil {
				return err
			}
		} else {
			resp.Header.SetContentLength(-1)
			if err = resp.Header.Write(w); err != nil {
				return err
			}
			if err = writeBodyChunked(w, resp.bodyStream); err != nil {
				return err
			}
		}
		if bsc, ok := resp.bodyStream.(io.Closer); ok {
			err = bsc.Close()
		}
		return err
	}

	resp.Header.SetContentLength(len(resp.body))
	if err = resp.Header.Write(w); err != nil {
		return err
	}
	_, err = w.Write(resp.body)
	return err
}
Example #16
0
func (*protocol) WriteEnd(w *bufio.Writer) error {
	_, err := w.Write(_CRLF)
	if err != nil {
		return err
	}
	return w.Flush()
}
Example #17
0
func readFile(path string) {
	var w *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	fmt.Fprintf(w, "read file start: %s\n", path)

	file, err := os.OpenFile(path, os.O_RDONLY, 0666) // 打开文件

	if err != nil {
		fmt.Printf("open file err: %v\n", err)
		return
	}

	defer file.Close() // 延时关闭文件

	var buff []byte = make([]byte, 1024)
	var r *bufio.Reader = bufio.NewReader(file)

	for {
		n, _ := r.Read(buff)
		if n <= 0 {
			fmt.Fprintf(w, "read file end: %s %d\n", path, n)
			return
		}

		w.Write(buff[0:n])
	}
}
Example #18
0
// sendResponse send resp to client, sendResponse must be goroutine safe.
func (server *Server) writeTCPResponse(wr *bufio.Writer, proto *Proto) (err error) {
	if Conf.Debug {
		log.Debug("write proto: %v", proto)
	}
	// if no available memory bufio.Writer auth flush response
	if err = ioutil.WriteBigEndianInt32(wr, int32(rawHeaderLen)+int32(len(proto.Body))); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt16(wr, rawHeaderLen); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt16(wr, proto.Ver); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt32(wr, proto.Operation); err != nil {
		return
	}
	if err = ioutil.WriteBigEndianInt32(wr, proto.SeqId); err != nil {
		return
	}
	if proto.Body != nil {
		if _, err = wr.Write(proto.Body); err != nil {
			return
		}
	}
	proto.Reset()
	return
}
func filterNWriteTerms(w bufio.Writer, r io.Reader,
	counts []int, thresh int) []int {

	nnzCount := 0
	scanner := bufio.NewScanner(r)
	//read lines from the file
	var currLine string
	var currWords []string

	isHeaderRead := false
	fmt.Println("start writing")
	for scanner.Scan() {
		if !isHeaderRead {
			isHeaderRead = true
			fmt.Println("writing header")
			w.WriteString(scanner.Text())
			w.WriteString("\n")
			continue
		}

		//read current line
		currLine = scanner.Text()
		currLine = strings.Trim(currLine, " ")
		if len(currLine) == 0 {
			w.Write([]byte("\n"))
			continue
		}
		//read words from line
		currWords = strings.Split(currLine, " ")
		var currWordInd int64
		//fmt.Println(currWords)
		for i := 0; i < len(currWords); i += 2 {
			currWordInd, _ = strconv.ParseInt(currWords[i], 10, 64)
			if counts[currWordInd-1] >= thresh {
				//fmt.Println(currWordInd, counts[currWordInd-1], currWords[i], currWords[i+1])
				w.WriteString(currWords[i])
				w.WriteString(" ")
				w.WriteString(currWords[i+1])
				w.WriteString(" ")
				nnzCount++
			}
		}
		w.WriteString("\n")
	}

	fmt.Println("new nnz count: ", nnzCount)

	//get all new nnz ind
	nnzs := make([]int, nnzCount)
	j := 0
	for i, count := range counts {
		if count >= thresh {
			nnzs[j] = i + 1
			j++
		}
	}

	w.Flush()
	return nnzs
}
Example #20
0
File: fml.go Project: FIPress/fml
func (f *FML) writeTo(parentKey string, writer *bufio.Writer) {
	for key, val := range f.dict {
		fullKey := key
		if len(parentKey) != 0 {
			fullKey = parentKey + "." + key
		}
		switch v := val.(type) {
		case []*FML:
			defer func() {
				writeNodeName(fullKey, writer)
				for i := 0; i < len(v); i++ {
					writer.Write([]byte{'-', ' '})
					v[i].writeTo(fullKey, writer)
				}
			}()
		case *FML:
			defer func() {
				writeNodeName(fullKey, writer)
				v.writeTo(fullKey, writer)
			}()
		default:
			//log.Println("write",key)
			writer.WriteString(key)
			writer.WriteByte(':')
			writer.WriteString(wrapVal(v))
			//writer.WriteString("after key value")
			writer.WriteByte('\n')
		}
	}
}
Example #21
0
func readAndWriteTLS(r *bufio.Reader, w *bufio.Writer, con *tls.Conn, verbose bool) <-chan bool {
	c := make(chan bool)
	go func() {
		defer func() {
			(*con).Close()
			c <- false
		}()
		for {
			buf := make([]byte, 1024)
			message, errRead := r.Read(buf)
			if errRead != nil {
				if errRead != io.EOF {
					if verbose {
						log.Println("READ ERROR: ", errRead)
					}
				}
				break
			}
			_, errWrite := w.Write(buf[:message])
			w.Flush()
			if errWrite != nil {
				if verbose {
					log.Println("WRITE ERROR: ", errWrite)
				}
				return
			}
		}
	}()
	return c
}
Example #22
0
// Write writes response to w.
//
// Write doesn't flush response to w for performance reasons.
//
// See also WriteTo.
func (resp *Response) Write(w *bufio.Writer) error {
	if resp.SkipResponse {
		return nil
	}
	sendBody := !resp.mustSkipBody()

	if resp.bodyStream != nil {
		return resp.writeBodyStream(w, sendBody)
	}

	body := resp.bodyBytes()
	bodyLen := len(body)
	if sendBody || bodyLen > 0 {
		resp.Header.SetContentLength(bodyLen)
	}
	if err := resp.Header.Write(w); err != nil {
		return err
	}
	if sendBody {
		if _, err := w.Write(body); err != nil {
			return err
		}
	}
	return nil
}
Example #23
0
/*
	Frame physical write.
*/
func (f *Frame) writeFrame(w *bufio.Writer, l string) error {
	// Write the frame Command
	if _, e := w.WriteString(f.Command + "\n"); e != nil {
		return e
	}
	// Content length - Always add it if client does not suppress it and
	// does not supply it.
	if _, ok := f.Headers.Contains("suppress-content-length"); !ok {
		if _, clok := f.Headers.Contains("content-length"); !clok {
			f.Headers = append(f.Headers, "content-length", strconv.Itoa(len(f.Body)))
		}
	}
	// Write the frame Headers
	for i := 0; i < len(f.Headers); i += 2 {
		if l > SPL_10 && f.Command != CONNECT {
			f.Headers[i] = encode(f.Headers[i])
			f.Headers[i+1] = encode(f.Headers[i+1])
		}
		_, e := w.WriteString(f.Headers[i] + ":" + f.Headers[i+1] + "\n")
		if e != nil {
			return e
		}
	}
	// Write the last Header LF
	if e := w.WriteByte('\n'); e != nil {
		return e
	}
	// Write the body
	if len(f.Body) != 0 { // Foolish to write 0 length data
		if _, e := w.Write(f.Body); e != nil {
			return e
		}
	}
	return nil
}
Example #24
0
// writeTo serializes the element to the writer w.
func (e *Element) writeTo(w *bufio.Writer) {
	w.WriteByte('<')
	if e.Space != "" {
		w.WriteString(e.Space)
		w.WriteByte(':')
	}
	w.WriteString(e.Tag)
	for _, a := range e.Attr {
		w.WriteByte(' ')
		a.writeTo(w)
	}
	if len(e.Child) > 0 {
		w.WriteString(">")
		for _, c := range e.Child {
			c.writeTo(w)
		}
		w.Write([]byte{'<', '/'})
		if e.Space != "" {
			w.WriteString(e.Space)
			w.WriteByte(':')
		}
		w.WriteString(e.Tag)
		w.WriteByte('>')
	} else {
		w.Write([]byte{'/', '>'})
	}
}
Example #25
0
File: ihex.go Project: iand/go
// Function IHex.Write writes the representation of the file to the writer `wr`.
func (ix *IHex) Write(wr *bufio.Writer) (err error) {
	for start, data := range ix.areas {
		i := 0

		for i < len(data) {
			var chunk []byte

			if i+16 <= len(data) {
				chunk = data[i : i+16]
			} else {
				chunk = data[i:]
			}

			_, err := wr.Write([]byte(makeLine(0x00, start, chunk)))
			if err != nil {
				return err
			}

			start += 16
		}
	}

	_, err = wr.Write([]byte(makeLine(0x01, 0, []byte{})))
	if err != nil {
		return err
	}

	return nil
}
Example #26
0
func (c *DefaultServerCodec) WriteResponse(wr *bufio.Writer, proto *Proto) (err error) {
	log.Debug("write proto: %v", proto)
	// packlen =header(16) + body
	if err = binary.Write(wr, binary.BigEndian, uint32(rawHeaderLen)+uint32(len(proto.Body))); err != nil {
		log.Error("packLen: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, rawHeaderLen); err != nil {
		log.Error("headerLen: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Ver); err != nil {
		log.Error("protoVer: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.Operation); err != nil {
		log.Error("operation: binary.Write() error(%v)", err)
		return
	}
	if err = binary.Write(wr, binary.BigEndian, proto.SeqId); err != nil {
		log.Error("seqId: binary.Write() error(%v)", err)
		return
	}
	if proto.Body != nil {
		if _, err = wr.Write(proto.Body); err != nil {
			log.Error("body: wr.Write() error(%v)", err)
			return
		}
	}
	return wr.Flush()
}
Example #27
0
func emit_allele_path_item(ofp *bufio.Writer) {

	for k := range g_allele_path_item {

		for i := 0; i < len(g_allele_path_item[k]); i++ {
			s := fmt.Sprintf("%d,%d,%d,%d,%d,%s\n",
				g_allele_path_item[k][i].AlleleId,
				g_allele_path_item[k][i].PathItemIndex,
				g_allele_path_item[k][i].SequenceId,
				g_allele_path_item[k][i].Start,
				g_allele_path_item[k][i].Length,
				g_allele_path_item[k][i].StrandIsForward)
			ofp.Write([]byte(s))
		}
	}

	return

	for allele_name := range g_allele_sequenceid_path {

		allele_id := g_allele_name_id_map[allele_name]

		for i := 0; i < len(g_allele_sequenceid_path[allele_name]); i++ {
			seqid := g_allele_sequenceid_path[allele_name][i]
			s := fmt.Sprintf("%d,%d,%d,%s,%s,%s\n", allele_id, i, seqid, "NULL", "NULL", "'TRUE'")
			ofp.Write([]byte(s))
		}

	}
}
Example #28
0
func (p *Packet) Write(w io.Writer) (err error) {
	var bw *bufio.Writer

	if w1, ok := w.(*bufio.Writer); ok {
		bw = w1
	} else {
		bw = bufio.NewWriter(w)
	}
	defer bw.Flush()

	err = binary.Write(bw, binary.LittleEndian, p.OpCode)
	if err != nil {
		return err
	}
	err = binary.Write(bw, binary.LittleEndian, p.Req)
	if err != nil {
		return err
	}
	err = binary.Write(bw, binary.LittleEndian, p.TxID)
	if err != nil {
		return err
	}
	err = binary.Write(bw, binary.LittleEndian, p.Length)
	if err != nil {
		return err
	}
	if p.Length > 0 {
		_, err = bw.Write(p.Value)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #29
0
//	writeTree writes a .sgf tree from the treeNodes array
//		w is a buffered I/O writer
//		n is the TreeNodeIdx of the root of this tree
//		needs is a bool that is true when a \n is needed
//		nMov keeps a count of moves per line.
//	writeTree first writes one node, then recursively calls writeTree
//	writeTree is only called from writeGame, which has one active call, and one that is never reached (&& false)
func (p *GameTree) writeTree(w *bufio.Writer, n TreeNodeIdx, needs bool, nMov int, nMovPerLine int) (err error) {
	defer u(tr("writeTree"))
	if needs == true {
		if nMov > 0 {
			err = w.WriteByte('\n')
			nMov = 0
		}
		err = w.WriteByte('(')
	}
	if err == nil {
		if nMov == nMovPerLine {
			err = w.WriteByte('\n')
			nMov = 0
		}
		err = w.WriteByte(';')
		// write the node
		typ := p.treeNodes[n].TNodType
		switch typ {
		case GameInfoNode:
			//           fmt.Println("writing GameInfoNode\n")
			err = p.writeProperties(w, n, true)
		case InteriorNode:
			//           fmt.Println("writing InteriorNode\n")
			err = p.writeProperties(w, n, false)
		case BlackMoveNode:
			_, err = w.WriteString("B[")
			_, err = w.Write(SGFCoords(ah.NodeLoc(p.treeNodes[n].propListOrNodeLoc), p.IsFF4()))
			err = w.WriteByte(']')
			nMov += 1
		case WhiteMoveNode:
			_, err = w.WriteString("W[")
			_, err = w.Write(SGFCoords(ah.NodeLoc(p.treeNodes[n].propListOrNodeLoc), p.IsFF4()))
			err = w.WriteByte(']')
			nMov += 1
		default:
			fmt.Println("*** unsupported TreeNodeType in writeTree")
			err = errors.New("writeTree: unsupported TreeNodeType" + strconv.FormatInt(int64(typ), 10))
			return err
		}
		if err == nil {
			// write the children
			lastCh := p.treeNodes[n].Children
			if lastCh != nilTreeNodeIdx && err == nil {
				ch := p.treeNodes[lastCh].NextSib
				chNeeds := (lastCh != ch)
				err = p.writeTree(w, ch, chNeeds, nMov, nMovPerLine)
				for ch != lastCh && err == nil {
					ch = p.treeNodes[ch].NextSib
					//					nMov += 1
					err = p.writeTree(w, ch, chNeeds, nMov, nMovPerLine)
				}
			}
			if (err == nil) && (needs == true) {
				err = w.WriteByte(')')
			}
		}
	}
	return err
}
Example #30
0
File: api.go Project: allmad/madq
func (r *Request) WriteTo(w *bufio.Writer) error {
	w.Write(rpc.FlagReq)
	w.Write(r.Method.Bytes())
	if err := rpc.WriteItems(w, r.Args); err != nil {
		return logex.Trace(err)
	}
	return nil
}