#include <ctype.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "sftlib.h"

PROTO int DB_filelength( FILE *fp )
{
	int len;
	fseek(fp,0,SEEK_END);
	len=ftell(fp);
	fseek(fp,0,SEEK_SET);
	return len;
}


PROTO int DB_atoi( char *str)
{
	int ret;
	sscanf(str, "%i", &ret);
	return ret;
}





#define SEPARATE  "!#$%&(){}<>[]=,;*/+-.?|~@`"
static int kflg=0, kflgo=0;
static int DB_fgetk( FILE *fp) /* 漢字の時は2バイト読み込む */
{
	int ch;
	kflgo=kflg;
	ch=fgetc(fp);
	if( (kflgo==0) && ((ch>=0x81 && ch<=0x9f) || (ch>=0xe0 && ch<=0xfe)) ) {
		kflg=1; /* 漢字の時 */
	} else kflg=0;
	return ch;
}

PROTO int DB_schch( FILE *fp, int x )
{
	int ch ;
	kflgo=kflg=0;
	while(1) {
		ch=DB_fgetk(fp);
		if( ch==EOF )  break;
		if( (kflgo==0) && (ch==x) )  break;
	}
	return ch;
}

PROTO int DB_schstrch( FILE *fp, char *str )
{
	int ch ;
	kflgo=kflg=0;
	while(1) {
		ch=DB_fgetk(fp);
		if( ch==EOF )  break;
    	if( (kflg==0) && (strchr( str , ch)!=NULL)  ) break; 
	}
	return ch;
}


PROTO int getstr( FILE *fp,char *buf,int len) /*" FILE から文字列を読み込む "*/
                               /* 最後NULLを入れますlen=sizeof(buf)は2以上 */
										/* 文字列のバイト数を返すよ   */
{
	int ret;
	do{
		ret=getstr_sub( fp, buf, len); /* getstrのしたうけ */
		if( buf[0]=='#' ) DB_schch( fp, '\n' );
	} while( buf[0]=='#' );
	return ret;
}

PROTO int getstr_sub( FILE *fp, char *buf, int len) /* getstrのしたうけ */
{   
	int i, ch;

	i=0; 
	kflg=0;
	while(	isspace(ch=DB_fgetk(fp)) ); /*' 空白スキップ '*/
	--len; /*' 最後にNULを格納するために1バイト確保する '*/

	if( ch==EOF) {
		buf[0]=0;
		return -1;
	}

	if( kflg==0 && strchr( SEPARATE , ch)!=NULL  ) { 
		buf[0]=ch;
		buf[1]=0;
		return 1;
	}

	if( ch=='\"' )  {
		while(1) {
			ch=DB_fgetk( fp );
			if( (ch==EOF) ) break;
			if( i>= len ) break;
			if( (ch=='\"') && (kflgo==0) ) break;
			buf[i]=ch; i++;
		}
		if(kflg==1) { --i;ungetc(buf[i], fp); }
		buf[i]=0;
		return (i);
	}

	while(1){
		buf[i]=ch; i++;
		ch=DB_fgetk(fp);
		if( ch==EOF ) { break;}
		if( i>= len )     { break;}
		if( isspace(ch) ) { break;}
		if( strchr( SEPARATE "\"", ch) && (kflgo==0)  ){
			ungetc(ch,fp); break;
		}
	}
	if(kflg==1) {--i; ungetc(buf[i], fp);} 
	buf[i]='\0';
	return (i);
}


PROTO ulbit_t  DB_ctob( char x)   /* 英字1文字をビットに変換する */
{
	unsigned long ret;
	char xx;
	xx= toupper(x);
	if( xx <'A' || xx>'Z' ) ret=0;
		else 	ret=(1 << ( xx-'A' ));
	return ret;
}


PROTO ulbit_t  DB_ctoi( char x)   /* 数字1文字をビットに変換する */
{
	unsigned long ret;
	if( x <'0' || x>'9' ) ret=0;
		else 	ret=(1 << ( x-'0' ));
	return ret;
}


