/*  Copyright (C) 2001-2002  Kenichi Suto
 *
 *  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 "defs.h"

// ここからは内部関数
void get_tag_name(gchar *text, gchar *tag){
	gchar *p;
	gint i;

	p = text;
	for(i=0; ; i++, p++){
		tag[i] = '\0';
		if((*p == ' ') || (*p == '>') || (*p == '\0'))
			break;
		tag[i] = *p;
	}

}

// 開始タグを取り出す。"<"">"は含まない
void get_start_tag(gchar *text, gchar *tag){
	gchar *p;
	gint i;


	p = strchr(text, '<');
	if(p == NULL){
//		fprintf(stderr, "get_start_tag: format error\n");
		tag[0] = '\0';
		return;
	}

	// ">"までを開始タグとする
	p++;
	for(i=0; ; i++, p++){
		if(i == 511)
			break;
		if(*p == '\0'){
			tag[0] = '\0';
			return;
		}
		tag[i] = '\0';
		if(*p == '>')
			break;
		tag[i] = *p;
	}

}

// 終了タグを取り出す。"<"">"は含まない
// また、"/"も含まない
void get_end_tag(gchar *text, gchar *tag_name, gchar *tag){
	gchar buff[512];
	gchar *p;
	gint i;

	sprintf(buff, "</%s",tag_name);

	p = strstr(text, buff);
	if(p == NULL){
		fprintf(stderr, "get_end_tag: format error tag_name=%s\n", tag_name);
		tag[0] = '\0';
		return;
	}

	// ">"までを終了タグとする
	p = p + 2;
	for(i=0; ; i++, p++){
		tag[i] = '\0';
		if(*p == '>')
			break;
		tag[i] = *p;
	}

}

void get_content(gchar *text, gchar *tag_name, gchar **content, gint *content_length){
	gchar buff[512];
	gchar *p;

	sprintf(buff, "<%s", tag_name);

	p = strstr(text, buff);
	if((p == NULL) || (p != text)){
		fprintf(stderr, "get_content: format error\n");
		*content = NULL;
		*content_length = 0;
		return;
	}

	// 開始タグの終わりを探す
	while(1){
		if(*p == '>')
			break;
		p++;
	}

	p++;
	*content = p;

	// 終了タグを探す
	sprintf(buff, "</%s", tag_name);

	p = strstr(text, buff);
	if(p == NULL){
		fprintf(stderr, "get_content: format error\n");
		//終了タグがない場合もある
//		*content = NULL;
//		*content_length = 0;
		*content_length = strlen(*content);
		return;
	}
	*content_length = p - *content;

}




void get_attr(gchar *tag, gchar *name, gchar *value){
	gchar buff[512];
	gchar *p;
	gint i;

	sprintf(buff, "%s=", name);

	p = strstr(tag, buff);
	if(p == NULL){
//		fprintf(stderr, "get_attr: attribute %s not found in %s\n", name, tag);
		value[0] = '\0';
		return;
	}

	p = p + strlen(name)+1;
	if(*p == '\"')
		p++;
	for(i=0; ; i++, p++){
		value[i] = '\0';
		if((*p == '\0') || (*p == ' ') || (*p == '>') || (*p == '\"'))
			break;
		value[i] = *p;
	}

}

void skip_start_tag(gchar **text, gchar *tag_name){
	gchar buff[512];
	gchar *p;

	sprintf(buff, "<%s", tag_name);

	p = strstr(*text, buff);
	if(p == NULL){
		fprintf(stderr, "skip_start_tag: format error.\n");
		return;
	}

	while(1){
		if(*p == '\0'){
			*text = p;
			return;
		}
		if(*p == '>')
			break;
		p++;
	}

	p++;
	*text = p;
}

void skip_end_tag(gchar **text, gchar *tag_name){
	gchar buff[512];
	gchar *p;

	sprintf(buff, "</%s", tag_name);

	p = strstr(*text, buff);
	if(p == NULL){
		fprintf(stderr, "skip_end_tag: format error.\n");
		// 終了タグがない場合があるので、とりあえず開始タグをスキップする
		skip_start_tag(text, tag_name);

		return;
	}

	while(1){
		if(*p == '\0'){
			*text = p;
			return;
		}
		if(*p == '>')
			break;
		p++;
	}

	p++;
	*text = p;
}



syntax highlighted by Code2HTML, v. 0.9.1