dict.c
author viric@mandarina
Sat, 08 Sep 2007 09:13:15 +0200
changeset 29 84abeba4ef3b
parent 14 a961bb8806b9
permissions -rw-r--r--
Removing binary.
#include <stdio.h>
#include "dictre.h"

static char * get_string(FILE *index, char *buffer)
{
    int c;
    int count;

    count = 0;
    do
    {
        c = fgetc(index);
        if(c == EOF)
        {
            count = 1; /* for next [count-1] */
            break;
        }
        buffer[count++] = (char) c;
    } while(c != '\t' && c != '\n');

    buffer[count-1] = '\0';
}

char * get_word(FILE *index)
{
    char buffer[500];
    char *out;
    int len;

    get_string(index, buffer);

    if ((len = strlen(buffer)) > 0)
    {
        out = (char *) malloc(len + 1);
        strcpy(out, buffer);
    }
    else
        out = 0;

    return out;
}

static int char2val(char letter)
{
    switch(letter)
    {
        case 'A': return 0;
        case 'B': return 1;
        case 'C': return 2;
        case 'D': return 3;
        case 'E': return 4;
        case 'F': return 5;
        case 'G': return 6;
        case 'H': return 7;
        case 'I': return 8;
        case 'J': return 9;
        case 'K': return 10;
        case 'L': return 11;
        case 'M': return 12;
        case 'N': return 13;
        case 'O': return 14;
        case 'P': return 15;
        case 'Q': return 16;
        case 'R': return 17;
        case 'S': return 18;
        case 'T': return 19;
        case 'U': return 20;
        case 'V': return 21;
        case 'W': return 22;
        case 'X': return 23;
        case 'Y': return 24;
        case 'Z': return 25;
        case 'a': return 26;
        case 'b': return 27;
        case 'c': return 28;
        case 'd': return 29;
        case 'e': return 30;
        case 'f': return 31;
        case 'g': return 32;
        case 'h': return 33;
        case 'i': return 34;
        case 'j': return 35;
        case 'k': return 36;
        case 'l': return 37;
        case 'm': return 38;
        case 'n': return 39;
        case 'o': return 40;
        case 'p': return 41;
        case 'q': return 42;
        case 'r': return 43;
        case 's': return 44;
        case 't': return 45;
        case 'u': return 46;
        case 'v': return 47;
        case 'w': return 48;
        case 'x': return 49;
        case 'y': return 50;
        case 'z': return 51;
        case '0': return 52;
        case '1': return 53;
        case '2': return 54;
        case '3': return 55;
        case '4': return 56;
        case '5': return 57;
        case '6': return 58;
        case '7': return 59;
        case '8': return 60;
        case '9': return 61;
        case '+': return 62;
        case '/': return 63;
        default:
                  return 0;
    }
}

int str2int_len(const char *str, int length)
{
    int i = 0;
    int val = 0;

    while (i < length)
    {
        val = char2val(str[i]) + val * 64;
        ++i;
    }

    return val;
}

int str2int(const char *str)
{
    int length = strlen(str);
    return str2int_len(str, length);
}

int get_int(FILE *index)
{
    char buffer[500];
    int val;

    get_string(index, buffer);

    if (strlen(buffer) > 0)
    {
        val = str2int(buffer);
    } else
        val = -1;

    return val;
}

char * get_def(FILE *fdefs, int offset, int length)
{
    char *out;
    fseek(fdefs, offset, SEEK_SET);

    out = (char *) malloc(length+1);
    fread(out, length, 1, fdefs);
    out[length] = 0;
    return out;
}

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 */
}

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;
}