Beispiel #1
0
func addItem(dict core.Dict, key string, value string) {

	if key == "" {
		fmt.Println("add no key item")
		return
	}
	// item := dict.GetItem(key)
	v := dict.GetValue(key)
	if v != nil && v.ToString() == value {
		return
	}

	if value == "" {
		fmt.Printf("add no value item #%v#\n", key)
		return
	}

	item := new(tool.Item)
	*item = tool.Item{

		Key:  0,
		Desc: key,

		Value: value,
	}

	// dict.AddItem(key, item)
	dict.AddValue(key, item)
	// fmt.Printf("++++%v\n", key)
	return
}
Beispiel #2
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 #3
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
}