/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001-2003 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include "korean.h"
#include <string.h>

#ifdef _MSC_VER
// Visual C++는 strncasecmp를 지원하지 않는다
#define strncasecmp strnicmp
#endif

#if USE_555_FORMAT
// Assume the 16 bit graphics data is in 5-5-5 format
#define RGB_TO_16(r,g,b)	(((((r)>>3)&0x1F) << 10) | ((((g)>>3)&0x1F) << 5) | (((b)>>3)&0x1F))
#define RED_FROM_16(x)		((((x)>>10)&0x1F) << 3)
#define GREEN_FROM_16(x)	((((x)>>5)&0x1F) << 3)
#define BLUE_FROM_16(x)		(((x)&0x1F) << 3)

//#elif defined(__GP32__) //ph0x
// GP32 format 5-5-5-1 (first bit means intensity)
//#define RGB_TO_16(r,g,b)	(((((r)>>3)&0x1F) << 11) | ((((g)>>3)&0x1F) << 6) | (((b)>>3)&0x1F)<<1)
//#define RED_FROM_16(x)		((((x)>>11)&0x1F) << 3)
//#define GREEN_FROM_16(x)	((((x)>>6) &0x1F) << 3)
//#define BLUE_FROM_16(x)		((((x)>>1) &0x1F) << 3)

#else
// Assume the 16 bit graphics data is in 5-6-5 format
#define RGB_TO_16(r,g,b)	(((((r)>>3)&0x1F) << 11) | ((((g)>>2)&0x3F) << 5) | (((b)>>3)&0x1F))
#define RED_FROM_16(x)		((((x)>>11)&0x1F) << 3)
#define GREEN_FROM_16(x)	((((x)>>5)&0x3F) << 2)
#define BLUE_FROM_16(x)		(((x)&0x1F) << 3)
#endif

//enum FONT_PATTERN {	NORMAL_PATTERN = 0,	INC16_PATTERN,	DEC16_PATTERN,
//					INC8_PATTERN,	DEC8_PATTERN,	DECINC_PATTERN,	INCDEC_PATTERN, };
enum FONT_FILE_TYPE{HANGUL_FNT,	ENGLISH_FNT,};

/*char HANKSB[2350][32];*/
char HANKSB[2350][30]; // 16 x 15
char HANKSS[2350][18]; //  9 x 10
char EFB[256][16]; // 8 x 16
char EFS[256][8]; //  8 x 8

int ENGLISH_WIDTH = 0;
int HANGUL_WIDTH  = 0;
int SPACE_WIDTH   = 0;

int kFontHeight=0; // 한글의 폰트 높이
int eFontHeight=0; // 영문의 폰트 높이

//char *pPattern;
//char pattNormal[16]   ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


FILE *korOutput;

int xxi;				// 콘솔용
char strBuf[8000][256];	// 콘솔용 대사 임시 저장(메모리 낭비가... 2mb나...)

char gameFN[16];
char Kostr[16];

char k_parse_buffer[1024]; // 메모리를 동적이 아니라 정적으로...

STRKOR KorStrings[48];
STRKOR kor_str_desc[48];
STRKORSMUSH kor_str_smush[48];

typedef struct {
	char *data;
	uint16 attr;
}STRKORBUFFER;

STRKORBUFFER *korBuffer;	// 한글 대사 파일이 저장될 곳
//char **korBuffer;	// 한글 대사 파일이 저장될 곳
int korMax;			// 대사의 총 라인 수

int desc_num;		//description의 현재 출력 번호.

KOR_Color *kpalette;

#ifdef YOPY
bool _kor_font = false;		// 큰 폰트 사용?
bool _kor_mode = false;		// 한글 자막 사용?
bool _not_eng = false;		// 영어 자막 출력?
bool isCMI;			// COMI인가?
#else
bool _kor_font;		// 큰 폰트 사용?
bool _kor_mode;		// 한글 자막 사용?
bool _not_eng;		// 영어 자막 출력?
bool isCMI;			// COMI인가?
#endif

