summaryrefslogtreecommitdiff
path: root/docs/examples/cssom-example-1.c
blob: 663f86fff330ff5956c91125b6f55a105c2a9ff1 (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
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */

/**
 *This is an example that shows how to use
 *the CSSOM api of the libcroco CSS2 parsing library.
 *It just parses the CSS document given in argument and
 *it dumps it (serializes) it on the screen.
 *
 *To compile it using gcc, type
 *
 *gcc -g `croco-config --cflags`  `croco-config --libs` -o cssom-example-1 cssom-example-1.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 ;
	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;
	}

	/*****************************************************
	 *Enough plumbering... now, the real libcroco stuffs.
	 ***************************************************/

        /*
         *What we want here is to simply parse
         *a CSS document using the cssom api.
         */
        status = cr_om_parser_simply_parse_file ((const guchar*)argv[i] /*sheet*/,
                                                 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 ;
	}

	return 0 ;
}