Add scripts
This commit is contained in:
87
scripts/health/utils.c
Normal file
87
scripts/health/utils.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
void indent(int i)
|
||||
{
|
||||
for (; i > 0; i--) printf(" ");
|
||||
}
|
||||
|
||||
|
||||
// Prints to the provided buffer a nice number of bytes (KB, MB, GB, etc)
|
||||
// Returns bytes written
|
||||
int pretty_bytes(char* buf, long long bytes)
|
||||
{
|
||||
const char* suffixes[7];
|
||||
suffixes[0] = "B";
|
||||
suffixes[1] = "KB";
|
||||
suffixes[2] = "MB";
|
||||
suffixes[3] = "GB";
|
||||
suffixes[4] = "TB";
|
||||
suffixes[5] = "PB";
|
||||
suffixes[6] = "EB";
|
||||
uint s = 0; // which suffix to use
|
||||
double count = bytes;
|
||||
while (count >= 1024 && s < 7)
|
||||
{
|
||||
s++;
|
||||
count /= 1024;
|
||||
}
|
||||
if (count - floor(count) == 0.0)
|
||||
return sprintf(buf, "%d %s", (int)count, suffixes[s]);
|
||||
else
|
||||
return sprintf(buf, "%.1f %s", count, suffixes[s]);
|
||||
}
|
||||
|
||||
long long pretty_bytes_to_num(char* buf)
|
||||
{
|
||||
// First letter of suffix in order
|
||||
const char suffixes[7] = {'B', 'K', 'M', 'G', 'T', 'P', 'E'};
|
||||
char suffix;
|
||||
float fnum;
|
||||
long long num = 1;
|
||||
int n;
|
||||
|
||||
errno = 0;
|
||||
n = sscanf(buf,"%f%c", &fnum, &suffix);
|
||||
if (n == 2) {
|
||||
//TODO error handling
|
||||
} else if (errno != 0) {
|
||||
perror("scanf");
|
||||
} else {
|
||||
fprintf(stderr, "No matching characters\n");
|
||||
}
|
||||
|
||||
for (int s=0;s<sizeof(suffixes); s++){
|
||||
if (suffix == suffixes[s]) break;
|
||||
num *= 1024;
|
||||
}
|
||||
|
||||
return (long long )(num*fnum);
|
||||
}
|
||||
|
||||
size_t utf8len(const char *s)
|
||||
{
|
||||
size_t len = 0;
|
||||
for (; *s; ++s) if ((*s & 0xC0) != 0x80) ++len;
|
||||
return len;
|
||||
}
|
||||
|
||||
void debug(const char *format, ...){
|
||||
#ifdef DEBUG
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
#endif
|
||||
}
|
||||
|
||||
void indent_debug(int i){
|
||||
#ifdef DEBUG
|
||||
indent(i);
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user