summaryrefslogtreecommitdiff
path: root/docs/examples/cssom-example.c
blob: 9acffcbe2dd73f59de3118e0d614b721715cad19 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */

/**
 *This is an example that shows how to use
 *the libcroco css2 parsing library.
 *
 *To compile it using gcc, type
 *
 *gcc `croco-config --cflags`  `croco-config --libs` -o cssom-example cssom-example.c
 *
 *Prior to that, you must have compiled and installed libcroco, of course.
 *
 *@author Dodji Seketeli <dodji@seketeli.org>
 */

/*
 *$Id$
 */

#include <string.h>
#include <libcroco.h>

/**
 *Displays the usage of this program.
 *@param prg_name the name of the program.
 */
void
display_usage (char *prg_name)
{
	printf ("\nusage: %s [options] <css file to parse>\n\n", prg_name) ;
	printf ("\twhere options are:\n") ;
	printf ("\t--help|h\tdisplays this help\n") ;
	printf ("\n") ;
}


/**
 *Main entry point.
 *This function illustrates a way to use libcroco.
 *In this example, we will use a CSS Object Model parser
 *to parse a cascading style sheet and dump what we have parsed on stdout.
 *
 *The goal of the object model parser is to parse a css and build
 *an abstract tree out of it. One can then walk the tree to perform
 *whatever action he wants. In our case, the function used to
 *dump the tree on stdout will walk the tree and dump each one of its
 *components (a.k.a. css statements).
 */
int
main (int argc, char **argv)
{
	short i = 0 ;
	enum CRStatus status = CR_OK ;
	CROMParser *parser = NULL ;
	CRStyleSheet *stylesheet = NULL ;

	/*first parse command line arguments*/
	for (i = 1 ; i < argc ; i++)
	{
		if (argv[i][0] != '-')
			break ;
		if (!strcmp (argv[i],"--help") 
		    || !strcmp (argv[i], "-h"))
		{
			display_usage (argv[0]) ;
			return 0 ;
		}
		else
		{
			display_usage (argv[0]) ;
			return 0 ;
		}
	}
	if (i >= argc)
	{
		display_usage (argv[0]) ;
		return 0;
	}

	/*
	 *instanciate a new css object model parser.
	 */	
	parser = cr_om_parser_new (NULL) ;

	if (!parser)
	{
		fprintf (stderr, 
			 "Arrgh!!! Couldn't instanciate the parser.\n");
		return -1 ;
	}

	status = cr_om_parser_parse_file (parser, 
					  argv[i] /*the style sheet 
						    file path*/, 
					  CR_ASCII /*the encoding*/,
					  &stylesheet) ;

	if (status == CR_OK && stylesheet)
	{
		/*
		 *everything went well, 
		 *so dump the stylesheet on stdout.
		 */
		cr_stylesheet_dump (stylesheet, stdout) ;
	}

	if (stylesheet)
	{
		/*
		 *free the the memory used to hold the css
		 *object model
		 */
		cr_stylesheet_destroy (stylesheet) ;
		stylesheet = NULL ;
	}

	/*free the memory used by the parser*/
	cr_om_parser_destroy (parser) ;
	parser = NULL ;

	return 0 ;
}