summaryrefslogtreecommitdiff
path: root/tools/stat/libstat.h
blob: 42fa054b2a6672f5bc9d1978e47c7869f4197450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include <stdint.h>
#include <inttypes.h>
#include <time.h>

int libstat_init();
void libstat_unit_translate(uint64_t number, float * f_number, char * unit);
void libstat_strtime(char * strtime, int str_max, time_t time);

/**
 * \brief 格式化字符串
 * \param str 被格式化的字符串
 * \param fmt 格式说明字符串,"l"为左对齐,"r"为右对齐,"c"为居中
 * \param width 宽度,需大于等于被格式化字符串的长度
 * \param spacewidth 尾部添加的空格数量
 * \param output 输出缓冲区指针
 * \param sz_output 输出缓冲区大小
 * \return 实际输出的长度
 */
int libstat_table_format_item(const char * str, const char * fmt,
	int width, int spacewidth, char * output, int sz_output);

/**
 * \brief 填充字符串
 * \param ch 被填充的字符
 * \param fmt 填充字符串格式
 * \param width 宽度
 * \param spacewidth  尾部添加的空格数量
 * \param output  输出缓冲区指针
 * \param sz_output 输出缓冲区大小
 * \return 实际输出的长度
 */
int libstat_table_fill_item(const char ch, const char * fmt,
	int width, int spacewidth, char * output, int sz_output);

#define SPEED_ITEM_WIDTH		12
#define COUNT_ITEM_WIDTH		20
#define NAME_ITEM_WIDTH			8
#define ID_ITEM_WIDTH			6
#define SPACE_WIDTH				2

#define __WRITE_TABLE_ITEM(_name, _fmt, _iwidth)								\
do {																			\
	linecur += libstat_table_format_item(_name, _fmt, _iwidth, SPACE_WIDTH,		\
	linebuf + linecur, sizeof(linebuf) - linecur);								\
} while(0);																		\

#define __WRITE_TABLE_CLINE(_ch, _fmt, _iwidth)									\
do {																			\
	linecur += libstat_table_fill_item(_ch, _fmt, _iwidth, SPACE_WIDTH,			\
	linebuf + linecur, sizeof(linebuf) - linecur);								\
} while(0);

#define __TRANS_HUMAN(_data, _str)												\
do {																			\
	float f_data; char unit;													\
	libstat_unit_translate(_data, &f_data, &unit);								\
	sprintf(_str, "%7.2f%c", f_data, unit);										\
}while(0)																		\

static inline char * __trans_data(uint64_t data, char * buffer, size_t sz_buffer)
{
	snprintf(buffer, sz_buffer, "%"PRIu64, data);
	return buffer;
}

#define __TRANS_DATA(_data, _buffer) (__trans_data(_data, _buffer, sizeof(_buffer)))