~ubuntu-branches/ubuntu/hoary/swig1.3/hoary

« back to all changes in this revision

Viewing changes to Source/Modules1.1/php4.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2004-08-02 15:57:10 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040802155710-bm292q1d6x6tw7gc
Tags: 1.3.21-5ubuntu1
Fix linking for ruby, python, perl and tcl bindings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * PHP4 Support
3
 
 *
4
 
 * Richard Palmer
5
 
 * richard@magicality.org
6
 
 * Nov 2001
7
 
 *
8
 
 * Portions copyright Sun Microsystems (c) 2001
9
 
 * Tim Hockin <thockin@sun.com>
10
 
 */
11
 
 
12
 
static char cvsroot[] = "$Header: /cvs/projects/SWIG/Source/Modules1.1/Attic/php4.cxx,v 1.1.2.25 2002/01/31 00:47:18 beazley Exp $";
13
 
 
14
 
#include <ctype.h>
15
 
 
16
 
#include "mod11.h"
17
 
#include "php4.h"
18
 
#include "swigconfig.h"
19
 
 
20
 
static char *usage = (char*)"\
21
 
PHP4 Options (available with -php4)\n\
22
 
        -shadow         - Create shadow classes.\n\
23
 
        -dlname name    - Set module prefix.\n\
24
 
        -make           - Create simple makefile.\n\
25
 
        -phpfull        - Create full make files.\n\
26
 
        -nosync         - No syncronisation of variables.\n\n";
27
 
 
28
 
#define PHP_READONLY    1<<0
29
 
 
30
 
static String *module = 0;
31
 
static String *cap_module = 0;
32
 
static String *dlname = 0;
33
 
 
34
 
static String *f_cinit = 0;
35
 
static String *f_oinit = 0;
36
 
static String *f_init = 0;
37
 
static String *f_entry = 0;
38
 
//static char   *package = 0;   // Name of the package
39
 
static char     *c_pkgstr;      // Name of the package
40
 
static char     *php_pkgstr;    // Name of the package
41
 
static char *shadow_classname;
42
 
 
43
 
static Wrapper  *f_c;
44
 
static Wrapper  *f_php;
45
 
static int      gen_extra = 0;
46
 
static int      gen_make = 0;
47
 
static int      no_sync = 0;
48
 
 
49
 
static File       *f_runtime = 0;
50
 
static File       *f_h = 0;
51
 
static File       *f_header  = 0;
52
 
static File       *f_wrappers = 0;
53
 
static File       *f_phpcode = 0;
54
 
 
55
 
static String     *s_header;
56
 
static String     *s_wrappers;
57
 
static String     *s_init;
58
 
static String     *s_vinit;
59
 
static String     *s_cinit;
60
 
static String     *s_oinit;
61
 
static String     *s_entry;
62
 
static String     *pragma_incl;
63
 
static String     *pragma_code;
64
 
static String     *pragma_phpinfo;
65
 
 
66
 
/* Variables for using PHP classes */
67
 
static String     *php;         /* Class initialization code */
68
 
static String     *class_name = 0;
69
 
static String     *base_class = 0;
70
 
static String     *real_classname = 0;
71
 
static String     *class_type = 0;
72
 
static String     *realpackage = 0;
73
 
static String     *package = 0;
74
 
 
75
 
static Hash     *shadow_classes;
76
 
static Hash     *shadow_php_vars;
77
 
static Hash     *shadow_c_vars;
78
 
static String   *shadow_classdef;
79
 
static String   *shadow_code;
80
 
static char     *shadow_variable_name = 0;
81
 
static int      classdef_emitted = 0;
82
 
static int      have_default_constructor = 0;
83
 
static int      native_func = 0;        // Set to 1 when wrapping a native function
84
 
static int      enum_flag = 0; // Set to 1 when wrapping an enum
85
 
static int      static_flag = 0; // Set to 1 when wrapping a static functions or member variables
86
 
static int      const_flag = 0; // Set to 1 when wrapping a const member variables
87
 
static int      variable_wrapper_flag = 0; // Set to 1 when wrapping a member variable/enum/const
88
 
static int      wrapping_member = 0;
89
 
static int      written_base_class = 0; // XX hack to prevent base class duplicated
90
 
 
91
 
static String *shadow_enum_code = 0;
92
 
static String *php_enum_code = 0;
93
 
static String *all_shadow_extra_code = 0; 
94
 
                //Extra code for all shadow classes from %pragma
95
 
static String *this_shadow_extra_code = 0; 
96
 
                //Extra Code for current single shadow class freom %pragma
97
 
static String *all_shadow_import = 0; 
98
 
                //import for all shadow classes from %pragma
99
 
static String *this_shadow_import = 0; 
100
 
                //import for current shadow classes from %pragma
101
 
static String *module_baseclass = 0; 
102
 
                //inheritance for module class from %pragma
103
 
static String *all_shadow_baseclass = 0; 
104
 
                //inheritence for all shadow classes from %pragma
105
 
static String *this_shadow_baseclass = 0; 
106
 
                //inheritance for shadow class from %pragma and cpp_inherit
107
 
static int        class_renamed = 0;
108
 
static int        shadow        = 0;
109
 
 
110
 
/* Test to see if a type corresponds to something wrapped with a shadow class */
111
 
static String *is_shadow(SwigType *t) {
112
 
        String *r;
113
 
        SwigType *lt = SwigType_ltype(t);
114
 
        r = Getattr(shadow_classes,lt);
115
 
        Delete(lt);
116
 
        return r;
117
 
}
118
 
 
119
 
// Return the type of the c array
120
 
static SwigType *get_array_type(SwigType *t) {
121
 
  SwigType *ta = 0;
122
 
    if (SwigType_type(t) == T_ARRAY) {
123
 
        SwigType *aop;
124
 
        ta = Copy(t);
125
 
        aop = SwigType_pop(ta);
126
 
    }
127
 
    return ta;
128
 
}
129
 
 
130
 
 
131
 
static void emit_banner(File *f) {
132
 
Printf(f, "/* ----------------------------------------------------------------------------\n");
133
 
Printf(f, " * This file was automatically generated by SWIG (http://www.swig.org).\n");
134
 
Printf(f, " * Version: %s\n", SWIG_VERSION);
135
 
Printf(f, " *\n");
136
 
Printf(f, " * Do not make changes to this file unless you know what you are doing--modify\n");
137
 
Printf(f, " * the SWIG interface file instead.\n");
138
 
Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
139
 
}
140
 
 
141
 
/* -----------------------------------------------------------------------------
142
 
 * get_pointer()
143
 
 * ----------------------------------------------------------------------------- */
144
 
static void
145
 
get_pointer(char *iname, char *srcname, char *src, char *dest,
146
 
            SwigType *t, String *f, char *ret) {
147
 
 
148
 
  SwigType_remember(t);
149
 
  SwigType *lt = SwigType_ltype(t);
150
 
  Printv(f, "if (SWIG_ConvertPtr(", src, ",(void **) ", dest, ",", 0);
151
 
 
152
 
  /* If we're passing a void pointer, we give the pointer conversion a NULL
153
 
     pointer, otherwise pass in the expected type. */
154
 
 
155
 
  if (Cmp(lt,"p.void") == 0) {
156
 
    Printf(f, " 0 ) < 0) {\n");
157
 
  } else {
158
 
    Printv(f, "SWIGTYPE", SwigType_manglestr(t), ") < 0) {\n",0);
159
 
  }
160
 
 
161
 
  Printv(f,
162
 
         "zend_error(E_ERROR, \"Type error in ", srcname, " of ", iname,
163
 
         " Expected %s\", SWIGTYPE", SwigType_manglestr(t), "->name);\n", ret,
164
 
         ";\n",
165
 
         "}\n",
166
 
         0);
167
 
  Delete(lt);
168
 
}
169
 
 
170
 
 
171
 
void
172
 
PHP4::main(int argc, char *argv[]) {
173
 
        int i;
174
 
        SWIG_library_directory("php4");
175
 
        for(i = 1; i < argc; i++) {
176
 
          if (argv[i]) {
177
 
            if(strcmp(argv[i], "-phpfull") == 0) {
178
 
              gen_extra = 1;
179
 
              Swig_mark_arg(i);
180
 
            } else if(strcmp(argv[i], "-dlname") == 0) {
181
 
              if (argv[i+1]) {
182
 
                  dlname = NewString(argv[i+1]);
183
 
                  Swig_mark_arg(i);
184
 
                  Swig_mark_arg(i+1);
185
 
                  i++;
186
 
               } else {
187
 
                  Swig_arg_error();
188
 
               }
189
 
            }  else if(strcmp(argv[i], "-shadow") == 0) {
190
 
                shadow = 1;
191
 
                Swig_mark_arg(i);
192
 
            } else if(strcmp(argv[i], "-make") == 0) {
193
 
                gen_make = 1;
194
 
                Swig_mark_arg(i);
195
 
            } else if(strcmp(argv[i], "-nosync") == 0) {
196
 
                no_sync = 1;
197
 
                Swig_mark_arg(i);
198
 
            } else if(strcmp(argv[i], "-help") == 0) {
199
 
                fputs(usage, stderr);
200
 
            }
201
 
          }
202
 
        }
203
 
          
204
 
        Preprocessor_define((void *) "SWIGPHP 1", 0);
205
 
        Preprocessor_define((void *) "SWIGPHP4 1", 0);
206
 
        SWIG_typemap_lang("php4");
207
 
        /* DB: Suggest using a language configuration file */
208
 
        SWIG_config_file("php4.swg");
209
 
}
210
 
 
211
 
static
212
 
void create_simple_make(void) {
213
 
        File *f_make;
214
 
 
215
 
        f_make = NewFile((void *)"makefile", "w");
216
 
        if(CPlusPlus)
217
 
                Printf(f_make, "CC=g++\n");
218
 
        else
219
 
                Printf(f_make, "CC=gcc\n");
220
 
 
221
 
        Printf(f_make,
222
 
              "OBJS=%s_wrap.o\n"
223
 
              "PROG=lib%s.so\n"
224
 
              "CFLAGS=-fpic\n"
225
 
              "LDFLAGS=-shared\n"
226
 
              "PHP_INC=`php-config --includes`\n"
227
 
              "EXTRA_INC=\n"
228
 
              "EXTRA_LIB=\n\n",
229
 
              module, module);
230
 
 
231
 
        Printf(f_make,
232
 
              "$(PROG): $(OBJS)\n"
233
 
              "\t$(CC) $(LDFLAGS) $(OBJS) -o $(PROG) $(EXTRA_LIB)\n\n"
234
 
              "%%.o: %%.%s\n"
235
 
              "\t$(CC) $(EXTRA_INC) $(PHP_INC) $(CFLAGS) -c $<\n",
236
 
              (CPlusPlus?"cxx":"c"));
237
 
 
238
 
        Close(f_make);
239
 
}
240
 
 
241
 
static
242
 
