File: ClearSilver.xs

package info (click to toggle)
clearsilver 0.10.5-1+squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,440 kB
  • ctags: 3,139
  • sloc: ansic: 24,586; python: 4,233; sh: 2,945; cs: 1,429; ruby: 819; java: 735; makefile: 600; perl: 118; lisp: 34; sql: 21
file content (457 lines) | stat: -rw-r--r-- 7,832 bytes parent folder | download | duplicates (6)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ClearSilver.h"

/* #define DEBUG_MODE 1
 */

typedef struct {
  HDF*      hdf;
  NEOERR* err; 	
} perlHDF; 

typedef struct {
  CSPARSE* cs;
  NEOERR* err;
} perlCS;	

typedef perlHDF* ClearSilver__HDF;
typedef perlCS* ClearSilver__CS;

static char* g_sort_func_name;

static void debug(char* fmt, ...)
{
#ifdef DEBUG_MODE
  va_list argp;
  va_start(argp, fmt);
  vprintf(fmt, argp);
  va_end(argp);
#endif
}

static NEOERR *output (void *ctx, char *s)
{
  sv_catpv((SV*)ctx, s);

  return STATUS_OK;
}

static int sortFunction(const void* in_a, const void* in_b)
{
  HDF** hdf_a;
  HDF** hdf_b;
  perlHDF a, b;
  SV* sv_a;
  SV* sv_b;	
  int count;	
  int ret;

  dSP;

  hdf_a = (HDF**)in_a;
  hdf_b = (HDF**)in_b;

  /* convert to a type Perl can access */
  a.hdf = *hdf_a;
  a.err = STATUS_OK;
  b.hdf = *hdf_b;
  b.err = STATUS_OK;

  ENTER;
  SAVETMPS;

  PUSHMARK(SP);
  sv_a = sv_newmortal();
  sv_setref_pv(sv_a, "ClearSilver::HDF", (void*)&a);

  sv_b = sv_newmortal();
  sv_setref_pv(sv_b, "ClearSilver::HDF", (void*)&b);

  XPUSHs(sv_a);
  XPUSHs(sv_b);

  PUTBACK;

  count = call_pv(g_sort_func_name, G_SCALAR);

  SPAGAIN;

  if (count != 1)
    croak("Big trouble\n");

  PUTBACK;

  ret = POPi;

  FREETMPS;
  LEAVE;

  return ret;
}





MODULE = ClearSilver		PACKAGE = ClearSilver::HDF	PREFIX = perlhdf_

ClearSilver::HDF
perlhdf_new(self)
        char* self
    PREINIT:	
	ClearSilver__HDF hdf;
    CODE:
	debug("%s\n", self);
	hdf = (ClearSilver__HDF)malloc(sizeof(perlHDF));
	if (!hdf) {
	  RETVAL = NULL;
	} else {
	  hdf->err = hdf_init(&(hdf->hdf));
	  RETVAL = hdf;
	}
    OUTPUT:
        RETVAL

void
perlhdf_DESTROY(hdf)
        ClearSilver::HDF hdf;
    CODE:
        debug("hdf_DESTROY:%x\n", hdf);
        hdf_destroy(&(hdf->hdf));


int
perlhdf_setValue(hdf, key, value)
	ClearSilver::HDF hdf
	char* key
	char* value 
    CODE:
        hdf->err = hdf_set_value(hdf->hdf, key, value);
	if (hdf->err == STATUS_OK) {
	    RETVAL = 0;
	} else {
	    RETVAL = 1;
	}	
    OUTPUT:
        RETVAL


char*
perlhdf_getValue(hdf, key, default_value)
	ClearSilver::HDF hdf
	char* key
	char* default_value
    CODE:
        RETVAL = hdf_get_value(hdf->hdf, key, default_value);
    OUTPUT:
        RETVAL


