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

135 lines
2.6 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-11-27 10:25:14 +09:00
// ReadLines reads contents from file and splits them by new line.
// A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).
func ReadLines(filename string) ([]string, error) {
return ReadLinesOffsetN(filename, 0, -1)
}
2014-11-27 10:25:14 +09:00
// ReadLines reads contents from file and splits them by new line.
// The offset tells at which line number to start.
// The count determines the number of lines to read (starting from offset):
// n >= 0: at most n lines
// n < 0: whole file
2014-11-27 10:25:14 +09:00
func ReadLinesOffsetN(filename string, offset uint, n int) ([]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)
for i := 0; i < n+int(offset) || n < 0; i++ {
line, err := r.ReadString('\n')
if err != nil {
break
}
if i < int(offset) {
continue
}
2014-04-18 16:34:47 +09:00
ret = append(ret, strings.Trim(line, "\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
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
2014-11-27 10:32:35 +09:00
func StringContains(target []string, src string) bool {
2014-04-29 14:59:22 +09:00
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
}
2014-11-27 10:32:35 +09:00
func PathExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}