void create_extra_files(void) {
243
 
        File *f_extra;
244
 
 
245
 
        if(gen_extra) {
246
 
                /* Write out Makefile.in */
247
 
        f_extra = NewFile((void *)"Makefile.in", "w");
248
 
        if (!f_extra) {
249
 
                Printf(stderr,"Unable to open Makefile.in\n");
250
 
                SWIG_exit(EXIT_FAILURE);
251
 
        }
252
 
 
253
 
        Printf(f_extra,
254
 
            "# $Id: php4.cxx,v 1.1.2.25 2002/01/31 00:47:18 beazley Exp $\n\n");
255
 
        Printf(f_extra, "LTLIBRARY_NAME          = lib%s.la\n", module);
256
 
        Printf(f_extra, "LTLIBRARY_SOURCES       = %s_wrap.c\n", module);
257
 
        Printf(f_extra, "LTLIBRARY_SHARED_NAME   = %s.la\n", module);
258
 
        Printf(f_extra, "LTLIBRARY_SHARED_LIBADD = $(%s_SHARED_LIBADD)\n\n",
259
 
            cap_module);
260
 
        Printf(f_extra, "include $(top_srcdir)/build/dynlib.mk\n");
261
 
        Close(f_extra);
262
 
 
263
 
        /* Now config.m4 */
264
 
        f_extra = NewFile((void *)"config.m4", "w");
265
 
        if (!f_extra) {
266
 
                Printf(stderr, "Unable to open config.m4\n");
267
 
                SWIG_exit(EXIT_FAILURE);
268
 
        }
269
 
 
270
 
        Printf(f_extra,
271
 
            "dnl $Id: php4.cxx,v 1.1.2.25 2002/01/31 00:47:18 beazley Exp $\n");
272
 
        Printf(f_extra,
273
 
            "dnl config.m4 for extension %s\n\n", module);
274
 
        Printf(f_extra,
275
 
            "dnl Comments in this file start with the string 'dnl'.\n");
276
 
        Printf(f_extra,
277
 
            "dnl Remove where necessary. This file will not work\n");
278
 
        Printf(f_extra,
279
 
            "dnl without editing.\n\n");
280
 
 
281
 
        Printf(f_extra,
282
 
            "dnl If your extension references somthing external, use:\n\n");
283
 
        Printf(f_extra,
284
 
            "dnl PHP_ARG_WITH(%s, for %s support,\n", module, module);
285
 
        Printf(f_extra,
286
 
            "dnl Make sure that the comment is aligned:\n");
287
 
        Printf(f_extra,
288
 
            "dnl [  --with-%s           Include %s support])\n\n",
289
 
            module, module);
290
 
 
291
 
        Printf(f_extra,
292
 
            "dnl Otherwise use enable:\n\n");
293
 
        Printf(f_extra,
294
 
            "PHP_ARG_ENABLE(%s, whether to enable %s support,\n",
295
 
                module, module);
296
 
        Printf(f_extra,
297
 
            "dnl Make sure that the comment is aligned:\n");
298
 
        Printf(f_extra,
299
 
            "[  --enable-%s     Enable %s support])\n\n", module, module);
300
 
 
301
 
        Printf(f_extra,
302
 
            "if test \"$PHP_%s\" != \"no\"; then\n", cap_module);
303
 
        Printf(f_extra,
304
 
            "  dnl Write more examples of tests here\n\n");
305
 
        Printf(f_extra,
306
 
            "  dnl # --with-%s -> check with-path\n", module);
307
 
        Printf(f_extra,
308
 
            "  dnl # you might want to change this\n"
309
 
            "  dnl SEARCH_PATH=\"/usr/local /usr\"\n");
310
 
        Printf(f_extra,
311
 
            "  dnl # you most likely want to change this\n"
312
 
            "  dnl SEARCH_FOR=\"/include/%s.h\"\n", module);
313
 
        Printf(f_extra,
314
 
            "  dnl # path given as parameter\n"
315
 
            "  dnl if test -r $PHP_%s/; then\n", module);
316
 
        Printf(f_extra,
317
 
            "  dnl   %s_DIR=$PHP_%s\n", cap_module, cap_module);
318
 
        Printf(f_extra,
319
 
            "  dnl else # search default path list\n");
320
 
        Printf(f_extra,
321
 
            "  dnl   AC_MSG_CHECKING(for %s files in default path)\n", module);
322
 
        Printf(f_extra,
323
 
            "  dnl   for i in $SEARCH_PATH; do\n");
324
 
        Printf(f_extra,
325
 
            "  dnl     if test -r $i/$SEARCH_FOR; then\n");
326
 
        Printf(f_extra,
327
 
            "  dnl       %s_DIR=$i\n", cap_module);
328
 
        Printf(f_extra,
329
 
            "  dnl       AC_MSG_RESULT(found in $i)\n");
330
 
        Printf(f_extra,
331
 
            "  dnl     fi\n");
332
 
        Printf(f_extra,
333
 
            "  dnl   done\n");
334
 
        Printf(f_extra,
335
 
            "  dnl fi\n");
336
 
        Printf(f_extra,
337
 
            "  dnl\n");
338
 
        Printf(f_extra,
339
 
            "  dnl if test -z \"$%s_DIR\"; then\n", cap_module);
340
 
        Printf(f_extra,
341
 
            "  dnl   AC_MSG_RESULT(not found)\n");
342
 
        Printf(f_extra,
343
 
            "  dnl   AC_MSG_ERROR(Please reinstall the %s distribution)\n",
344
 
            module);
345
 
        Printf(f_extra,
346
 
            "  dnl fi\n\n");
347
 
        Printf(f_extra,
348
 
            "  dnl # --with-%s -> add include path\n", module);
349
 
        Printf(f_extra,
350
 
            "  dnl PHP_ADD_INCLUDE($%s_DIR/include)\n\n", cap_module);
351
 
        Printf(f_extra,
352
 
            "  dnl #--with-%s -> check for lib and symbol presence\n", module);
353
 
        Printf(f_extra,
354
 
            "  dnl LIBNAME=%s # you may want to change this\n", module);
355
 
        Printf(f_extra,
356
 
            "  dnl LIBSYMBOL=%s #  you most likely want to change this\n",
357
 
            module);
358
 
        Printf(f_extra,
359
 
            "  dnl old_LIBS=$LIBS\n");
360
 
        Printf(f_extra,
361
 
            "  dnl LIBS=\"$LIBS -L$%s_DIR/lib -lm -ldl\"\n", cap_module);
362
 
        Printf(f_extra,
363
 
            "  dnl AC_CHECK_LIB($LIBNAME, $LIBSYMBOL, [AC_DEFINE(HAVE_%sLIB,1,[ ])],\n",
364
 
            cap_module);
365
 
        Printf(f_extra,
366
 
            "  dnl [AC_MSG_ERROR(wrong %s lib version or lib not found)])\n",
367
 
            module);
368
 
        Printf(f_extra,
369
 
            "  dnl LIBS=$old_LIBS\n");
370
 
        Printf(f_extra,
371
 
            "  dnl\n");
372
 
        Printf(f_extra,
373
 
            "  dnl PHP_SUBST(%s_SHARED_LIBADD)\n", cap_module);
374
 
        Printf(f_extra,
375
 
            "  dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $%s_DIR/lib, SAPRFC_SHARED_LIBADD)\n\n",
376
 
            cap_module);
377
 
        Printf(f_extra,
378
 
            "  PHP_EXTENSION(%s, $ext_shared)\n", module);
379
 
        Printf(f_extra,"fi\n");
380
 
        Close(f_extra);
381
 
 
382
 
        /*  CREDITS */
383
 
        f_extra = NewFile((void *)"CREDITS", "w");
384
 
        if (!f_extra) {
385
 
                Printf(stderr,"Unable to open CREDITS\n");
386
 
                SWIG_exit(EXIT_FAILURE);
387
 
        }
388
 
        Printf(f_extra, "%s\n", module);
389
 
        Close(f_extra);
390
 
  }
391
 
}
392
 
 
393
 
static const char *php_header =
394
 
"/*"
395
 
"\n  +----------------------------------------------------------------------+"
396
 
"\n  | PHP version 4.0                                                      |"
397
 
"\n  +----------------------------------------------------------------------+"
398
 
"\n  | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |"
399
 
"\n  +----------------------------------------------------------------------+"
400
 
"\n  | This source file is subject to version 2.02 of the PHP license,      |"
401
 
"\n  | that is bundled with this package in the file LICENSE, and is        |"
402
 
"\n  | available at through the world-wide-web at                           |"
403
 
"\n  | http://www.php.net/license/2_02.txt.                                 |"
404
 
"\n  | If you did not receive a copy of the PHP license and are unable to   |"
405
 
"\n  | obtain it through the world-wide-web, please send a note to          |"
406
 
"\n  | license@php.net so we can mail you a copy immediately.               |"
407
 
"\n  +----------------------------------------------------------------------+"
408
 
"\n  | Authors:                                                             |"
409
 
"\n  |                                                                      |"
410
 
"\n  +----------------------------------------------------------------------+"
411
 
"\n */\n";
412
 
 
413
 
int
414
 
PHP4::top(Node *n) {
415
 
 
416
 
  String *filen;
417
 
  String *outfile;
418
 
 
419
 
  /* Initialize all of the output files */
420
 
  outfile = Getattr(n,"outfile");
421
 
  
422
 
  /* main output file */
423
 
  f_runtime = NewFile(outfile,"w");
424
 
  if (!f_runtime) {
425
 
        Printf(stderr,"*** Can't open '%s'\n", outfile);
426
 
        SWIG_exit(EXIT_FAILURE);
427
 
  }
428
 
  
429
 
  Swig_banner(f_runtime);
430
 
 
431
 
  /* sections of the output file */
432
 
  s_init = NewString("/* init section */\n");
433
 
  s_header = NewString("/* header section */\n");
434
 
  s_wrappers = NewString("/* wrapper section */\n");
435
 
  /* subsections of the init section */
436
 
  s_vinit = NewString("/* vinit subsection */\n");
437
 
  s_cinit = NewString("/* cinit subsection */\n");
438
 
  s_oinit = NewString("/* oinit subsection */\n");
439
 
 
440
 
  pragma_phpinfo = NewString("");
441
 
 
442
 
 
443
 
  /* Register file targets with the SWIG file handler */
444
 
  Swig_register_filebyname("runtime",f_runtime);
445
 
  Swig_register_filebyname("init",s_init);
446
 
  Swig_register_filebyname("header",s_header);
447
 
  Swig_register_filebyname("wrapper",s_wrappers);
448
 
 
449
 
  shadow_classes = NewHash();
450
 
  shadow_classdef = NewString("");
451
 
  shadow_code = NewString("");
452
 
  php_enum_code = NewString("");
453
 
  module_baseclass = NewString("");
454
 
  all_shadow_extra_code = NewString("");
455
 
  all_shadow_import = NewString("");
456
 
  all_shadow_baseclass = NewString("");
457
 
  
458
 
  /* Set the module name */
459
 
  module = Copy(Getattr(n,"name"));
460
 
  cap_module = NewStringf("%(upper)s",module);
461
 
 
462
 
  if(shadow) {
463
 
        realpackage = module;
464
 
        package = NewStringf("%sc", module);
465
 
   }
466
 
 
467
 
  /* Set the dlname */
468
 
  if (!dlname) {
469
 
#if defined(_WIN32) || defined(__WIN32__)
470
 
         dlname = NewStringf("%s.dll", module);
471
 
#else
472
 
         dlname = NewStringf("lib%s.so", module);
473
 
#endif
474
 
  }
475
 
 
476
 
  /* PHP module file */
477
 
  filen = NewString(module);
478
 
  Printf(filen, ".php");
479
 
  f_phpcode = NewFile(filen, "w");
480
 
  if (!f_phpcode) {
481
 
          Printf(stderr, "*** Can't open '%s'\n", filen);
482
 
          SWIG_exit(EXIT_FAILURE);
483
 
  }
484
 
 
485
 
  Printf(f_phpcode, "<?php\n\n");
486
 
 
487
 
  emit_banner(f_phpcode);
488
 
 
489
 
  Printf(f_phpcode,
490
 
        "global $%s_LOADED__;\n"
491
 
        "if ($%s_LOADED__) return;\n"
492
 
        "$%s_LOADED__ = true;\n\n"
493
 
        "/* if our extension has not been loaded, do what we can */\n"
494
 
        "if (!extension_loaded(\"%s\")) {\n"
495
 
        "       if (!dl(\"%s\")) return;\n"
496
 
        "}\n\n", cap_module, cap_module, cap_module, module, dlname);
497
 
 
498
 
 
499
 
  /* sub-sections of the php file */
500
 
  pragma_code = NewString("");
501
 
  pragma_incl = NewString("");
502
 
 
503
 
  /* Initialize the rest of the module */
504
 
 
505
 
  f_c = NewWrapper();
506
 
  f_php = NewWrapper();
507
 
  Printf(f_c->def, "static void Swig_sync_c(void) {\n");
508
 
  Printf(f_php->def, "static void Swig_sync_php(void) {\n");
509
 
  
510
 
  /* start the header section */
511
 
  Printf(s_header, php_header);
512
 
  Printf(s_header,
513
 
      "#define SWIG_init        init%s\n\n"
514
 
      "#define SWIG_name        \"%s\"\n"
515
 
      "#ifdef HAVE_CONFIG_H\n"
516
 
      "#include \"config.h\"\n"
517
 
      "#endif\n\n"
518
 
      "#ifdef __cplusplus\n"
519
 
      "extern \"C\" {\n"
520
 
      "#endif\n"
521
 
      "#include \"php.h\"\n"
522
 
      "#include \"php_ini.h\"\n"
523
 
      "#include \"php_%s.h\"\n"
524
 
      "#ifdef __cplusplus\n"
525
 
      "}\n"
526
 
      "#endif\n\n",
527
 
      module, module, module);
528
 
 
529
 
  /* Create the .h file too */
530
 
  filen = NewString("");
531
 
  Printv(filen, Swig_file_dirname(outfile), "php_", module, ".h", 0);
532
 
  f_h = NewFile(filen, "w");
533
 
  if (!f_h) {
534
 
        Printf(stderr,"Unable to open %s\n", filen);
535
 
        SWIG_exit(EXIT_FAILURE);
536
 
  }
537
 
 
538
 
  Swig_banner(f_h);
539
 
  Printf(f_h, php_header);
540
 
 
541
 
   Printf(f_h, "\n\n"
542
 
      "#ifndef PHP_%s_H\n"
543
 
      "#define PHP_%s_H\n\n"
544
 
      "extern zend_module_entry %s_module_entry;\n"
545
 
      "#define phpext_%s_ptr &%s_module_entry\n\n"
546
 
      "#ifdef PHP_WIN32\n"
547
 
      "# define PHP_%s_API __declspec(dllexport)\n"
548
 
      "#else\n"
549
 
      "# define PHP_%s_API\n"
550
 
      "#endif\n\n"
551
 
      "PHP_MINIT_FUNCTION(%s);\n"
552
 
      "PHP_MSHUTDOWN_FUNCTION(%s);\n"
553
 
      "PHP_RINIT_FUNCTION(%s);\n"
554
 
      "PHP_RSHUTDOWN_FUNCTION(%s);\n"
555
 
      "PHP_MINFO_FUNCTION(%s);\n\n",
556
 
      cap_module, cap_module, module, module, module, cap_module, cap_module,
557
 
      module, module, module, module, module);
558
 
 
559
 
  /* start the function entry section */
560
 
  s_entry = NewString("/* entry subsection */\n");
561
 
  Printf(s_entry, "static void Swig_sync_c(void);\n");
562
 
  Printf(s_entry, "static void Swig_sync_php(void);\n\n");
563
 
 
564
 
  Printf(s_entry,"/* Every user visible function must have an entry */\n");
565
 
  Printf(s_entry,"function_entry %s_functions[] = {\n", module);
566
 
 
567
 
  /* Start variable init function (to be put in module init function) */
568
 
  Printf(s_cinit,
569
 
        "    int i;\n"
570
 
        "    for (i = 0; swig_types_initial[i]; i++) {\n"
571
 
        "        swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);\n"
572
 
        "    }\n");
573
 
 
574
 
  /* start the init section */
575
 
  if (gen_extra)
576
 
        Printf(s_init,"#ifdef COMPILE_DL_%s\n", cap_module);
577
 
  Printf(s_init,
578
 
        "#ifdef __cplusplus\n"
579
 
        "extern \"C\" {\n"
580
 
        "#endif\n"
581
 
        "ZEND_GET_MODULE(%s)\n"
582
 
        "#ifdef __cplusplus\n"
583
 
        "}\n"
584
 
        "#endif\n\n",
585
 
        module);
586
 
  if (gen_extra)
587
 
        Printf(s_init,"#endif\n\n");
588
 
 
589
 
  Printf(s_init,"PHP_MINIT_FUNCTION(%s)\n{\n", module);
590
 
  Printf(s_init,"    return SUCCESS;\n");
591
 
  Printf(s_init,"}\n");
592
 
 
593
 
  Printf(s_init,"PHP_MSHUTDOWN_FUNCTION(%s)\n{\n", module);
594
 
  Printf(s_init,"    return SUCCESS;\n");
595
 
  Printf(s_init,"}\n");
596
 
 
597
 
 
598
 
  /* finish our init section */
599
 
  Printf(s_vinit, "/* end vinit subsection */\n");
600
 
 
601
 
  /* We need this after all classes written out, not sure where to put it
602
 
   * but here.
603
 
   */
604
 
  Printf(s_oinit, "CG(active_class_entry) = NULL;\n");  
605
 
 
606
 
  Printf(s_oinit, "/* end oinit subsection */\n");
607
 
  Printf(s_init,"PHP_RINIT_FUNCTION(%s)\n{\n", module);
608
 
 
609
 
  /* Emit all of the code */
610
 
  Language::top(n);
611
 
  
612
 
  /* Constants generated during top call */
613
 
  Printf(s_init, "%s\n", s_cinit); 
614
 
  Printf(s_cinit, "/* end cinit subsection */\n");
615
 
 
616
 
  Printf(s_init, "%s\n%s\n", s_vinit, s_oinit);
617
 
  Delete(s_cinit);
618
 
  Delete(s_vinit);
619
 
 
620
 
  Printf(s_init, "    return SUCCESS;\n");
621
 
  Printf(s_init,"}\n");
622
 
 
623
 
  Printf(s_init,"PHP_RSHUTDOWN_FUNCTION(%s)\n{\n", module);
624
 
  Printf(s_init,"    return SUCCESS;\n");
625
 
  Printf(s_init,"}\n");
626
 
 
627
 
  Printf(s_init,"PHP_MINFO_FUNCTION(%s)\n{\n", module);
628
 
  Printf(s_init,"%s", pragma_phpinfo);
629
 
  Printf(s_init,"}\n");
630
 
  Printf(s_init, "/* end init section */\n");
631
 
 
632
 
  /* Complete header file */
633
 
 
634
 
  Printf(f_h,
635
 
    "/*If you declare any globals in php_%s.h uncomment this:\n", module);
636
 
  Printf(f_h,"ZEND_BEGIN_MODULE_GLOBALS(%s)\n", module);
637
 
  Printf(f_h,"ZEND_END_MODULE_GLOBALS(%s)\n", module);
638
 
  Printf(f_h,"*/\n");
639
 
 
640
 
  Printf(f_h,"#ifdef ZTS\n");
641
 
  Printf(f_h,"#define %s_D  zend_%s_globals *%s_globals\n", cap_module,
642
 
    module, module);
643
 
  Printf(f_h,"#define %s_DC  , %s_D\n",  cap_module, cap_module);
644
 
  Printf(f_h,"#define %s_C  %s_globals\n", cap_module, module);
645
 
  Printf(f_h,"#define %s_CC  , %s_C\n", cap_module, cap_module);
646
 
  Printf(f_h,"#define %s_SG(v)  (%s_globals->v)\n", cap_module, module);
647
 
  Printf(f_h,"#define %s_FETCH()  zend_%s_globals *%s_globals "
648
 
    "= ts_resource(%s_globals_id)\n", cap_module, module, module, module);
649
 
  Printf(f_h,"#else\n");
650
 
  Printf(f_h,"#define %s_D\n", cap_module);  
651
 
  Printf(f_h,"#define %s_DC\n", cap_module);
652
 
  Printf(f_h,"#define %s_C\n", cap_module);
653
 
  Printf(f_h,"#define %s_CC\n", cap_module);
654
 
  Printf(f_h,"#define %s_SG(v)  (%s_globals.v)\n", cap_module, module);
655
 
  Printf(f_h,"#define %s_FETCH()\n", cap_module);
656
 
  Printf(f_h,"#endif\n\n");
657
 
  Printf(f_h,"#endif /* PHP_%s_H */\n", cap_module);
658
 
        
659
 
  Close(f_h);
660
 
 
661
 
  Printf(s_header, "%s", s_entry);
662
 
 
663
 
  Printf(s_header,"     {NULL, NULL, NULL}\n};\n\n");
664
 
  Printf(s_header,"zend_module_entry %s_module_entry = {\n", module);
665
 
  Printf(s_header,"#if ZEND_MODULE_API_NO > 20010900\n");
666
 
  Printf(s_header,"    STANDARD_MODULE_HEADER,\n");
667
 
  Printf(s_header,"#endif\n");
668
 
  Printf(s_header,"    \"%s\",\n", module);
669
 
  Printf(s_header,"    %s_functions,\n", module);
670
 
  Printf(s_header,"    PHP_MINIT(%s),\n", module);
671
 
  Printf(s_header,"    PHP_MSHUTDOWN(%s),\n", module);
672
 
  Printf(s_header,"    PHP_RINIT(%s),\n", module);
673
 
  Printf(s_header,"    PHP_RSHUTDOWN(%s),\n", module);
674
 
  Printf(s_header,"    PHP_MINFO(%s),\n", module);
675
 
  Printf(s_header,"#if ZEND_MODULE_API_NO > 20010900\n");
676
 
  Printf(s_header,"    NO_VERSION_YET,\n");
677
 
  Printf(s_header,"#endif\n");
678
 
  Printf(s_header,"    STANDARD_MODULE_PROPERTIES\n");
679
 
  Printf(s_header,"};\n\n");
680
 
 
681
 
  Printv(f_runtime, s_header, 0);
682
 
 
683
 
  String *type_table = NewString("");
684
 
  SwigType_emit_type_table(f_runtime,type_table);
685
 
  Printf(f_runtime,"%s",type_table);
686
 
  Delete(type_table);
687
 
 
688
 
  Printf(f_c->code, "\n}\n");
689
 
  Printf(f_php->code, "\n}\n");
690
 
 
691
 
  Wrapper_print(f_c, s_wrappers);
692
 
  Wrapper_print(f_php, s_wrappers);
693
 
 
694
 
  Printf(s_header, "/* end header section */\n");
695
 
  Printf(s_wrappers, "/* end wrapper section */\n");
696
 
 
697
 
  Printv(f_runtime, s_wrappers, s_init, 0);
698
 
  Delete(s_header);
699
 
  Delete(s_wrappers);
700
 
  Delete(s_init);
701
 
  Close(f_runtime);
702
 
  Printf(f_phpcode, "%s\n%s\n?>\n", pragma_incl, pragma_code);
703
 
  Close(f_phpcode); 
704
 
 
705
 
  create_extra_files();
706
 
 
707
 
  if(!gen_extra && gen_make)
708
 
          create_simple_make();
709
 
 
710
 
  return SWIG_OK;
711
 
}
712
 
 
713
 
 
714
 