void add2stack(char *buf, uint16 xpos, uint16 ypos, short delay, uint8 pal)
{
	int num;
	for(num = 0; num < MAX_KOR - 1; num++)
		if(!(KorStrings[num].buffer[0] && 0))
			break;
	KorStrings[num].xpos = xpos;
	KorStrings[num].ypos = ypos;
	KorStrings[num].delay = delay;
	KorStrings[num].color = pal;
	strcpy(KorStrings[num].buffer, buf);
	KorStrings[num].remainflag = 1;
	KorStrings[num].remainstart = 1;
	KorStrings[num].remainend = 0;
	//printf("Init string %s as #%d x %d y %d delay %d color %d\n",KorStrings[num].buffer,num,xpos,ypos,delay,pal);
}
void add2stack_desc(char *buf, uint16 xpos, uint16 ypos, short delay, uint8 pal)
{
	int num;
	//for(num=0;num<MAX_KOR-1;num++)
	//for(num=0;num<3;num++)
		//if(!(kor_str_desc[num].buffer[0] && (kor_str_desc[num].delay)))
		//if(!(kor_str_desc[num].remainflag))
			//break;
	//desc_num = 1 - desc_num;
	desc_num = 0;
	num = desc_num;
	strcpy(kor_str_desc[num].buffer, buf);
	kor_str_desc[num].xpos = xpos;
	kor_str_desc[num].ypos = ypos;
	kor_str_desc[num].delay = delay;
	kor_str_desc[num].color = pal;
	kor_str_desc[num].remainflag = 1;
	kor_str_desc[num].remainstart = 1;
	kor_str_desc[num].remainend = 0;
	//printf("Init desc as #%d x %d y %d delay %d color %d\n",num,xpos,ypos,delay,pal);//*/
}
void add2stack_smush(char *buf, long xpos, long ypos, uint8 col, int tmp)
{
	int num;
	for(num = 0; num < MAX_KOR - 1; num++)
		if(!(kor_str_smush[num].buffer[0] && kor_str_smush[num].remain))
			break;
	kor_str_smush[num].xpos = MAX(xpos, 2);
	//kor_str_smush[num].xright=320-xpos;
	kor_str_smush[num].ypos = MAX(ypos, 2);
	kor_str_smush[num].color = col;
	kor_str_smush[num].remain = 1;
	strcpy(kor_str_smush[num].buffer, buf);
	//printf("SMUSH #%d %x flag %d\n%s\n%s\n",
			//num, &kor_str_smush[num].buffer[0], tmp, buf, kor_str_smush[num].buffer);
	//printf("SMUSH #%d %s x %d y %d c %d flag %d\n",
			//num, kor_str_smush[num].buffer, kor_str_smush[num].xpos,
			//kor_str_smush[num].ypos, kor_str_smush[num].color, tmp);
}

void LoadKorFiles()
{
#ifdef YOPY
	if(_kor_font)
		InitXddFont(1,"/usr/share/scummvmk/font/koreanb.fnt", "/usr/share/scummvmk/font/engb.fnt");
	else
		InitXddFont(0,"/usr/share/scummvmk/font/koreans.fnt", "/usr/share/scummvmk/font/engs.fnt");
#else
	if(_kor_font)
		InitXddFont(1,"font/koreanb.fnt", "font/engb.fnt");
	else
		InitXddFont(0,"font/koreans.fnt", "font/engs.fnt");
#endif

	strcpy(Kostr, gameFN);
	LoadKorString(Kostr);

	if(_kor_mode) fprintf(stderr, "한글 자막을 사용합니다\n");
	if(_kor_font) fprintf(stderr, "큰 한글 폰트를 사용합니다\n");
	if(_not_eng)  fprintf(stderr, "영문 자막을 출력하지 않습니다\n");

	if(korOutput = fopen(gameFN, "r")) {
		int k;
		char buf0[256];
		char *pt = buf0;

		for(k = 0; !feof(korOutput); k++) {
			fgets(pt, 256, korOutput);
		}
		fclose(korOutput);
	
		xxi = k;

		//korOutput=fopen(gameFN,"rt");
		korOutput=fopen(gameFN, "r");

		for(k = 0; k < xxi; k++) {
			fgets(pt, 256, korOutput);
			if(strlen(pt)){
				strcpy(strBuf[k], pt);
				strBuf[k][strlen(strBuf[k]) - 1] = 0;
			}
		}
		fclose(korOutput);
	
	}
	korOutput=fopen(gameFN, "at");
}

