/*
	str.c
	文字列の関数
*/

#define NULL (0)
#define OFF (0)
#define ON (1)

int instr( char a, const char *s ){ /* 文字列sの文字aの位置 */
	int i;
	for(i=0;s[i]!=NULL;i++)
		if(s[i]==a) return i+1;
	return NULL;
}

int split( char *s, char **str, const char *sep){ /* 文字の切り分け */
	int c = 0,f = OFF;
	str[c] = s;
	while( *++s != NULL ){
		if(*s>0x80 && *s<0xa0
			|| *s>0xdf && *s<0xf0 ){ /* 全角文字なら */
			if(f==ON)
				str[++c]=s++;
			f=OFF;
			continue;
		}
		if(instr(*s,sep)){ /* セパレート文字なら */
			if(f==OFF)
				*s=NULL;
			f=ON;
		} else {
			if(f==ON)
				str[++c]=s;
			f=OFF;
		}
	}
	return c+1;
}

int strcmp( const char *a,const char *b){
	for( ;*a!=NULL && *b!=NULL;a++,b++)
		if(*a!=*b) break;
	return *b-*a;
}

char *strcpy( char *a,const char *b){ /* 文字列のコピー */
	char *c;
	c=a;
	while(*b!=NULL) *c++=*b++;
	*c=NULL;
	return a;
}

char *strcat( char *a,const char *b){ /* 文字列の連結 */
	char *c;
	c=a;
	while(*c!=NULL) c++; /* 最後まで進める */
	while(*b!=NULL) *c++=*b++;
	*c=NULL;
	return a;
}

char *koumoku( char *k ){ /* 項目とりだし */
	char *h;
	for(h=k; *k >= 'A' && *k <='Z' ;k++); /* 大文字であるなら */
	if(h==k) return NULL; /* エラー */
	while( instr(*k,"\t: ")) *k++ = NULL;
	for(h=k;*h!=NULL;h++)
		if(*h=='\n') *h=NULL; /* 改行コードを 消す */
	return k;		/* 返り値は本体のポインタ */
}
