Browse Source

+ improved version of the code skeleton

master
Alex 'AdUser' Z 13 years ago
parent
commit
5deb390ed7
  1. 67
      src/torrent2json.c

67
src/torrent2json.c

@ -1,43 +1,74 @@
#include <ctype.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
#include <yajl/yajl_common.h>
#include <yajl/yajl_gen.h>
#define BUF_SIZE 4096
char buf[BUF_SIZE];
/* FD's */
int in;
int out;
void (*handler)(int);
void (*select_handler(char chr))(int)
{
int chr;
int fd = dup(0);
handler = NULL;
while (true)
{
chr = read(fd, &chr, 1);
switch (chr)
switch ((isdigit(chr) != 0) ? '0' : chr)
{
case 'd' : /* dictionary */
/* print_dict(fd);*/
break;
case 'e' : /* dictionary, integer, or list end */
break;
case 'i' : /* integer */
break;
case 'l' : /* list */
break;
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' : /* string */
case '0' : /* string */
break;
default:
fprintf(stderr, "Unknown marker: %u\n", chr);
break;
}
return handler;
}
int
main(int argc, char **argv)
{
int chr = '\0';
in = dup(0);
out = dup(1);
read(in, &chr, 1);
if (chr != 'd')
{
fprintf(stderr, "Malformed torrent file\n");
exit(EXIT_FAILURE);
}
/* some work here */
while (true)
{
handler = select_handler(chr);
if (handler == NULL)
break;
handler(in);
}
exit(0);
exit(EXIT_SUCCESS);
}

Loading…
Cancel
Save