Browse Source

+ improved version of the code skeleton

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

89
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)
{
handler = NULL;
switch ((isdigit(chr) != 0) ? '0' : chr)
{
case 'd' : /* dictionary */
break;
case 'e' : /* dictionary, integer, or list end */
break;
case 'i' : /* integer */
break;
case 'l' : /* list */
break;
case '0' : /* string */
break;
default:
fprintf(stderr, "Unknown marker: %u\n", chr);
break;
}
return handler;
}
int
main(int argc, char **argv)
{ {
int chr; int chr = '\0';
int fd = dup(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) while (true)
{ {
chr = read(fd, &chr, 1); handler = select_handler(chr);
switch (chr)
{ if (handler == NULL)
case 'd' : /* dictionary */ break;
/* print_dict(fd);*/
break; handler(in);
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 */
break;
default:
fprintf(stderr, "Unknown marker: %u\n", chr);
break;
}
break;
} }
exit(0); exit(EXIT_SUCCESS);
} }

Loading…
Cancel
Save