// ReadKVStrings reads a file of line terminated key=value pairs, and overrides any keys
// present in the file with additional pairs specified in the override parameter
435525
func ReadKVStrings(files []string, override []string) ([]string, error) {
	return readKVStrings(files, override, nil)
}
730370
func KeyValuesFromLines(content []byte) ([]Pair, error) {
	var kvs []Pair

	scanner := bufio.NewScanner(bytes.NewReader(content))
	currentLine := 0
	for scanner.Scan() {
		// Process the current line, retrieving a key/value pair if
		// possible.
		scannedBytes := scanner.Bytes()
		kv, err := KeyValuesFromLine(scannedBytes, currentLine)
		if err != nil {
			return nil, err
		}
		currentLine++

		if len(kv.Key) == 0 {
			// no key means line was empty or a comment
			continue
		}

		kvs = append(kvs, kv)
	}
	return kvs, nil
}
624506
func ReadMap(file string, isGbk bool, keyField string, out interface{}) (err error) {
	//catch panic
	defer func() {
		if rerr := recover(); rerr != nil {
			err = errors.New(fmt.Sprintf("read csv file: %v, error: %v", file, rerr))
		}
	}()

	if out == nil {
		return errors.New("Cannot remake from <nil>")
	}

	outv := reflect.ValueOf(out)

	outt := outv.Type()
	outk := outt.Kind()

	if outk != reflect.Ptr {
		return errors.New("Cannot reflect into non-pointer")
	}
	mapv := outv.Elem()
	mapt := mapv.Type()
	mapk := mapv.Kind()

	if mapk != reflect.Map {
		return errors.New("Pointer must point to a slice")
	}

	//make map
	if mapv.IsNil() {
		mapv.Set(reflect.MakeMap(mapt))
	}

	elmt := mapt.Elem()
	elmIsPtr := false
	//element is ptr
	if elmt.Kind() == reflect.Ptr{
		elmt = elmt.Elem()
		elmIsPtr = true
	}

	//map field => value
	idxs := make(map[string]int)
	for i := 0; i < elmt.NumField(); i++ {
		name := elmt.Field(i).Tag.Get("csv")
		if len(name) <= 0 {
			name = elmt.Field(i).Name
		}
		idxs[format(name)] = i
	}

	err = ReadRaw(file, isGbk, func(fields []Field) error {
		elmv := reflect.Indirect(reflect.New(elmt))
		keyi := 0
		isMatchKey := false
		for _, f := range fields {
			if len(f.Name) <= 0 {
				continue
			}
			idx, ok := idxs[format(f.Name)]
			if !ok {
				continue
			}
			if f.Name == keyField {
				keyi = idx
				isMatchKey = true
			}
			fValue := elmv.Field(idx)
			setValue(&fValue, f)
		}
		if !isMatchKey{
			return errors.New(fmt.Sprintf("Primary key not found, \"%v\" not has field name \"%v\"",file, keyField))
		}
		if elmIsPtr{
			mapv.SetMapIndex(elmv.Field(keyi), elmv.Addr())
		}else{
			mapv.SetMapIndex(elmv.Field(keyi), elmv)
		}
		return nil
	})

	return err
}
1354138
func (delimited *Delimited) ReadNS(key string) golhashmap.HashMap {
	var hmap golhashmap.HashMap
	hmap = make(golhashmap.HashMap)
	key = "key::" + key
	val := delimited.db.GetVal(key)
	if val == "" {
		return hmap
	}
	children := strings.Split(val, ",")
	for _, child := range children {

		childKey := strings.Split(child, "key::")[1]
		childVal := delimited.db.GetVal("val::" + childKey)
		if childVal != "" {
			hmap[childKey] = childVal
		}
	}
	return hmap
}
1094322
func (s *Section) KeyStrings() []string {
	list := make([]string, len(s.keyList))
	copy(list, s.keyList)
	return list
}
663499
func (z *Reader) readString() (string, error) {
	var err error
	needConv := false
	for i := 0; ; i++ {
		if i >= len(z.buf) {
			return "", ErrHeader
		}
		z.buf[i], err = z.r.ReadByte()
		if err != nil {
			return "", err
		}
		if z.buf[i] > 0x7f {
			needConv = true
		}
		if z.buf[i] == 0 {
			// Digest covers the NUL terminator.
			z.digest = crc32.Update(z.digest, crc32.IEEETable, z.buf[:i+1])

			// Strings are ISO 8859-1, Latin-1 (RFC 1952, section 2.3.1).
			if needConv {
				s := make([]rune, 0, i)
				for _, v := range z.buf[:i] {
					s = append(s, rune(v))
				}
				return string(s), nil
			}
			return string(z.buf[:i]), nil
		}
	}
}
41219
func (d *Decoder) readBytes(delim byte) (line []byte, err error) {
	line, err = d.r.ReadBytes(delim)
	if d.raw {
		d.buf = append(d.buf, line...)
	}
	d.n += len(line)
	return
}
1351951
func ReadVMX(path string) (map[string]string, error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}

	return ParseVMX(string(data)), nil
}
1130690
func ParseString(s string) (key, value string, err error) {
	s = strings.TrimSpace(s)
	if strings.HasPrefix(s, "#") {
		err = ErrCommentln
		return
	}
	if s == "" {
		err = ErrEmptyln
		return
	}
	if !strings.Contains(s, "=") {
		err = ErrInvalidln
		return
	}

	var (
		buf            bytes.Buffer
		quoteType      rune
		hasEq, inQuo   bool
		mapPos, escPos int = posKey, -1
	)

	for i, r := range s {
		if inQuo {
			if i == escPos {
				switch r {
				case 'n':
					buf.WriteString("\n")
				case 'r':
					buf.WriteString("\r")
				default:
					buf.WriteRune(r)
				}
				continue
			}
			// Mark escapes
			if r == '\\' {
				escPos = i + 1
				continue
			}
		}
		// Check for quote delimiters
		if r == '\'' || r == '"' {
			// Look for closing delimiter
			if inQuo && r == quoteType {
				inQuo = false
				// Don't parse beyond a value's terminating quote
				if mapPos == posVal {
					break
				}
				continue
			}
			// Mark quote as delimiter if at start of key/val
			if !inQuo && buf.Len() == 0 {
				quoteType = r
				inQuo = true
				continue
			}
		}
		// If we're inside quotes and not being escaped,
		// ignore certain tokens.
		if !inQuo {
			// Whitespace is ignored outside quoted content
			if unicode.IsSpace(r) {
				continue
			}
			// Hash indicates a comment segment. Cease parsing.
			if r == '#' {
				break
			}
			// If we are parsing the key, '=' indicates the key is written to buffer.
			// Save the key string, and inc pos to value parsing.
			if mapPos == posKey && r == '=' {
				key = buf.String()
				buf.Reset()
				hasEq = true
				mapPos++
				continue
			}
		}
		buf.WriteRune(r)
	}
	// If we've never encountered a '=', the line is invalid. (probably quoted incorrectly)
	if !hasEq {
		err = ErrInvalidln
		return
	}
	value = buf.String()
	// While unlikely, an unquoted value may begin with a literal quote.
	// This would trigger our escape sequence for the char, but never close the quo delimiter.
	// Prepend the quote we marked to compensate for this.
	if inQuo {
		value = string(quoteType) + value
	}
	return
}
1235766
func ReadCString(r io.Reader) (s string, err error) {
	var be [1]byte
	charBuf := be[0:1]

	var accum bytes.Buffer

	for {
		n, err := r.Read(charBuf)

		if err != nil {
			return "", err
		}

		// Handle the case of no error, yet no bytes were
		// retrieved.
		if n < 1 {
			continue
		}

		switch charBuf[0] {
		case '\000':
			return string(accum.Bytes()), nil
		default:
			accum.Write(charBuf)
		}
	}
}
701574
func parseStringSlice(str interface{}) ([]string, error) {
	switch h := str.(type) {
	case string:
		return []string{h}, nil
	case []interface{}:
		s := make([]string, len(h))
		for i, hook := range h {
			sHook, ok := hook.(string)
			if !ok {
				return nil, ErrMalformedStringArray
			}
			s[i] = sHook
		}
		return s, nil
	case nil:
		return []string{}, nil
	default:
		return nil, ErrMalformedStringArray
	}
}
642071
func (s *StateSettings) ReadSettings(key string) (map[string]interface{}, error) {
	if s, err := readSettings(s.backend.db(), s.collection, key); err != nil {
		return nil, err
	} else {
		return s.Map(), nil
	}
}
27021
func ReadNetString(r io.Reader) (data []byte, err error) {
	sizeBuf := make([]byte, 4)
	_, err = r.Read(sizeBuf)
	if err != nil {
		return nil, err
	}
	size := binary.BigEndian.Uint32(sizeBuf)
	if size == 0 {
		return nil, nil
	}
	data = make([]byte, size)
	_, err = r.Read(data)
	if err != nil {
		return nil, err
	}
	return
}
214579
func CoerceStrings(v interface{}) []string {
	switch val := v.(type) {
	case string:
		if val == "" {
			return nil
		}
		return strings.Split(val, ",")
	case []string:
		return val
	case []interface{}:
		sva := make([]string, 0)
		for _, av := range val {
			switch aval := av.(type) {
			case string:
				sva = append(sva, aval)
			default:
				sv, err := CoerceString(av)
				if err == nil && sv != "" {
					sva = append(sva, sv)
				}
			}
		}
		return sva
	}
	return []string{CoerceStringShort(v)}
}
146170
func (b *SelectStmt) ReturnStrings() ([]string, error) {
	var v []string
	_, err := b.Load(&v)
	return v, err
}
94061
func (d Data) GetStringsSplit(key string, delim string) []string {
	if !d.KeyExists(key) || len(d.Values[key]) == 0 {
		return nil
	}
	return strings.Split(d.Values[key][0], delim)
}
31862
func ReadValues(data []byte) (vals Values, err error) {
	err = yaml.Unmarshal(data, &vals)
	if len(vals) == 0 {
		vals = Values{}
	}
	return
}