vtkWrapTcl.c 29.8 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkWrapTcl.c

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13
14
15
16

=========================================================================*/

#include <stdio.h>
17
#include <string.h>
18
#include "vtkParse.h"
19
#include "vtkConfigure.h"
20
21
22

int numberOfWrappedFunctions = 0;
FunctionInfo *wrappedFunctions[1000];
23
extern FunctionInfo *currentFunction;
24
25


Ken Martin's avatar
Ken Martin committed
26
void output_temp(FILE *fp, int i, int aType, char *Id, int count)
27
28
{
  /* handle VAR FUNCTIONS */
29
  if (aType == 0x5000)
30
31
32
33
34
35
    {
    fprintf(fp,"    vtkTclVoidFuncArg *temp%i = new vtkTclVoidFuncArg;\n",i);
    return;
    }
  
  /* ignore void */
36
  if (((aType % 0x10) == 0x2)&&(!((aType % 0x1000)/0x100)))
37
38
39
40
41
    {
    return;
    }

  /* for const * return types prototype with const */
42
  if ((i == MAX_ARGS) && (aType % 0x2000 >= 0x1000))
43
44
45
46
47
48
49
50
    {
    fprintf(fp,"    const ");
    }
  else
    {
    fprintf(fp,"    ");
    }

51
  if ((aType % 0x100)/0x10 == 0x1)
52
53
54
55
    {
    fprintf(fp,"unsigned ");
    }

56
  switch (aType % 0x10)
57
    {
58
59
60
61
62
63
64
65
66
    case 0x1:   fprintf(fp,"float  "); break;
    case 0x7:   fprintf(fp,"double "); break;
    case 0x4:   fprintf(fp,"int    "); break;
    case 0x5:   fprintf(fp,"short  "); break;
    case 0x6:   fprintf(fp,"long   "); break;
    case 0x2:     fprintf(fp,"void   "); break;
    case 0x3:     fprintf(fp,"char   "); break;
    case 0x9:     fprintf(fp,"%s ",Id); break;
    case 0xA:   fprintf(fp,"vtkIdType "); break;
67
68
    case 0xB:   fprintf(fp,"long long "); break;
    case 0xC:   fprintf(fp,"__int64 "); break;
69
    case 0xD:   fprintf(fp,"signed char "); break;
70
    case 0xE:   fprintf(fp,"bool "); break;
71
    case 0x8: return;
72
73
    }

Ken Martin's avatar
Ken Martin committed
74
  /* handle array arguements */
75
  if (count > 0x1)
Ken Martin's avatar
Ken Martin committed
76
77
78
79
80
    {
    fprintf(fp,"temp%i[%i];\n",i,count);
    return;
    }
  
81
  switch ((aType % 0x1000)/0x100)
82
    {
83
84
85
86
87
88
    case 0x1: fprintf(fp, " *"); break; /* act " &" */
    case 0x2: fprintf(fp, "&&"); break;
    case 0x3: fprintf(fp, " *"); break;
    case 0x4: fprintf(fp, "&*"); break;
    case 0x5: fprintf(fp, "*&"); break;
    case 0x7: fprintf(fp, "**"); break;
89
90
91
92
93
94
95
96
97
98
99
100
    default: fprintf(fp,"  "); break;
    }
  
  fprintf(fp,"temp%i",i);
  fprintf(fp,";\n");
}

/* when the cpp file doesn't have enough info use the hint file */
void use_hints(FILE *fp)
{
  int  i;

101
102
103
104
105
106
107
108
#define INDENT "    "

  fprintf(fp,INDENT "if(temp%i)\n",MAX_ARGS);
  fprintf(fp,INDENT "  {\n");
  fprintf(fp,INDENT "  char tempResult[1024];\n");

  fprintf(fp,INDENT "  sprintf(tempResult,\"");

109
  /* use the hint */
110
  switch (currentFunction->ReturnType % 0x1000)
111
    {
112
    case 0x301: case 0x307:  
113
      for (i = 0; i < currentFunction->HintSize; i++)
Ken Martin's avatar
Ken Martin committed
114
115
116
        {
        fprintf(fp,"%%g ");
        }
117
      break;
118

119
    case 0x304: case 0x305: case 0x30D:
120
121
122
#ifndef VTK_USE_64BIT_IDS
    case 0x30A:
#endif
123
      for (i = 0; i < currentFunction->HintSize; i++)
Ken Martin's avatar
Ken Martin committed
124
125
126
        {
        fprintf(fp,"%%i ");
        }
127
      break;
128

129
130
131
132
133
134
    case 0x30E:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%i ");
        }
      break;
135

136
137
138
139
140
141
    case 0x306:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%li ");
        }
      break;
142

143
144
145
146
147
148
149
150
151
152
153
154
#ifdef VTK_USE_64BIT_IDS
    case 0x30A:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
