dict.c
author viric <viriketo@gmail.com>
Fri, 30 Mar 2012 18:56:20 +0200
changeset 33 ebbedaa090be
parent 14 a961bb8806b9
permissions -rw-r--r--
Adding what I had in the web for zparsetext (akcentiga)
viric@0
     1
#include <stdio.h>
viric@0
     2
#include "dictre.h"
viric@0
     3
viric@0
     4
static char * get_string(FILE *index, char *buffer)
viric@0
     5
{
viric@0
     6
    int c;
viric@0
     7
    int count;
viric@0
     8
viric@0
     9
    count = 0;
viric@0
    10
    do
viric@0
    11
    {
viric@0
    12
        c = fgetc(index);
viric@0
    13
        if(c == EOF)
viric@0
    14
        {
viric@0
    15
            count = 1; /* for next [count-1] */
viric@0
    16
            break;
viric@0
    17
        }
viric@0
    18
        buffer[count++] = (char) c;
viric@0
    19
    } while(c != '\t' && c != '\n');
viric@0
    20
viric@0
    21
    buffer[count-1] = '\0';
viric@0
    22
}
viric@0
    23
viric@0
    24
char * get_word(FILE *index)
viric@0
    25
{
viric@0
    26
    char buffer[500];
viric@0
    27
    char *out;
viric@0
    28
    int len;
viric@0
    29
viric@0
    30
    get_string(index, buffer);
viric@0
    31
viric@0
    32
    if ((len = strlen(buffer)) > 0)
viric@0
    33
    {
viric@0
    34
        out = (char *) malloc(len + 1);
viric@0
    35
        strcpy(out, buffer);
viric@0
    36
    }
viric@0
    37
    else
viric@0
    38
        out = 0;
viric@0
    39
viric@0
    40
    return out;
viric@0
    41
}
viric@0
    42
viric@0
    43
static int char2val(char letter)
viric@0
    44
{
viric@0
    45
    switch(letter)
viric@0
    46
    {
viric@0
    47
        case 'A': return 0;
viric@0
    48
        case 'B': return 1;
viric@0
    49
        case 'C': return 2;
viric@0
    50
        case 'D': return 3;
viric@0
    51
        case 'E': return 4;
viric@0
    52
        case 'F': return 5;
viric@0
    53
        case 'G': return 6;
viric@0
    54
        case 'H': return 7;
viric@0
    55
        case 'I': return 8;
viric@0
    56
        case 'J': return 9;
viric@0
    57
        case 'K': return 10;
viric@0
    58
        case 'L': return 11;
viric@0
    59
        case 'M': return 12;
viric@0
    60
        case 'N': return 13;
viric@0
    61
        case 'O': return 14;
viric@0
    62
        case 'P': return 15;
viric@0
    63
        case 'Q': return 16;
viric@0
    64
        case 'R': return 17;
viric@0
    65
        case 'S': return 18;
viric@0
    66
        case 'T': return 19;
viric@0
    67
        case 'U': return 20;
viric@0
    68
        case 'V': return 21;
viric@0
    69
        case 'W': return 22;
viric@0
    70
        case 'X': return 23;
viric@0
    71
        case 'Y': return 24;
viric@0
    72
        case 'Z': return 25;
viric@0
    73
        case 'a': return 26;
viric@0
    74
        case 'b': return 27;
viric@0
    75
        case 'c': return 28;
viric@0
    76
        case 'd': return 29;
viric@0
    77
        case 'e': return 30;
viric@0
    78
        case 'f': return 31;
viric@0
    79
        case 'g': return 32;
viric@0
    80
        case 'h': return 33;
viric@0
    81
        case 'i': return 34;
viric@0
    82
        case 'j': return 35;
viric@0
    83
        case 'k': return 36;
viric@0
    84
        case 'l': return 37;
viric@0
    85
        case 'm': return 38;
viric@0
    86
        case 'n': return 39;
viric@0
    87
        case 'o': return 40;
viric@0
    88
        case 'p': return 41;
viric@0
    89
        case 'q': return 42;
viric@0
    90
        case 'r': return 43;
viric@0
    91
        case 's': return 44;
viric@0
    92
        case 't': return 45;
viric@0
    93
        case 'u': return 46;
viric@0
    94
        case 'v': return 47;
viric@0
    95
        case 'w': return 48;
viric@0
    96
        case 'x': return 49;
viric@0
    97
        case 'y': return 50;
viric@0
    98
        case 'z': return 51;
viric@0
    99
        case '0': return 52;
viric@0
   100
        case '1': return 53;
viric@0
   101
        case '2': return 54;
viric@0
   102
        case '3': return 55;
viric@0
   103
        case '4': return 56;
viric@0
   104
        case '5': return 57;
viric@0
   105
        case '6': return 58;
viric@0
   106
        case '7': return 59;
viric@0
   107
        case '8': return 60;
viric@0
   108
        case '9': return 61;
viric@0
   109
        case '+': return 62;
viric@8
   110
        case '/': return 63;
viric@0
   111
        default:
viric@0
   112
                  return 0;
viric@0
   113
    }
viric@0
   114
}
viric@0
   115
