Commit c6b9039f authored by John Koleszar's avatar John Koleszar
Browse files

Restyle code

Approximate the Google style guide[1] so that that there's a written
document to follow and tools to check compliance[2].

[1]: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
[2]: http://google-styleguide.googlecode.com/svn/trunk/cpplint/cpplint.py

Change-Id: Idf40e3d8dddcc72150f6af127b13e5dab838685f
parent 8697c6e4
...@@ -25,241 +25,214 @@ extern void die(const char *fmt, ...); ...@@ -25,241 +25,214 @@ extern void die(const char *fmt, ...);
#endif #endif
struct arg arg_init(char **argv) struct arg arg_init(char **argv) {
{ struct arg a;
struct arg a;
a.argv = argv;
a.argv = argv; a.argv_step = 1;
a.argv_step = 1; a.name = NULL;
a.name = NULL; a.val = NULL;
a.val = NULL; a.def = NULL;
a.def = NULL; return a;
return a;
} }
int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) int arg_match(struct arg *arg_, const struct arg_def *def, char **argv) {
{ struct arg arg;
struct arg arg;
if (!argv[0] || argv[0][0] != '-') if (!argv[0] || argv[0][0] != '-')
return 0; return 0;
arg = arg_init(argv); arg = arg_init(argv);
if (def->short_name if (def->short_name
&& strlen(arg.argv[0]) == strlen(def->short_name) + 1 && strlen(arg.argv[0]) == strlen(def->short_name) + 1
&& !strcmp(arg.argv[0] + 1, def->short_name)) && !strcmp(arg.argv[0] + 1, def->short_name)) {
{
arg.name = arg.argv[0] + 1; arg.name = arg.argv[0] + 1;
arg.val = def->has_val ? arg.argv[1] : NULL; arg.val = def->has_val ? arg.argv[1] : NULL;
arg.argv_step = def->has_val ? 2 : 1; arg.argv_step = def->has_val ? 2 : 1;
} } else if (def->long_name) {
else if (def->long_name) int name_len = strlen(def->long_name);
{
int name_len = strlen(def->long_name); if (strlen(arg.argv[0]) >= name_len + 2
&& arg.argv[0][1] == '-'
if (strlen(arg.argv[0]) >= name_len + 2 && !strncmp(arg.argv[0] + 2, def->long_name, name_len)
&& arg.argv[0][1] == '-' && (arg.argv[0][name_len + 2] == '='
&& !strncmp(arg.argv[0] + 2, def->long_name, name_len) || arg.argv[0][name_len + 2] == '\0')) {
&& (arg.argv[0][name_len+2] == '='
|| arg.argv[0][name_len+2] == '\0')) arg.name = arg.argv[0] + 2;
{ arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
arg.argv_step = 1;
arg.name = arg.argv[0] + 2;
arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
arg.argv_step = 1;
}
} }
}
if (arg.name && !arg.val && def->has_val) if (arg.name && !arg.val && def->has_val)
die("Error: option %s requires argument.\n", arg.name); die("Error: option %s requires argument.\n", arg.name);
if (arg.name && arg.val && !def->has_val) if (arg.name && arg.val && !def->has_val)
die("Error: option %s requires no argument.\n", arg.name); die("Error: option %s requires no argument.\n", arg.name);
if (arg.name if (arg.name
&& (arg.val || !def->has_val)) && (arg.val || !def->has_val)) {
{ arg.def = def;
arg.def = def; *arg_ = arg;
*arg_ = arg; return 1;
return 1; }
}
return 0; return 0;
} }
const char *arg_next(struct arg *arg) const char *arg_next(struct arg *arg) {
{ if (arg->argv[0])
if (arg->argv[0]) arg->argv += arg->argv_step;
arg->argv += arg->argv_step;
return *arg->argv; return *arg->argv;
} }
char **argv_dup(int argc, const char **argv) char **argv_dup(int argc, const char **argv) {
{ char **new_argv = malloc((argc + 1) * sizeof(*argv));
char **new_argv = malloc((argc + 1) * sizeof(*argv));
memcpy(new_argv, argv, argc * sizeof(*argv)); memcpy(new_argv, argv, argc * sizeof(*argv));
new_argv[argc] = NULL; new_argv[argc] = NULL;
return new_argv; return new_argv;
} }
void arg_show_usage(FILE *fp, const struct arg_def *const *defs) void arg_show_usage(FILE *fp, const struct arg_def *const *defs) {
{ char option_text[40] = {0};
char option_text[40] = {0};
for (; *defs; defs++) for (; *defs; defs++) {
{ const struct arg_def *def = *defs;
const struct arg_def *def = *defs; char *short_val = def->has_val ? " <arg>" : "";
char *short_val = def->has_val ? " <arg>" : ""; char *long_val = def->has_val ? "=<arg>" : "";
char *long_val = def->has_val ? "=<arg>" : "";
if (def->short_name && def->long_name) if (def->short_name && def->long_name) {
{ char *comma = def->has_val ? "," : ", ";
char *comma = def->has_val ? "," : ", ";
snprintf(option_text, 37, "-%s%s%s --%s%6s", snprintf(option_text, 37, "-%s%s%s --%s%6s",
def->short_name, short_val, comma, def->short_name, short_val, comma,
def->long_name, long_val); def->long_name, long_val);
} } else if (def->short_name)
else if (def->short_name) snprintf(option_text, 37, "-%s%s",
snprintf(option_text, 37, "-%s%s", def->short_name, short_val);
def->short_name, short_val); else if (def->long_name)
else if (def->long_name) snprintf(option_text, 37, " --%s%s",
snprintf(option_text, 37, " --%s%s", def->long_name, long_val);
def->long_name, long_val);
fprintf(fp, " %-37s\t%s\n", option_text, def->desc); fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
if(def->enums) if (def->enums) {
{ const struct arg_enum_list *listptr;
const struct arg_enum_list *listptr;
fprintf(fp, " %-37s\t ", ""); fprintf(fp, " %-37s\t ", "");
for(listptr = def->enums; listptr->name; listptr++) for (listptr = def->enums; listptr->name; listptr++)
fprintf(fp, "%s%s", listptr->name, fprintf(fp, "%s%s", listptr->name,
listptr[1].name ? ", " : "\n"); listptr[1].name ? ", " : "\n");
}
} }
}
} }
unsigned int arg_parse_uint(const struct arg *arg) unsigned int arg_parse_uint(const struct arg *arg) {
{ long int rawval;
long int rawval; char *endptr;
char *endptr;
rawval = strtol(arg->val, &endptr, 10); rawval = strtol(arg->val, &endptr, 10);
if (arg->val[0] != '\0' && endptr[0] == '\0') if (arg->val[0] != '\0' && endptr[0] == '\0') {
{ if (rawval >= 0 && rawval <= UINT_MAX)
if (rawval >= 0 && rawval <= UINT_MAX) return rawval;
return rawval;
die("Option %s: Value %ld out of range for unsigned int\n", die("Option %s: Value %ld out of range for unsigned int\n",
arg->name, rawval); arg->name, rawval);
} }
die("Option %s: Invalid character '%c'\n", arg->name, *endptr); die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
return 0; return 0;
} }
int arg_parse_int(const struct arg *arg) int arg_parse_int(const struct arg *arg) {
{ long int rawval;
long int rawval; char *endptr;
char *endptr;
rawval = strtol(arg->val, &endptr, 10); rawval = strtol(arg->val, &endptr, 10);
if (arg->val[0] != '\0' && endptr[0] == '\0') if (arg->val[0] != '\0' && endptr[0] == '\0') {
{ if (rawval >= INT_MIN && rawval <= INT_MAX)
if (rawval >= INT_MIN && rawval <= INT_MAX) return rawval;
return rawval;
die("Option %s: Value %ld out of range for signed int\n", die("Option %s: Value %ld out of range for signed int\n",
arg->name, rawval); arg->name, rawval);
} }
die("Option %s: Invalid character '%c'\n", arg->name, *endptr); die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
return 0; return 0;
} }
struct vpx_rational struct vpx_rational {
{ int num; /**< fraction numerator */
int num; /**< fraction numerator */ int den; /**< fraction denominator */
int den; /**< fraction denominator */
}; };
struct vpx_rational arg_parse_rational(const struct arg *arg) struct vpx_rational arg_parse_rational(const struct arg *arg) {
{ long int rawval;
long int rawval; char *endptr;
char *endptr; struct vpx_rational rat;
struct vpx_rational rat;
/* parse numerator */
/* parse numerator */ rawval = strtol(arg->val, &endptr, 10);
rawval = strtol(arg->val, &endptr, 10);
if (arg->val[0] != '\0' && endptr[0] == '/') {
if (arg->val[0] != '\0' && endptr[0] == '/') if (rawval >= INT_MIN && rawval <= INT_MAX)
{ rat.num = rawval;
if (rawval >= INT_MIN && rawval <= INT_MAX) else die("Option %s: Value %ld out of range for signed int\n",
rat.num = rawval; arg->name, rawval);
else die("Option %s: Value %ld out of range for signed int\n", } else die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
arg->name, rawval);
} /* parse denominator */
else die("Option %s: Expected / at '%c'\n", arg->name, *endptr); rawval = strtol(endptr + 1, &endptr, 10);
/* parse denominator */ if (arg->val[0] != '\0' && endptr[0] == '\0') {
rawval = strtol(endptr + 1, &endptr, 10); if (rawval >= INT_MIN && rawval <= INT_MAX)
rat.den = rawval;
if (arg->val[0] != '\0' && endptr[0] == '\0') else die("Option %s: Value %ld out of range for signed int\n",
{ arg->name, rawval);
if (rawval >= INT_MIN && rawval <= INT_MAX) } else die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
rat.den = rawval;
else die("Option %s: Value %ld out of range for signed int\n", return rat;
arg->name, rawval);
}
else die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
return rat;
} }
int arg_parse_enum(const struct arg *arg) int arg_parse_enum(const struct arg *arg) {
{ const struct arg_enum_list *listptr;
const struct arg_enum_list *listptr; long int rawval;
long int rawval; char *endptr;
char *endptr;
/* First see if the value can be parsed as a raw value */
rawval = strtol(arg->val, &endptr, 10);
if (arg->val[0] != '\0' && endptr[0] == '\0')
{
/* Got a raw value, make sure it's valid */
for(listptr = arg->def->enums; listptr->name; listptr++)
if(listptr->val == rawval)
return rawval;
}
/* Next see if it can be parsed as a string */ /* First see if the value can be parsed as a raw value */
for(listptr = arg->def->enums; listptr->name; listptr++) rawval = strtol(arg->val, &endptr, 10);
if(!strcmp(arg->val, listptr->name)) if (arg->val[0] != '\0' && endptr[0] == '\0') {
return listptr->val; /* Got a raw value, make sure it's valid */
for (listptr = arg->def->enums; listptr->name; listptr++)
if (listptr->val == rawval)
return rawval;
}
die("Option %s: Invalid value '%s'\n", arg->name, arg->val); /* Next see if it can be parsed as a string */
return 0; for (listptr = arg->def->enums; listptr->name; listptr++)
if (!strcmp(arg->val, listptr->name))
return listptr->val;
die("Option %s: Invalid value '%s'\n", arg->name, arg->val);
return 0;
} }
int arg_parse_enum_or_int(const struct arg *arg) int arg_parse_enum_or_int(const struct arg *arg) {
{ if (arg->def->enums)
if(arg->def->enums) return arg_parse_enum(arg);
return arg_parse_enum(arg); return arg_parse_int(arg);
return arg_parse_int(arg);
} }
...@@ -13,29 +13,26 @@ ...@@ -13,29 +13,26 @@
#define ARGS_H #define ARGS_H
#include <stdio.h> #include <stdio.h>
struct arg struct arg {
{ char **argv;
char **argv; const char *name;
const char *name; const char *val;
const char *val; unsigned int argv_step;
unsigned int argv_step; const struct arg_def *def;
const struct arg_def *def;
}; };
struct arg_enum_list struct arg_enum_list {
{ const char *name;
const char *name; int val;
int val;
}; };
#define ARG_ENUM_LIST_END {0} #define ARG_ENUM_LIST_END {0}
typedef struct arg_def typedef struct arg_def {
{ const char *short_name;
const char *short_name; const char *long_name;
const char *long_name; int has_val;
int has_val; const char *desc;
const char *desc; const struct arg_enum_list *enums;
const struct arg_enum_list *enums;
} arg_def_t; } arg_def_t;
#define ARG_DEF(s,l,v,d) {s,l,v,d, NULL} #define ARG_DEF(s,l,v,d) {s,l,v,d, NULL}
#define ARG_DEF_ENUM(s,l,v,d,e) {s,l,v,d,e} #define ARG_DEF_ENUM(s,l,v,d,e) {s,l,v,d,e}
......
This diff is collapsed.
...@@ -25,190 +25,167 @@ ...@@ -25,190 +25,167 @@
static char *exec_name; static char *exec_name;
static int verbose = 0; static int verbose = 0;
static const struct static const struct {
{ const char *name;
const char *name; const vpx_codec_iface_t *iface;
const vpx_codec_iface_t *iface; } ifaces[] = {
} ifaces[] =
{
#if CONFIG_VP8_DECODER #if CONFIG_VP8_DECODER
{"vp8", &vpx_codec_vp8_dx_algo}, {"vp8", &vpx_codec_vp8_dx_algo},
#endif #endif
}; };
static void usage_exit(void) static void usage_exit(void) {
{ int i;
int i;
printf("Usage: %s <options>\n\n"
printf("Usage: %s <options>\n\n" "Options:\n"
"Options:\n" "\t--codec <name>\tCodec to use (default=%s)\n"
"\t--codec <name>\tCodec to use (default=%s)\n" "\t-h <height>\tHeight of the simulated video frame, in pixels\n"
"\t-h <height>\tHeight of the simulated video frame, in pixels\n" "\t-w <width> \tWidth of the simulated video frame, in pixels\n"
"\t-w <width> \tWidth of the simulated video frame, in pixels\n" "\t-v \tVerbose mode (show individual segment sizes)\n"
"\t-v \tVerbose mode (show individual segment sizes)\n" "\t--help \tShow this message\n"
"\t--help \tShow this message\n" "\n"
"\n" "Included decoders:\n"
"Included decoders:\n" "\n",
"\n", exec_name,
exec_name, ifaces[0].name);
ifaces[0].name);
for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++)
for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) printf(" %-6s - %s\n",
printf(" %-6s - %s\n", ifaces[i].name,
ifaces[i].name, vpx_codec_iface_name(ifaces[i].iface));
vpx_codec_iface_name(ifaces[i].iface));
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
} }
static void usage_error(const char *fmt, ...) static void usage_error(const char *fmt, ...) {
{ va_list ap;
va_list ap; va_start(ap, fmt);
va_start(ap, fmt); vprintf(fmt, ap);
vprintf(fmt, ap); printf("\n");
printf("\n"); usage_exit();
usage_exit();
} }
void my_mem_dtor(vpx_codec_mmap_t *mmap) void my_mem_dtor(vpx_codec_mmap_t *mmap) {
{ if (verbose)
if (verbose) printf("freeing segment %d\n", mmap->id);
printf("freeing segment %d\n", mmap->id);
free(mmap->priv); free(mmap->priv);
} }
int main(int argc, char **argv) int main(int argc, char **argv) {
{ vpx_codec_ctx_t decoder;
vpx_codec_ctx_t decoder; vpx_codec_iface_t *iface = ifaces[0].iface;
vpx_codec_iface_t *iface = ifaces[0].iface; vpx_codec_iter_t iter;
vpx_codec_iter_t iter; vpx_codec_dec_cfg_t cfg;
vpx_codec_dec_cfg_t cfg; vpx_codec_err_t res = VPX_CODEC_OK;
vpx_codec_err_t res = VPX_CODEC_OK; unsigned int alloc_sz = 0;
unsigned int alloc_sz = 0; unsigned int w = 352;
unsigned int w = 352; unsigned int h = 288;
unsigned int h = 288; int i;
int i;
exec_name = argv[0];
for (i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "--codec"))
{
if (i + 1 < argc)
{
int j, k = -1;
i++;
for (j = 0; j < sizeof(ifaces) / sizeof(ifaces[0]); j++)
if (!strcmp(ifaces[j].name, argv[i]))
k = j;
if (k >= 0)
iface = ifaces[k].iface;
else
usage_error("Error: Unrecognized argument (%s) to --codec\n",
argv[i]);
}
else
usage_error("Error: Option --codec requires argument.\n");
}
else if (!strcmp(argv[i], "-v"))
verbose = 1;
else if (!strcmp(argv[i], "-h"))