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

import (
	"bufio"
L
Lukas Lueg 已提交
11
	"errors"
W
WAKAYAMA Shirou 已提交
12
	"os"
13
	"reflect"
W
WAKAYAMA Shirou 已提交
14
	"strconv"
W
WAKAYAMA Shirou 已提交
15 16 17
	"strings"
)

18 19
var NotImplementedError = errors.New("not implemented yet")

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

S
Shirou WAKAYAMA 已提交
28
	var ret []string
W
WAKAYAMA Shirou 已提交
29 30 31 32 33 34 35 36

	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 已提交
37
	return ret, nil
W
WAKAYAMA Shirou 已提交
38
}
S
Shirou WAKAYAMA 已提交
39

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

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

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

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

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

81 82 83 84 85 86 87 88 89
// 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
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

// 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
}
113 114 115 116 117 118 119

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