int
perlhdf_copy(hdf, name, src);
        ClearSilver::HDF hdf
        char* name
        ClearSilver::HDF src
    CODE:
        hdf->err = hdf_copy(hdf->hdf, name, src->hdf);
        if (hdf->err == STATUS_OK) {
            RETVAL = 0;
        } else {
            RETVAL = 1;
        }
    OUTPUT:
        RETVAL

int
perlhdf_readFile(hdf, filename)
	ClearSilver::HDF hdf
	char* filename
    CODE:
        hdf->err = hdf_read_file(hdf->hdf, filename);
	if (hdf->err == STATUS_OK) {
	    RETVAL = 1;
	} else {
	    RETVAL = 0;
	}	
    OUTPUT:
        RETVAL

int
perlhdf_writeFile(hdf, filename)
       ClearSilver::HDF hdf
       char* filename
    CODE:
        hdf->err = hdf_write_file(hdf->hdf, filename);
       if (hdf->err == STATUS_OK) {
           RETVAL = 1;
       } else {
           RETVAL = 0;
       }
    OUTPUT:
        RETVAL

ClearSilver::HDF
perlhdf_getObj(hdf, name)
	ClearSilver::HDF hdf;
	char* name
    PREINIT:	
	HDF* tmp_hdf;
	perlHDF* perlhdf;
    CODE:
	do {
	    tmp_hdf = hdf_get_obj(hdf->hdf, name);
	    if (!tmp_hdf) {
	        RETVAL = NULL;
		break;
	    }
	    perlhdf = (perlHDF*)malloc(sizeof(perlHDF));
	    if (!perlhdf) {
	        RETVAL = NULL;
	        break;
	    }
            perlhdf->hdf = tmp_hdf;
	    perlhdf->err = STATUS_OK;
	    RETVAL = perlhdf;
	} while (0);
    OUTPUT:
        RETVAL

ClearSilver::HDF
perlhdf_objChild(hdf)
	ClearSilver::HDF hdf;
    PREINIT:	
	HDF* tmp_hdf;
	perlHDF* child;
    CODE:
	do {
	    tmp_hdf = hdf_obj_child(hdf->hdf);
	    if (!tmp_hdf) {
	        RETVAL = NULL;
		break;
	    }
	    child = (perlHDF*)malloc(sizeof(perlHDF));
	    if (!child) {
	        RETVAL = NULL;
	        break;
	    }
            child->hdf = tmp_hdf;
	    child->err = STATUS_OK;
	    RETVAL = child;
	} while (0);
    OUTPUT:
        RETVAL
		

ClearSilver::HDF
perlhdf_getChild(hdf, name)
	ClearSilver::HDF hdf;
	char* name;
    PREINIT:	
	HDF* tmp_hdf;
	perlHDF* child;
    CODE:
	do {
	    tmp_hdf = hdf_get_child(hdf->hdf, name);
	    if (!tmp_hdf) {
	        RETVAL = NULL;
		break;
	    }
	    child = (perlHDF*)malloc(sizeof(perlHDF));
	    if (!child) {
	        RETVAL = NULL;
	        break;
	    }
            child->hdf = tmp_hdf;
	    child->err = STATUS_OK;
	    RETVAL = child;
	} while (0);
    OUTPUT:
        RETVAL

char*
perlhdf_objValue(hdf)
	ClearSilver::HDF hdf;
    CODE:
	RETVAL = hdf_obj_value(hdf->hdf);
    OUTPUT:
        RETVAL  

char*
perlhdf_objName(hdf)
	ClearSilver::HDF hdf;
    CODE:
	RETVAL = hdf_obj_name(hdf->hdf);
    OUTPUT:
        RETVAL  

ClearSilver::HDF
perlhdf_objNext(hdf)
	ClearSilver::HDF hdf;
    PREINIT:	
	HDF* tmp_hdf;
	perlHDF* next;
    CODE:
	do {
	    tmp_hdf = hdf_obj_next(hdf->hdf);
	    if (!tmp_hdf) {
	        RETVAL = NULL;
		break;
	    }
	    next = (perlHDF*)malloc(sizeof(perlHDF));
	    if (!next) {
	      RETVAL = NULL;
	      break;
            }
	    next->hdf = tmp_hdf;
	    next->err = STATUS_OK;
	    RETVAL = next;
	} while (0);
    OUTPUT:
        RETVAL

