/*
	猫の目ビュア 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 <sys/comm.h>
#include <sys/key.h>
#include <fcntl.h>

#include "neko_view.h"


	static char sbuf[30] = "                            ";

void rs_clearerr(void)
{
	comm_close();
	comm_open();
}

int atoi(char far *str)
{
	int n, num;
	
	num = 0;
	while(*str != 0x00){
		n = *str - '0';
		num = num * 10 + n;
		str++;
	}
	
	return num;
}

int rcv_string(char far *comment, char far *data)
{
	char far *p;
	int n = 0;
	int c;
	int key;
	
	comm_send_string(comment);
	comm_set_timeout(0, -1);
	rs_clearerr();
	
	p = data;
	while(1){
		key = key_press_check();
		if((key & KEY_B) != 0){
			data[0] = 0x00;
			break;
		}
		c = comm_receive_char();
		if((c == 0x0d) || (c == 0x0a)){
			break;
		}
		if((c == '\t') || (c >= 0x20)){
			*p++ = (char)c;
			n++;
			comm_send_char(c);
		}
	}
	*p = 0x00;
	
	comm_set_timeout(-1, -1);
	rs_clearerr();
	
	return n;
}


int get_fname_fsize(char far *fname, int far *fsize)
{
	static char tmp[100];
	
	text_put_string(3, 7, "パソコンからファイル名を入力してください");
	rcv_string("% file name ? ", tmp);
	
	if(strlen(tmp) == 0){
		return -1;
	}
	strcpy(fname, "/ram0/");
	strcat(fname, tmp);
	comm_send_string("\n  save file name : <");
	comm_send_string(fname);
	comm_send_string(">\n");
	text_put_string(0, 7, sbuf);

	text_put_string(1, 7, "パソコンからファイルのサイズを入力してください");
	rcv_string("% file size [byte] ? ", tmp);
/*
	comm_send_string("\n  save file size : <");
	comm_send_string(tmp);
	comm_send_string(" byte>\n");
*/
	if(strlen(tmp) == 0){
		return -1;
	}
	
	*fsize = atoi(tmp);
	sprintf(tmp, "%d", *fsize);
	comm_send_string("\n% save file size : <");
	comm_send_string(tmp);
	comm_send_string("byte>\n");
	
	text_put_string(0, 7, sbuf);
	
	return 0;
}

int rcv_txt(char *fstr)
{
	FILE far *nfp;
	static char fname[100];
	static int fsize;
	int rtn;
	int c;
	int end_flg = 1;
	int key;
	
	comm_set_baudrate(0);
	comm_open();
	
	text_put_string(3, 5, "パソコンからテキストデータを受信します");
	text_put_string(5, 7, "9600bpsでつないでください");
	text_put_string(1, 9, "パソコンの準備ができたらAボタンを押してください");

	text_put_string(11, 17, "B:cancel");
	
	while(1){
		key = key_wait();
		if((key & KEY_B) != 0){
			rtn = -1;
			goto endend;
		}
		else if((key & KEY_A) != 0){
			break;
		}
	}
	text_put_string(0, 5, sbuf);
	text_put_string(0, 7, sbuf);
	text_put_string(0, 9, sbuf);
	
	rtn = get_fname_fsize(fname, &fsize);
	text_put_string(11, 17, "        ");
	
	if(rtn < 0){
		rtn = -1;
		goto endend;
	}
	
	fsize = fsize / 128 + 1;
	
	creat(fname, 6, fsize);
	nfp = fopen(fname, "w");
	if(nfp == NULL){
		comm_send_string("Error!!!!!!\n");
		text_put_string(0, 10, "Can't open file.");
		key_wait();
		rtn = -1;
		goto endend;
	}
	
	text_put_string(2, 5, "パソコンからテキストデータを送信してください");
	text_put_string(2, 7, ".(ピリオド)だけの行を受信すると終了します");
	
	comm_send_string("% send text data, please. \n");
	/*
	text_put_string(1, 8, "Receiving data now. ");
	text_put_string(2, 17, "'.' : end");
	*/
	while(1){
		c = comm_receive_char();
		if(c < 0){
			rs_clearerr();
			continue;
		}
		if(c == '.'){
			if(end_flg != 0){
				break;
			}
			end_flg = 0;
		}
		else if(c < 0x20){
			if((c != 0x0d) && (c != 0x0a) && (c != '\t')){
				continue;
			}
			end_flg++;
		}
		else{
			end_flg = 0;
		}
loop:
		if(fputc((char)c, nfp) == EOF){
			clearerr(nfp);
			goto loop;
		}
	/*
		cnt++;
		if((cnt % 128) == 0){
			sprintf(buff, "%d byte done.", cnt);
			text_put_string(2, 10, buff);
		}
	*/
	}
	
	fclose(nfp);
	strcpy(fstr, fname);
	rtn = 0;
	
endend:
	comm_close();
	comm_set_baudrate(1);
	
	return rtn;
}