char *TransDescKor(char *buf) // 인공지능적(?)으로 다른 대사들을 한글로 고침.
{
	int Fin;
	char *tbuf, *sbuf;
	sbuf = tbuf = k_parse_buffer;
	static int CurrKor;

	if(buf[0] == 0x07)
		*buf++;

	Fin = 0;
	int i;
	for(i = CurrKor; i < korMax; i += 2){
		if(!strncasecmp(buf, korBuffer[i].data, strlen(korBuffer[i].data)) && strlen(korBuffer[i].data) == strlen(buf)) {
			strcpy(tbuf, korBuffer[i + 1].data);
			CurrKor = i;
			Fin = 1;
			break;
		}
	}
	if(Fin == 0){
		for(i = 0; i < CurrKor; i += 2){ //한번 더 찾아 줌
			if( !strncasecmp(buf,korBuffer[i].data,strlen(korBuffer[i].data)) && strlen(korBuffer[i].data)==strlen(buf) ) {
				strcpy(tbuf, korBuffer[i + 1].data);
				CurrKor = i;
				Fin = 1;
				break;
			}
		}
	}
	if(Fin==0)
		if(isCMI)
			strcpy(tbuf, buf);
		else
			*tbuf = 0;
	return tbuf;
}

char *Convert2Korean(char *buf) //20030110: 치명적인 메모리 침범 버그를 고침.
{
	int Fin;
	char *tbuf,*sbuf;
	sbuf=tbuf=k_parse_buffer;
	static int CurrKor;

		bool mFlg;
		mFlg = 0;
		for(int mi = 0; mi < xxi; mi++) {
			if(!strncasecmp(buf, strBuf[mi], strlen(buf))){
				mFlg = 1;
				break;
			}
		}
		if(!mFlg) {
			strcpy(strBuf[xxi], buf);
			xxi++;
			fprintf(korOutput, "%s\n", buf);
		} //임시 파일에 저장

	Fin = 0;
	//memset(tbuf,0,1023);
	for(int i = CurrKor; i < korMax; i += 2){
		if(!strncasecmp(buf, korBuffer[i].data, strlen(korBuffer[i].data)) && strlen(korBuffer[i].data) == strlen(buf)) {
			strcpy(tbuf, korBuffer[i + 1].data);
			CurrKor = i;
			Fin = 1;
			break;
		}
	}
	if(Fin==0){
		for(i=0;i<CurrKor;i+=2){ //한번 더 찾아 줌
			if(!strncasecmp(buf, korBuffer[i].data, strlen(korBuffer[i].data)) && strlen(korBuffer[i].data) == strlen(buf)) {
				strcpy(tbuf, korBuffer[i + 1].data);
				CurrKor = i;
				Fin = 1;
				break;
			}
		}
	}
	if(Fin == 0)
		if(_not_eng) {
			strcpy(tbuf, buf); //못 찾으면, 영어 문장으로 대체...
			while(*tbuf){
				if(!strncasecmp(tbuf, "<1>", strlen("<1>"))) {
					*tbuf = '\\';
					*(tbuf + 1) = '\\';
					*(tbuf + 2) = 'n';
				}
				if(!strncasecmp(buf, "<3>", strlen("<3>"))) {
					*tbuf = '\\';
					*(tbuf + 1) = '\\';
					*(tbuf + 2) = 'n';
				}
				if(!strncasecmp(tbuf, "<c>", strlen("<c>"))) {
					*tbuf = '\\';
					*(tbuf +1) = '\\';
					*(tbuf +2) = 'c';
					// *(buf+3)=' ';
				}
				tbuf++;
			}
			tbuf = sbuf;
		} else
			*tbuf = 0; //영어문장 안나올때만 대체함.
	return tbuf;
}

//////////////////////
//한글 출력
//////////////////////
Uint16 RGB2(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B)
{
    return SDL_MapRGB(screen->format, R, G, B);
}

