formatting shit
This commit is contained in:
39
src/utils.c
39
src/utils.c
@@ -1,18 +1,20 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <glob.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define MKPATH(path, dir, file) do { \
|
||||
char *nt = stpcpy(path, dir); \
|
||||
*nt++ = '/'; stpcpy(nt, file); \
|
||||
#define MKPATH(path, dir, file) \
|
||||
do { \
|
||||
char *nt = stpcpy(path, dir); \
|
||||
*nt++ = '/'; \
|
||||
stpcpy(nt, file); \
|
||||
} while (0)
|
||||
|
||||
inline void timespec_add_ms(struct timespec *ts, const uint16_t ms) {
|
||||
@@ -30,7 +32,8 @@ inline uint64_t timespec_diff_us(const struct timespec *a, const struct timespec
|
||||
}
|
||||
|
||||
inline char *pretty_num(const double num, char *buf, const size_t bufsz) {
|
||||
const uint8_t decimals = (num < 10) ? 2 : (num < 100) ? 1 : 0;
|
||||
const uint8_t decimals = (num < 10) ? 2 : (num < 100) ? 1
|
||||
: 0;
|
||||
snprintf(buf, bufsz, "%.*f", decimals, num);
|
||||
return buf;
|
||||
}
|
||||
@@ -47,13 +50,12 @@ inline char *pretty_bytes(const uint64_t bytes, int8_t start_suffix, char *buf,
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
inline bool file_exists(const char* path) {
|
||||
inline bool file_exists(const char *path) {
|
||||
return access(path, F_OK) == 0;
|
||||
}
|
||||
|
||||
/* path has to have at least 8 free chars at the end for glob */
|
||||
int get_hwmon_dir(char* path) {
|
||||
int get_hwmon_dir(char *path) {
|
||||
glob_t g;
|
||||
strncat(path, "/hwmon*", 10);
|
||||
|
||||
@@ -66,47 +68,48 @@ int get_hwmon_dir(char* path) {
|
||||
}
|
||||
|
||||
/* read n bytes from path into buffer */
|
||||
inline size_t read_file(const char* path, char* buffer, const size_t n) {
|
||||
inline size_t read_file(const char *path, char *buffer, const size_t n) {
|
||||
const int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
applog(LOG_ERR, "Error reading %s", path);
|
||||
return 0;
|
||||
}
|
||||
const ssize_t len = read(fd, buffer, n);
|
||||
if (len >= 0) buffer[len] = '\0';
|
||||
if (len >= 0)
|
||||
buffer[len] = '\0';
|
||||
close(fd);
|
||||
return len;
|
||||
}
|
||||
|
||||
inline void fast_read_num_u64(const char* dir, const char* file, char* buf, uint64_t* result, const uint32_t divider) {
|
||||
inline void fast_read_num_u64(const char *dir, const char *file, char *buf, uint64_t *result, const uint32_t divider) {
|
||||
char path[256];
|
||||
MKPATH(path, dir, file);
|
||||
read_file(path, buf, 32);
|
||||
*result = strtoull(buf, NULL, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_u16(const char* dir, const char* file, char* buf, uint16_t* result, const uint32_t divider) {
|
||||
inline void fast_read_num_u16(const char *dir, const char *file, char *buf, uint16_t *result, const uint32_t divider) {
|
||||
char path[256];
|
||||
MKPATH(path, dir, file);
|
||||
read_file(path, buf, 32);
|
||||
*result = strtol(buf, NULL, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_u8(const char* dir, const char* file, char* buf, uint8_t* result, const uint32_t divider) {
|
||||
inline void fast_read_num_u8(const char *dir, const char *file, char *buf, uint8_t *result, const uint32_t divider) {
|
||||
char path[256];
|
||||
MKPATH(path, dir, file);
|
||||
read_file(path, buf, 32);
|
||||
*result = strtol(buf, NULL, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_i16(const char* dir, const char* file, char* buf, int16_t* result, const uint32_t divider) {
|
||||
inline void fast_read_num_i16(const char *dir, const char *file, char *buf, int16_t *result, const uint32_t divider) {
|
||||
char path[256];
|
||||
MKPATH(path, dir, file);
|
||||
read_file(path, buf, 32);
|
||||
*result = strtol(buf, NULL, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_f(const char* dir, const char* file, char* buf, float* result, const uint32_t divider) {
|
||||
inline void fast_read_num_f(const char *dir, const char *file, char *buf, float *result, const uint32_t divider) {
|
||||
char path[256];
|
||||
MKPATH(path, dir, file);
|
||||
read_file(path, buf, 32);
|
||||
|
||||
Reference in New Issue
Block a user