Browse Source

* changed integer parsing (negative integer)

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

15
src/torrent2json.c

@ -21,12 +21,11 @@ get_integer(int chr)
{ /* 'chr' always 'i' in this case */ { /* 'chr' always 'i' in this case */
int c = '\0'; int c = '\0';
bool cont = true; bool cont = true;
bool negative = false;
long int value = 0; /* signed */ long int value = 0; /* signed */
while (cont) while (cont)
{ {
if (!cont) break;
c = fgetc(in); c = fgetc(in);
switch ((isdigit(c) != 0) ? '0' : c) /* little hack here */ switch ((isdigit(c) != 0) ? '0' : c) /* little hack here */
{ {
@ -34,19 +33,23 @@ get_integer(int chr)
value *= 10; /* (0 * 10) -> 0, so it works correct */ value *= 10; /* (0 * 10) -> 0, so it works correct */
value += c - '0'; value += c - '0';
break; break;
case '-' :
value *= -1;
break;
case 'e' : /* integer end marker */ case 'e' : /* integer end marker */
cont = false; cont = false;
break; break;
default : /* or garbled data */ case '-' :
if (value == 0) /* true if '-' - first char after 'i' */
{
negative = true;
break;
} /* else we consider it as garbage */
default :
fprintf(stderr, "Garbage after integer: %i%c<\n", value, c); fprintf(stderr, "Garbage after integer: %i%c<\n", value, c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
} }
} }
if (negative) value *= -1;
yajl_gen_integer(gen, value); yajl_gen_integer(gen, value);
} }

Loading…
Cancel
Save