void Put_Eng16(Uint16 *v, int nWidth, Uint16 color, char *pData)
{
	int w;
	if(_kor_font) w=16; else w=8;
	for(int i=0;i<w;i++)
	{
		if(*pData)
		{
			if(*pData & 0X80) *(v)   = color;
			if(*pData & 0X40) *(v+1) = color;
			if(*pData & 0X20) *(v+2) = color;
			if(*pData & 0X10) *(v+3) = color;
			if(*pData & 0X08) *(v+4) = color;
			if(*pData & 0X04) *(v+5) = color;
			if(*pData & 0X02) *(v+6) = color;
			if(*pData & 0X01) *(v+7) = color;
		}
		v += nWidth;
		pData++;
	}
}

void Put_Han16(uint16 *dst, int _screenWidth, uint16 _color, char *src)
{
	const byte revBitMask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
	int y, x;
	byte bits = 0;
	
	int height, width;
	
	height = _kor_font ? 15 : 9;
	width = 16;
	for (y = 0; y < height /*&& y + drawTop < vs->height*/; y++) {
		for (x = 0; x < width; x++) {
			if ((x % 8) == 0)
				bits = *src++;
			if ((bits & revBitMask[x % 8])/* && y + drawTop >= 0*/) {
				*dst = _color;
			}
			dst++;
		}
		dst += _screenWidth - width;
	}
}

int GetKsIndex(Uint8 fstByte, Uint8 sndByte)
{
	if(sndByte <= 36) // 이상한 일이 일어나는 건 sndByte = 32 일때 뿐이다.
		sndByte += 218;
	return ((int)fstByte  - 0xb0) * 94 + (int)sndByte - 0xa1;
}

int TopMG(Uint8 eb)
{
	if(_kor_font)
		return 0;
	else
		switch(eb) {
		case 'g':
		case 'p':
		case 'q':
		case 'y':
		case ',':
		case '!':
			return 1;
		default:
			return 0;
			break;
		}
}

int GetEW(Uint8 eb)
{
	if(_kor_font)
		return ENGLISH_WIDTH;
	else
		switch(eb) {
		case '_':
			return 9;
		case '5':
		case 'J':
		case 'M':
		case 'W':
		case 'm':
		case 'w':
		case '*':
			return 8;
		case 'e':
			return 7;
		case 'f':
		case 'I':
		case 'i':
		case 'l':
		case 'c':
			return 6;
		case '.':
		case '!':
			return 5;
		case ' ':
			return 4;
		default:
			return 7;
			break;
		}
}