#  if defined(_MSC_VER)
        fprintf(fp,"%%I64i ");
#  else
        fprintf(fp,"%%lli ");
#  endif
        }
      break;
#endif
155

156
157
158
159
160
161
    case 0x30B:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%lli ");
        }
      break;
162

163
164
165
166
167
168
    case 0x30C:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%I64i ");
        }
      break;
169

170
171
172
173
    case 0x313: case 0x314: case 0x315:
#ifndef VTK_USE_64BIT_IDS
    case 0x31A:
#endif
Ken Martin's avatar
Ken Martin committed
174
      for (i = 0; i < currentFunction->HintSize; i++)
Ken Martin's avatar
Ken Martin committed
175
176
177
        {
        fprintf(fp,"%%u ");
        }
Ken Martin's avatar
Ken Martin committed
178
      break;
179

180
181
182
183
184
185
    case 0x316:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%lu ");
        }
      break;
186

187
188
189
190
191
192
193
194
195
196
197
198
#ifdef VTK_USE_64BIT_IDS
    case 0x31A:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
#  if defined(_MSC_VER)
        fprintf(fp,"%%I64u ");
#  else
        fprintf(fp,"%%llu ");
#  endif
        }
      break;
#endif
199

200
201
202
203
204
205
    case 0x31B:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%llu ");
        }
      break;
206

207
208
209
210
211
212
    case 0x31C:
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%I64u ");
        }
      break;
213
    }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

  fprintf(fp,"\"");
  for (i = 0; i < currentFunction->HintSize; i++)
    {
    fprintf(fp,",temp%i[%i]",MAX_ARGS,i);
    }
  fprintf(fp,");\n");

  fprintf(fp,INDENT "  Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
  fprintf(fp,INDENT "  }\n");
  fprintf(fp,INDENT "else\n");
  fprintf(fp,INDENT "  {\n");
  fprintf(fp,INDENT "  Tcl_SetResult(interp, \"\", TCL_VOLATILE);\n");
  fprintf(fp,INDENT "  }\n");

#undef INDENT
230
231
232
233
}

void return_result(FILE *fp)
{
234
  switch (currentFunction->ReturnType % 0x1000)
235
    {
236
    case 0x2:
237
      fprintf(fp,"    Tcl_ResetResult(interp);\n"); 
238
      break;
239
    case 0x1: case 0x7: 
240
241
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%g\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
242
              MAX_ARGS); 
243
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
244
      break;
245
246
247
248
    case 0x4:  
#ifndef VTK_USE_64BIT_IDS
    case 0xA:
#endif
249
    case 0xD:
250
251
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%i\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
252
              MAX_ARGS); 
253
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
254
      break;
255
256
257
258
259
260
    case 0xE:
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%i\",(int)temp%i);\n",
              MAX_ARGS); 
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
261
    case 0x5:
262
263
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%hi\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
264
              MAX_ARGS); 
265
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
266
      break;
267
    case 0x6:
268
269
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%li\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
270
              MAX_ARGS);
271
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
272
      break;
273
274
275
276
277
278
279
280
281
282
283
284
285
#ifdef VTK_USE_64BIT_IDS
    case 0xA:
      fprintf(fp,"    char tempResult[1024];\n");
#  if defined(_MSC_VER)
      fprintf(fp,"    sprintf(tempResult,\"%%I64i\",temp%i);\n",
              MAX_ARGS); 
#  else
      fprintf(fp,"    sprintf(tempResult,\"%%lli\",temp%i);\n",
              MAX_ARGS); 
#  endif
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
#endif
286
287
288
289
290
291
292
293
294
295
296
297
    case 0xB:
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%lli\",temp%i);\n",
              MAX_ARGS);
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
    case 0xC:
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%I64i\",temp%i);\n",
              MAX_ARGS);
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
298
299
300
301
    case 0x14:
#ifndef VTK_USE_64BIT_IDS
    case 0x1A:
#endif
302
303
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%u\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
304
              MAX_ARGS); 
305
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
306
      break;
307
    case 0x15: 
308
309
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%hu\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
310
              MAX_ARGS);  
311
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
312
      break;
313
    case 0x16:  
314
315
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%lu\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
316
              MAX_ARGS);
317
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
318
      break;
319
    case 0x13:
320
321
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%hu\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
322
              MAX_ARGS); 
323
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
324
      break;
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifdef VTK_USE_64BIT_IDS
    case 0x1A:  
      fprintf(fp,"    char tempResult[1024];\n");
#  if defined(_MSC_VER)
      fprintf(fp,"    sprintf(tempResult,\"%%I64u\",temp%i);\n",
              MAX_ARGS); 
#  else
      fprintf(fp,"    sprintf(tempResult,\"%%llu\",temp%i);\n",
              MAX_ARGS); 
