//---------------------------------------------------------------------------------------------
// 文字コード変換関数群
//---------------------------------------------------------------------------------------------

#ifndef	__CODE2CODE__
#define __CODE2CODE__	1

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

//---------------------------------------------------------------------------------------------

// SHIFT-JIS <-> UTF16変換テーブル用
// 双方UWord範囲
typedef struct {
	UWord	code1;
	UWord	code2;
} U16SJIS;	// 旧名 CODE2CODE (v3.00で変名)

extern const U16SJIS U2STable[];
extern const U16SJIS S2UTable[];
extern int U2STableSize;
extern int S2UTableSize;

//---------------------------------------------------------------------------------------------
// 文字コードはすべてULongで統一 (v3.00)

int		iskanji1Sjis(ULong code);
int		iskanji2Sjis(ULong code);
//
int		setTableSize(const U16SJIS *codeTable);
void	AllTableSize(void);
UWord	u16Sjis(UWord code ,const U16SJIS *codeTable,int sizeOfTable);	// 旧名 code2Code();v3.00
ULong	Utf16ToUtf8(ULong n);
ULong	Utf8ToUtf16(ULong b);
ULong	getUtf8(UByte *(*src0));
ULong	getSjis(UByte *(*src0));
ULong	getSjisNoX68(UByte *(*src0));
ULong	getSjisOrUtf8(UByte *(*src0),int fsjis);
int		utf8Len(UByte *str);
int		sjisLen(UByte *str);
UByte	*storeUtf8(UByte *des,ULong c);
UByte	*storeSjis(UByte *des,ULong c);
UByte	*storeSjisOrUtf8(UByte *des,ULong c,int fsjis);
int		utf8Position(UByte *str,int mojis);
int		sjisPosition(UByte *str,int mojis);
UByte	*utf8Mid(UByte *str1,int pos,int ln);
UByte	*sjisMid(UByte *str1,int pos,int ln);
int		calcLongSize(ULong c);
UByte	*sjisToUtf8S(UByte *source,UByte *buff);
UByte	*utf8ToSjisS(UByte *source,UByte *buff);
ULong	sjisToUtf8(ULong c);
ULong	utf8ToSjis(ULong c);
ULong	sjisToUtf16(ULong c);
ULong	utf16ToSjis(ULong c);

#define CODE_TOCHU	0xffffffff
ULong	chars2Sjis(UByte c);
ULong	chars2Utf8(UByte c);

typedef enum {
	FILE_ASCII=0,	// ASCIIコード範囲しかない
	FILE_UTF8,		// UTF8
	FILE_SJIS,		// SJIS
} CODETYPE;
CODETYPE	checkFileSjisUtf8(FILE *fp,int *fLineNo);
UByte		*loadSourceFileWithCodeConvert(FILE *fp,CODETYPE codeType,UByte *source,int fcutLineNo,UByte crlf);
int			countTabsInFile(FILE *fp);

//------------------------------------------------------------------
// デバッグ用

#ifdef CORE_DEBUG
void CheckCodeTable(const U16SJIS *codeTable);
#else
#define	CheckCodeTable(codeTable)
#endif

//---------------------------------------------------------------------------------------------

#endif
