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 <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.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; handler = NULL;
int fd = dup(0);
while (true) switch ((isdigit(chr) != 0) ? '0' : chr)
{
chr = read(fd, &chr, 1);
switch (chr)
{ {
case 'd' : /* dictionary */ case 'd' : /* dictionary */
/* print_dict(fd);*/ break;
case 'e' : /* dictionary, integer, or list end */
break; break;
case 'i' : /* integer */ case 'i' : /* integer */
break; break;
case 'l' : /* list */ case 'l' : /* list */
break; break;
case '1' : case '0' : /* string */
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' : /* string */
break; break;
default: default:
fprintf(stderr, "Unknown marker: %u\n", chr); fprintf(stderr, "Unknown marker: %u\n", chr);
break; 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; break;
handler(in);
} }
exit(0); exit(EXIT_SUCCESS);
} }

Loading…
Cancel
Save