#  endif
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
#endif
338
339
340
341
342
343
344
345
346
347
348
349
    case 0x1B:
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%llu\",temp%i);\n",
              MAX_ARGS);
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
    case 0x1C:
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%I64u\",temp%i);\n",
              MAX_ARGS);
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
350
    case 0x303:
351
      fprintf(fp,"    if (temp%i)\n      {\n      Tcl_SetResult(interp, const_cast<char *>(temp%i), TCL_VOLATILE);\n",MAX_ARGS,MAX_ARGS); 
352
353
      fprintf(fp,"      }\n    else\n      {\n");
      fprintf(fp,"      Tcl_ResetResult(interp);\n      }\n"); 
354
      break;
355
    case 0x3:
356
357
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%c\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
358
              MAX_ARGS); 
359
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
360
      break;
361
362
    case 0x109:
    case 0x309:  
363
      fprintf(fp,"      vtkTclGetObjectFromPointer(interp,(void *)(temp%i),\"%s\");\n",MAX_ARGS,currentFunction->ReturnClass);
364
365
366
367
      break;

    /* handle functions returning vectors */
    /* this is done by looking them up in a hint file */
368
    case 0x301: case 0x307:
369
    case 0x304: case 0x305: case 0x306: case 0x30A: case 0x30B: case 0x30C: case 0x30D: case 0x30E:
370
    case 0x313: case 0x314: case 0x315: case 0x316: case 0x31A: case 0x31B: case 0x31C:
371
372
373
      use_hints(fp);
      break;
    default:
374
      fprintf(fp,"    Tcl_SetResult(interp, (char *)(\"unable to return result.\"), TCL_VOLATILE);\n");
375
376
377
378
379
380
      break;
    }
}

void get_args(FILE *fp, int i)
{
Ken Martin's avatar
Ken Martin committed
381
382
383
384
385
386
387
388
389
390
  int j;
  int start_arg = 2;
  
  /* what arg do we start with */
  for (j = 0; j < i; j++)
    {
    start_arg = start_arg + 
      (currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
    }
  
391
  /* handle VAR FUNCTIONS */
392
  if (currentFunction->ArgTypes[i] == 0x5000)
393
394
395
396
397
398
399
    {
    fprintf(fp,"    temp%i->interp = interp;\n",i);
    fprintf(fp,"    temp%i->command = strcpy(new char [strlen(argv[2])+1],argv[2]);\n",i);
    return;
    }

  /* ignore void */
400
401
  if (((currentFunction->ArgTypes[i] % 0x10) == 0x2)&&
      (!((currentFunction->ArgTypes[i] % 0x1000)/0x100)))
402
403
404
405
    {
    return;
    }
  
406
  switch (currentFunction->ArgTypes[i] % 0x1000)
407
    {
408
    case 0x1: case 0x7:  
409
      fprintf(fp,
Ken Martin's avatar
Ken Martin committed
410
411
              "    if (Tcl_GetDouble(interp,argv[%i],&tempd) != TCL_OK) error = 1;\n",
              start_arg); 
412
413
      fprintf(fp,"    temp%i = tempd;\n",i);
      break;
414
    case 0x4: case 0x5: case 0x6: case 0xA: case 0xB: case 0xC: case 0xD:
415
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
416
              start_arg); 
417
418
      fprintf(fp,"    temp%i = tempi;\n",i);
      break;
419
420
421
    case 0xE:
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
              start_arg); 
422
      fprintf(fp,"    temp%i = tempi ? true : false;\n",i);
423
      break;
424
    case 0x3:
Ken Martin's avatar
Ken Martin committed
425
      fprintf(fp,"    temp%i = *(argv[%i]);\n",i,start_arg);
426
      break;
427
    case 0x13:
428
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
429
              start_arg); 
430
      fprintf(fp,"    temp%i = static_cast<unsigned char>(tempi);\n",i);
431
      break;
432
    case 0x14: case 0x1A: case 0x1B: case 0x1C:
433
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
434
              start_arg); 
435
      fprintf(fp,"    temp%i = static_cast<unsigned int>(tempi);\n",i);
436
      break;
437
    case 0x15:
438
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
439
              start_arg); 
440
      fprintf(fp,"    temp%i = static_cast<unsigned short>(tempi);\n",i);
441
      break;
442
    case 0x16:
Ken Martin's avatar
Ken Martin committed
443
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
Ken Martin's avatar
Ken Martin committed
444
              start_arg); 
445
      fprintf(fp,"    temp%i = static_cast<unsigned long>(tempi);\n",i);
Ken Martin's avatar
Ken Martin committed
446
      break;
447
    case 0x303:
Ken Martin's avatar
Ken Martin committed
448
      fprintf(fp,"    temp%i = argv[%i];\n",i,start_arg);
449
      break;
450
451
    case 0x109:
    case 0x309:
452
      fprintf(fp,"    temp%i = (%s *)(vtkTclGetPointerFromObject(argv[%i],(char *)(\"%s\"),interp,error));\n",i,currentFunction->ArgClasses[i],start_arg,
Ken Martin's avatar
Ken Martin committed
453
              currentFunction->ArgClasses[i]);
454
      break;
455
456
    case 0x2:    
    case 0x9:
457
      break;
Ken Martin's avatar
Ken Martin committed
458
459
    default:
      if (currentFunction->ArgCounts[i] > 1)
Ken Martin's avatar
Ken Martin committed
460
461
462
        {
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
          {
463
          switch (currentFunction->ArgTypes[i] % 0x100)
Ken Martin's avatar
Ken Martin committed
464
            {
465
            case 0x1: case 0x7:  
Ken Martin's avatar
Ken Martin committed
466
467
468
469
470
              fprintf(fp,
                      "    if (Tcl_GetDouble(interp,argv[%i],&tempd) != TCL_OK) error = 1;\n",
                      start_arg); 
              fprintf(fp,"    temp%i[%i] = tempd;\n",i,j);
              break;
471
            case 0x4: case 0x5: case 0x6: case 0xA: case 0xB: case 0xC: case 0xD:
Ken Martin's avatar
Ken Martin committed
472
473
474
475
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
                      start_arg); 
              fprintf(fp,"    temp%i[%i] = tempi;\n",i,j);
              break;
476
477
478
            case 0xE:
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
                      start_arg); 
479
              fprintf(fp,"    temp%i[%i] = tempi ? true : false;\n",i,j);
480
              break;
481
            case 0x3:
Ken Martin's avatar
Ken Martin committed
482
483
              fprintf(fp,"    temp%i[%i] = *(argv[%i]);\n",i,j,start_arg);
              break;
484
            case 0x13:
Ken Martin's avatar
Ken Martin committed
485
486
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
                      start_arg); 
487
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned char>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
488
              break;
489
            case 0x14: case 0x1A:
Ken Martin's avatar
Ken Martin committed
490
491
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
                      start_arg); 
492
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned int>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
493
              break;
494
            case 0x15:
Ken Martin's avatar
Ken Martin committed
495
496
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
                      start_arg); 
497
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned short>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
498
              break;
499
            case 0x16:
Ken Martin's avatar
Ken Martin committed
500
501
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
                      start_arg); 
502
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned long>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
503
504
505
506
507
              break;
            }
          start_arg++;
          }
        }
Ken Martin's avatar
Ken Martin committed
508
      
509
510
511
512
513
514
515
516
517
    }
}

void outputFunction(FILE *fp, FileInfo *data)
{
  int i;
  int args_ok = 1;
 
  /* some functions will not get wrapped no matter what else */
518
  if (currentFunction->IsOperator || 
519
      currentFunction->ArrayFailure ||
Ken Martin's avatar
Ken Martin committed
520
521
      !currentFunction->IsPublic ||
      !currentFunction->Name) 
522
523
524
525
526
527
528
    {
    return;
    }
  
  /* check to see if we can handle the args */
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
529
    if ((currentFunction->ArgTypes[i] % 0x10) == 0x8) args_ok = 0;
Ken Martin's avatar
Ken Martin committed
530
    /* if its a pointer arg make sure we have the ArgCount */
531
532
533
534
    if ((currentFunction->ArgTypes[i] % 0x1000 >= 0x100) &&
        (currentFunction->ArgTypes[i] % 0x1000 != 0x303)&&
        (currentFunction->ArgTypes[i] % 0x1000 != 0x309)&&
        (currentFunction->ArgTypes[i] % 0x1000 != 0x109)) 
Ken Martin's avatar
Ken Martin committed
535
536
      {
      if (currentFunction->NumberOfArguments > 1 ||
Ken Martin's avatar
Ken Martin committed
537
538
539
540
          !currentFunction->ArgCounts[i])
        {
        args_ok = 0;
        }
Ken Martin's avatar
Ken Martin committed
541
      }
542
543
544
545
546
547
    if ((currentFunction->ArgTypes[i] % 0x100 >= 0x10)&&
        (currentFunction->ArgTypes[i] != 0x13)&&
        (currentFunction->ArgTypes[i] != 0x14)&&
        (currentFunction->ArgTypes[i] != 0x15)&&
        (currentFunction->ArgTypes[i] != 0x16)&&
        (currentFunction->ArgTypes[i] != 0x1A)) args_ok = 0;
548
    }
549
550
551
552
  if ((currentFunction->ReturnType % 0x10) == 0x8) args_ok = 0;
  if (((currentFunction->ReturnType % 0x1000)/0x100 != 0x3)&&
      ((currentFunction->ReturnType % 0x1000)/0x100 != 0x1)&&
      ((currentFunction->ReturnType % 0x1000)/0x100)) args_ok = 0;
