Some improvements.
#include <stdio.h>
#include <netinet/in.h>
/*
* idx2index - Part of the flow to convert a StarDict index to a dictd index.
* Author: LluĂs Batlle
* In order to convert a StarDict idx file to a dictd index file, pass:
* ./idx2index < file.idx | LC_ALL=POSIX sort > file.index
* */
static char num_to_ia5char(int n)
{
/* From RFC 1421 */
if (n >= 0 && n <= 25)
return 'A' + n;
else if (n >= 26 && n <= 51)
return 'a' + (n - 26);
else if (n >= 52 && n <= 61)
return '0' + (n - 52);
else if (n == 62)
return '+';
else if (n == 63)
return '-';
else
return '!'; /* Error */
}
static int num_to_ia5(char *dest, int n)
{
char tmp[20];
int i, max;
for(i =0; i <= 10; ++i)
{
tmp[i] = num_to_ia5char(n % 64);
if (n < 64)
break;
n /= 64;
}
max = i;
/* reverse the number */
for (i=0; i<=max; ++i)
dest[i] = tmp[max-i];
/* Ending '\0' */
dest[max+1] = '\0';
return max;
}
static int get_int()
{
int i;
fread(&i, sizeof(int) , 1, stdin);
i = ntohl(i); /* Network to Host order */
return i;
}
static int get_word(char * word)
{
int c;
int count = 0;
do
{
c = getchar();
if (c == EOF)
break;
word[count] = (char) c;
++count;
} while (c != 0);
return count;
}
int main()
{
char word[256];
do
{
int offset, length;
int res;
char c_offset[20], c_length[20];
res = get_word(word);
if (res == 0)
break;
offset = get_int();
num_to_ia5(c_offset, offset);
length = get_int();
num_to_ia5(c_length, length);
printf("%s\t%s\t%s\n", word, c_offset, c_length);
} while(1);
return 0;
}