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

120 lines
2.1 KiB
Go
Raw Normal View History

//
// gopsutil is a port of psutil(http://pythonhosted.org/psutil/).
// This covers these architectures.
2014-06-12 17:18:45 +09:00
// - linux (amd64, arm)
// - freebsd (amd64)
// - windows (amd64)
2014-04-22 09:44:22 +09:00
package gopsutil
2014-04-18 16:34:47 +09:00
import (
"bufio"
2014-08-15 19:18:30 +02:00
"errors"
2014-04-18 16:34:47 +09:00
"os"
2014-05-16 11:33:35 +09:00
"reflect"
2014-04-26 15:44:22 +09:00
"strconv"
2014-04-18 16:34:47 +09:00
"strings"
)
var NotImplementedError = errors.New("not implemented yet")
2014-04-30 16:22:54 +09:00
// readLines read contents from file and split by new line.
func readLines(filename string) ([]string, error) {
2014-04-18 16:34:47 +09:00
f, err := os.Open(filename)
if err != nil {
return []string{""}, err
}
defer f.Close()
2014-04-30 16:16:07 +09:00
var ret []string
2014-04-18 16:34:47 +09:00
r := bufio.NewReader(f)
line, err := r.ReadString('\n')
for err == nil {
ret = append(ret, strings.Trim(line, "\n"))
line, err = r.ReadString('\n')
}
2014-04-28 15:31:23 +09:00
return ret, nil
2014-04-18 16:34:47 +09:00
}
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-30 16:16:07 +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 mustParseInt32(val string) int32 {
2014-04-26 15:44:22 +09:00
vv, _ := strconv.ParseInt(val, 10, 32)
return int32(vv)
}
// Parse to uint64 without error
func mustParseUint64(val string) uint64 {
2014-04-26 15:44:22 +09:00
vv, _ := strconv.ParseInt(val, 10, 64)
return uint64(vv)
}
2014-04-29 14:59:22 +09:00
// Parse to Float64 without error
func mustParseFloat64(val string) float64 {
vv, _ := strconv.ParseFloat(val, 64)
return vv
}
2014-04-29 14:59:22 +09:00
// 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
}
2014-05-16 11:33:35 +09:00
// 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
}
func pathExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}