/*
	猫の目ビュア for WonderWitch (on WonderSwan, WonderSwanColor)
			
		Copyright (c) 2000,2001, B-soft. All rights reserved.
		Programmed by KAKKO
*/

#include <stdio.h>
#include <string.h>
#include <sys/bios.h>

#include "neko_view.h"

extern void ini_font(void);
extern void drw_neko(int n);
extern void drw_page(char far *buff);
extern void drw_header(void);

static FILE far *fp = NULL;
static int page = 0;
static long page_pos[100];
static long total_pos = 0;
static int eof_flg = 0;

static int sc1[] = {0, 3, 0, 6, 0, 9, 0, 12, 0, 15, 0, 18, 0};
static int sc2[] = {0, 0, 4, 0, 7, 0, 10, 0, 13, 0, 16, 0, 19};


void tx_put_string(int posX, int posY, unsigned char *str)
{
	if((posY % 2) != 0){
		text_set_screen(0);
		text_put_string(posX, sc1[posY], str);
	}
	else{
		text_set_screen(1);
		text_put_string(posX, sc2[posY], str);
	}
	text_set_screen(0);

}

void tx_put_char(int posX, int posY, int str)
{
	if((posY % 2) != 0){
		text_set_screen(0);
		text_put_char(posX, sc1[posY], str);
	}
	else{
		text_set_screen(1);
		text_put_char(posX, sc2[posY], str);
	}
	text_set_screen(0);
}


int rea_txt(void)
{
	int i, j, k;
	int tn;
	int ch;
	int sp = ' ';
	char buff[10];
	
	page_pos[0] = 0;
	
	if(fp == NULL){
		return -1;
	}
	
	fgetc(fp);		/* flush */
	clearerr(fp);
	fseek(fp, page_pos[page], SEEK_SET);
	
	i = 0;
	j = 1;
	while(1){
		ch = fgetc(fp);
		total_pos++;
		if(ch == EOF){
			while(j < MAX_LINE_NUM){
				while(i < CHAR_NUM_OF_ONELINE){
					tx_put_char(i, j, sp);
					i++;
				}
				j++;
				i = 0;
			}
			total_pos--;
			break;
		}
		
		if(ch > 0x80){
			ch = ch << 8;
			ch |= fgetc(fp);
			total_pos++;
		}
		if(ch == 0x0a){
			while(i < CHAR_NUM_OF_ONELINE){
				tx_put_char(i, j, sp);
				i++;
			}
			j++;
			i = 0;
			ch = fgetc(fp);
			total_pos++;
			if(ch != 0x0d){
				ungetc(ch, fp);
				total_pos--;
			}
		}
		else if(ch == 0x0d){
			while(i < CHAR_NUM_OF_ONELINE){
				tx_put_char(i, j, sp);
				i++;
			}
			j++;
			i = 0;
			ch = fgetc(fp);
			total_pos++;
			if(ch != 0x0a){
				ungetc(ch, fp);
				total_pos--;
			}
		}
		else{
			if(ch == '\t'){
				tn = TAB_NUM - (i % TAB_NUM);
				for(k = 0; k < tn; k++){
					tx_put_char(i++, j, sp);
				}
			}
			else{
				tx_put_char(i, j, ch);
				i++;
			}
			if(i >= CHAR_NUM_OF_ONELINE){
				i = 0;
				j++;
			}
		}
		if(j > LINE_NUM){
			break;
		}
	}
	if(ch != EOF){
		page++;
		page_pos[page] = total_pos;
		sprintf(buff, "%d", page);
		eof_flg = 0;
	}
	else{
		clearerr(fp);
		fseek(fp, page_pos[page], SEEK_SET);
		total_pos = page_pos[page];
		sprintf(buff, "%d", page + 1);
		eof_flg++;
		drw_header();
	}
	
	ini_font();
	drw_neko(page % 2);
	drw_page(buff);

	return page;
}

int rea_prv_txt(void)
{
	if(page < 1){
		return 0;
	}
	
	if(eof_flg == 0){
		page -= 2;
	}
	else{
		page--;
	}
	
	if(page < 0){
		page = 0;
		fseek(fp, 0, SEEK_SET);
		total_pos = 0;
	}
	else{
		fseek(fp, page_pos[page], SEEK_SET);
		total_pos = page_pos[page];
	}
	
	return rea_txt();
}


int rea_now_txt(void)
{
	clearerr(fp);
	rewind(fp);
	
	if(page <= 0){
		return 0;
	}
	
	page -= 1;
	fseek(fp, page_pos[page], SEEK_SET);
	total_pos = page_pos[page];
	
	return rea_txt();
}


int cls_fil(void)
{
	page = 0;
	total_pos = 0;
	page_pos[0] = 0;
	
	return fclose(fp);
}


int opn_fil(char *fname)
{
	if(fp != NULL){
		cls_fil();
	}
	
	fp = fopen(fname, "r");
	
	if(fp == NULL){
		return -1;
	}
	page = 0;
	total_pos = 0;
	page_pos[0] = 0;
	
	return 0;
}