553
  if (currentFunction->NumberOfArguments && 
554
      (currentFunction->ArgTypes[0] == 0x5000)
555
556
      &&(currentFunction->NumberOfArguments != 1)) args_ok = 0;

Ken Martin's avatar
Ken Martin committed
557
  /* we can't handle void * return types */
558
  if ((currentFunction->ReturnType % 0x1000) == 0x302) 
Ken Martin's avatar
Ken Martin committed
559
560
561
562
    {
    args_ok = 0;
    }
  
563
  /* watch out for functions that dont have enough info */
564
  switch (currentFunction->ReturnType % 0x1000)
565
    {
566
    case 0x301: case 0x307:
567
    case 0x304: case 0x305: case 0x306: case 0x30A: case 0x30B: case 0x30C: case 0x30D: case 0x30E:
568
    case 0x313: case 0x314: case 0x315: case 0x316: case 0x31A: case 0x31B: case 0x31C:
569
570
571
572
573
574
575
576
577
      args_ok = currentFunction->HaveHint;
      break;
    }
  
  /* if the args are OK and it is not a constructor or destructor */
  if (args_ok && 
      strcmp(data->ClassName,currentFunction->Name) &&
      strcmp(data->ClassName,currentFunction->Name + 1))
    {
Ken Martin's avatar
Ken Martin committed
578
579
580
581
582
583
    int required_args = 0;
    
    /* calc the total required args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      required_args = required_args + 
Ken Martin's avatar
Ken Martin committed
584
        (currentFunction->ArgCounts[i] ? currentFunction->ArgCounts[i] : 1);
Ken Martin's avatar
Ken Martin committed
585
586
      }
    
587
588
589
590
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#if !defined(VTK_LEGACY_REMOVE)\n");
      }
591
    fprintf(fp,"  if ((!strcmp(\"%s\",argv[1]))&&(argc == %i))\n    {\n",
Ken Martin's avatar
Ken Martin committed
592
            currentFunction->Name, required_args + 2);
593
594
595
596
597
    
    /* process the args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      output_temp(fp, i, currentFunction->ArgTypes[i],
Ken Martin's avatar
Ken Martin committed
598
599
                  currentFunction->ArgClasses[i], 
                  currentFunction->ArgCounts[i]);
600
601
      }
    output_temp(fp, MAX_ARGS,currentFunction->ReturnType,
Ken Martin's avatar
Ken Martin committed
602
                currentFunction->ReturnClass, 0);
603

604
    /* only use the error variable if we have arguments to parse */
605
    if (currentFunction->NumberOfArguments)
606
      {
607
608
609
610
611
612
613
      fprintf(fp,"    error = 0;\n\n");
      /* now get the required args from the stack */
      for (i = 0; i < currentFunction->NumberOfArguments; i++)
        {
        get_args(fp,i);
        }
      fprintf(fp,"    if (!error)\n    {\n");
614
615
      }
    
616
    switch (currentFunction->ReturnType % 0x1000)
617
      {
618
      case 0x2:
619
        fprintf(fp,"    op->%s(",currentFunction->Name);
Ken Martin's avatar
Ken Martin committed
620
        break;
621
      case 0x109:
622
        fprintf(fp,"    temp%i = &(op)->%s(",MAX_ARGS,currentFunction->Name);
Ken Martin's avatar
Ken Martin committed
623
        break;
624
      default:
625
        fprintf(fp,"    temp%i = (op)->%s(",MAX_ARGS,currentFunction->Name);
626
627
628
629
      }
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      if (i)
Ken Martin's avatar
Ken Martin committed
630
631
632
        {
        fprintf(fp,",");
        }
633
      if (currentFunction->ArgTypes[i] == 0x109)
Ken Martin's avatar
Ken Martin committed
634
635
636
        {
        fprintf(fp,"*(temp%i)",i);
        }
637
      else if (currentFunction->ArgTypes[i] == 0x5000)
Ken Martin's avatar
Ken Martin committed
638
        {
639
        fprintf(fp,"vtkTclVoidFunc,static_cast<void *>(temp%i)",i);
Ken Martin's avatar
Ken Martin committed
640
        }
641
      else
Ken Martin's avatar
Ken Martin committed
642
643
644
        {
        fprintf(fp,"temp%i",i);
        }
645
646
647
      }
    fprintf(fp,");\n");
    if (currentFunction->NumberOfArguments && 
648
        (currentFunction->ArgTypes[0] == 0x5000))
649
      {
650
      fprintf(fp,"    op->%sArgDelete(vtkTclVoidFuncArgDelete);\n",
Ken Martin's avatar
Ken Martin committed
651
              currentFunction->Name);
652
653
      }
    return_result(fp);
654
655
    fprintf(fp,"    return TCL_OK;\n");
    
656
    /* close the if error */