int
perlhdf_sortObj(hdf, func_name)
	ClearSilver::HDF hdf;
	char* func_name;
    PREINIT:	
        NEOERR* err;
    CODE:
	g_sort_func_name = func_name;
        err = hdf_sort_obj(hdf->hdf, sortFunction);
        RETVAL = 0;
    OUTPUT:
        RETVAL


int
perlhdf_setSymlink(hdf, src, dest)
	ClearSilver::HDF hdf;
	char* src;
	char* dest;
    PREINIT:
	NEOERR* err;
    CODE:
      	err = hdf_set_symlink (hdf->hdf, src, dest);
       	if (err == STATUS_OK) {
       	    RETVAL = 1;
       	} else {
       	    RETVAL = 0;
       	}
    OUTPUT:
        RETVAL


int
perlhdf_removeTree(hdf, name)
	ClearSilver::HDF hdf;
	char* name;
    PREINIT:	
        NEOERR* err;
    CODE:
        err = hdf_remove_tree(hdf->hdf, name);
       	if (err == STATUS_OK) {
       	    RETVAL = 1;
       	} else {
       	    RETVAL = 0;
       	}
    OUTPUT:
        RETVAL


MODULE = ClearSilver		PACKAGE = ClearSilver::CS	PREFIX = perlcs_

ClearSilver::CS
perlcs_new(self, hdf)
	char* self
        ClearSilver::HDF hdf;
    PREINIT:
        perlCS* cs;
    CODE:
	cs = (perlCS*)malloc(sizeof(perlCS));
	if (!cs) {
	  RETVAL = NULL;
	} else {
	  cs->err = cs_init(&(cs->cs), hdf->hdf);
	  if (cs->err == STATUS_OK) {
	    cs->err = cgi_register_strfuncs(cs->cs);
	  }
	  RETVAL = cs;
	}
    OUTPUT:
        RETVAL

void
perlcs_DESTROY(cs)
	ClearSilver::CS cs;
    CODE:
	debug("perlcs_DESTROY() is called\n");
	cs_destroy(&(cs->cs));

void
perlcs_displayError(cs)
	ClearSilver::CS cs;
    CODE:
	nerr_log_error(cs->err);

char *
perlcs_render(cs)
	ClearSilver::CS cs
    CODE:
    {
	SV *str = newSV(0);
	cs->err = cs_render(cs->cs, str, output);
	if (cs->err == STATUS_OK) {
	  ST(0) = sv_2mortal(str);
	} else {
	  SvREFCNT_dec(str);
	  ST(0) = &PL_sv_undef;
	}
	XSRETURN(1);
    }

int
perlcs_parseFile(cs, cs_file)
        ClearSilver::CS cs
	char* cs_file
    CODE:
	do {
	    cs->err =  cs_parse_file(cs->cs, cs_file);
	    if (cs->err != STATUS_OK) {
	        cs->err = nerr_pass(cs->err);
		RETVAL = 0;
		break;
	    }
	    RETVAL = 1;
        } while (0);
    OUTPUT:
        RETVAL

int
perlcs_parseString(cs, in_str)
        ClearSilver::CS cs
	char* in_str
    PREINIT:
	char* cs_str;
	int len;
    CODE:
	do {
	    len = strlen(in_str);
	    cs_str = (char *)malloc(len);
	    if (!cs_str) {
	        RETVAL = 0;
		break;
	    }
	    strcpy(cs_str, in_str);
            cs->err =  cs_parse_string(cs->cs, cs_str, len);
	    if (cs->err != STATUS_OK)
		RETVAL = 0;
	    RETVAL = 1;
       } while (0);
    OUTPUT:
        RETVAL