1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  GThumb
5  *
6  *  Copyright (C) 2003, 2010 Free Software Foundation, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef ALBUMTHEME_PRIVATE_H
23 #define ALBUMTHEME_PRIVATE_H
24 
25 #include <glib.h>
26 
27 /* A list of GthTag elements that describes a parsed .gthtml file */
28 
29 extern GList *yy_parsed_doc;
30 
31 /* GthMem: the memory stack used to evaluate an expression. */
32 
33 typedef struct {
34 	int *data;
35 	int  top;
36 } GthMem;
37 
38 GthMem*   gth_mem_new        (int     size);
39 void      gth_mem_free       (GthMem *mem);
40 void      gth_mem_set_empty  (GthMem *mem);
41 gboolean  gth_mem_is_empty   (GthMem *mem);
42 void      gth_mem_push       (GthMem *mem,
43 			      int     val);
44 int       gth_mem_pop        (GthMem *mem);
45 int       gth_mem_get_pos    (GthMem *mem,
46 			      int     pos);
47 int       gth_mem_get        (GthMem *mem);
48 int       gth_mem_get_top    (GthMem *mem);
49 
50 /* GthCell: contains an element of the expression, therefore a GthExpr is a
51  * series of GthCells. */
52 
53 typedef enum {
54 	GTH_OP_ADD,
55 	GTH_OP_SUB,
56 	GTH_OP_MUL,
57 	GTH_OP_DIV,
58 	GTH_OP_NEG,
59 	GTH_OP_NOT,
60 	GTH_OP_AND,
61 	GTH_OP_OR,
62 	GTH_OP_CMP_EQ,
63 	GTH_OP_CMP_NE,
64 	GTH_OP_CMP_LT,
65 	GTH_OP_CMP_GT,
66 	GTH_OP_CMP_LE,
67 	GTH_OP_CMP_GE,
68 } GthOp;
69 
70 typedef enum {
71 	GTH_CELL_TYPE_OP,
72 	GTH_CELL_TYPE_VAR,
73 	GTH_CELL_TYPE_STRING,
74 	GTH_CELL_TYPE_INTEGER
75 } GthCellType;
76 
77 typedef struct {
78 	int         ref;
79 	GthCellType type;
80 	union {
81 		GthOp    op;
82 		char    *var;
83 		GString *string;
84 		int      integer;
85 	} value;
86 } GthCell;
87 
88 GthCell*   gth_cell_new   (void);
89 GthCell*   gth_cell_ref   (GthCell *cell);
90 void       gth_cell_unref (GthCell *cell);
91 
92 /* GthExpr */
93 
94 typedef struct _GthExpr GthExpr;
95 
96 typedef int (*GthGetVarValueFunc) (GthExpr    *expr,
97 				   int        *index,
98 				   const char *var_name,
99 				   gpointer    data);
100 
101 struct _GthExpr {
102 	int                  ref;
103 	GthCell            **data;
104 	int                  top;
105 	GthGetVarValueFunc   get_var_value_func;
106 	gpointer             get_var_value_data;
107 };
108 
109 GthExpr *  gth_expr_new                    (void);
110 GthExpr *  gth_expr_ref                    (GthExpr            *e);
111 void       gth_expr_unref                  (GthExpr            *e);
112 void       gth_expr_set_empty              (GthExpr            *e);
113 gboolean   gth_expr_is_empty               (GthExpr            *e);
114 void       gth_expr_push_expr              (GthExpr            *e,
115 					    GthExpr            *e2);
116 void       gth_expr_push_op                (GthExpr            *e,
117 					    GthOp               op);
118 void       gth_expr_push_var               (GthExpr            *e,
119 					    const char         *name);
120 void       gth_expr_push_string            (GthExpr            *e,
121 					    const char         *value);
122 void       gth_expr_push_integer           (GthExpr            *e,
123 					    int                 value);
124 void       gth_expr_pop                    (GthExpr            *e);
125 GthCell *  gth_expr_get_pos                (GthExpr            *e,
126 					    int                 pos);
127 GthCell *  gth_expr_get                    (GthExpr            *e);
128 int        gth_expr_get_top                (GthExpr            *e);
129 void       gth_expr_set_get_var_value_func (GthExpr            *e,
130 					    GthGetVarValueFunc  f,
131 					    gpointer            data);
132 void       gth_expr_print                  (GthExpr            *e);
133 int        gth_expr_eval                   (GthExpr            *e);
134 void       gth_expr_list_unref             (GList              *list);
135 
136 /* GthAttribute */
137 
138 typedef enum {
139 	GTH_ATTRIBUTE_EXPR,
140 	GTH_ATTRIBUTE_STRING
141 } GthAttributeType;
142 
143 typedef struct {
144 	char             *name;
145 	GthAttributeType  type;
146 	union {
147 		GthExpr *expr;
148 		char    *string;
149 	} value;
150 } GthAttribute;
151 
152 GthAttribute *  gth_attribute_new_expression  (const char   *name,
153 					       GthExpr      *expr);
154 GthAttribute *  gth_attribute_new_string      (const char   *name,
155 					       const char   *string);
156 void            gth_attribute_free            (GthAttribute *attribute);
157 
158 /* GthCondition */
159 
160 typedef struct {
161 	GthExpr *expr;
162 	GList   *document; /* GthTag list */
163 } GthCondition;
164 
165 GthCondition * gth_condition_new           (GthExpr      *expr);
166 void           gth_condition_free          (GthCondition *cond);
167 void           gth_condition_add_document  (GthCondition *cond,
168 					    GList        *document);
169 
170 /* GthLoop */
171 
172 typedef enum {
173 	GTH_TAG_HEADER = 0,
174 	GTH_TAG_FOOTER,
175 	GTH_TAG_LANGUAGE,
176 	GTH_TAG_THEME_LINK,
177 	GTH_TAG_IMAGE,
178 	GTH_TAG_IMAGE_LINK,
179 	GTH_TAG_IMAGE_IDX,
180 	GTH_TAG_IMAGE_DIM,
181 	GTH_TAG_IMAGE_ATTRIBUTE,
182 	GTH_TAG_IMAGES,
183 	GTH_TAG_FILE_NAME,
184 	GTH_TAG_FILE_PATH,
185 	GTH_TAG_FILE_SIZE,
186 	GTH_TAG_PAGE_LINK,
187 	GTH_TAG_PAGE_IDX,
188 	GTH_TAG_PAGE_ROWS,
189 	GTH_TAG_PAGE_COLS,
190 	GTH_TAG_PAGES,
191 	GTH_TAG_THUMBNAILS,
192 	GTH_TAG_TIMESTAMP,
193 	GTH_TAG_TRANSLATE,
194 	GTH_TAG_HTML,
195 	GTH_TAG_SET_VAR,
196 	GTH_TAG_EVAL,
197 	GTH_TAG_IF,
198 	GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION,
199 	GTH_TAG_FOR_EACH_IMAGE_CAPTION,
200 	GTH_TAG_FOR_EACH_IN_RANGE,
201 	GTH_TAG_ITEM_ATTRIBUTE,
202 	GTH_TAG_INVALID
203 } GthTagType;
204 
205 typedef struct {
206 	GthTagType  type;
207 	GList      *document; /* GthTag list */
208 } GthLoop;
209 
210 #define GTH_LOOP(x) ((GthLoop *)(x))
211 
212 GthLoop *   gth_loop_new           (GthTagType  loop_type);
213 void        gth_loop_free          (GthLoop    *loop);
214 GthTagType  gth_loop_get_type      (GthLoop    *loop);
215 void        gth_loop_add_document  (GthLoop    *loop,
216 				    GList      *document);
217 
218 /* GthRangeLopp */
219 
220 typedef struct {
221 	GthLoop  parent;
222 	char    *iterator;
223 	GthExpr *first_value;
224 	GthExpr *last_value;
225 } GthRangeLoop;
226 
227 #define GTH_RANGE_LOOP(x) ((GthRangeLoop *)(x))
228 
229 GthLoop *  gth_range_loop_new       (void);
230 void       gth_range_loop_free      (GthRangeLoop *loop);
231 void       gth_range_loop_set_range (GthRangeLoop *loop,
232 				     const char   *iterator,
233 				     GthExpr      *first_value,
234 				     GthExpr      *last_value);
235 
236 /* GthTag */
237 
238 typedef struct {
239 	GthTagType type;
240 	union {
241 		GList        *attributes;  /* GthAttribute list */
242 		char         *html;        /* html */
243 		GList        *cond_list;   /* GthCondition list */
244 		GthLoop      *loop;        /* a loop tag */
245 		GthRangeLoop *range_loop;
246 	} value;
247 	GList *document; /* GthTag list */
248 } GthTag;
249 
250 GthTag *     gth_tag_new                 (GthTagType  type,
251 				          GList      *attributes);
252 GthTag *     gth_tag_new_html            (const char *html);
253 GthTag *     gth_tag_new_condition       (GList      *cond_list);
254 GthTag *     gth_tag_new_loop            (GthLoop    *loop);
255 void         gth_tag_add_document        (GthTag     *tag,
256 				          GList      *document);
257 void         gth_tag_free                (GthTag     *tag);
258 GthTagType   gth_tag_get_type_from_name  (const char *tag_name);
259 const char * gth_tag_get_name_from_type  (GthTagType  tag_type);
260 
261 /* Utils */
262 
263 int      gth_albumtheme_yyparse      (void);
264 void     gth_parsed_doc_print_tree   (GList *document);
265 void     gth_parsed_doc_free         (GList *document);
266 
267 #endif /* ALBUMTHEME_PRIVATE_H */
268