common.go 2.0 KB
Newer Older
1 2 3 4 5 6
//
// gopsutil is a port of psutil(http://pythonhosted.org/psutil/).
// This covers these architectures.
//  - linux
//  - freebsd
//  - window
7
package gopsutil
W
WAKAYAMA Shirou 已提交
8 9 10 11

import (
	"bufio"
	"os"
12
	"reflect"
W
WAKAYAMA Shirou 已提交
13
	"strconv"
W
WAKAYAMA Shirou 已提交
14 15 16
	"strings"
)

W
WAKAYAMA shirou 已提交
17 18
// readLines read contents from file and split by new line.
func readLines(filename string) ([]string, error) {
W
WAKAYAMA Shirou 已提交
19 20 21 22 23 24
	f, err := os.Open(filename)
	if err != nil {
		return []string{""}, err
	}
	defer f.Close()

S
Shirou WAKAYAMA 已提交
25
	var ret []string
W
WAKAYAMA Shirou 已提交
26 27 28 29 30 31 32 33

	r := bufio.NewReader(f)
	line, err := r.ReadString('\n')
	for err == nil {
		ret = append(ret, strings.Trim(line, "\n"))
		line, err = r.ReadString('\n')
	}

S
Shirou WAKAYAMA 已提交
34
	return ret, nil
W
WAKAYAMA Shirou 已提交
35
}
S
Shirou WAKAYAMA 已提交
36

W
go fmt  
WAKAYAMA shirou 已提交
37
func byteToString(orig []byte) string {
S
Shirou WAKAYAMA 已提交
38
	n := -1
W
WAKAYAMA shirou 已提交
39
	l := -1
W
go fmt  
WAKAYAMA shirou 已提交
40
	for i, b := range orig {
W
WAKAYAMA shirou 已提交
41
		// skip left side null
W
go fmt  
WAKAYAMA shirou 已提交
42
		if l == -1 && b == 0 {
W
WAKAYAMA shirou 已提交
43 44
			continue
		}
W
go fmt  
WAKAYAMA shirou 已提交
45
		if l == -1 {
W
WAKAYAMA shirou 已提交
46 47 48
			l = i
		}

S
Shirou WAKAYAMA 已提交
49 50 51 52 53
		if b == 0 {
			break
		}
		n = i + 1
	}
W
go fmt  
WAKAYAMA shirou 已提交
54
	if n == -1 {
S
Shirou WAKAYAMA 已提交
55 56
		return string(orig)
	}
S
Shirou WAKAYAMA 已提交
57
	return string(orig[l:n])
S
Shirou WAKAYAMA 已提交
58
}
W
WAKAYAMA Shirou 已提交
59 60

// Parse to int32 without error
61
func mustParseInt32(val string) int32 {
W
WAKAYAMA Shirou 已提交
62 63 64 65 66
	vv, _ := strconv.ParseInt(val, 10, 32)
	return int32(vv)
}

// Parse to uint64 without error
67
func mustParseUint64(val string) uint64 {
W
WAKAYAMA Shirou 已提交
68 69 70
	vv, _ := strconv.ParseInt(val, 10, 64)
	return uint64(vv)
}
71

72
// Parse to Float64 without error
73
func mustParseFloat64(val string) float64 {
74 75 76 77
	vv, _ := strconv.ParseFloat(val, 64)
	return vv
}

78 79 80 81 82 83 84 85 86
// Check the target string slice containes src or not
func stringContains(target []string, src string) bool {
	for _, t := range target {
		if t == src {
			return true
		}
	}
	return false
}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

// get struct attributes.
// This method is used only for debugging platform dependent code.
func attributes(m interface{}) map[string]reflect.Type {
	typ := reflect.TypeOf(m)
	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
	}

	attrs := make(map[string]reflect.Type)
	if typ.Kind() != reflect.Struct {
		return nil
	}

	for i := 0; i < typ.NumField(); i++ {
		p := typ.Field(i)
		if !p.Anonymous {
			attrs[p.Name] = p.Type
		}
	}

	return attrs
}
110 111 112 113 114 115 116

func pathExists(filename string) bool {
	if _, err := os.Stat(filename); err == nil {
		return true
	}
	return false
}