657
658
659
660
661
    if (currentFunction->NumberOfArguments)
      {
      fprintf(fp,"    }\n");
      }
    
662
    fprintf(fp,"    }\n");
663
664
665
666
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#endif\n");
      }
Ken Martin's avatar
Ken Martin committed
667
    
668
669
670
671
672
673
674
675
    wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
    numberOfWrappedFunctions++;
    }
}

/* print the parsed structures */
void vtkParseOutput(FILE *fp, FileInfo *data)
{
676
  int i,j;
677
678
  
  fprintf(fp,"// tcl wrapper for %s object\n//\n",data->ClassName);
679
  fprintf(fp,"#define VTK_WRAPPING_CXX\n");
680
681
  if (strcmp("vtkObjectBase",data->ClassName) != 0)
    {
682
      /* Block inclusion of full streams. */
683
684
    fprintf(fp,"#define VTK_STREAMS_FWD_ONLY\n");
    }
Will Schroeder's avatar
Will Schroeder committed
685
  fprintf(fp,"#include \"vtkSystemIncludes.h\"\n");
686
687
  fprintf(fp,"#include \"%s.h\"\n\n",data->ClassName);
  fprintf(fp,"#include \"vtkTclUtil.h\"\n");
688
  fprintf(fp,"#include <vtkstd/stdexcept>\n");
689
  fprintf(fp,"#include <vtksys/ios/sstream>\n");
690
691
  if (data->IsConcrete)
    {
692
693
    if (strcmp(data->ClassName, "vtkRenderWindowInteractor") == 0)
      {
694
      fprintf(fp,"#include \"vtkToolkits.h\"\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
695
      fprintf(fp,"#if defined( VTK_USE_X ) && defined( VTK_USE_TK )\n");
696
      fprintf(fp,"# include \"vtkXRenderWindowTclInteractor.h\"\n");
Yves Starreveld's avatar
   
Yves Starreveld committed
697
      fprintf(fp,"#endif\n");
698
699
700

      fprintf(fp,"\nClientData %sNewCommand()\n{\n",data->ClassName);

Andy Cedilnik's avatar
Andy Cedilnik committed
701
      fprintf(fp,"#if defined( VTK_USE_X ) && defined( VTK_USE_TK )\n");
702
      fprintf(fp,"  %s *temp = vtkXRenderWindowTclInteractor::New();\n",
Ken Martin's avatar
Ken Martin committed
703
              data->ClassName);
704
705
706
      fprintf(fp,"#else\n");
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
      fprintf(fp,"#endif\n");
707
      fprintf(fp,"  return static_cast<ClientData>(temp);\n}\n\n");
708
709
710
711
712
      }
    else
      {
      fprintf(fp,"\nClientData %sNewCommand()\n{\n",data->ClassName);
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
713
      fprintf(fp,"  return static_cast<ClientData>(temp);\n}\n\n");
714
      }
715
716
717
718
719
720
721
722
    }
  
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    fprintf(fp,"int %sCppCommand(%s *op, Tcl_Interp *interp,\n             int argc, char *argv[]);\n",data->SuperClasses[i],data->SuperClasses[i]);
    }
  fprintf(fp,"int VTKTCL_EXPORT %sCppCommand(%s *op, Tcl_Interp *interp,\n             int argc, char *argv[]);\n",data->ClassName,data->ClassName);
  fprintf(fp,"\nint VTKTCL_EXPORT %sCommand(ClientData cd, Tcl_Interp *interp,\n             int argc, char *argv[])\n{\n",data->ClassName);
723
  fprintf(fp,"  if ((argc == 2)&&(!strcmp(\"Delete\",argv[1]))&& !vtkTclInDelete(interp))\n    {\n");
724
725
  fprintf(fp,"    Tcl_DeleteCommand(interp,argv[0]);\n");
  fprintf(fp,"    return TCL_OK;\n    }\n");
726
  fprintf(fp,"   return %sCppCommand(static_cast<%s *>(static_cast<vtkTclCommandArgStruct *>(cd)->Pointer),interp, argc, argv);\n}\n",data->ClassName,data->ClassName);
727
728
729
730
731
732
  
  fprintf(fp,"\nint VTKTCL_EXPORT %sCppCommand(%s *op, Tcl_Interp *interp,\n             int argc, char *argv[])\n{\n",data->ClassName,data->ClassName);
  fprintf(fp,"  int    tempi;\n");
  fprintf(fp,"  double tempd;\n");
  fprintf(fp,"  static char temps[80];\n");
  fprintf(fp,"  int    error;\n\n");
733
734
735
736
  fprintf(fp,"  error = 0; error = error;\n");
  fprintf(fp,"  tempi = 0; tempi = tempi;\n");
  fprintf(fp,"  tempd = 0; tempd = tempd;\n");
  fprintf(fp,"  temps[0] = 0; temps[0] = temps[0];\n\n");
737

738
  fprintf(fp,"  if (argc < 2)\n    {\n    Tcl_SetResult(interp,(char *)(\"Could not find requested method.\"), TCL_VOLATILE);\n    return TCL_ERROR;\n    }\n");
739
740
741
742
743

  /* stick in the typecasting and delete functionality here */
  fprintf(fp,"  if (!interp)\n    {\n");
  fprintf(fp,"    if (!strcmp(\"DoTypecasting\",argv[0]))\n      {\n");
  fprintf(fp,"      if (!strcmp(\"%s\",argv[1]))\n        {\n",
Ken Martin's avatar
Ken Martin committed
744
          data->ClassName);
745
  fprintf(fp,"        argv[2] = static_cast<char *>(static_cast<void *>(op));\n");
746
747
748
749
750
  fprintf(fp,"        return TCL_OK;\n        }\n");

  /* check our superclasses */
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
751
    fprintf(fp,"      if (%sCppCommand(static_cast<%s *>(op),interp,argc,argv) == TCL_OK)\n        {\n",
Ken Martin's avatar
Ken Martin committed
752
            data->SuperClasses[i],data->SuperClasses[i]);
753
754
755
756
    fprintf(fp,"        return TCL_OK;\n        }\n");      
    }
  fprintf(fp,"      }\n    return TCL_ERROR;\n    }\n\n");
  
