Keep all convtbl-related constants and strings in convtbl.[ch].

This commit is contained in:
Yaroslav Tykhiy 2006-11-27 16:14:32 +00:00
parent f5d4751c8a
commit aee371e363
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=164672
3 changed files with 57 additions and 48 deletions

View File

@ -29,25 +29,25 @@
*/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "convtbl.h"
struct convtbl convtbl[] = {
/* mul, scale, str */
[SC_BYTE] = { BYTE, BYTES, "bytes" },
[SC_KILOBYTE] = { BYTE, KILO, "KB" },
[SC_MEGABYTE] = { BYTE, MEGA, "MB" },
[SC_GIGABYTE] = { BYTE, GIGA, "GB" },
/* mul, scale, str, name */
[SC_BYTE] = { BYTE, BYTES, "B", "byte" },
[SC_KILOBYTE] = { BYTE, KILO, "KB", "kbyte" },
[SC_MEGABYTE] = { BYTE, MEGA, "MB", "mbyte" },
[SC_GIGABYTE] = { BYTE, GIGA, "GB", "gbyte" },
[SC_BIT] = { BIT, BITS, "b" },
[SC_KILOBIT] = { BIT, KILO, "Kb" },
[SC_MEGABIT] = { BIT, MEGA, "Mb" },
[SC_GIGABIT] = { BIT, GIGA, "Gb" },
[SC_BIT] = { BIT, BITS, "b", "bit" },
[SC_KILOBIT] = { BIT, KILO, "Kb", "kbit" },
[SC_MEGABIT] = { BIT, MEGA, "Mb", "mbit" },
[SC_GIGABIT] = { BIT, GIGA, "Gb", "gbit" },
[SC_AUTO] = { 0, 0, "" }
[SC_AUTO] = { 0, 0, "", "auto" }
};
static
struct convtbl *
get_tbl_ptr(const uintmax_t size, const int scale)
@ -90,3 +90,38 @@ get_string(const uintmax_t size, const int scale)
tp = get_tbl_ptr(size, scale);
return (tp->str);
}
int
get_scale(const char *name)
{
int i;
for (i = 0; i <= SC_AUTO; i++)
if (strcmp(convtbl[i].name, name) == 0)
return (i);
return (-1);
}
const char *
get_helplist()
{
int i;
size_t len;
static char *buf;
if (buf == NULL) {
len = 0;
for (i = 0; i <= SC_AUTO; i++)
len += strlen(convtbl[i].name) + 2;
if ((buf = malloc(len)) != NULL) {
buf[0] = '\0';
for (i = 0; i <= SC_AUTO; i++) {
strcat(buf, convtbl[i].name);
if (i < SC_AUTO)
strcat(buf, ", ");
}
} else
return ("");
}
return (buf);
}

View File

@ -49,7 +49,7 @@ enum scale {
SC_KILOBIT,
SC_MEGABIT,
SC_GIGABIT,
SC_AUTO
SC_AUTO /* KEEP THIS LAST */
};
#define BIT (8)
@ -59,11 +59,14 @@ struct convtbl {
uintmax_t mul;
uintmax_t scale;
const char *str;
const char *name;
};
extern struct convtbl convtbl[];
extern double convert(const uintmax_t, const int);
extern const char *get_helplist(void);
extern int get_scale(const char *);
extern const char *get_string(const uintmax_t, const int);
#endif /* ! _CONVTBL_H_ */

View File

@ -28,55 +28,26 @@
* $FreeBSD$
*/
#include <string.h>
#include "systat.h"
#include "extern.h"
#include "convtbl.h"
int curscale = SC_AUTO;
static int selectscale(const char *);
int curscale = SC_AUTO;
int
ifcmd(const char *cmd, const char *args)
{
int scale;
if (prefix(cmd, "scale")) {
if (*args != '\0' && selectscale(args) != -1)
;
if ((scale = get_scale(args)) != -1)
curscale = scale;
else {
move(CMDLINE, 0);
clrtoeol();
addstr("what scale? kbit, kbyte, mbit, mbyte, " \
"gbit, gbyte, auto");
addstr("what scale? ");
addstr(get_helplist());
}
}
return (1);
}
static int
selectscale(const char *args)
{
int retval = 0;
#define streq(a,b) (strcmp(a,b) == 0)
if (streq(args, "default") || streq(args, "auto"))
curscale = SC_AUTO;
else if (streq(args, "kbit"))
curscale = SC_KILOBIT;
else if (streq(args, "kbyte"))
curscale = SC_KILOBYTE;
else if (streq(args, "mbit"))
curscale = SC_MEGABIT;
else if (streq(args, "mbyte"))
curscale = SC_MEGABYTE;
else if (streq(args, "gbit"))
curscale = SC_GIGABIT;
else if (streq(args, "gbyte"))
curscale = SC_GIGABYTE;
else
retval = -1;
return (retval);
}