File: proc_file_info.go

package info (click to toggle)
golang-github-jouyouyun-hardware 0.1.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 580 kB
  • sloc: ansic: 43; makefile: 4
file content (45 lines) | stat: -rw-r--r-- 719 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package utils

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func ProcGetByKey(filename, delim string, keySet map[string]string,
	fall bool) error {
	fr, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer fr.Close()

	l := len(keySet)
	count := 0
	scanner := bufio.NewScanner(fr)
	for scanner.Scan() {
		if count >= l && !fall {
			break
		}
		text := scanner.Text()
		if len(text) == 0 {
			continue
		}
		items := strings.SplitN(text, delim, 2)
		if len(items) != 2 {
			continue
		}
		key := strings.TrimSpace(items[0])
		_, ok := keySet[key]
		if !ok {
			continue
		}
		keySet[key] = strings.TrimSpace(items[1])
		count++
	}
	if l != count {
		return fmt.Errorf("not found all keys")
	}
	return nil
}