viric@14
   116
int str2int_len(const char *str, int length)
viric@0
   117
{
viric@0
   118
    int i = 0;
viric@0
   119
    int val = 0;
viric@0
   120
viric@0
   121
    while (i < length)
viric@0
   122
    {
viric@0
   123
        val = char2val(str[i]) + val * 64;
viric@0
   124
        ++i;
viric@0
   125
    }
viric@0
   126
viric@0
   127
    return val;
viric@0
   128
}
viric@0
   129
viric@14
   130
int str2int(const char *str)
viric@14
   131
{
viric@14
   132
    int length = strlen(str);
viric@14
   133
    return str2int_len(str, length);
viric@14
   134
}
viric@14
   135
viric@0
   136
int get_int(FILE *index)
viric@0
   137
{
viric@0
   138
    char buffer[500];
viric@0
   139
    int val;
viric@0
   140
viric@0
   141
    get_string(index, buffer);
viric@0
   142
viric@0
   143
    if (strlen(buffer) > 0)
viric@0
   144
    {
viric@0
   145
        val = str2int(buffer);
viric@0
   146
    } else
viric@0
   147
        val = -1;
viric@0
   148
viric@0
   149
    return val;
viric@0
   150
}
viric@0
   151
viric@0
   152
char * get_def(FILE *fdefs, int offset, int length)
viric@0
   153
{
viric@0
   154
    char *out;
viric@0
   155
    fseek(fdefs, offset, SEEK_SET);
viric@0
   156
viric@10
   157
    out = (char *) malloc(length+1);
viric@0
   158
    fread(out, length, 1, fdefs);
viric@10
   159
    out[length] = 0;
viric@0
   160
    return out;
viric@0
   161
}
viric@8
   162
viric@8
   163
static char num_to_ia5char(int n)
viric@8
   164
{
viric@8
   165
    /* From RFC 1421 */
viric@8
   166
    if (n >= 0 && n <= 25)
viric@8
   167
        return 'A' + n;
viric@8
   168
    else if (n >= 26 && n <= 51)
viric@8
   169
        return 'a' + (n - 26);
viric@8
   170
    else if (n >= 52 && n <= 61)
viric@8
   171
        return '0' + (n - 52);
viric@8
   172
    else if (n == 62)
viric@8
   173
        return '+';
viric@8
   174
    else if (n == 63)
viric@8
   175
        return '/';
viric@8
   176
    else
viric@8
   177
        return '!'; /* Error */
viric@8
   178
}
viric@8
   179
viric@8
   180
int num_to_ia5(char *dest, int n)
viric@8
   181
{
viric@8
   182
    char tmp[20];
viric@8
   183
viric@8
   184
    int i, max;
viric@8
   185
   
viric@8
   186
    for(i =0; i <= 10; ++i)
viric@8
   187
    {
viric@8
   188
        tmp[i] = num_to_ia5char(n % 64);
viric@8
   189
        if (n < 64)
viric@8
   190
            break;
viric@8
   191
        n /= 64;
viric@8
   192
    }
viric@8
   193
viric@8
   194
    max = i;
viric@8
   195
viric@8
   196
    /* reverse the number */
viric@8
   197
    for (i=0; i<=max; ++i)
viric@8
   198
        dest[i] = tmp[max-i];
viric@8
   199
viric@8
   200
    /* Ending '\0' */
viric@8
   201
    dest[max+1] = '\0';
viric@8
   202
    return max;
viric@8
   203
}