Updated indentation and styling so that it's consistant

master
Ronald1985 4 years ago
parent fa8fc9e199
commit 4ebbdf88bb

@ -10,88 +10,89 @@ import "core:time"
import "core:unicode" import "core:unicode"
read_entire_file_from_filename :: proc(name: string, allocator := context.allocator) -> ([]byte, bool) { read_entire_file_from_filename :: proc(name: string, allocator := context.allocator) -> ([]byte, bool) {
context.allocator = allocator context.allocator = allocator
fd, err := os.open(name, os.O_RDONLY, 0) fd, err := os.open(name, os.O_RDONLY, 0)
if err != 0 { if err != 0 {
return nil, false return nil, false
} }
defer os.close(fd) defer os.close(fd)
return read_entire_file_from_handle(fd, allocator) return read_entire_file_from_handle(fd, allocator)
} }
read_entire_file_from_handle :: proc(fd: os.Handle, allocator := context.allocator) -> ([]byte, bool) { read_entire_file_from_handle :: proc(fd: os.Handle, allocator := context.allocator) -> ([]byte, bool) {
context.allocator = allocator context.allocator = allocator
length: i64 length: i64
err: os.Errno err: os.Errno
if length, err = os.file_size(fd); err != 0 { if length, err = os.file_size(fd); err != 0 {
return nil, false return nil, false
} }
BLOCK_SIZE :: 4096 BLOCK_SIZE :: 4096
length = max(length, BLOCK_SIZE) length = max(length, BLOCK_SIZE)
_data: [dynamic]byte _data: [dynamic]byte
read_err: os.Errno read_err: os.Errno
bytes_read, bytes_total: int bytes_read, bytes_total: int
resize(&_data, int(length)) resize(&_data, int(length))
for { for {
bytes_read, read_err = os.read(fd, _data[bytes_total:]) bytes_read, read_err = os.read(fd, _data[bytes_total:])
if bytes_read == 0 { if bytes_read == 0 {
break break
} }
bytes_total += bytes_read bytes_total += bytes_read
resize(&_data, bytes_total + BLOCK_SIZE) resize(&_data, bytes_total + BLOCK_SIZE)
} }
return _data[:bytes_total], true return _data[:bytes_total], true
} }
get_key :: proc(s: string) -> (string, bool) { get_key :: proc(s: string) -> (string, bool) {
if len(s) > 1 && s[len(s) - 1] == ':' { if len(s) > 1 && s[len(s) - 1] == ':' {
// Yes, this ends in a colon and is a key // Yes, this ends in a colon and is a key
return s[:len(s) - 1], true return s[:len(s) - 1], true
} }
return s, false return s, false
} }
// TODO: Maybe look at re-writing this?
parse_meminfo :: proc(meminfo: string) -> (map[string]f64, bool) { parse_meminfo :: proc(meminfo: string) -> (map[string]f64, bool) {
s := strings.fields(meminfo) s := strings.fields(meminfo)
orig := s orig := s
defer delete(orig) defer delete(orig)
values: map[string]f64 values: map[string]f64
last_key := "" last_key := ""
for len(s) > 0 { for len(s) > 0 {
key, key_ok := get_key(s[0]) key, key_ok := get_key(s[0])
if !key_ok { if !key_ok {
// Must've been a suffix, so let's multiply the last value // Must've been a suffix, so let's multiply the last value
switch key { switch key {
case "kB": case "kB":
values[last_key] *= 1024 values[last_key] *= 1024
} }
s = s[1:] s = s[1:]
continue continue
} }
s = s[1:] // Advance s = s[1:] // Advance
if val, val_ok := strconv.parse_f64(s[0]); !val_ok { if val, val_ok := strconv.parse_f64(s[0]); !val_ok {
break break
} else { } else {
values[key] = val values[key] = val
s = s[1:] s = s[1:]
} }
last_key = key last_key = key
} }
return values, true return values, true
} }
@ -129,7 +130,7 @@ parse_cpuinfo :: proc(cpuinfo: string) -> (map[string]string, bool) {
for line in strings.split_lines_iterator(&cpuinfo_string) { for line in strings.split_lines_iterator(&cpuinfo_string) {
key, _, value = strings.partition(line, ":") key, _, value = strings.partition(line, ":")
values[strings.trim_space(key)] = strings.trim_space(value) values[strings.trim_space(key)] = strings.trim_space(value)
} }
return values, true return values, true
@ -166,7 +167,7 @@ main :: proc() {
fmt.println("CPU Name: ", cpu_name) fmt.println("CPU Name: ", cpu_name)
} }
for { for i := 1; i < 10; i += 1 {
if mem_usage_perc, ok = get_ram_usage_perc(); !ok { if mem_usage_perc, ok = get_ram_usage_perc(); !ok {
fmt.fprintln(os.stderr, "Failed to read memory usage") fmt.fprintln(os.stderr, "Failed to read memory usage")
} }

Loading…
Cancel
Save