#if 0
715
 
void
716
 
PHP4::set_module(char *mod_name) {
717
 
        char *c;
718
 
        if(module) return;
719
 
        module = NewString(mod_name);
720
 
        cap_module = Copy(module);
721
 
        for(c = Char(cap_module); *c != '\0'; c++) {
722
 
                if((*c >= 'a') && (*c <= 'z'))
723
 
                        *c-=32; 
724
 
        }
725
 
}
726
 
 
727
 
#endif
728
 
 
729
 
/*
730
 
void
731
 
PHP4::add_method(char *name, char *function, int kw) {
732
 
        fprintf(stderr, "Would add method %s\n", name);
733
 
}
734
 
*/
735
 
 
736
 
/* Just need to append function names to function table to register with
737
 
   PHP
738
 
*/
739
 
 
740
 
void
741
 
PHP4::create_command(char *cname, char *iname) {
742
 
        char *lower_cname = strdup(cname);
743
 
        char *c;
744
 
 
745
 
        for(c = lower_cname; *c != '\0'; c++) {
746
 
                if(*c >= 'A' && *c <= 'Z')
747
 
                        *c = *c + 32;
748
 
        }
749
 
 
750
 
        Printf(s_entry,
751
 
            "   ZEND_NAMED_FE(%s,\n"
752
 
            "           %s, NULL)\n", lower_cname,iname);
753
 
        Printf(f_h, "ZEND_NAMED_FUNCTION(%s);\n", iname);
754
 
 
755
 
        free(lower_cname);
756
 
}
757
 
 
758
 
int
759
 