Ken Martin's avatar
Ken Martin committed
757
758
759
760
761
  /* add the GetSuperClassName */
  if (data->NumberOfSuperClasses)
    {
    fprintf(fp,"  if (!strcmp(\"GetSuperClassName\",argv[1]))\n");
    fprintf(fp,"    {\n");
762
    fprintf(fp,"    Tcl_SetResult(interp,(char *)(\"%s\"), TCL_VOLATILE);\n",data->SuperClasses[0]);
Ken Martin's avatar
Ken Martin committed
763
764
765
    fprintf(fp,"    return TCL_OK;\n");
    fprintf(fp,"    }\n\n");      
    }
766
  
767
768
  fprintf(fp,"  try\n    {\n");

769
770
771
772
773
774
775
776
777
  /* insert function handling code here */
  for (i = 0; i < data->NumberOfFunctions; i++)
    {
    currentFunction = data->Functions + i;
    outputFunction(fp, data);
    }
  
  /* add the ListInstances method */
  fprintf(fp,"\n  if (!strcmp(\"ListInstances\",argv[1]))\n    {\n");
778
  fprintf(fp,"    vtkTclListInstances(interp,(ClientData)(%sCommand));\n",data->ClassName);
779
780
781
782
783
784
785
786
  fprintf(fp,"    return TCL_OK;\n    }\n");
  
  /* add the ListMethods method */
  fprintf(fp,"\n  if (!strcmp(\"ListMethods\",argv[1]))\n    {\n");
  /* recurse up the tree */
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    fprintf(fp,"    %sCppCommand(op,interp,argc,argv);\n",
Ken Martin's avatar
Ken Martin committed
787
            data->SuperClasses[i]);
788
789
790
    }
  /* now list our methods */
  fprintf(fp,"    Tcl_AppendResult(interp,\"Methods from %s:\\n\",NULL);\n",data->ClassName);
Ken Martin's avatar
Ken Martin committed
791
  fprintf(fp,"    Tcl_AppendResult(interp,\"  GetSuperClassName\\n\",NULL);\n");
792
793
  for (i = 0; i < numberOfWrappedFunctions; i++)
    {
794
795
    int numArgs = 0;

796
    currentFunction = wrappedFunctions[i];
797
798
799
800
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#if !defined(VTK_LEGACY_REMOVE)\n");
      }
Ken Martin's avatar
Ken Martin committed
801
            
802
803
804
805
    /* calc the total required args */
    for (j = 0; j < currentFunction->NumberOfArguments; j++)
      {
      numArgs = numArgs + 
Ken Martin's avatar
Ken Martin committed
806
        (currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
807
808
809
      }

    if (numArgs > 1)
810
811
      {
      fprintf(fp,"    Tcl_AppendResult(interp,\"  %s\\t with %i args\\n\",NULL);\n",
Ken Martin's avatar
Ken Martin committed
812
              currentFunction->Name, numArgs);
813
      }
814
    if (numArgs == 1)
815
      {
Ken Martin's avatar
Ken Martin committed
816
817
          fprintf(fp,"    Tcl_AppendResult(interp,\"  %s\\t with 1 arg\\n\",NULL);\n",
                  currentFunction->Name);
818
      }
819
    if (numArgs == 0)
820
821
      {
      fprintf(fp,"    Tcl_AppendResult(interp,\"  %s\\n\",NULL);\n",
Ken Martin's avatar
Ken Martin committed
822
              currentFunction->Name);
823
      }
824
825
826
827
828

    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#endif\n");
      }