int PutFont(SDL_Surface *screen, int xPos, int yPos, int w, int h,
			Uint16 color,char *pStr,short ClipWidth )//16bit 전용
{
	int px;
	Uint8 fstByte, secByte;
	Uint16 *ptr;
	px = xPos;
    if ( SDL_MUSTLOCK(screen) ) {
        if ( SDL_LockSurface(screen) < 0 ) {
			printf("PutFont() : Lock 실패\n");
            return xPos;
        }
    }
	ptr = (Uint16 *)screen->pixels + yPos * screen->pitch / 2 + xPos;
	while(*pStr){
		fstByte = *pStr++;
		if ( (fstByte & 0x80) /*&& !(fstByte==0xfa)*/ ) {  // 한글
			if ( xPos >= 0 && xPos <= (w - (HANGUL_WIDTH+(1 - ClipWidth))) ) {
		if(yPos<(isCMI ? 480 : 200)-kFontHeight) {
					ptr =(Uint16 *)screen->pixels + yPos * screen->pitch / 2 + xPos;
					if (_kor_font)	Put_Han16(ptr,(screen->pitch / 2),color,HANKSB[GetKsIndex(fstByte,*pStr)]);
									Put_Han16(ptr,(screen->pitch / 2),color,HANKSS[GetKsIndex(fstByte,*pStr)]);
				}
			} else {
				if(xPos > (w - (HANGUL_WIDTH+(1-ClipWidth)))) {
					xPos = px;
					yPos+=kFontHeight+2;
					if(yPos<(isCMI ? 480 : 200)-kFontHeight) {
						ptr = (Uint16 *)screen->pixels + yPos * screen->pitch / 2 + xPos;
						if (_kor_font)	Put_Han16(ptr,(screen->pitch / 2),color,HANKSB[GetKsIndex(fstByte,*pStr)]);
						else			Put_Han16(ptr,(screen->pitch / 2),color,HANKSS[GetKsIndex(fstByte,*pStr)]);
					}
				}
			}
			ptr += HANGUL_WIDTH;
			pStr++;
			xPos += HANGUL_WIDTH;

		} else {
			if ( (fstByte != ' ' /*&& fstByte !=0xfa*/) ) {// 영어
				if ( fstByte == '\\' )	{
					secByte = *pStr++;

					if ( secByte == 'n' || secByte == 'N' )	{
						xPos  = px;
						yPos+=kFontHeight+2;
						ptr = (Uint16 *)screen->pixels + (yPos) * screen->pitch / 2 + xPos;
					} else if ( secByte == 'c' || secByte == 'C' )	{
						uint8 d=*pStr++;
						if (color) // 검은 색(0,0,0)이 아니라면
							color = RGB_TO_16(kpalette[d].r, kpalette[d].g, kpalette[d].b);
					} else pStr--;

				} else if ( fstByte == 0x0d ) {
					secByte = *pStr++;

					if ( secByte == 0x0a )	{
						xPos  = px;
						yPos+=kFontHeight+2;
						ptr = (Uint16 *)screen->pixels + (yPos) * screen->pitch / 2 + xPos;
					} else pStr--;

				} else {

				        if ( xPos >= 0 && xPos <= (w - (ENGLISH_WIDTH+(1-ClipWidth))) )	{
							if(yPos<(isCMI ? 480 : 200)-kFontHeight) {
								ptr = (Uint16 *)screen->pixels + (yPos+1+TopMG(fstByte)) * screen->pitch / 2 + xPos;
								if(_kor_font)	Put_Eng16(ptr,(screen->pitch / 2),color,*(EFB+fstByte));
								else			Put_Eng16(ptr,(screen->pitch / 2),color,*(EFS+fstByte));
							}
						} else {
							if(xPos>(w - (ENGLISH_WIDTH+(1-ClipWidth)))) {
								xPos = px;
								yPos+=kFontHeight;
								if(yPos<(isCMI ? 480 : 200)-kFontHeight) {
									ptr = (Uint16 *)screen->pixels + (yPos+1+TopMG(fstByte)) * screen->pitch / 2 + xPos;
									if(_kor_font)	Put_Eng16(ptr,(screen->pitch / 2),color,*(EFB+fstByte));
									else			Put_Eng16(ptr,(screen->pitch / 2),color,*(EFS+fstByte));
								}
							}
						}
						ptr += GetEW(fstByte);

					xPos += GetEW(fstByte);
				}
			} else {
				ptr += GetEW(' ');
				xPos += GetEW(' ');
			}
		}
	}
	if ( SDL_MUSTLOCK(screen) )
        SDL_UnlockSurface(screen);
	return xPos;
}

int PutFontOutline( SDL_Surface *screen, int xPos,int yPos, int w, int h, uint16 color,char *fmt,... )
{
	char buffer[256], str[256];

	va_list argptr;
	va_start(argptr, fmt);
	vsprintf(buffer,fmt,argptr);
	va_end(argptr);
	if(!isCMI) {
		strcpy(str, buffer);
		PutFont(screen, xPos, yPos - 1, w, h, 0, str, 0);
		strcpy(str, buffer);
		PutFont(screen, xPos, yPos + 1, w, h, 0, str, 0);
	}

	strcpy(str, buffer);
	PutFont(screen, xPos-1, yPos, w, h, 0, str, -1);
	strcpy(str, buffer);
	PutFont(screen, xPos+1, yPos, w, h, 0, str, +1);
	
	if(!isCMI) {
		strcpy(str, buffer);
		PutFont(screen, xPos-1, yPos-1, w, h, 0, str, -1);
		strcpy(str, buffer);
		PutFont(screen, xPos+1, yPos+1, w, h, 0, str, +1);
		strcpy(str, buffer);
		PutFont(screen, xPos-1, yPos+1, w, h, 0, str, -1);
		strcpy(str, buffer);
		PutFont(screen, xPos+1, yPos-1, w, h, 0, str, +1);
	}

	return PutFont(screen, xPos, yPos, w, h, color, buffer, 0);
}

