Browse Source

Added ARRAY_COUNT() macro to get number of elements in const arrays.

master
Marq Schneider 14 years ago
parent
commit
a6328070d9
5 changed files with 17 additions and 10 deletions
  1. +2
    -3
      btpd/cli_if.c
  2. +2
    -3
      cli/btcli.c
  3. +2
    -1
      cli/list.c
  4. +4
    -3
      cli/stat.c
  5. +7
    -0
      misc/utils.h

+ 2
- 3
btpd/cli_if.c View File

@@ -1,4 +1,5 @@
#include "btpd.h" #include "btpd.h"
#include "utils.h"


#include <sys/un.h> #include <sys/un.h>
#include <iobuf.h> #include <iobuf.h>
@@ -440,8 +441,6 @@ static struct {
{ "tget", 4, cmd_tget } { "tget", 4, cmd_tget }
}; };


static int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);

static int static int
cmd_dispatch(struct cli *cli, const char *buf) cmd_dispatch(struct cli *cli, const char *buf)
{ {
@@ -451,7 +450,7 @@ cmd_dispatch(struct cli *cli, const char *buf)


cmd = benc_mem(benc_first(buf), &cmdlen, &args); cmd = benc_mem(benc_first(buf), &cmdlen, &args);


for (int i = 0; i < ncmds; i++) {
for (int i = 0; i < ARRAY_COUNT(cmd_table); i++) {
if ((cmdlen == cmd_table[i].nlen && if ((cmdlen == cmd_table[i].nlen &&
strncmp(cmd_table[i].name, cmd, cmdlen) == 0)) { strncmp(cmd_table[i].name, cmd, cmdlen) == 0)) {
return cmd_table[i].fun(cli, benc_nelems(buf) - 1, args); return cmd_table[i].fun(cli, benc_nelems(buf) - 1, args);


+ 2
- 3
cli/btcli.c View File

@@ -1,6 +1,7 @@
#include <stdarg.h> #include <stdarg.h>


#include "btcli.h" #include "btcli.h"
#include "utils.h"


const char *btpd_dir; const char *btpd_dir;
struct ipc *ipc; struct ipc *ipc;
@@ -120,8 +121,6 @@ static struct {
{ "stat", cmd_stat, usage_stat } { "stat", cmd_stat, usage_stat }
}; };


int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);

static void static void
usage(void) usage(void)
{ {
@@ -191,7 +190,7 @@ main(int argc, char **argv)


optind = 0; optind = 0;
int found = 0; int found = 0;
for (int i = 0; !found && i < ncmds; i++) {
for (int i = 0; !found && i < ARRAY_COUNT(cmd_table); i++) {
if (strcmp(argv[0], cmd_table[i].name) == 0) { if (strcmp(argv[0], cmd_table[i].name) == 0) {
found = 1; found = 1;
if (help) if (help)


+ 2
- 1
cli/list.c View File

@@ -1,4 +1,5 @@
#include "btcli.h" #include "btcli.h"
#include "utils.h"


void void
usage_list(void) usage_list(void)
@@ -170,7 +171,7 @@ cmd_list(int argc, char **argv)
IPC_TVAL_PCCOUNT, IPC_TVAL_PCSEEN, IPC_TVAL_PCGOT, IPC_TVAL_SESSUP, IPC_TVAL_PCCOUNT, IPC_TVAL_PCSEEN, IPC_TVAL_PCGOT, IPC_TVAL_SESSUP,
IPC_TVAL_SESSDWN, IPC_TVAL_RATEUP, IPC_TVAL_RATEDWN, IPC_TVAL_IHASH, IPC_TVAL_SESSDWN, IPC_TVAL_RATEUP, IPC_TVAL_RATEDWN, IPC_TVAL_IHASH,
IPC_TVAL_DIR }; IPC_TVAL_DIR };
size_t nkeys = sizeof(keys) / sizeof(keys[0]);
size_t nkeys = ARRAY_COUNT(keys);
struct items itms; struct items itms;
while ((ch = getopt_long(argc, argv, "aif:", list_opts, NULL)) != -1) { while ((ch = getopt_long(argc, argv, "aif:", list_opts, NULL)) != -1) {
switch (ch) { switch (ch) {


+ 4
- 3
cli/stat.c View File

@@ -1,4 +1,5 @@
#include "btcli.h" #include "btcli.h"
#include "utils.h"


void void
usage_stat(void) usage_stat(void)
@@ -56,7 +57,7 @@ static enum ipc_tval stkeys[] = {
IPC_TVAL_CSIZE IPC_TVAL_CSIZE
}; };


static size_t nstkeys = sizeof(stkeys) / sizeof(stkeys[0]);
#define NSTKEYS ARRAY_COUNT(stkeys)


static void static void
print_stat(struct btstat *st) print_stat(struct btstat *st)
@@ -129,10 +130,10 @@ again:
bzero(&cba.tot, sizeof(cba.tot)); bzero(&cba.tot, sizeof(cba.tot));
cba.tot.state = IPC_TSTATE_INACTIVE; cba.tot.state = IPC_TSTATE_INACTIVE;
if (tps == NULL) if (tps == NULL)
err = btpd_tget_wc(ipc, IPC_TWC_ACTIVE, stkeys, nstkeys,
err = btpd_tget_wc(ipc, IPC_TWC_ACTIVE, stkeys, NSTKEYS,
stat_cb, &cba); stat_cb, &cba);
else else
err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba);
err = btpd_tget(ipc, tps, ntps, stkeys, NSTKEYS, stat_cb, &cba);
if (err != IPC_OK) if (err != IPC_OK)
diemsg("command failed (%s).\n", ipc_strerror(err)); diemsg("command failed (%s).\n", ipc_strerror(err));
if (names) if (names)


+ 7
- 0
misc/utils.h View File

@@ -0,0 +1,7 @@
#ifndef BTPD_UTILS_H
#define BTPD_UTILS_H

/* get the number of elements in a static table */
#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))

#endif

Loading…
Cancel
Save