829
830
831
832
833
834
    }
  fprintf(fp,"    return TCL_OK;\n    }\n");
  
  /* try superclasses */
  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
835
    fprintf(fp,"\n  if (%sCppCommand(static_cast<%s *>(op),interp,argc,argv) == TCL_OK)\n",
Ken Martin's avatar
Ken Martin committed
836
            data->SuperClasses[i], data->SuperClasses[i]);
837
838
839
    fprintf(fp,"    {\n    return TCL_OK;\n    }\n");
    }
  
840
  /* Add the Print method to vtkObjectBase. */
841
  if (!strcmp("vtkObjectBase",data->ClassName))
842
843
    {
    fprintf(fp,"  if ((!strcmp(\"Print\",argv[1]))&&(argc == 2))\n    {\n");
844
    fprintf(fp,"    vtksys_ios::ostringstream buf_with_warning_C4701;\n");
845
846
    fprintf(fp,"    op->Print(buf_with_warning_C4701);\n");
    fprintf(fp,"    buf_with_warning_C4701.put('\\0');\n");
847
    fprintf(fp,"    Tcl_SetResult(interp,const_cast<char *>(buf_with_warning_C4701.str().c_str()),\n");
848
    fprintf(fp,"      TCL_VOLATILE);\n");
849
    fprintf(fp,"    return TCL_OK;\n    }\n");
850
851
    /* Add the PrintRevisions method to vtkObjectBase. */
    fprintf(fp,"  if ((!strcmp(\"PrintRevisions\",argv[1]))&&(argc == 2))\n    {\n");
852
    fprintf(fp,"    vtksys_ios::ostringstream buf_with_warning_C4701;\n");
853
854
    fprintf(fp,"    op->PrintRevisions(buf_with_warning_C4701);\n");
    fprintf(fp,"    buf_with_warning_C4701.put('\\0');\n");
855
    fprintf(fp,"    Tcl_SetResult(interp,const_cast<char *>(buf_with_warning_C4701.str().c_str()),\n");
856
    fprintf(fp,"      TCL_VOLATILE);\n");
857
    fprintf(fp,"    return TCL_OK;\n    }\n");    
858
    }
859
  /* Add the AddObserver method to vtkObject. */
860
861
  if (!strcmp("vtkObject",data->ClassName))
    {
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
    fprintf(fp,"  if ((!strcmp(\"AddObserver\",argv[1]))&&(argc >= 4))\n    {\n");
    fprintf(fp,"    error = 0;\n");
    fprintf(fp,"    if (argc > 4 && Tcl_GetDouble(interp,argv[4],&tempd) != TCL_OK) error = 1;\n");
    fprintf(fp,"    if (!error)\n      {\n");
    fprintf(fp,"      vtkTclCommand *cbc = vtkTclCommand::New();\n");
    fprintf(fp,"      cbc->SetInterp(interp);\n");
    fprintf(fp,"      cbc->SetStringCommand(argv[3]);\n");
    fprintf(fp,"      unsigned long      temp20;\n");
    fprintf(fp,"      if (argc > 4)\n        {\n");
    fprintf(fp,"        temp20 = op->AddObserver(argv[2],cbc,tempd);\n");
    fprintf(fp,"        }\n      else\n        {\n");
    fprintf(fp,"        temp20 = op->AddObserver(argv[2],cbc);\n");
    fprintf(fp,"        }\n");
    fprintf(fp,"      cbc->Delete();\n");
    fprintf(fp,"      char tempResult[1024];\n");
    fprintf(fp,"      sprintf(tempResult,\"%%li\",temp20);\n");
    fprintf(fp,"      Tcl_SetResult(interp,tempResult,TCL_VOLATILE);\n");
    fprintf(fp,"      return TCL_OK;\n      }\n");
    fprintf(fp,"    }\n");
881
882
883
    }
  fprintf(fp,"\n  if ((argc >= 2)&&(!strstr(interp->result,\"Object named:\")))\n    {\n");
  fprintf(fp,"    char temps2[256];\n    sprintf(temps2,\"Object named: %%s, could not find requested method: %%s\\nor the method was called with incorrect arguments.\\n\",argv[0],argv[1]);\n    Tcl_AppendResult(interp,temps2,NULL);\n    }\n");
884
885
886
887
888
889
  fprintf(fp,"    }\n");
  fprintf(fp,"  catch (vtkstd::exception &e)\n");
  fprintf(fp,"    {\n");
  fprintf(fp,"    Tcl_AppendResult(interp, \"Uncaught exception: \",  e.what(), \"\\n\", NULL);\n");
  fprintf(fp,"    return TCL_ERROR;\n");
  fprintf(fp,"    }\n");
890
891
892
  fprintf(fp,"  return TCL_ERROR;\n}\n");

}