Beispiel #1
0
// Format realize interface core.Trans
func (t *CSVTrans) Format(dstPath string, srcPath string, dict core.Dict) map[string]int {
	d := dict.GetData()
	nilMap := make(map[string]int)

	// file, err := os.Open(srcPath)
	// if err != nil {
	// 	fmt.Println("open file err : " + err.Error())
	// 	return nil
	// }
	dfile, sfile, err := openFiles(dstPath, srcPath)
	if err != nil {
		return nil
	}
	defer func() {
		dfile.Close()
		sfile.Close()
	}()

	r := bufio.NewReader(sfile)
	w := bufio.NewWriter(dfile)

	f := func(lineByte []byte) {

		lineStr := string(lineByte)
		t.formatLine(&lineStr, d, nilMap)
		w.WriteString(lineStr)
		// if needN {
		// 	w.WriteByte('\n')
		// }
	}

L:
	for {

		leftByte, _ := r.Peek(r.Buffered())
		lineByte, err := r.ReadSlice('\n')

		switch err {
		case nil:
			f(lineByte)
		case io.EOF:
			if len(leftByte) != 0 {
				f(lineByte)
			}
			break L
		default:
			fmt.Println("")
			break L
		}
	}

	w.Flush()

	return nilMap
}
Beispiel #2
0
func format(encoder *xml.Encoder, decoder *xml.Decoder, d core.Dict) (nilMap map[string]int) {

	// nilArr  := make([]string, 10)
	// nilCnt  := 0
	nilMap = make(map[string]int)
	dict := d.GetData()

	focus := false
	for tk, e := decoder.Token(); e == nil; tk, e = decoder.Token() {

	S:
		switch tmp := tk.(type) {

		case xml.StartElement:
			focus = tmp.Name.Local == "string"
			// fmt.Printf("token show : %v\n", tmp.Name.Space)

		// case xml.EndElement:
		case xml.CharData:
			if !focus {
				break S
			}

			content := string([]byte(tmp))
			if strings.IndexFunc(content, matchF) == -1 {
				break S
			}

			if value, ok := dict[content]; ok {

				tk = xml.CharData([]byte(value.ToString()))
			} else {

				if _, ok := nilMap[content]; !ok {

					nilMap[content] = 0
				}
			}

		default:
		}
		encoder.EncodeToken(tk)
	}

	return
}