PHP4::functionWrapper(Node *n) {
760
 
  char *name = GetChar(n,"name");
761
 
  char *iname = GetChar(n,"sym:name");
762
 
  SwigType *d = Getattr(n,"type");
763
 
  ParmList *l = Getattr(n,"parms");
764
 
  Parm *p;
765
 
  char source[256],target[256],temp[256],argnum[32],args[32];
766
 
  int pcount,i,j,numopt;
767
 
  String *tm;
768
 
  Wrapper *f;
769
 
  int need_save, num_saved = 0;
770
 
  String *cleanup, *outarg;
771
 
 
772
 
  if(shadow && wrapping_member && !enum_flag) {
773
 
    String *member_function_name = NewString("");
774
 
    String *php_function_name = NewString(iname);
775
 
    if(strcmp(iname, Char(Swig_name_set(Swig_name_member(shadow_classname, shadow_variable_name)))) == 0) {
776
 
        Printf(member_function_name, "set");
777
 
        if(!no_sync) {
778
 
          Setattr(shadow_c_vars, php_function_name, name);
779
 
        }
780
 
    } else {
781
 
        Printf(member_function_name, "get");
782
 
        if(!no_sync) 
783
 
           Setattr(shadow_php_vars, php_function_name, name);
784
 
    }
785
 
    Putc(toupper((int )*shadow_variable_name), member_function_name);
786
 
    Printf(member_function_name, "%s", shadow_variable_name+1);
787
 
 
788
 
    cpp_func(Char(member_function_name), d, l, php_function_name);
789
 
 
790
 
    Delete(php_function_name);
791
 
    Delete(member_function_name);
792
 
  }
793
 
 
794
 
  if(!shadow)
795
 
          create_command(iname, Char(Swig_name_wrapper(iname)));
796
 
 
797
 
  outarg = cleanup = NULL;
798
 
  f     = NewWrapper();
799
 
  numopt = 0;
800
 
 
801
 
  outarg = NewString("");
802
 
 
803
 
  Printv(f->def, "ZEND_NAMED_FUNCTION(" , Swig_name_wrapper(iname), ") {\n", 0);
804
 
 
805
 
  emit_args(d, l, f);
806
 
  /* Attach standard typemaps */
807
 
  emit_attach_parmmaps(l,f);
808
 
 
809
 
  int num_arguments = emit_num_arguments(l);
810
 
  int num_required  = emit_num_required(l);
811
 
  numopt = num_arguments - num_required;
812
 
 
813
 
  sprintf(args, "%s[%d]", "zval **args", num_arguments); 
814
 
  
815
 
  Wrapper_add_local(f, "args",args);
816
 
 
817
 
  Printf(f->code, "Swig_sync_c();\n\n");/* Keep PHP4 / C vars in sync */
818
 
 
819
 
  if(numopt > 0) {
820
 
    Wrapper_add_local(f, "arg_count", "int arg_count");
821
 
    Printf(f->code,"arg_count = ZEND_NUM_ARGS();\n");
822
 
    Printf(f->code,"if(arg_count<%d || arg_count>%d)\n",num_required,num_arguments);
823
 
    Printf(f->code,"\tWRONG_PARAM_COUNT;\n\n");
824
 
 
825
 
    /* Verified args, retrieve them... */
826
 
    Printf(f->code,"if(zend_get_parameters_array_ex(arg_count,args)!=SUCCESS)");
827
 
    Printf(f->code, "\n\t\tWRONG_PARAM_COUNT;\n\n");
828
 
 
829
 
  } else {
830
 
   Printf(f->code, "if((ZEND_NUM_ARGS() != %d) || (zend_get_parameters_array_ex(%d, args) != SUCCESS)) {\n", num_arguments, num_arguments);
831
 
   Printf(f->code, "WRONG_PARAM_COUNT;\n}\n\n");
832
 
  }
833
 
  
834
 
  /* Now convert from php to C variables */
835
 
 
836
 
  for (i = 0, p = l; i < num_arguments; i++) {
837
 
    /* Skip ignored arguments */
838
 
    while (Getattr(p,"tmap:ignore")) {
839
 
      p = Getattr(p,"tmap:ignore:next");
840
 
    }
841
 
    SwigType *pt = Getattr(p,"type");
842
 
    String   *pn = Getattr(p,"name");
843
 
 
844
 
    sprintf(source, "args[%d]", i);
845
 
    sprintf(target, "%s", Char(Getattr(p,"lname")));
846
 
    sprintf(argnum, "%d", i+1);
847
 
 
848
 
    /* Check if optional */
849
 
    
850
 
    if(i>= (num_required))
851
 
      Printf(f->code,"\tif(arg_count > %d) {\n", i);
852
 
    
853
 
    Setattr(p,"emit:input", source);
854
 
 
855
 
    if ((tm = Getattr(p,"tmap:in"))) {
856
 
      Replace(tm,"$target",target,DOH_REPLACE_ANY);
857
 
      Replace(tm,"$source",source,DOH_REPLACE_ANY);
858
 
      Replace(tm,"$input", source,DOH_REPLACE_ANY);
859
 
      Printf(f->code,"%s\n",tm);
860
 
      p = Getattr(p,"tmap:in:next");
861
 
      if (i >= num_required) {
862
 
        Printf(f->code,"}\n");
863
 
      }
864
 
      continue;
865
 
    } else {
866
 
      switch(SwigType_type(pt)) {
867
 
      case T_BOOL:
868
 
      case T_INT :
869
 
      case T_SHORT :
870
 
      case T_LONG :
871
 
      case T_SCHAR :
872
 
      case T_UINT :
873
 
      case T_USHORT :
874
 
      case T_ULONG :
875
 
      case T_UCHAR :
876
 
        Printf(f->code,"convert_to_long_ex(args[%d]);\n", i);
877
 
        Printf(f->code,"%s =(%s)Z_LVAL_PP(args[%d]);\n", target, SwigType_lstr(pt,0),i);
878
 
        break;
879
 
      case T_CHAR :
880
 
        Printf(f->code,"convert_to_string_ex(args[%d]);\n", i);
881
 
        Printf(f->code,"\t%s = (char) *Z_STRVAL_PP(args[%d]);\n", target, i);
882
 
        break;
883
 
      case T_DOUBLE :
884
 
      case T_FLOAT :
885
 
        Printf(f->code,"convert_to_double_ex(args[%d]);\n", i);
886
 
        Printf(f->code,"\t%s = (%s)Z_DVAL_PP(args[%d]);\n", target, SwigType_lstr(pt,0), i);
887
 
        break;
888
 
      case T_VOID :
889
 
        break;
890
 
      case T_USER :
891
 
        SwigType_add_pointer(pt);
892
 
        sprintf(temp,"argument %d", i+1);
893
 
        Printf(f->code,"convert_to_string_ex(args[%d]);\n", i);
894
 
        get_pointer(iname, temp, source, target, pt, f->code, (char *)"RETURN_FALSE");
895
 
        SwigType_del_pointer(pt);
896
 
        break;
897
 
      case T_POINTER: case T_ARRAY: case T_REFERENCE:
898
 
        sprintf(temp,"argument %d", i+1);
899
 
        Printf(f->code,"convert_to_string_ex(args[%d]);\n", i);
900
 
        get_pointer(iname,temp,source,target, pt, f->code, (char *)"");
901
 
        break;
902
 
      case T_STRING:
903
 
        Printf(f->code,"convert_to_string_ex(args[%d]);\n", i);
904
 
        Printf(f->code,"\t%s = (char *)Z_STRVAL_PP(args[%d]);\n", target, i);
905
 
        break;
906
 
      default :
907
 
        Printf(stderr,"%s : Line %d, Unable to use type %s as a function argument.\n", input_file, line_number, SwigType_str(pt,0));
908
 
        break;
909
 
      }
910
 
      p = nextSibling(p);
911
 
    }
912
 
    if (i>= num_required)
913
 
      Printf(f->code,"\t}\n");
914
 
  }
915
 
 
916
 
  /* Insert constraint checking code */
917
 
  for (p = l; p;) {
918
 
    if ((tm = Getattr(p,"tmap:check"))) {
919
 
      Replace(tm,"$target",Getattr(p,"lname"),DOH_REPLACE_ANY);
920
 
      Printv(f->code,tm,"\n",0);
921
 
      p = Getattr(p,"tmap:check:next");
922
 
    } else {
923
 
      p = nextSibling(p);
924
 
    }
925
 
  }
926
 
  
927
 
  /* Insert cleanup code */
928
 
  for (i = 0, p = l; p; i++) {
929
 
    if ((tm = Getattr(p,"tmap:freearg"))) {
930
 
      Replace(tm,"$source",Getattr(p,"lname"),DOH_REPLACE_ANY);
931
 
      Printv(cleanup,tm,"\n",0);
932
 
      p = Getattr(p,"tmap:freearg:next");
933
 
    } else {
934
 
      p = nextSibling(p);
935
 
    }
936
 
  }
937
 
 
938
 
  /* Insert argument output code */
939
 
  num_saved = 0;
940
 
  for (i=0,p = l; p;i++) {
941
 
    if ((tm = Getattr(p,"tmap:argout"))) {
942
 
      Replace(tm,"$source",Getattr(p,"lname"),DOH_REPLACE_ANY);
943
 
      Replace(tm,"$input",Getattr(p,"lname"),DOH_REPLACE_ANY);
944
 
      Replace(tm,"$target","return_value",DOH_REPLACE_ANY);
945
 
      Replace(tm,"$result","return_value",DOH_REPLACE_ANY);
946
 
      String *in = Getattr(p,"emit:input");
947
 
      if (in) {
948
 
        sprintf(temp,"_saved[%d]", num_saved);
949
 
        Replace(tm,"$arg",temp, DOH_REPLACE_ANY);
950
 
        Printf(f->code,"_saved[%d] = %s;\n", num_saved, in);
951
 
        num_saved++;
952
 
      }
953
 
      Printv(outarg,tm,"\n",0);
954
 
      p = Getattr(p,"tmap:argout:next");
955
 
    } else {
956
 
      p = nextSibling(p);
957
 
    }
958
 
  }
959
 
 
960
 
  if(num_saved) {
961
 
    sprintf(temp, "_saved[%d]",num_saved);
962
 
    Wrapper_add_localv(f,"_saved","zval *",temp,0);
963
 
  }
964
 
 
965
 
  /* emit function call*/
966
 
  
967
 
  emit_action(n,f);
968
 
 
969
 
  if((tm = Swig_typemap_lookup((char*)"out",d,iname,(char*)"result",(char*)"result",(char*)"return_value",0))) {
970
 
    Replaceall(tm, "$input", "result");
971
 
    Replaceall(tm, "$source", "result");
972
 
    Replaceall(tm, "$target", "return_value");
973
 
    Printf(f->code, "%s\n", tm);
974
 
  } else {
975
 
    if(SwigType_type(d) != T_VOID) {
976
 
      switch(SwigType_type(d)) {
977
 
      case T_INT:
978
 
      case T_BOOL:
979
 
      case T_UINT:
980
 
      case T_SHORT:
981
 
      case T_USHORT:
982
 
      case T_LONG:
983
 
      case T_ULONG:
984
 
      case T_SCHAR:
985
 
      case T_UCHAR:
986
 
        Printf(f->code,"RETURN_LONG((long)result);\n");
987
 
        break;
988
 
      case T_DOUBLE:
989
 
      case T_FLOAT:
990
 
        Printf(f->code,"RETURN_DOUBLE((double)result);\n");
991
 
        break;
992
 
      case T_CHAR:
993
 
        Wrapper_add_local(f,"_ctemp","char ctemp[2]");
994
 
        Printv(f->code,
995
 
               tab4, "ctemp[0] = result;\n",
996
 
               tab4, "ctemp[1] = 0;\n",
997
 
               tab4, "RETURN_STRING(ctemp, 1);\n", 0);
998
 
        break;
999
 
      case T_USER:
1000
 
        SwigType_add_pointer(d);
1001
 
        SwigType_remember(d);
1002
 
        Printv(f->code, "SWIG_SetPointerZval(return_value, (void *)result, SWIGTYPE", SwigType_manglestr(d), ");\n", 0);
1003
 
        SwigType_del_pointer(d);
1004
 
        break;
1005
 
      case T_STRING:
1006
 
        Printf(f->code,"RETURN_STRING(result, 1);\n");
1007
 
        break;
1008
 
        
1009
 
      case T_POINTER:
1010
 
        SwigType_remember(d);
1011
 
        Printv(f->code, tab4, "SWIG_SetPointerZval(return_value, (void *)result, SWIGTYPE", SwigType_manglestr(d), ");\n", 0);
1012
 
        break;
1013
 
      default:
1014
 
        Printf(stderr,"%s: Line %d, Unable to use return type %s in function %s.\n", input_file, line_number, SwigType_str(d,0), name);
1015
 
        break;
1016
 
        
1017
 
      }
1018
 
    }
1019
 
  }
1020
 
  
1021
 
  if(outarg)
1022
 
    Printv(f->code,outarg,0);
1023
 
  
1024
 
  if(cleanup)
1025
 
    Printv(f->code,cleanup,0);
1026
 
  
1027
 
  if((tm = Swig_typemap_lookup((char*)"ret",d,iname,(char *)"result", (char*)"result",(char*)"",0))) {
1028
 
    Printf(f->code,"%s\n", tm);
1029
 
  }
1030
 
  
1031
 
  Replaceall(f->code,"$cleanup",cleanup);
1032
 
  Replaceall(f->code,"$symname",iname);
1033
 
  
1034
 
  Printf(f->code, "\nSwig_sync_php();\n");
1035
 
  Printf(f->code, "\n}");
1036
 
  
1037
 
  Wrapper_print(f,s_wrappers);
1038
 
  return SWIG_OK;
1039
 
}
1040
 
 
1041
 
int
1042
 