PROTO int data2ind( soft *x , index *y)
{
	char datebuf[64];
	char *code;
	int  x1h, x2h, x3h, x4h, x5h, x6h ;
	time_t t;
		y->dataid=0;
		y->softno=0;
        y->system[0]=x->d_system[0];  /* システム          [A-Z] */
        y->system[1]=x->d_system[1];  /* システム          [A-Z] */
        y->system[2]=x->d_system[2];  /* システム          [A-Z] */
        y->name1h=x->d_name1h;        /* タイトルキー      [A-Z] */
        y->name2h=x->d_name2h;        /* サブタイトルキー  [A-Z] */
        y->makerh=x->d_makerh;        /* メーカーキー      [A-Z] */
        y->abouth=x->d_abouth;        /* 概要キー          [A-Z] */
        y->infomh=x->d_infomh;        /* 周辺機器キー      [A-Z] */
        y->media =x->d_media;         /* メディア           注1    */
        y->sort  =x->d_sort;          /* 分類コード         注2    */
        y->teika =x->d_teika;         /* 定価               円     */
        y->ram   =x->d_ram;           /* 必要メモリ         K Byte */
        y->hd    =x->d_hd;            /* 必要ハードディスク K Byte */
		y->date3 =x->d_date3;         /* 発売日                 */
		y->date4 =x->d_date4;         /* 貸出日                 */
		y->date5 =x->d_date5;         /* 返却予定日             */
		y->date6 =x->d_date6;         /* xxxxxx日                 */
		y->date7 =x->d_date7;         /* yyyyyy日               */
		y->date8 =x->d_date8;         /* zzzzzz日               */
		memset( y->filer, ' ', sizeof(y->filer));
        y->code[0]=x->d_code[0] ;     /* 管理コード         注3    */
        y->code[1]=x->d_code[1] ;     /* 管理コード         注3    */
        y->code[2]=x->d_code[2] ;     /* 管理コード         注3    */
        y->code[3]=x->d_code[3] ;     /* 管理コード         注3    */
		y->codenl=' ';                /* SPACE */
			x1h=isprint(y->name1h)?y->name1h:'-';
			x2h=isprint(y->name2h)?y->name2h:'-';
			x3h=isprint(y->makerh)?y->makerh:'-';
			x4h=isprint(y->abouth)?y->abouth:'-';
			x5h=isprint(y->infomh)?y->infomh:'-';
			code=DB_btoc(y->sort);
			if( strlen(code) ==0 ) x6h='-';
			else if(strlen(code)==1 ) x6h=*code;
			else x6h='*';

			t=x->d_date2;
			sprintf( datebuf,"%s", DB_ctime(&t) );

		sprintf( y->lstbf, "%-*s %c%c%c%c%c:%c %-29s",
			TNAME1_LEN, x->d_name1 , 
			x1h,        /* タイトルキー      [A-Z] */
			x2h,        /* サブタイトルキー  [A-Z] */
			x3h,        /* メーカーキー      [A-Z] */
			x4h,        /* 概要キー          [A-Z] */
			x5h,        /* 周辺機器キー      [A-Z] */
			x6h,        /* ジャンル                */
			datebuf			);
		y->lstbfnl='\0';              /* NULL */
	return 0;
}

PROTO int getcldata( soft *x )
{
	memset( x, 0, sizeof(soft) );
    x->d_system[0]='C';          /* システム         3[A-Z] */
    x->d_system[1]=' ';          /* システム         3[A-Z] */
    x->d_system[2]=' ';          /* システム         3[A-Z] */
    x->d_name1h=' ';             /* タイトルキー      [A-Z] */
    x->d_name2h=' ';             /* サブタイトルキー  [A-Z] */
    x->d_makerh=' ';             /* メーカーキー      [A-Z] */
    x->d_abouth=' ';             /* 概要キー          [A-Z] */
    x->d_infomh=' ';             /* 周辺機器キー      [A-Z] */
    x->d_code[0]=' ';            /* 管理コード         注3    */
    x->d_code[1]=' ';            /* 管理コード         注3    */
    x->d_code[2]=' ';            /* 管理コード         注3    */
    x->d_code[3]=' ';            /* 管理コード         注3    */
	return 0;
}



PROTO char *DB_btoc( ulbit_t x ) /* ビットを英字1文字に変換 */
{
	static char ret[32];
	int i,j;
	j=0;
	for( i=0; i< 26; i++)
	{
	    if( ((x)&(1<<i))!=0 ) {ret[j]='A'+i;j++ ;}
	} 
	ret[j]=0;
	return ret;
}

PROTO int DB_cuttlsp( char *str, int len )
{
	int i, l;
	l=_min(strlen(str), len);
	for( i=l-1; i>=0; --i) {
		if(str[i]!=' ') break;
		str[i]='\0';
	}
	return 0;
}

PROTO char *DB_ctime( time_t *t)
{
	static char ret[28];
	static char* week[7]={ "日","月","火","水","木","金","土"};
	struct tm tx;
	if( *t!=0) {
		tx=*localtime(t);
		sprintf( ret, "%4d年%2d月%2d日(%2s)%02d:%02d:%02d", 
	             1900+(tx.tm_year), 
	             1+(tx.tm_mon), 
	             (tx.tm_mday), 
	             week[tx.tm_wday],
	             (tx.tm_hour), 
	             (tx.tm_min), 
	             (tx.tm_sec) 
		);
	} else strcpy(ret," ------------------------ ");
	return ret;
}
