1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-28 13:48:49 +08:00
shirou_gopsutil/common.go

71 lines
1.1 KiB
Go
Raw Normal View History

//
// gopsutil is a port of psutil(http://pythonhosted.org/psutil/).
// This covers these architectures.
// - linux
// - freebsd
// - window
2014-04-22 09:44:22 +09:00
package gopsutil
2014-04-18 16:34:47 +09:00
import (
"bufio"
"os"
2014-04-26 15:44:22 +09:00
"strconv"
2014-04-18 16:34:47 +09:00
"strings"
)
// Read contents from file and split by new line.
func ReadLines(filename string) ([]string, error) {
f, err := os.Open(filename)
if err != nil {
return []string{""}, err
}
defer f.Close()
ret := make([]string, 0)
r := bufio.NewReader(f)
line, err := r.ReadString('\n')
for err == nil {
ret = append(ret, strings.Trim(line, "\n"))
line, err = r.ReadString('\n')
}
return ret, err
}
2014-04-22 17:38:47 +09:00
2014-04-22 17:39:51 +09:00
func byteToString(orig []byte) string {
2014-04-22 17:38:47 +09:00
n := -1
2014-04-22 19:13:27 +09:00
l := -1
2014-04-23 10:33:55 +09:00
for i, b := range orig {
2014-04-22 19:13:27 +09:00
// skip left side null
2014-04-23 10:33:55 +09:00
if l == -1 && b == 0 {
2014-04-22 19:13:27 +09:00
continue
}
2014-04-23 10:33:55 +09:00
if l == -1 {
2014-04-22 19:13:27 +09:00
l = i
}
2014-04-22 17:38:47 +09:00
if b == 0 {
break
}
n = i + 1
}
2014-04-22 17:39:51 +09:00
if n == -1 {
2014-04-22 17:38:47 +09:00
return string(orig)
2014-04-22 17:39:51 +09:00
} else {
2014-04-22 19:13:27 +09:00
return string(orig[l:n])
2014-04-22 17:38:47 +09:00
}
}
2014-04-26 15:44:22 +09:00
// Parse to int32 without error
func parseInt32(val string) int32 {
vv, _ := strconv.ParseInt(val, 10, 32)
return int32(vv)
}
// Parse to uint64 without error
func parseUint64(val string) uint64 {
vv, _ := strconv.ParseInt(val, 10, 64)
return uint64(vv)
}