zrus.c
author viric@llimona
Tue, 28 Aug 2007 01:03:24 +0200
changeset 11 68ea18fe402c
child 12 c755c945a96a
permissions -rw-r--r--
Adding code for the zprocess, for processing the Zaliznjak dictionary.

#include <stdio.h>
#include "dictre.h"

static int closed_accent(const unsigned char *tmp)
{
    if (tmp[0] == 0xcc && tmp[1] == 0x81)
        return 1;
    return 0;
}

static int open_accent(const unsigned char *tmp)
{
    if (tmp[0] == 0x60)
        return 1;
    return 0;
}

/* Must free what is needed */
char * mix_accents(char *a, const char *b)
{
    int ia,ib,o;
    char *out;
    char tmp[MAXWORD];

    ia = 0;
    ib = 0;
    o = 0;
    while(a[ia] != 0 || b[ib] != 0)
    {
        if (closed_accent(&a[ia]))
        {
            tmp[o] = a[ia];
            tmp[o+1] = a[ia+1];
            o+=2;
            ia+=2;
            if(closed_accent(&b[ib]))
                ib+=2;
            continue;
        } else if (closed_accent(&b[ib]))
        {
            tmp[o] = b[ib];
            tmp[o+1] = b[ib+1];
            o+=2;
            ib+=2;
            continue;
        } else if (open_accent(&a[ia]))
        {
            tmp[o] = b[ia];
            o+=1;
            ia+=1;
            if (open_accent(&b[ib]))
                ib+=1;
            continue;
        } else if (open_accent(&b[ib]))
        {
            tmp[o] = b[ib];
            o+=1;
            ib+=1;
            continue;
        }
        else
        {
            /* Letter */
            tmp[o] = a[ia];
            if (a[ia] != 0)
                ++ia;
            if (b[ib] != 0)
                ++ib;
            ++o;
        }
    }
    tmp[o] = 0;
    out = strdup(tmp);
    free(a);
    return out;
}

void remove_accent(unsigned char *dest, const unsigned char *from)
{
    int i,o;

    i = 0;
    o = 0;
    while (from[i] != 0)
    {
        if (from[i] == 0xcc && from[i+1] == 0x81)
            i+=2;
        else if (from[i] == 0x60)
            ++i;
        else
        {
            dest[o] = from[i];
            ++o;
            ++i;
        }
    }
    dest[o] = 0;
}