summaryrefslogtreecommitdiff
path: root/tests/streamhtmlparser/test2.cpp
blob: 3c41cae2a11533796dd15a3f8fdb7d0515c855d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "htmlparser_cpp.h"

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;
using namespace streamhtmlparser;

int main( int argc, char *argv[] )
{
	if( argc != 2 ) {
		cerr << "Usage: test2 <HTML file>\n" << endl;
		return 1;
	}
	
	char *htmlFileName = argv[1];
	
	HtmlParser parser;
	
	ifstream htmlFile( htmlFileName );
	if( !htmlFile.good( ) ) {
		cerr << "ERROR: Can't open file '" << htmlFileName << "'" << endl;
		return 1;
	}

	string link;
	char buf[1] = {0};
	bool in_link = false;

	while( htmlFile.good( ) && !htmlFile.eof( ) ) {
		buf[0] = htmlFile.get( );
		parser.Parse( buf, 1 );
				
		if( parser.state( ) == HtmlParser::STATE_VALUE && parser.tag( ) != NULL && parser.attribute( ) != NULL && parser.value( ) != NULL ) {
			if( strcmp( parser.tag( ), "a" ) == 0 && strcmp( parser.attribute( ), "href" ) == 0 ) {				
				link = parser.value( );
				in_link = true;
			}
		} else if( in_link && parser.state( ) == HtmlParser::STATE_TAG ) {
			cout << link << endl;
			link.clear( );
			in_link = false;
		} else if( parser.state( ) == HtmlParser::STATE_ERROR ) {
			cerr << endl << "ERROR at " << endl;
			return 1;
		}
	}
		
	return 0;
}