bool LoadFont(bool size, char *pFile, Uint16 wType)
{
	FILE *fp;
	fp = fopen(pFile, "rb");
	if(fp == NULL)
		return false;

	if(wType == ENGLISH_FNT) {
		if(size)
			fread(EFB, 4096, 1, fp);
		else
			fread(EFS, 2048, 1, fp);
	} else {
		if(size)
			fread(HANKSB, 70500, 1, fp);
		else
			fread(HANKSS, 42300, 1, fp);
	}

	fclose(fp);
	return true;
}

void SetFontWidth(int nHangul,int nEnglish,int nSpace)
{
	HANGUL_WIDTH  = nHangul;
	ENGLISH_WIDTH = nEnglish;
	SPACE_WIDTH   = nSpace;
}

bool InitXddFont(bool size, char *pHan,char *pEng)
{
	bool ret = false;

	//pPattern = &pattNormal[0];
	if(pHan) {
		ret = LoadFont(size, pHan, HANGUL_FNT);
		if(!ret) {
			fprintf(stderr, "WARNING: 한글 폰트 파일을 로드할 수 없습니다!\n");
			return false;
		}
		if(size) {
			SetFontWidth(16, ENGLISH_WIDTH, 8);
			kFontHeight = 16;
		} else {
			SetFontWidth(10, ENGLISH_WIDTH, 8);
			kFontHeight = 10;
		}
	}

	if(pEng) {
		ret = LoadFont(size, pEng, ENGLISH_FNT);
		SetFontWidth(HANGUL_WIDTH,8,8);
		eFontHeight=8;
	}
	fprintf(stderr,"한글 폰트가 로드되었습니다!\n");
	return ret;
}
bool DeleteKorString()
{
	for(int k = 0; k < korMax; k++)
		free(korBuffer[k].data);
	delete []korBuffer;
	return true;
}
bool LoadKorString(char *gn)
{
	FILE *fp;
	char fname[256];

	if(!strncasecmp(gn, "monkeyega", 9))
		strcpy(gn, "monkey");
	if(!strncasecmp(gn, "monkeyvga", 9))
		strcpy(gn, "monkey");
	if(!strncasecmp(gn, "monkey1", 7))
		strcpy(gn, "monkey");
	if(!strncasecmp(gn, "indy3ega", 8))
		strcpy(gn, "indy3");

#ifdef YOPY
	sprintf(fname, "/usr/share/scummvmk/sub/kor_%s.dat", gn);
#else
	sprintf(fname, "sub/kor_%s.dat", gn);
#endif
	
	if(!(fp = fopen(fname, "rt"))) {
		fprintf(stderr, "WARNING: 한글 자막 파일을 로드할 수 없습니다!\n");
		return false;
	}

	int k;
	char buf[1024];
	char *ptr = buf;

	for(k = 0; !feof(fp); k++) {
		fgets(ptr, 256, fp);
	} //파일의 라인 수를 계산
	fseek(fp, 0, SEEK_SET);
	//fclose(fp);

	korBuffer = new STRKORBUFFER[k];
	korMax = k;

	//fp = fopen(fname, "rt");
	for(k = 0; k < korMax; k++) {
		fgets(ptr, 256, fp);
		korBuffer[k].data = new char[strlen(ptr) + 1];
		if(strlen(ptr)) {
			if(ptr[1] == '%') {
				*ptr++;
				korBuffer[k].attr = 1;
			}
			strcpy(korBuffer[k].data, ptr);
			if(korBuffer[k].data[strlen(korBuffer[k].data) - 1] == '\n' ||
				korBuffer[k].data[strlen(korBuffer[k].data) - 1] == 0x0a ||
				korBuffer[k].data[strlen(korBuffer[k].data) - 1] == 0x0d)
				korBuffer[k].data[strlen(korBuffer[k].data) - 1] = 0;
		}
		//korBuffer[k].data[strlen(korBuffer[k].data)-1]=0;
		//printf("@%s@\n",korBuffer[k].data);
	}
	fclose(fp);

	fprintf(stdout, "한글 자막 파일이 로드되었습니다 -- 총 %d 라인\n", korMax);

	return true;
}
