"Initial commit to Gerrit"
[profile/ivi/libcroco.git] / docs / examples / cssom-example-1.c
1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */
2
3 /**
4  *This is an example that shows how to use
5  *the CSSOM api of the libcroco CSS2 parsing library.
6  *It just parses the CSS document given in argument and
7  *it dumps it (serializes) it on the screen.
8  *
9  *To compile it using gcc, type
10  *
11  *gcc -g -Wall `croco-0.6-config --cflags`  `croco-0.6-config --libs` -o cssom-example-1 cssom-example-1.c
12  *
13  *Prior to that, you must have compiled and installed libcroco, of course.
14  *
15  *@author Dodji Seketeli
16  */
17
18 #include <string.h>
19 #include <libcroco/libcroco.h>
20
21 /**
22  *Displays the usage of this program.
23  *@param prg_name the name of the program.
24  */
25 void
26 display_usage (char *prg_name)
27 {
28         printf ("\nusage: %s [options] <css file to parse>\n\n", 
29                 prg_name) ;
30         printf ("\twhere options are:\n") ;
31         printf ("\t--help|h\tdisplays this help\n") ;
32         printf ("\n") ;
33 }
34
35
36 /**
37  *Main entry point.
38  *This function illustrates a way to use libcroco.
39  *In this example, we will use a CSS Object Model parser
40  *to parse a cascading style sheet and dump what we have parsed on stdout.
41  *
42  *The goal of the object model parser is to parse a css and build
43  *an abstract tree out of it. One can then walk the tree to perform
44  *whatever action he wants. In our case, the function used to
45  *dump the tree on stdout will walk the tree and dump each one of its
46  *components (a.k.a. css statements).
47  */
48 int
49 main (int argc, char **argv)
50 {
51         short i = 0 ;
52         enum CRStatus status = CR_OK ;
53         CRStyleSheet *stylesheet = NULL ;
54
55         /*first parse command line arguments*/
56         for (i = 1 ; i < argc ; i++)
57         {
58                 if (argv[i][0] != '-')
59                         break ;
60                 if (!strcmp (argv[i],"--help") 
61                     || !strcmp (argv[i], "-h"))
62                 {
63                         display_usage (argv[0]) ;
64                         return 0 ;
65                 }
66                 else
67                 {
68                         display_usage (argv[0]) ;
69                         return 0 ;
70                 }
71         }
72         if (i >= argc)
73         {
74                 display_usage (argv[0]) ;
75                 return 0;
76         }
77
78         /*****************************************************
79          *Enough plumbering... now, the real libcroco stuffs.
80          ***************************************************/
81
82         /*
83          *What we want here is to simply parse
84          *a CSS document using the cssom api.
85          */
86         status = cr_om_parser_simply_parse_file ((const guchar*)argv[i] /*sheet*/,
87                                                  CR_ASCII /*the encoding*/,
88                                                  &stylesheet) ;
89         if (status == CR_OK && stylesheet)
90         {
91                 /*
92                  *everything went well, 
93                  *so dump the stylesheet on stdout.
94                  */
95                 cr_stylesheet_dump (stylesheet, stdout) ;
96         }
97
98         if (stylesheet)
99         {
100                 /*
101                  *free the the memory used to hold the css
102                  *object model
103                  */
104                 cr_stylesheet_destroy (stylesheet) ;
105                 stylesheet = NULL ;
106         }
107
108         return 0 ;
109 }