PHP4::variableWrapper(Node *n) {
1043
 
  char *name = GetChar(n,"name");
1044
 
  char *iname = GetChar(n,"sym:name");
1045
 
  SwigType *t = Getattr(n,"type");
1046
 
  int flags = 0;
1047
 
 
1048
 
  if(ReadOnly) {
1049
 
          flags |= PHP_READONLY;
1050
 
  }
1051
 
 
1052
 
 
1053
 
  SwigType_remember(t);
1054
 
 
1055
 
  /* Write out initilization code */
1056
 
  
1057
 
  switch(SwigType_type(t)) {
1058
 
  case T_INT:
1059
 
  case T_BOOL:
1060
 
  case T_UINT:
1061
 
  case T_SHORT:
1062
 
  case T_USHORT:
1063
 
  case T_LONG:
1064
 
  case T_ULONG:
1065
 
  case T_SCHAR:
1066
 
  case T_UCHAR:
1067
 
        Printf(s_vinit,
1068
 
            "{\n"
1069
 
            "    zval *z_var;\n"
1070
 
            "    MAKE_STD_ZVAL(z_var);\n"
1071
 
            "    z_var->type = IS_LONG;\n"
1072
 
            "    z_var->value.lval = %s;\n"
1073
 
            "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1074
 
            "        (void *)&z_var, sizeof(zval *), NULL);\n"
1075
 
            "}\n", name, name, strlen(name)+1);
1076
 
        break;
1077
 
 
1078
 
  case T_DOUBLE:
1079
 
  case T_FLOAT:
1080
 
        Printf(s_vinit,
1081
 
            "{\n"
1082
 
            "    zval *z_var;\n"
1083
 
            "    MAKE_STD_ZVAL(z_var);\n"
1084
 
            "    z_var->type = IS_DOUBLE;\n"
1085
 
            "    z_var->value.dval = %s;\n"
1086
 
            "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1087
 
            "        (void *)&z_var, sizeof(zval *), NULL);\n"
1088
 
            "}\n", name, name, strlen(name)+1);
1089
 
        break;
1090
 
 
1091
 
  case T_CHAR:
1092
 
        Printf(s_vinit,
1093
 
            "{\n"
1094
 
            "    zval *z_var;\n"
1095
 
            "    char c[2];\n"
1096
 
            "    MAKE_STD_ZVAL(z_var);\n"
1097
 
            "    c[0] = %s;\n"
1098
 
            "    c[1] = 0;\n"
1099
 
            "    z_var->type = IS_STRING;\n"
1100
 
            "    z_var->value.str.val = estrdup(c);\n"
1101
 
            "    z_var->value.str.len = 2;\n"
1102
 
            "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1103
 
            "        (void *)&z_var, sizeof(zval *), NULL);\n"
1104
 
            "}\n", name, name, strlen(name)+1);
1105
 
        break;
1106
 
 
1107
 
  case T_STRING:
1108
 
        Printf(s_vinit,
1109
 
            "{\n"
1110
 
            "    zval *z_var;\n"
1111
 
            "    MAKE_STD_ZVAL(z_var);\n"
1112
 
            "    z_var->type = IS_STRING;\n"
1113
 
            "    if(%s) {\n"
1114
 
            "        z_var->value.str.val = estrdup(%s);\n"
1115
 
            "        z_var->value.str.len = strlen(%s)+1;\n"
1116
 
            "    } else {\n"
1117
 
            "        z_var->value.str.val = 0;\n"
1118
 
            "        z_var->value.str.len = 0;\n"
1119
 
            "    }\n"
1120
 
            "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1121
 
            "        (void *)&z_var, sizeof(zval *), NULL);\n"
1122
 
            "}\n", name, name, name, name, strlen(name)+1);
1123
 
 
1124
 
        break;
1125
 
 
1126
 
  case T_USER:
1127
 
        SwigType_add_pointer(t);
1128
 
        Printf(s_vinit,
1129
 
            "{\n"
1130
 
            "    zval *z_var;\n"
1131
 
            "    MAKE_STD_ZVAL(z_var);\n"
1132
 
            "    SWIG_SetPointerZval(z_var, (void*)&%s, SWIGTYPE%s);\n"
1133
 
            "    zend_hash_add(&EG(symbol_table), \"%s\", %d, "
1134
 
            "        (void *)&z_var, sizeof(zval *), NULL);\n"
1135
 
            "}\n", name, SwigType_manglestr(t), name, strlen(name)+1);
1136
 
        SwigType_del_pointer(t);
1137
 
        break;
1138
 
  case T_ARRAY:
1139
 
        {
1140
 
        int setable = 0;
1141
 
        SwigType *aop;
1142
 
        SwigType *ta = Copy(t);
1143
 
        aop = SwigType_pop(ta);
1144
 
        if(SwigType_type(ta) == T_CHAR) {
1145
 
                String *dim = SwigType_array_getdim(aop, 0);
1146
 
                Printf(s_vinit,
1147
 
                    "{\n"
1148
 
                    "    zval *z_var;\n"
1149
 
                    "    MAKE_STD_ZVAL(z_var);\n"
1150
 
                    "    z_var->type = IS_STRING;\n"
1151
 
                    "    if(%s) {\n"
1152
 
                    "        z_var->value.str.val = estrndup(%s, %s);\n"
1153
 
                    "        z_var->value.str.len = strlen(%s)+1;\n"
1154
 
                    "    }\n"
1155
 
                    "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1156
 
                    "        (void *)&z_var, sizeof(zval *), NULL);\n"
1157
 
                    "}\n", name, name, Char(dim), name, name, strlen(name)+1);
1158
 
        } else {
1159
 
                Printf(s_vinit,
1160
 
                    "{\n"
1161
 
                    "    zval *z_var;\n"
1162
 
                    "    MAKE_STD_ZVAL(z_var);\n"
1163
 
                    "    SWIG_SetPointerZval(z_var, (void *)%s, SWIGTYPE%s);\n"
1164
 
                    "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1165
 
                    "        (void *)&z_var, sizeof(zval *), NULL);\n"
1166
 
                    "}\n", name, SwigType_manglestr(t), name, strlen(name)+1);
1167
 
        }
1168
 
        Delete(ta);
1169
 
        Delete(aop);
1170
 
        }
1171
 
        break;
1172
 
  case T_POINTER:
1173
 
  case T_REFERENCE:
1174
 
        Printf(s_vinit, "{\n\tzval *z_var;\n");
1175
 
        Printf(s_vinit, "\tMAKE_STD_ZVAL(z_var);\n");
1176
 
        Printf(s_vinit, "\tSWIG_SetPointerZval(z_var, (void*)%s, SWIGTYPE%s);\n", name,SwigType_manglestr(t));
1177
 
        Printf(s_vinit, "\tzend_hash_add(&EG(symbol_table), \"%s\", %d, (void *)&z_var, sizeof(zval *), NULL);\n}\n", name, strlen(name)+1);
1178
 
        Printf(s_vinit,
1179
 
            "{\n"
1180
 
            "    zval *z_var;\n"
1181
 
            "    MAKE_STD_ZVAL(z_var);\n"
1182
 
            "    SWIG_SetPointerZval(z_var, (void*)%s, SWIGTYPE%s);\n"
1183
 
            "    zend_hash_add(&EG(symbol_table), \"%s\", %d,"
1184
 
            "        (void *)&z_var, sizeof(zval *), NULL);\n"
1185
 
            "}\n", name, SwigType_manglestr(t), name, strlen(name)+1);
1186
 
 
1187
 
        break;
1188
 
  default:
1189
 
        /* error */
1190
 
        break;
1191
 
  }
1192
 
 
1193
 
 
1194
 
  /* Now code to set php values */
1195
 
 
1196
 
  switch(SwigType_type(t)) {
1197
 
  case T_INT:
1198
 
  case T_BOOL:
1199
 
  case T_UINT:
1200
 
  case T_SHORT:
1201
 
  case T_USHORT:
1202
 
  case T_LONG:
1203
 
  case T_ULONG:
1204
 
  case T_SCHAR:
1205
 
  case T_UCHAR:
1206
 
        Wrapper_add_local(f_php, "z_var", "zval **z_var");
1207
 
        Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1208
 
        Printf(f_php->code, "if(%s != (%s)((*z_var)->value.lval)) {\n", name, SwigType_lstr(t, 0));
1209
 
        Printf(f_php->code, "(*z_var)->value.lval = (long)%s;\n", name);
1210
 
        Printf(f_php->code, "}\n");
1211
 
        break;
1212
 
 
1213
 
  case T_DOUBLE:
1214
 
  case T_FLOAT:
1215
 
        Wrapper_add_local(f_php, "z_var", "zval **z_var");
1216
 
        Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1217
 
        Printf(f_php->code, "if(%s != (%s)((*z_var)->value.dval)) {\n", name, SwigType_lstr(t, 0));
1218
 
        Printf(f_php->code, "(*z_var)->value.dval = (double)%s;\n", name);
1219
 
        Printf(f_php->code, "}\n");
1220
 
        break;
1221
 
  case T_CHAR:
1222
 
        Wrapper_add_local(f_php, "z_var", "zval **z_var");
1223
 
        Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1224
 
        Printf(f_php->code, "if(%s != *((*z_var)->value.str.val)) {\n", name);
1225
 
        Printf(f_php->code, "char c[2];\n");
1226
 
        Printf(f_php->code, "efree((*z_var)->value.str.val);\n"); 
1227
 
        Printf(f_php->code, "c[0] = %s;\n", name);
1228
 
        Printf(f_php->code, "c[1] = 0;\n");
1229
 
        Printf(f_php->code, "(*z_var)->value.str.val = estrdup(c);\n");
1230
 
        Printf(f_php->code, "}\n");
1231
 
        break;
1232
 
  case T_STRING:
1233
 
        Wrapper_add_local(f_php, "z_var", "zval **z_var");
1234
 
        Wrapper_add_local(f_php, "s1", "char *s1");
1235
 
        Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1236
 
        Printf(f_php->code, "s1 = Z_STRVAL_PP(z_var);\n");
1237
 
        Printf(f_php->code, "if((s1 == NULL) || (%s == NULL) || zend_binary_strcmp(s1, strlen(s1), %s, strlen(%s) )) {\n", name, name, name);
1238
 
        Printf(f_php->code, "if(s1)\n");
1239
 
        Printf(f_php->code, "efree(s1);\n");
1240
 
        Printf(f_php->code, "if(%s) {\n", name);
1241
 
        Printf(f_php->code, "(*z_var)->value.str.val = estrdup(%s);\n", name);
1242
 
        Printf(f_php->code, "(*z_var)->value.str.len = strlen(%s)+1;\n", name);
1243
 
        Printf(f_php->code, "} else {\n");
1244
 
        Printf(f_php->code, "(*z_var)->value.str.val = 0;\n");
1245
 
        Printf(f_php->code, "(*z_var)->value.str.len = 0;\n");
1246
 
        Printf(f_php->code, "}\n}\n");
1247
 
        break;
1248
 
  case T_USER:
1249
 
        SwigType_add_pointer(t);
1250
 
        Wrapper_add_local(f_php, "z_var", "zval **z_var");
1251
 
        Printf(f_php->code, "{\nzend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1252
 
        Printf(f_php->code, "SWIG_SetPointerZval(*z_var, (void*)&%s, SWIGTYPE%s);\n", name, SwigType_manglestr(t));
1253
 
        Printf(f_php->code, "}\n");
1254
 
        SwigType_del_pointer(t);
1255
 
        break;
1256
 
  case T_ARRAY:
1257
 
        {
1258
 
        int setable = 0;
1259
 
        SwigType *aop;
1260
 
        SwigType *ta = Copy(t);
1261
 
        aop = SwigType_pop(ta);
1262
 
        if(SwigType_type(ta) == T_CHAR) {
1263
 
                String *dim = SwigType_array_getdim(aop, 0);
1264
 
                Wrapper_add_local(f_php, "z_var", "zval **z_var");
1265
 
                Wrapper_add_local(f_php, "s1", "char *s1");
1266
 
                Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1267
 
                Printf(f_php->code, "s1 = Z_STRVAL_PP(z_var);\n");
1268
 
                Printf(f_php->code, "if((s1 == NULL) || zend_binary_strcmp(s1, strlen(s1), %s, strlen(%s) )) {\n", name, name);
1269
 
                Printf(f_php->code, "if(%s) {\n", name);
1270
 
                Printf(f_php->code, "(*z_var)->value.str.val = estrdup(%s);\n", name);
1271
 
                Printf(f_php->code, "(*z_var)->value.str.len = strlen(%s)+1;\n", name);
1272
 
                Printf(f_php->code, "} else {\n");
1273
 
                Printf(f_php->code, "(*z_var)->value.str.val = 0;\n");
1274
 
                Printf(f_php->code, "(*z_var)->value.str.len = 0;\n");
1275
 
                Printf(f_php->code, "}\n}\n");
1276
 
        } else {
1277
 
                Wrapper_add_local(f_php, "z_var", "zval **z_var");
1278
 
                Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1279
 
                Printf(f_php->code, "if(%s) {\n", name);
1280
 
                Printf(f_php->code, "SWIG_SetPointerZval(*z_var, (void*)%s, SWIGTYPE);\n", name, SwigType_manglestr(t));
1281
 
                /* Arrays are not modified directly by PHP vars */
1282
 
        }
1283
 
        Delete(ta);
1284
 
        Delete(aop);
1285
 
        }
1286
 
        break;
1287
 
  case T_POINTER:
1288
 
  case T_REFERENCE:
1289
 
        Wrapper_add_local(f_php, "z_var", "zval **z_var");
1290
 
        Printf(f_php->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1291
 
        Printf(f_php->code, "SWIG_SetPointerZval(*z_var, (void *)%s, SWIGTYPE%s);\n", name, SwigType_manglestr(t));
1292
 
        break;
1293
 
  default:
1294
 
        /* error */
1295
 
        break;
1296
 
  }
1297
 
        
1298
 
  /* Now code to set c values */
1299
 
 
1300
 
  if(!(flags & PHP_READONLY)) {
1301
 
 
1302
 
  switch(SwigType_type(t)) {
1303
 
  case T_INT:
1304
 
  case T_BOOL:
1305
 
  case T_UINT:
1306
 
  case T_SHORT:
1307
 
  case T_USHORT:
1308
 
  case T_LONG:
1309
 
  case T_ULONG:
1310
 
  case T_SCHAR:
1311
 
  case T_UCHAR:
1312
 
        Wrapper_add_local(f_c, "z_var", "zval **z_var");
1313
 
        Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1314
 
        Printf(f_c->code, "if(%s != (%s)((*z_var)->value.lval)) {\n", name, SwigType_lstr(t, 0));
1315
 
        Printf(f_c->code, "%s = Z_LVAL_PP(z_var);\n", name);
1316
 
        Printf(f_c->code, "}\n");
1317
 
        break;
1318
 
  case T_DOUBLE:
1319
 
  case T_FLOAT:
1320
 
        Wrapper_add_local(f_c, "z_var", "zval **z_var");
1321
 
        Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1322
 
        Printf(f_c->code, "if(%s != (%s)((*z_var)->value.dval)) {\n", name, SwigType_lstr(t, 0));
1323
 
        Printf(f_c->code, "%s = Z_DVAL_PP(z_var);\n", name);
1324
 
        Printf(f_c->code, "\n}\n");
1325
 
        break;
1326
 
  case T_CHAR:
1327
 
        Wrapper_add_local(f_c, "z_var", "zval **z_var");
1328
 
        Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1329
 
        Printf(f_c->code, "if(%s != *((*z_var)->value.str.val)) {\n", name);
1330
 
        Printf(f_c->code, "%s = *((*z_var)->value.str.val);\n", name);
1331
 
        Printf(f_c->code, "\n}\n");
1332
 
        break;
1333
 
 
1334
 
  case T_STRING:
1335
 
        Wrapper_add_local(f_c, "z_var", "zval **z_var");
1336
 
        Wrapper_add_local(f_c, "s1", "char *s1");
1337
 
        Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1338
 
        Printf(f_c->code, "s1 = Z_STRVAL_PP(z_var);\n");
1339
 
        Printf(f_c->code, "if((s1 == NULL) || (%s == NULL) || zend_binary_strcmp(s1, strlen(s1), %s, strlen(%s) )) {\n", name, name, name);
1340
 
        Printf(f_c->code, "if(s1)\n");
1341
 
        Printf(f_c->code, "%s = estrdup(s1);\n", name, name);
1342
 
        Printf(f_c->code, "else\n");
1343
 
        Printf(f_c->code, "%s = NULL;\n", name);
1344
 
        Printf(f_c->code, "}\n");
1345
 
        break;
1346
 
 
1347
 
  case T_USER:
1348
 
        SwigType_add_pointer(t);
1349
 
        Wrapper_add_local(f_c, "z_var", "zval **z_var");
1350
 
        Printf(f_c->code, "{\n %s _temp;\n", SwigType_lstr(t,0));
1351
 
        Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1352
 
        get_pointer(name, (char*)"value", (char*)"*z_var", (char*)"&_temp", t, f_c->code,(char*)"return");
1353
 
        Printv(f_c->code, tab4, name, " = *(", SwigType_str(t,0), ") _temp;\n", 0);
1354
 
        Printf(f_c->code,"}\n");
1355
 
        SwigType_del_pointer(t);
1356
 
        break;
1357
 
  case T_ARRAY:
1358
 
        {
1359
 
        int setable = 0;
1360
 
        SwigType *aop;
1361
 
        SwigType *ta = Copy(t);
1362
 
        aop = SwigType_pop(ta);
1363
 
        if(SwigType_type(ta) == T_CHAR) {
1364
 
                String *dim = SwigType_array_getdim(aop, 0);
1365
 
                Wrapper_add_local(f_c, "z_var", "zval **z_var");
1366
 
                Wrapper_add_local(f_c, "s1", "char *s1");
1367
 
                Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1368
 
                Printf(f_c->code, "s1 = Z_STRVAL_PP(z_var);\n");
1369
 
                Printf(f_c->code, "if((s1 == NULL) || (%s == NULL) || zend_binary_strcmp(s1, strlen(s1), %s, strlen(%s) )) {\n", name, name, name);
1370
 
                Printf(f_c->code, "if(s1) {\n");
1371
 
                Printf(f_c->code, "strncpy(%s, s1, %s);\n", name, Char(dim));
1372
 
                Printf(f_c->code, "}\n}\n");
1373
 
        } else {
1374
 
                Wrapper_add_local(f_c, "z_var", "zval **z_var");
1375
 
                Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1376
 
                Printf(f_c->code, "if(%s) {\n", name);
1377
 
                Printf(f_c->code, "SWIG_SetPointerZval(*z_var, (void*)%s, SWIGTYPE%s);\n", name, SwigType_manglestr(t));
1378
 
                Printf(f_c->code, "}\n");
1379
 
                /* Arrays are not modified directly by PHP vars */
1380
 
        }
1381
 
        Delete(ta);
1382
 
        Delete(aop);
1383
 
        }
1384
 
        break;
1385
 
  case T_POINTER:
1386
 
  case T_REFERENCE:
1387
 
        Printf(f_c->code, "{\n");
1388
 
        Printf(f_c->code, "%s _temp;\n", SwigType_lstr(t,0));
1389
 
        Wrapper_add_local(f_c, "z_var", "zval **z_var");
1390
 
        Printf(f_c->code, "zend_hash_find(&EG(symbol_table), \"%s\", %d, (void **)&z_var);\n", name, strlen(name)+1);
1391
 
        get_pointer(name, (char*)"value", (char*)"*(z_var)", (char*)"&_temp", t,f_c->code, (char*)"return");
1392
 
        Printv(f_c->code, tab4, name, " = (", SwigType_str(t,0), ") _temp;\n", 0);
1393
 
        Printf(f_c->code, "}\n");
1394
 
        break;
1395
 
  default:
1396
 
        /* error */
1397
 
        break;
1398
 
  }
1399
 
  }
1400
 
  return SWIG_OK;
1401
 
}
1402
 
 
1403
 
int
1404
 
PHP4::constantWrapper(Node *n) {
1405
 
  char *name = GetChar(n,"name");
1406
 
  char *iname = GetChar(n,"sym:name");
1407
 
  SwigType *type = Getattr(n,"type");
1408
 
  char *value = GetChar(n,"value");
1409
 
        String *rval;
1410
 
        String *tm;
1411
 
 
1412
 
        SwigType_remember(type);
1413
 
 
1414
 
        switch(SwigType_type(type)) {
1415
 
        case T_STRING:
1416
 
                rval = NewStringf("\"%s\"", value);
1417
 
                break;
1418
 
        case T_CHAR:
1419
 
                rval = NewStringf("\'%s\'", value);
1420
 
                break;
1421
 
        default:
1422
 
                rval = NewString(value);
1423
 
        }
1424
 
 
1425
 
        if((tm = Swig_typemap_lookup_new("consttab", n, name, 0))) {
1426
 
                Replaceall(tm, "$source", value);
1427
 
                Replaceall(tm, "$target", name);
1428
 
                Replaceall(tm, "$value", value);
1429
 
                Printf(s_cinit, "%s\n", tm);
1430
 
        } else {
1431
 
        switch(SwigType_type(type)) {
1432
 
                case T_BOOL: 
1433
 
                case T_INT :
1434
 
                case T_SHORT :
1435
 
                case T_SCHAR:
1436
 
                case T_LONG:
1437
 
                     Printf(s_cinit,
1438
 
                     "    REGISTER_LONG_CONSTANT(\"%s\", %s, CONST_CS);\n",
1439
 
                     name, rval);
1440
 
                     break;
1441
 
                case T_DOUBLE: 
1442
 
                case T_FLOAT:
1443
 
                     Printf(s_cinit,
1444
 
                     "    REGISTER_DOUBLE_CONSTANT(\"%s\", %s, CONST_CS);\n",
1445
 
                     name, rval);
1446
 
                     break;
1447
 
                case T_CHAR:
1448
 
                     Printf(s_cinit,
1449
 
                     "{\n"
1450
 
                     "    char c[2];\n"
1451
 
                     "    c[0] = %s;\n"
1452
 
                     "    c[1] = '\\0';\n"
1453
 
                     "    REGISTER_STRING_CONSTANT(\"%s\", estrdup(c),"
1454
 
                     "        CONST_CS | CONST_PERSISTENT);\n"
1455
 
                     "}\n", rval, name);
1456
 
                     break;
1457
 
                case T_STRING:
1458
 
                     Printf(s_cinit,
1459
 
                     "    REGISTER_STRING_CONSTANT(\"%s\", %s,"
1460
 
                     "        CONST_CS | CONST_PERSISTENT);\n", name, rval);
1461
 
                     break;
1462
 
                case T_POINTER:
1463
 
                case T_ARRAY:
1464
 
                case T_REFERENCE:
1465
 
                      Printf(s_cinit,
1466
 
                      "{\n"
1467
 
                      "    char *cp;\n"
1468
 
                      "    SWIG_SetPointerChar(&cp, (void*)%s, SWIGTYPE%s);\n"
1469
 
                      "    REGISTER_STRING_CONSTANT(\"%s\", cp,"
1470
 
                      "        CONST_CS | CONST_PERSISTENT);\n"
1471
 
                      "}\n", value, SwigType_manglestr(type), name);
1472
 
                      break;
1473
 
                default:
1474
 
                        break;
1475
 
        }
1476
 
        return SWIG_OK;
1477
 
        }
1478
 
}
1479
 
 
1480
 
/*
1481
 
 * PHP4::pragma()
1482
 
 *
1483
 
 * Pragma directive.
1484
 
 *
1485
 
 * %pragma(php4) code="String"         # Includes a string in the .php file
1486
 
 * %pragma(php4) include="file.pl"     # Includes a file in the .php file
1487
 
 */
1488
 
void PHP4::pragma(char *lang, char *type, char *value) {
1489
 
       if (strcmp(lang,"php4") != 0) return;
1490
 
 
1491
 
       if (strcmp(type, "code") == 0) {
1492
 
               if (value)
1493
 
                       Printf(pragma_code, "%s\n", value);
1494
 
       } else if (strcmp(type, "include") == 0) {
1495
 
               if (value)
1496
 
                       Printf(pragma_incl, "include \"%s\";\n", value);
1497
 
       } else if (strcmp(type, "phpinfo") == 0) {
1498
 
               if (value)
1499
 
                       Printf(pragma_phpinfo, "%s\n", value);
1500
 
       } else {
1501
 
              Printf(stderr, "%s : Line %d. Unrecognized pragma.\n",
1502
 
                      input_file, line_number);
1503
 
       }
1504
 
}
1505
 
 
1506
 
/*
1507
 
void
1508
 
PHP4::usage_func(char *iname, SwigType *, ParmList *l) {
1509
 
 
1510
 
        ;
1511
 
}
1512
 
*/
1513
 
 
1514
 
void 
1515
 
PHP4::add_native(char *name, char *funcname, SwigType *, ParmList *) {
1516
 
        ;
1517
 
}
1518
 
 
1519
 
void
1520
 
PHP4::emit_shadow_classdef() {
1521
 
        String *baseclass = NULL;
1522
 
 
1523
 
        // Include Base class definition
1524
 
        
1525
 
        /*
1526
 
        if(this_shadow_baseclass && *Char(this_shadow_baseclass))
1527
 
                Printf(shadow_classdef, 
1528
 
                      "include(\"%s.php\");\n",
1529
 
                      this_shadow_baseclass);
1530
 
        */
1531
 
 
1532
 
        // Import statements
1533
 
        if(all_shadow_import)
1534
 
                Printf(shadow_classdef, "%s", all_shadow_import);
1535
 
        if(this_shadow_import)
1536
 
                Printf(shadow_classdef, "%s", this_shadow_import);
1537
 
        Printf(shadow_classdef, "\n");
1538
 
 
1539
 
        // Class modifiers XXX not in php
1540
 
        
1541
 
        Printf(shadow_classdef, "class $class ");
1542
 
 
1543
 
        // Inherited classes
1544
 
        if(this_shadow_baseclass && *Char(this_shadow_baseclass)) {
1545
 
                Printf(shadow_classdef, "extends %s ", this_shadow_baseclass);
1546
 
                baseclass = this_shadow_baseclass;
1547
 
        }
1548
 
        if(all_shadow_baseclass && *Char(all_shadow_baseclass)) {
1549
 
                Printf(shadow_classdef, "extends %s ", all_shadow_baseclass);
1550
 
                baseclass = all_shadow_baseclass;
1551
 
        }
1552
 
 
1553
 
        Printf(shadow_classdef, "{\n");
1554
 
 
1555
 
        // XXX No interfaces (?)
1556
 
        
1557
 
        // Display warning on attempt to use multiple inheritance
1558
 
        
1559
 
        char *search_str = Char(shadow_classdef);
1560
 
        int count = 0;
1561
 
        while((search_str = strstr(search_str, "extends"))) {
1562
 
                search_str += strlen("extends");
1563
 
                count++;
1564
 
        }
1565
 
        if(count > 1)
1566
 
                Printf(stderr, "Warning for shadow class %s: Multiple inheritance is not supported in PHP4.\n", shadow_classname);
1567
 
 
1568
 
        // Different code depending on whether or not the base class is a
1569
 
        // SWIG shadow class.
1570
 
        
1571
 
        if(baseclass && is_shadow(baseclass)) {
1572
 
                // Control which super constructor is called -
1573
 
                // we don't want 2 malloc/new c/c++ calls
1574
 
        } else {
1575
 
                String *k;
1576
 
 
1577
 
                Printv(shadow_classdef,
1578
 
                " var $_cPtr;\n",
1579
 
                " var $_cMemOwn;\n", 0);
1580
 
 
1581
 
                for(k = Firstkey(shadow_php_vars); k; k = Nextkey(shadow_php_vars)) {
1582
 
                        Printf(shadow_classdef, " var $%s;\n", Getattr(shadow_php_vars, k));
1583
 
                }
1584
 
 
1585
 
                Printv(shadow_classdef,
1586
 
                "\n",
1587
 
                " function getCPtr() {\n",
1588
 
                "    return $this->_cPtr;\n",
1589
 
                " }\n",
1590
 
                "\n",
1591
 
                " function setCPtr($cPtr, $own) {\n",
1592
 
                "    $this->_cPtr = $cPtr;\n",
1593
 
                "    $this->_cMemOwn = $own;\n",
1594
 
                " }\n",
1595
 
                "\n", 0);
1596
 
 
1597
 
                // No explicit super constructor call as this class does not
1598
 
                // have a SWIG base class.
1599
 
        }
1600
 
 
1601
 
        Replace(shadow_classdef, "$class", shadow_classname, DOH_REPLACE_ANY);
1602
 
 
1603
 
        if(all_shadow_extra_code)
1604
 
                Printv(shadow_classdef, all_shadow_extra_code, 0);
1605
 
 
1606
 
        if(this_shadow_extra_code)
1607
 
                Printv(shadow_classdef, this_shadow_extra_code, 0);
1608
 
}
1609
 
 
1610
 
 
1611
 
 
1612
 
int PHP4::classHandler(Node *n) {
1613
 
 
1614
 
        char bigbuf[1024];
1615
 
 
1616
 
        if(class_name) free(class_name);
1617
 
        class_name = Swig_copy_string(GetChar(n, "name"));
1618
 
 
1619
 
        if(shadow) {
1620
 
                char *classname = GetChar(n, "name");
1621
 
                char *rename = GetChar(n, "sym:name");
1622
 
                char *ctype = GetChar(n, "kind");
1623
 
 
1624
 
                shadow_classname = Swig_copy_string(rename);
1625
 
 
1626
 
                if(Strcmp(shadow_classname, module) == 0) {
1627
 
                        Printf(stderr, "class name cannot be equal to module name: %s\n", shadow_classname);
1628
 
                        SWIG_exit(1);
1629
 
                }
1630
 
 
1631
 
                Setattr(shadow_classes, classname, shadow_classname);
1632
 
 
1633
 
                if(ctype && strcmp(ctype, "struct") == 0) {
1634
 
                        sprintf(bigbuf, "struct %s", classname);
1635
 
                        Setattr(shadow_classes, bigbuf, shadow_classname);
1636
 
                }
1637
 
 
1638
 
                Clear(shadow_classdef);
1639
 
                Clear(shadow_code);
1640
 
 
1641
 
                have_default_constructor = 0;
1642
 
                shadow_enum_code = NewString("");
1643
 
                this_shadow_baseclass = NewString("");
1644
 
                this_shadow_extra_code = NewString("");
1645
 
                this_shadow_import = NewString("");
1646
 
 
1647
 
                shadow_c_vars = NewHash();
1648
 
                shadow_php_vars = NewHash();
1649
 
 
1650
 
                /* Deal with inheritance */
1651
 
                List *baselist = Getattr(n, "bases");
1652
 
                if(baselist) {
1653
 
                        Node *base = Firstitem(baselist);
1654
 
 
1655
 
                        if(is_shadow(Getattr(base, "name"))) {
1656
 
                                Printf(this_shadow_baseclass, "%s", Getattr(base, "name"));
1657
 
                        }
1658
 
                        base = Nextitem(baselist);
1659
 
                        if(base) {
1660
 
                                Printf(stderr, "Error: %s inherits from multiple base classes. Multiple inheritance is not supported by PHP4.\n", shadow_classname);
1661
 
                        }
1662
 
                } else { // XXX Must be base class ?
1663
 
                  /* Write out class init code */
1664
 
 
1665
 
                  if(!written_base_class) {
1666
 
                    written_base_class = 1;
1667
 
                    Printf(s_oinit,"{\nzend_class_entry *ce;\n");
1668
 
                    Printf(s_oinit,"CG(class_entry).type = ZEND_USER_CLASS;\n");
1669
 
                    Printf(s_oinit, "CG(class_entry).name = estrdup(\"%s\");\n", package);
1670
 
                    Printf(s_oinit, "CG(class_entry).name_length = strlen(\"%s\");\n", package);
1671
 
                    Printf(s_oinit, "CG(class_entry).refcount = (int *) emalloc(sizeof(int));\n");
1672
 
                    Printf(s_oinit, "*CG(class_entry).refcount = 1;\n");
1673
 
                    Printf(s_oinit, "CG(class_entry).constants_updated = 0;\n");
1674
 
 
1675
 
                    /* XXX do this ourselves */
1676
 
 
1677
 
                    Printf(s_oinit, "zend_str_tolower(CG(class_entry).name, CG(class_entry).name_length);\n");
1678
 
        
1679
 
                    /* Init class function hash */
1680
 
                
1681
 
                    Printf(s_oinit, "zend_hash_init(&CG(class_entry).function_table, 10, NULL, ZEND_FUNCTION_DTOR, 0);\n");
1682
 
                    Printf(s_oinit, "zend_hash_init(&CG(class_entry).default_properties, 10, NULL, ZVAL_PTR_DTOR, 0);\n");
1683
 
 
1684
 
                    /* XXX Handle inheritance ? */
1685
 
 
1686
 
                    Printf(s_oinit, "CG(class_entry).handle_function_call = NULL;\n");
1687
 
                    Printf(s_oinit, "CG(class_entry).handle_property_set = NULL;\n");
1688
 
                    Printf(s_oinit, "CG(class_entry).handle_property_get = NULL;\n");
1689
 
 
1690
 
                    /* Save class in class table */
1691
 
                    Printf(s_oinit, "zend_hash_update(CG(class_table), \"%s\", strlen(\"%s\")+1, &CG(class_entry), sizeof(zend_class_entry), (void **) &CG(active_class_entry));\n", package, package);
1692
 
 
1693
 
                    Printf(s_oinit, "}\n");
1694
 
 
1695
 
                  }
1696
 
 
1697
 
                }
1698
 
 
1699
 
        }
1700
 
 
1701
 
 
1702
 
        Language::classHandler(n);
1703
 
 
1704
 
        if(shadow) {
1705
 
 
1706
 
                emit_shadow_classdef();
1707
 
 
1708
 
                Printv(f_phpcode, shadow_classdef, shadow_code, 0);
1709
 
 
1710
 
                // Write the enum initialisation code in a static block
1711
 
                // These are all the enums defined withing the c++ class.
1712
 
 
1713
 
                // XXX Needed in PHP ?
1714
 
                if(strlen(Char(shadow_enum_code)) != 0 )
1715
 
                        Printv(f_phpcode, "{\n // enum\n", shadow_enum_code, " }\n", 0);
1716
 
                Printf(f_phpcode, "}\n");
1717
 
 
1718
 
                free(shadow_classname);
1719
 
                shadow_classname = NULL;
1720
 
 
1721
 
                Delete(shadow_enum_code); shadow_enum_code = NULL;
1722
 
                Delete(this_shadow_baseclass); this_shadow_baseclass = NULL;
1723
 
                Delete(this_shadow_extra_code); this_shadow_extra_code = NULL;
1724
 
                Delete(this_shadow_import); this_shadow_import = NULL;
1725
 
                Delete(shadow_c_vars); shadow_c_vars = NULL;
1726
 
                Delete(shadow_php_vars); shadow_php_vars = NULL;
1727
 
        }
1728
 
        return SWIG_OK;
1729
 
}
1730
 
 
1731
 
int
1732
 
PHP4::memberfunctionHandler(Node *n) {
1733
 
        char *name = GetChar(n, "name");
1734
 
        char *iname = GetChar(n, "sym:name");
1735
 
        SwigType *t = Getattr(n, "type");
1736
 
        ParmList *l = Getattr(n, "parms");
1737
 
 
1738
 
        this->Language::memberfunctionHandler(n);
1739
 
 
1740
 
        if(shadow) {
1741
 
                char *realname = iname ? iname : name;
1742
 
                String *php_function_name = Swig_name_member(shadow_classname, realname);
1743
 
 
1744
 
                cpp_func(iname, t, l, php_function_name);
1745
 
                /*
1746
 
 
1747
 
        Printf(s_oinit, "{\nzend_function function;\n");
1748
 
        Printf(s_oinit, "zend_internal_function *internal_function = (zend_internal_function *)&function;\n");
1749
 
        Printf(s_oinit, "internal_function->type= ZEND_INTERNAL_FUNCTION;\n");
1750
 
        Printf(s_oinit, "internal_function->handler = %s;\n", Swig_name_wrapper(iname));
1751
 
        Printf(s_oinit, "internal_function->arg_types = NULL;\n");
1752
 
        Printf(s_oinit, "internal_function->function_name = estrdup(\"%s\");\n", Swig_name_wrapper(iname));
1753
 
        Printf(s_oinit, "zend_hash_add(&CG(active_class_entry)->function_table, \"%s\", %d, &function, sizeof(zend_function), NULL);\n}\n", Swig_name_wrapper(name), strlen(Char(Swig_name_wrapper(name)))+1);
1754
 
 
1755
 
        */
1756
 
        }
1757
 
        return SWIG_OK;
1758
 
}
1759
 
 
1760
 
int
1761
 
PHP4::membervariableHandler(Node *n) {
1762
 
        char *name = GetChar(n,"name");
1763
 
        char *iname = GetChar(n, "sym:name");
1764
 
        SwigType *t = Getattr(n, "type");
1765
 
 
1766
 
 
1767
 
        shadow_variable_name = Swig_copy_string((iname) ? iname : name);
1768
 
 
1769
 
        wrapping_member = 1;
1770
 
        variable_wrapper_flag = 1;
1771
 
        Language::membervariableHandler(n);
1772
 
        wrapping_member = 0;
1773
 
        variable_wrapper_flag = 0;
1774
 
 
1775
 
        return SWIG_OK;
1776
 
}
1777
 
 
1778
 
int PHP4::staticmemberfunctionHandler(Node *n) {
1779
 
 
1780
 
        Language::staticmemberfunctionHandler(n);
1781
 
 
1782
 
        if(shadow) {
1783
 
                String *symname = Getattr(n, "sym:name");
1784
 
                String *php_function_name = Swig_name_member(shadow_classname, symname);
1785
 
                static_flag = 1;
1786
 
                cpp_func(Char(symname), Getattr(n, "type"), Getattr(n, "parms"), php_function_name);
1787
 
                static_flag = 0;
1788
 
        }
1789
 
 
1790
 
        return SWIG_OK;
1791
 
}
1792
 
 
1793
 
int PHP4::staticmembervariableHandler(Node *n) {
1794
 
        shadow_variable_name = GetChar(n, "sym:name");
1795
 
        SwigType *d = Getattr(n, "type");
1796
 
        ParmList *l = Getattr(n, "parms");
1797
 
        char *iname = GetChar(n, "sym:name");
1798
 
        String *static_name = NewStringf("%s::%s", class_name, iname);
1799
 
        Wrapper *f;
1800
 
 
1801
 
  /* A temporary(!) hack for static member variables.
1802
 
   * Php currently supports class functions, but not class variables.
1803
 
   * Until it does, we convert a class variable to a class function
1804
 
   * that returns the current value of the variable. E.g.
1805
 
   *
1806
 
   * class Example {
1807
 
   *    public:
1808
 
   *            static int ncount;
1809
 
   * };
1810
 
   *
1811
 
   * would be available in php as Example::ncount() 
1812
 
   */
1813
 
        static_flag = 1;
1814
 
        if(ReadOnly) {
1815
 
                const_flag = 1;
1816
 
        }
1817
 
        cpp_func(iname, d, 0, iname);
1818
 
        static_flag = 0;
1819
 
 
1820
 
        create_command(iname, Char(Swig_name_wrapper(iname)));
1821
 
 
1822
 
        f = NewWrapper();
1823
 
 
1824
 
        Printv(f->def, "ZEND_NAMED_FUNCTION(", Swig_name_wrapper(iname), ") {\n", 0);
1825
 
 
1826
 
        /* If a argument is given we set the variable. Then we return
1827
 
         * the current value
1828
 
        */
1829
 
 
1830
 
        Printf(f->code, "zval **args[1];\n");
1831
 
        Printf(f->code, "int argcount;\n\n");
1832
 
 
1833
 
        Printf(f->code, "argcount = ZEND_NUM_ARGS();\n");
1834
 
        Printf(f->code, "if(argcount > %d) WRONG_PARAM_COUNT;\n\n", (const_flag? 0 : 1));
1835
 
        if(!const_flag) {
1836
 
          Printf(f->code, "if(argcount) {\n");
1837
 
 
1838
 
          Printf(f->code, "if(zend_get_parameters_array_ex(argcount, args) != SUCCESS) WRONG_PARAM_COUNT;\n");
1839
 
 
1840
 
          switch(SwigType_type(d)) {
1841
 
                case T_BOOL:
1842
 
                case T_INT:
1843
 
                case T_SHORT:
1844
 
                case T_LONG:
1845
 
                case T_SCHAR:
1846
 
                case T_UINT:
1847
 
                case T_USHORT:
1848
 
                case T_ULONG:
1849
 
                case T_UCHAR:
1850
 
                        Printf(f->code, "convert_to_long_ex(args[0]);\n");
1851
 
                        Printf(f->code, "%s::%s = Z_LVAL_PP(args[0]);\n", class_name, iname);
1852
 
                        break;
1853
 
                case T_CHAR:
1854
 
                        Printf(f->code, "convert_to_string_ex(args[0]);\n");
1855
 
                        Printf(f->code, "%s::%s = estrdup(Z_STRVAL(args[0]));\n");
1856
 
                        break;
1857
 
                case T_DOUBLE:
1858
 
                case T_FLOAT:
1859
 
                        Printf(f->code, "convert_to_double_ex(args[0]);\n");
1860
 
                        Printf(f->code, "%s::%s = Z_DVAL_PP(args[0]);\n", class_name, iname);
1861
 
                        break;
1862
 
                case T_VOID:
1863
 
                        break;
1864
 
                case T_USER:
1865
 
                        Printf(f->code, "convert_to_string_ex(args[0]);\n");
1866
 
                        get_pointer(Char(iname), (char*)"variable", (char*)"args[0]", Char(static_name), d, f->code, (char *)"RETURN_FALSE");
1867
 
                        break;
1868
 
                case T_POINTER:
1869
 
                case T_ARRAY:
1870
 
                case T_REFERENCE:
1871
 
                        Printf(f->code, "convert_to_string_ex(args[0]);\n");
1872
 
                        get_pointer(Char(iname), (char*)"variable", (char*)"args[0]", Char(static_name), d, f->code, (char*)"RETURN_FALSE");
1873
 
                        break;
1874
 
                default:
1875
 
                        Printf(stderr,"%s : Line %d, Unable to use type %s as a class variable.\n", input_file, line_number, SwigType_str(d,0));
1876
 
                        break;
1877
 
                }
1878
 
                
1879
 
          Printf(f->code, "}\n\n");
1880
 
        
1881
 
        } /* end of const_flag */
1882
 
 
1883
 
        switch(SwigType_type(d)) {
1884
 
                case T_BOOL:
1885
 
                case T_INT:
1886
 
                case T_SHORT:
1887
 
                case T_LONG:
1888
 
                case T_SCHAR:
1889
 
                case T_UINT:
1890
 
                case T_USHORT:
1891
 
                case T_ULONG:
1892
 
                case T_UCHAR:
1893
 
                        Printf(f->code, "RETURN_LONG(%s::%s);\n", class_name, iname);
1894
 
                        break;
1895
 
                case T_DOUBLE:
1896
 
                case T_FLOAT:
1897
 
                        Printf(f->code, "RETURN_DOUBLE(%s);\n", static_name);
1898
 
                        break;
1899
 
                case T_CHAR:
1900
 
                        Printf(f->code, "{\nchar ctemp[2];\n");
1901
 
                        Printf(f->code, "ctemp[0] = %s;\n", static_name);
1902
 
                        Printf(f->code, "ctemp[1] = 0;\n");
1903
 
                        Printf(f->code, "RETURN_STRING(ctemp, 1);\n}\n");
1904
 
                        break;
1905
 
                case T_USER:
1906
 
                case T_POINTER:
1907
 
                        Printf(f->code, "SWIG_SetPointerZval(return_value, (void *)%s, SWIGTYPE%s);\n", static_name, SwigType_manglestr(d));
1908
 
                        break;
1909
 
                case  T_STRING:
1910
 
                        Printf(f->code, "RETURN_STRING(%s, 1);\n", static_name);
1911
 
                        break;
1912
 
                }
1913
 
 
1914
 
 
1915
 
        Printf(f->code, "}\n");
1916
 
 
1917
 
        const_flag = 0;
1918
 
 
1919
 
        Wrapper_print(f, s_wrappers);
1920
 
 
1921
 
        return SWIG_OK;
1922
 
}
1923
 
 
1924
 
void PHP4::SwigToPhpType(SwigType *t, String_or_char *pname, String* php_type, int shadow_flag) {
1925
 
        char *ptype = 0;
1926
 
 
1927
 
        if(shadow_flag)
1928
 
                ptype = PhpTypeFromTypemap((char*)"pstype", t, pname,(char*)"");
1929
 
        if(!ptype)
1930
 
                ptype = PhpTypeFromTypemap((char*)"ptype",t,pname,(char*)"");
1931
 
 
1932
 
 
1933
 
        if(ptype) {
1934
 
                Printf(php_type, ptype);
1935
 
                free(ptype);
1936
 
        }
1937
 
        else {
1938
 
                /* Map type here */
1939
 
                switch(SwigType_type(t)) {
1940
 
                        case T_CHAR:
1941
 
                        case T_SCHAR:
1942
 
                        case T_UCHAR:
1943
 
                        case T_SHORT:
1944
 
                        case T_USHORT:
1945
 
                        case T_INT:
1946
 
                        case T_UINT:
1947
 
                        case T_LONG:
1948
 
                        case T_ULONG:
1949
 
                        case T_FLOAT:
1950
 
                        case T_DOUBLE:
1951
 
                        case T_BOOL:
1952
 
                        case T_STRING:
1953
 
                        case T_VOID:
1954
 
                                Printf(php_type, "");
1955
 
                                break;
1956
 
                        case T_POINTER:
1957
 
                        case T_REFERENCE:
1958
 
                        case T_USER:
1959
 
                                if(shadow_flag && is_shadow(t))
1960
 
                                        Printf(php_type, Char(is_shadow(t)));
1961
 
                                else
1962
 
                                        Printf(php_type, "");
1963
 
                                break;
1964
 
                        case T_ARRAY:
1965
 
                                /* TODO */
1966
 
                                break;
1967
 
                        default:
1968
 
                                Printf(stderr, "SwigToPhpType: unhandled data type: %s\n", SwigType_str(t,0));
1969
 
                                break;
1970
 
                        }
1971
 
        }
1972
 
}
1973
 
 
1974
 
 
1975
 
char *PHP4::PhpTypeFromTypemap(char *op, SwigType *t, String_or_char *pname, String_or_char *lname) {
1976
 
        String *tms;
1977
 
        char bigbuf[1024];
1978
 
        char *tm;
1979
 
        char *c = bigbuf;
1980
 
        if(!(tms = Swig_typemap_lookup(op, t, pname, lname, (char*)"", (char*)"", NULL))) return NULL;
1981
 
 
1982
 
        tm = Char(tms);
1983
 
        while(*tm && (isspace(*tm) || *tm == '{')) tm++;
1984
 
        while(*tm && *tm != '}') *c++ = *tm++;
1985
 
        *c='\0';
1986
 
        return Swig_copy_string(bigbuf);
1987
 
}
1988
 
 
1989
 
 
1990
 
int PHP4::constructorHandler(Node *n) {
1991
 
 
1992
 
        char *iname = GetChar(n, "sym:name");
1993
 
        ParmList *l = Getattr(n, "parms");
1994
 
 
1995
 
        Language::constructorHandler(n);
1996
 
 
1997
 
        if(shadow) {
1998
 
                String *nativecall = NewString("");
1999
 
                String *php_function_name = NewString(iname);
2000
 
                char arg[256];
2001
 
 
2002
 
                 Printf(s_oinit, "{\nzend_function function;\n");
2003
 
                 Printf(s_oinit, "zend_internal_function *internal_function = (zend_internal_function *)&function;\n");
2004
 
                 Printf(s_oinit, "internal_function->type= ZEND_INTERNAL_FUNCTION;\n");
2005
 
                 Printf(s_oinit, "internal_function->handler = _wrap_new_%s;\n", iname);
2006
 
                 Printf(s_oinit, "internal_function->arg_types = NULL;\n");
2007
 
                 Printf(s_oinit, "internal_function->function_name = estrdup(\"new_%(lower)s\");\n", php_function_name);
2008
 
                 Printf(s_oinit, "zend_hash_add(&CG(active_class_entry)->function_table, \"new_%(lower)s\", %d, &function, sizeof(zend_function), NULL);\n}\n", php_function_name, strlen(Char(php_function_name))+5);
2009
 
                Printf(shadow_code, " function %s(", shadow_classname);
2010
 
 
2011
 
                if(iname != NULL)
2012
 
                        Printv(nativecall, tab4, "$this->_cPtr = ", package, "::", Swig_name_construct(iname), "(", 0);
2013
 
                else
2014
 
                        Printv(nativecall, tab4, "$this->_cPtr = ", module, "::", Swig_name_construct(shadow_classname), "(", 0);
2015
 
 
2016
 
                int pcount = ParmList_len(l);
2017
 
                if(pcount == 0) // must have default constructor
2018
 
                        have_default_constructor = 1;
2019
 
 
2020
 
                /* Output each parameter */
2021
 
                Parm *p = l;
2022
 
                for (int i = 0; i < pcount ; i++, p = nextSibling(p)) {
2023
 
                        SwigType *pt = Getattr(p, "type");
2024
 
                        String *pn = Getattr(p, "name");
2025
 
 
2026
 
        /* Produce string representation of source and target arguments */
2027
 
 
2028
 
                        if(pn && *(Char(pn)))
2029
 
                                strcpy(arg, Char(pn));
2030
 
                        else {
2031
 
                                sprintf(arg, "arg%d", i);
2032
 
                        }
2033
 
 
2034
 
                        if(is_shadow(pt)) {
2035
 
                                Printv(nativecall, "$", arg, "->getCPtr()", 0);
2036
 
                        } else 
2037
 
                                Printv(nativecall, "$", arg, 0);
2038
 
 
2039
 
                        /* Add to php shadow function header */
2040
 
                        Printf(shadow_code, "$%s", arg);
2041
 
 
2042
 
                        if(i != pcount-1) {
2043
 
                                Printf(nativecall, ", ");
2044
 
                                Printf(shadow_code, ", ");
2045
 
                        }
2046
 
                }
2047
 
 
2048
 
                Printf(shadow_code, ") {\n");
2049
 
                Printv(nativecall, ");\n", tab4, "$this->_cMemOwn = true;\n", 0);
2050
 
                /* register our shutdown function */
2051
 
 
2052
 
                Printv(nativecall, tab4,
2053
 
                                   "register_shutdown_function(array(&$this,",
2054
 
                                   "\"_destroy\"));\n", 0);
2055
 
 
2056
 
                /* Store new values in PHP */
2057
 
                if(!no_sync) {
2058
 
                        Printv(nativecall, tab4,
2059
 
                                   "$this->_sync_php();\n", 0);
2060
 
                }
2061
 
 
2062
 
                Printf(shadow_code, "%s", nativecall);
2063
 
                Printf(shadow_code, "  }\n\n");
2064
 
                Delete(nativecall);
2065
 
        }
2066
 
        return SWIG_OK;
2067
 
}
2068
 
 
2069
 
int PHP4::destructorHandler(Node *n) {
2070
 
 
2071
 
        Language::destructorHandler(n);
2072
 
 
2073
 
        if(shadow) {
2074
 
          Printf(shadow_code, " function _destroy() {\n");
2075
 
          Printf(shadow_code, "   if($this->_cPtr && $this->_cMemOwn) {\n");
2076
 
          Printf(shadow_code, "     %s::%s($this->_cPtr);\n", package, Swig_name_destroy(shadow_classname));
2077
 
          Printf(shadow_code, "     $this->_cPtr = 0;\n");
2078
 
          Printf(shadow_code, "   }\n");
2079
 
          Printf(shadow_code, " }\n\n");
2080
 
 
2081
 
          if(!no_sync) {
2082
 
            String *k;
2083
 
            Printf(shadow_code," function _sync_c() {\n ");
2084
 
 
2085
 
            for(k = Firstkey(shadow_c_vars); k ; k = Nextkey(shadow_c_vars)) {
2086
 
                    Printf(shadow_code, "%s::%s($this->_cPtr, $this->%s);\n",
2087
 
                                        package, k, 
2088
 
                                        Getattr(shadow_c_vars, k));
2089
 
            }
2090
 
 
2091
 
            Printf(shadow_code, "\n}\n");
2092
 
 
2093
 
            Printf(shadow_code," function _sync_php() {\n");
2094
 
 
2095
 
            for(k = Firstkey(shadow_php_vars);k;k = Nextkey(shadow_php_vars)) {
2096
 
                    Printf(shadow_code, "$this->%s = %s::%s($this->_cPtr);\n",
2097
 
                                         Getattr(shadow_php_vars, k),
2098
 
                                         package, k);
2099
 
            }
2100
 
 
2101
 
            Printf(shadow_code, "\n}\n");
2102
 
          }
2103
 
 
2104
 
          String *iname = Swig_name_destroy(GetChar(n, "sym:name"));
2105
 
 
2106
 
          Printf(s_oinit, "{\nzend_function function;\n");
2107
 
          Printf(s_oinit, "zend_internal_function *internal_function = (zend_internal_function *)&function;\n");
2108
 
          Printf(s_oinit, "internal_function->type= ZEND_INTERNAL_FUNCTION;\n");
2109
 
          Printf(s_oinit, "internal_function->handler = %s;\n", Swig_name_wrapper(iname));
2110
 
          Printf(s_oinit, "internal_function->arg_types = NULL;\n");
2111
 
          Printf(s_oinit, "internal_function->function_name = estrdup(\"%(lower)s\");\n", iname);
2112
 
          Printf(s_oinit, "zend_hash_add(&CG(active_class_entry)->function_table, \"%(lower)s\", %d, &function, sizeof(zend_function), NULL);\n}\n", iname, strlen(Char(iname))+1);
2113
 
        }
2114
 
        return SWIG_OK;
2115
 
}
2116
 
 
2117
 
int
2118
 
PHP4::memberconstantHandler(Node *n) {
2119
 
        shadow_variable_name = GetChar(n, "sym:name");
2120
 
        wrapping_member = 1;
2121
 
        Language::memberconstantHandler(n);
2122
 
        wrapping_member = 0;
2123
 
        return SWIG_OK;
2124
 
}
2125
 
 
2126
 
int
2127
 
PHP4::classforwardDeclaration(Node *n) {
2128
 
        String *name = Getattr(n, "name");
2129
 
        String *rename = Getattr(n, "sym:name");
2130
 
        String *type = Getattr(n, "kind");
2131
 
        String *stype;
2132
 
 
2133
 
        if(shadow) {
2134
 
                stype = NewString(name);
2135
 
                SwigType_add_pointer(stype);
2136
 
                Setattr(shadow_classes, stype, rename);
2137
 
                Delete(stype);
2138
 
                if(Len(type) > 0) {
2139
 
                        stype = NewStringf("%s %s", type, name);
2140
 
                        SwigType_add_pointer(stype);
2141
 
                        Setattr(shadow_classes, stype, rename);
2142
 
                        Delete(stype);
2143
 
                }
2144
 
        }
2145
 
        return SWIG_OK;
2146
 
}
2147
 
 
2148
 
int
2149
 
PHP4::typedefHandler(Node *n) {
2150
 
        SwigType *t = Getattr(n, "type");
2151
 
        String *name = Getattr(n, "name");
2152
 
        if(!shadow) return SWIG_OK;
2153
 
        if(is_shadow(t)) {
2154
 
                Setattr(shadow_classes, name, is_shadow(t));
2155
 
        }
2156
 
        return SWIG_OK;
2157
 
}
2158
 
 
2159
 
void 
2160
 
PHP4::cpp_func(char *iname, SwigType *t, ParmList *l, String *php_function_name) {
2161
 
        char arg[256];
2162
 
        String *nativecall = NewString("");
2163
 
        String *user_arrays = NewString("");
2164
 
        String *lower;
2165
 
        int gencomma = 0;
2166
 
 
2167
 
        if(!shadow) return;
2168
 
 
2169
 
        if(l) {
2170
 
          if(SwigType_type(Getattr(l, "type")) == T_VOID) {
2171
 
                l = nextSibling(l);
2172
 
          }
2173
 
        }
2174
 
 
2175
 
         
2176
 
         Printf(s_oinit, "{\nzend_function function;\n");
2177
 
         Printf(s_oinit, "zend_internal_function *internal_function = (zend_internal_function *)&function;\n");
2178
 
         Printf(s_oinit, "internal_function->type= ZEND_INTERNAL_FUNCTION;\n");
2179
 
         Printf(s_oinit, "internal_function->handler = %s;\n", Swig_name_wrapper(php_function_name));
2180
 
         Printf(s_oinit, "internal_function->arg_types = NULL;\n");
2181
 
         Printf(s_oinit, "internal_function->function_name = estrdup(\"%(lower)s\");\n", php_function_name);
2182
 
         Printf(s_oinit, "zend_hash_add(&CG(active_class_entry)->function_table, \"%(lower)s\", %d, &function, sizeof(zend_function), NULL);\n}\n", php_function_name, strlen(Char(php_function_name))+1);
2183
 
 
2184
 
        Printf(shadow_code, "function %s(", iname);
2185
 
        if(static_flag && !const_flag)
2186
 
                Printf(shadow_code, "$val = 0");
2187
 
 
2188
 
        if(!no_sync && !static_flag)
2189
 
                Printf(nativecall, "$this->_sync_c();\n\n");
2190
 
 
2191
 
        if((SwigType_type(t) != T_VOID) && !is_shadow(t)) {
2192
 
                if(static_flag && !const_flag)
2193
 
                        Printf(nativecall, "if($val) {\n");
2194
 
                Printf(nativecall, "    return ");
2195
 
                Printv(nativecall, package, "::", php_function_name, "(", 0);
2196
 
                if(!const_flag) {
2197
 
                  if(static_flag)
2198
 
                        Printf(nativecall, "$val");
2199
 
                  else 
2200
 
                        Printv(nativecall, "$this->_cPtr", 0);
2201
 
                }
2202
 
        } else if(SwigType_type(t) == T_VOID) {
2203
 
                if(static_flag && !const_flag)
2204
 
                        Printf(nativecall, "    if($val) {\n");
2205
 
                Printv(nativecall,"    ", package, "::",php_function_name,"(",0);
2206
 
                Printv(nativecall, "$this->_cPtr", 0);
2207
 
        } else if(is_shadow(t)) {
2208
 
                if(SwigType_type(t) == T_ARRAY) {
2209
 
                        Printf(nativecall, "    return %s::%s($this->_cPtr", 
2210
 
                               package, php_function_name);
2211
 
                } else {
2212
 
                String *shadowrettype = NewString("");
2213
 
                SwigToPhpType(t, iname, shadowrettype, shadow);
2214
 
                Printf(nativecall, "    $_sPtr = new %s();\n", shadowrettype);
2215
 
                Printf(nativecall, "    $_sPtr->_destroy();\n");
2216
 
                Printf(nativecall, "    $_iPtr = %s::%s($this->_cPtr",
2217
 
                       package, php_function_name);
2218
 
                }
2219
 
        }
2220
 
 
2221
 
 
2222
 
 
2223
 
        int pcount = ParmList_len(l);
2224
 
 
2225
 
        /* Output each parameter */
2226
 
 
2227
 
        Parm *p = l;
2228
 
 
2229
 
        /* Workaround to overcome Getignore(p) not working - p does not always
2230
 
         * have the Getignore attribute set. Noticeable when cpp_func is called
2231
 
         * from cpp_member_func()
2232
 
        */
2233
 
 
2234
 
        Wrapper *f = NewWrapper();
2235
 
        emit_args(NULL, l, f);
2236
 
        DelWrapper(f);
2237
 
 
2238
 
        /*Workaround end */
2239
 
 
2240
 
        for(int i= 0; i < pcount; i++, p = nextSibling(p)) {
2241
 
          if(Getattr(p, "ignore")) continue;
2242
 
 
2243
 
          if(!(variable_wrapper_flag && i==0))
2244
 
          {
2245
 
            SwigType *pt = Getattr(p, "type");
2246
 
            String   *pn = Getattr(p, "name");
2247
 
 
2248
 
            /* Produce string repesentation of source and target arguments */
2249
 
 
2250
 
            if(pn && *(Char(pn)))
2251
 
                strcpy(arg, Char(pn));
2252
 
            else {
2253
 
                sprintf(arg, "arg%d", i);
2254
 
            }
2255
 
 
2256
 
            Printf(nativecall, ", ");
2257
 
 
2258
 
            if(gencomma) 
2259
 
                    Printf(shadow_code, ",");
2260
 
 
2261
 
            gencomma = 1;
2262
 
 
2263
 
            if(is_shadow(pt)) {
2264
 
                Printv(nativecall, "$", arg, "->getCPtr()", 0);
2265
 
            } else {
2266
 
                Printv(nativecall, "$", arg, 0);
2267
 
            }
2268
 
 
2269
 
            /* Add to php shadow function header */
2270
 
 
2271
 
            Printf(shadow_code, "$%s", arg);
2272
 
 
2273
 
        }
2274
 
      }
2275
 
      
2276
 
      if(SwigType_type(t) == T_ARRAY && is_shadow(get_array_type(t))) {
2277
 
              Printf(nativecall, ");\n");
2278
 
      } else if(is_shadow(t)) {
2279
 
        switch(SwigType_type(t)) {
2280
 
                case T_USER:
2281
 
                        Printf(nativecall, ");\n");
2282
 
                        Printf(nativecall, 
2283
 
                               "    $_sPtr->setCPtr($_iPtr, true);\n");
2284
 
                        Printf(nativecall, "    return $_sPtr;\n");
2285
 
                        break;
2286
 
                case T_REFERENCE:
2287
 
                case T_POINTER:
2288
 
                        Printf(nativecall, ");\n");
2289
 
                        Printf(nativecall, 
2290
 
                               "    $_sPtr->setCPtr($_iPtr, false);\n");
2291
 
                        Printf(nativecall, "    return $_sPtr;\n");
2292
 
                        break;
2293
 
                default:
2294
 
                        Printf(stderr, "Internal Error: unknown shadow_type: %\n", SwigType_str(t,0));
2295
 
                        break;
2296
 
         }
2297
 
        } else {
2298
 
                Printf(nativecall,");\n");
2299
 
                if(static_flag && !const_flag) {
2300
 
                  Printf(nativecall, "    } else {\n");
2301
 
                  Printv(nativecall, "    return ", package, "::",
2302
 
                         php_function_name, "();\n",0);
2303
 
                }
2304
 
        }
2305
 
 
2306
 
        if(static_flag &&!const_flag)
2307
 
                Printf(nativecall, "}\n");
2308
 
 
2309
 
        Printf(shadow_code, ") {\n");
2310
 
        Printf(shadow_code, "    %s", nativecall);
2311
 
        if(!no_sync && !static_flag)
2312
 
                Printf(shadow_code, "    $this->_sync_php();\n");
2313
 
        Printf(shadow_code, "   }\n\n");
2314
 
 
2315
 
      Delete(nativecall);
2316
 
}