Browse Source

* some code cleanup

master
Alex 'AdUser' Z 13 years ago
parent
commit
d31b751a90
  1. 20
      src/torrent2json.c

20
src/torrent2json.c

@ -1,15 +1,12 @@
#include <ctype.h> #include <ctype.h>
#include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <yajl/yajl_gen.h> #include <yajl/yajl_gen.h>
/* FD's */ /* FD's */
FILE *in; FILE *in;
FILE *out; FILE *out;
@ -18,6 +15,7 @@ struct yajl_gen_t *gen = NULL;
void (*select_handler(int))(int); void (*select_handler(int))(int);
/* handlers */
void void
get_integer(int chr) get_integer(int chr)
{ /* 'chr' always 'i' in this case */ { /* 'chr' always 'i' in this case */
@ -65,7 +63,9 @@ get_string(int chr)
if ((buf = malloc(sizeof(char) * len + 1)) == NULL) if ((buf = malloc(sizeof(char) * len + 1)) == NULL)
{ {
#ifdef DEBUG
fprintf(stderr, "Can't allocate memory, exiting.\n"); fprintf(stderr, "Can't allocate memory, exiting.\n");
#endif
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -112,6 +112,7 @@ get_list(int chr)
yajl_gen_array_close(gen); yajl_gen_array_close(gen);
} }
/* general purpose functions */
void (*select_handler(int chr))(int) void (*select_handler(int chr))(int)
{ {
switch ((isdigit(chr) != 0) ? '0' : chr) switch ((isdigit(chr) != 0) ? '0' : chr)
@ -119,8 +120,6 @@ void (*select_handler(int chr))(int)
case 'd' : /* dictionary */ case 'd' : /* dictionary */
return get_dict; return get_dict;
break; break;
case 'e' : /* dictionary, integer, or list end */
break;
case 'i' : /* integer */ case 'i' : /* integer */
return get_integer; return get_integer;
break; break;
@ -131,7 +130,9 @@ void (*select_handler(int chr))(int)
return get_string; return get_string;
break; break;
default: default:
#ifdef DEBUG
fprintf(stderr, "Unknown marker: %u\n", chr); fprintf(stderr, "Unknown marker: %u\n", chr);
#endif
break; break;
} }
@ -146,12 +147,13 @@ main(int argc, char **argv)
const unsigned char *buf = NULL; const unsigned char *buf = NULL;
size_t len = 0; size_t len = 0;
in = stdin; /* FILE */ in = stdin;
out = stdout; /* FILE */ out = stdout;
gen = yajl_gen_alloc(NULL); gen = yajl_gen_alloc(NULL);
yajl_gen_config(gen, yajl_gen_beautify, 1); yajl_gen_config(gen, yajl_gen_beautify, 1);
yajl_gen_config(gen, yajl_gen_indent_string, " "); yajl_gen_config(gen, yajl_gen_indent_string, "\t");
while ((c = fgetc(in)) != EOF) while ((c = fgetc(in)) != EOF)
{ {
@ -160,7 +162,7 @@ main(int argc, char **argv)
} }
yajl_gen_get_buf(gen, &buf, &len); yajl_gen_get_buf(gen, &buf, &len);
if (len > 0) fprintf(out, "%s", buf); if (len > 0 && buf) fprintf(out, "%s", buf);
yajl_gen_free(gen); yajl_gen_free(gen);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

Loading…
Cancel
Save