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

58 lines
901 B
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"
"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
}
}