/*
 * Copyright (c) 1991-1994  Sony Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Sony Corporation
 * shall not be used in advertising or otherwise to promote the sale, use
 * or other dealings in this Software without prior written authorization
 * from Sony Corporation.
 *
 */

/*
 * $SonyRCSfile: string.c,v $
 * $SonyRevision: 1.1 $
 * $SonyDate: 1994/06/03 08:00:45 $
 */

#include "sj4_dict_const.h"

#include "sj4mkdic.h"

#include "sj_ucs2euc.h"

int bubun_str(u_char* p1, int l1, u_char* p2, int l2) {
	u_char* p;
	int	l;
	int	i;

	for(p = p1, l = l1; l > 0;) {
		if(l < l2) break;
		for(i = 0; i < l2; i++)
			if(*(p + i) != *(p2 + i)) break;

		if(i >= l2) return 1;

		i = codesize(*p);
		p += i;
		l -= i;
	}

	for(p = p2, l = l2; l > 0;) {
		if(l < l1) break;
		for(i = 0; i < l1; i++)
			if(*(p + i) != *(p1 + i)) break;

		if(i >= l1) return -1;

		i = codesize(*p);
		p += i;
		l -= i;
	}

	return 0;
}

int overlap_str(u_char* p1, int l1, u_char* p2, int l2) {
	u_char* p;
	int	l;
	int	i;
	int	j;
	u_char	tmp[MAXKANJILENGTH * 2 + MAXATRNUMBER * 2];

	for(p = p1, l = l1; l > 0;) {
		if(l < l2) {
			for(i = 0; (i < l) && (*(p + i) == *(p2 + i)); i++);

			if((i >= l) && (l2 + l1 - l < sizeof(tmp))) {
				for(i = 0; i < l1; i++)
					tmp[i] = *(p1 + i);
				for(j = l; j < l2; i++, j++)
					tmp[i] = *(p2 + j);

				if(check_hindo(tmp, i)) {
					return -1;
				}
			}
		}

		i = codesize(*p);
		p += i;
		l -= i;
	}

	for(p = p2, l = l2; l > 0;) {
		if(l < l1) {
			for(i = 0; (i < l) && (*(p + i) == *(p1 + i)); i++);

			if((i >= l) && (l1 + l2 - l < sizeof(tmp))) {

				for(i = 0; i < l2; i++)
					tmp[i] = *(p2 + i);
				for(j = l; j < l1; i++, j++)
					tmp[i] = *(p1 + j);

				if(check_hindo(tmp, i)) {
					return -1;
				}
			}
		}

		i = codesize(*p);
		p += i;
		l -= i;
	}

	return 0;
}

int istrlen(int* s) {
	int* p;

	for(p = s; *p; p++);

	return (p - s);
}

int istrcmp(int* s, int* d) {
	for(; *s && (*s == *d); s++, d++);

	return (*s - *d);
}

int top_strcmp(int* src, int* dst) {
	int i = 0;

	for(i = 0; *src && (*src == *dst); src++, dst++)
		i++;

	return (i > MAXYOMIASKNUMBER) ? MAXYOMIASKNUMBER : i;
}

int last_strcmp(int* src, int* dst) {
	int slen, dlen;

	slen = istrlen(src);
	dlen = istrlen(dst);

	if(dlen > MAXYOMIASKNUMBER)
		return 0;

	if(istrcmp(src + (slen - dlen), dst))
		return 0;

	return dlen;
}

int string_cmp(u_char* p1, int l1, u_char* p2, int l2) {
	while(l1 > 0 && l2 > 0) {
		if(*p1 != *p2) return (*p1 - *p2);
		p1++;
		l1--;
		p2++;
		l2--;
	}

	if(l1 == l2) return 0;
	if(l1 > l2) return 1;
	return -1;
}

void utf8_print(char* out, int c) {
	int i = 0, j = 0, k;

	if(c >= 0x10000) {
		out[i++] = ((c >> (6 * 3)) & 7) | 0xf0;

		j = 3;
	} else if(c >= 0x800) {
		out[i++] = ((c >> (6 * 2)) & 15) | 0xe0;

		j = 2;
	} else if(c >= 0x80) {
		out[i++] = ((c >> (6 * 1)) & 31) | 0xc0;

		j = 1;
	} else {
		out[i++] = c;
	}

	for(k = 0; k < j; k++) {
		out[i++] = ((c >> (6 * (j - k - 1))) & 63) | 0x80;
	}

	out[i++] = 0;
}

#ifdef UTF8
int unicode_to_eucjp(int utf) {
	if(0 <= utf && utf <= 0xffff) {
		const T_U2E_BITMAP_INDEX* b = &utf16_to_euc_jp_table[(utf >> 8) & 0xff];

		if(b->byType == 2) {
			if(b->dwBitmapIndex != 0) {
				b = &utf16_to_euc_jp_table[b->dwBitmapIndex + (utf & 0xff)];

				if(b->byType == 3) {
					return b->dwEucJpCode;
				}
			}
		}

		return utf;
	}

	return -1;
}
#endif
