vtkWrapTcl.c 43.4 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 <ctype.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include "vtkParse.h"
21
#include "vtkParseType.h"
22
#include "vtkConfigure.h"
23
24
25

int numberOfWrappedFunctions = 0;
FunctionInfo *wrappedFunctions[1000];
26
extern FunctionInfo *currentFunction;
27

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* convert special characters in a string into their escape codes,
   so that the string can be quoted in a source file (the specified
   maxlen must be at least 32 chars)*/
static const char *quote_string(const char *comment, int maxlen)
{
  static char *result = 0;
  static int oldmaxlen = 0;
  int i, j, n;

  if (maxlen > oldmaxlen)
    {
    if (result)
      {
      free(result);
      }
Francois Bertel's avatar
Francois Bertel committed
43
    result = (char *)malloc((size_t)(maxlen+1));
44
45
46
47
48
49
50
51
52
53
54
    oldmaxlen = maxlen;
    }

  if (comment == NULL)
    {
    return "";
    }

  j = 0;

  n = (int)strlen(comment);
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  for (i = 0; i < n; i++)
    {
    if (comment[i] == '\"')
      {
      strcpy(&result[j],"\\\"");
      j += 2;
      }
    else if (comment[i] == '\\')
      {
      strcpy(&result[j],"\\\\");
      j += 2;
      }
    else if (comment[i] == ']')
      {
      strcpy(&result[j],"\\\\]");
      j += 3;
      }
    else if (comment[i] == '[')
      {
      strcpy(&result[j],"\\\\[");
      j += 3;
      }
    else if (comment[i] == '\n')
      {
      strcpy(&result[j],"\\n");
      j += 2;
82
      }
83
84
85
86
87
88
89
90
91
92
    else if (isprint(comment[i]))
      {
      result[j] = comment[i];
      j++;
      }
    else
      {
      sprintf(&result[j],"\\%3.3o",comment[i]);
      j += 4;
      }
93

94
    if (j >= maxlen - 21)
95
      {
96
97
98
99
100
      sprintf(&result[j]," ...\\n [Truncated]\\n");
      j += (int)strlen(" ...\\n [Truncated]\\n");
      break;
      }
    }
101

102
103
104
105
  result[j] = '\0';

  return result;
}
106

Ken Martin's avatar
Ken Martin committed
107
void output_temp(FILE *fp, int i, int aType, char *Id, int count)
108
109
{
  /* handle VAR FUNCTIONS */
110
  if (aType == VTK_PARSE_FUNCTION)
111
112
113
114
    {
    fprintf(fp,"    vtkTclVoidFuncArg *temp%i = new vtkTclVoidFuncArg;\n",i);
    return;
    }
115

116
  /* ignore void */
117
  if ((aType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VOID)
118
119
120
121
122
    {
    return;
    }

  /* for const * return types prototype with const */
123
  if ((i == MAX_ARGS) && ((aType & VTK_PARSE_CONST) != 0))
124
125
126
127
128
129
130
131
    {
    fprintf(fp,"    const ");
    }
  else
    {
    fprintf(fp,"    ");
    }

132
  if ((aType & VTK_PARSE_UNSIGNED) != 0)
133
134
135
136
    {
    fprintf(fp,"unsigned ");
    }

137
  switch ((aType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
138
    {
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    case VTK_PARSE_FLOAT:       fprintf(fp,"float  "); break;
    case VTK_PARSE_DOUBLE:      fprintf(fp,"double "); break;
    case VTK_PARSE_INT:         fprintf(fp,"int    "); break;
    case VTK_PARSE_SHORT:       fprintf(fp,"short  "); break;
    case VTK_PARSE_LONG:        fprintf(fp,"long   "); break;
    case VTK_PARSE_VOID:        fprintf(fp,"void   "); break;
    case VTK_PARSE_CHAR:        fprintf(fp,"char   "); break;
    case VTK_PARSE_VTK_OBJECT:  fprintf(fp,"%s ",Id); break;
    case VTK_PARSE_ID_TYPE:     fprintf(fp,"vtkIdType "); break;
    case VTK_PARSE_LONG_LONG:   fprintf(fp,"long long "); break;
    case VTK_PARSE___INT64:     fprintf(fp,"__int64 "); break;
    case VTK_PARSE_SIGNED_CHAR: fprintf(fp,"signed char "); break;
    case VTK_PARSE_BOOL:        fprintf(fp,"bool "); break;
    case VTK_PARSE_UNKNOWN:     return;
153
154
    }

Ken Martin's avatar
Ken Martin committed
155
  /* handle array arguements */
156
  if (count > 1)
Ken Martin's avatar
Ken Martin committed
157
158
159
160
    {
    fprintf(fp,"temp%i[%i];\n",i,count);
    return;
    }
161
162

  switch (aType & VTK_PARSE_INDIRECT)
163
    {
164
165
166
167
168
    case VTK_PARSE_REF:             fprintf(fp, " *"); break; /* act " &" */
    case VTK_PARSE_POINTER:         fprintf(fp, " *"); break;
    case VTK_PARSE_POINTER_REF:     fprintf(fp, "*&"); break;
    case VTK_PARSE_POINTER_POINTER: fprintf(fp, "**"); break;
    default:                        fprintf(fp,"  "); break;
169
    }
170

171
172
173
174
175
176
177
178
179
  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;

180
181
182
183
184
#define INDENT "    "

  fprintf(fp,INDENT "if(temp%i)\n",MAX_ARGS);
  fprintf(fp,INDENT "  {\n");
  fprintf(fp,INDENT "  char tempResult[1024];\n");
185
  fprintf(fp,INDENT "  *tempResult = '\\0';\n");
186

187
  /* special case for double: use Tcl_PrintDouble to control precision */
188
189
190
191
  if (((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
       != VTK_PARSE_FLOAT_PTR) &&
      ((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
       != VTK_PARSE_DOUBLE_PTR))
192
193
194
    {
    fprintf(fp,INDENT "  sprintf(tempResult,\"");
    }
195

196
  /* use the hint */
197
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
198
    {
199
200
    case VTK_PARSE_FLOAT_PTR:
    case VTK_PARSE_DOUBLE_PTR:
201
202
      fprintf(fp,INDENT "  char converted[1024];\n");
      fprintf(fp,INDENT "  *converted = '\\0';\n");
203
      for (i = 0; i < currentFunction->HintSize; i++)
Ken Martin's avatar
Ken Martin committed
204
        {
205
206
207
        fprintf(fp,INDENT "  Tcl_PrintDouble(interp,temp%i[%i], converted);\n",MAX_ARGS,i);
        fprintf(fp,INDENT "  strcat(tempResult, \" \");\n");
        fprintf(fp,INDENT "  strcat(tempResult, converted);\n");
Ken Martin's avatar
Ken Martin committed
208
        }
209
      break;
210

211
212
213
    case VTK_PARSE_INT_PTR:
    case VTK_PARSE_SHORT_PTR:
    case VTK_PARSE_SIGNED_CHAR_PTR:
214
#ifndef VTK_USE_64BIT_IDS
215
    case VTK_PARSE_ID_TYPE_PTR:
216
#endif
217
      for (i = 0; i < currentFunction->HintSize; i++)
Ken Martin's avatar
Ken Martin committed
218
219
220
        {
        fprintf(fp,"%%i ");
        }
221
      break;
222

223
    case VTK_PARSE_BOOL_PTR:
224
225
226
227
228
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%i ");
        }
      break;
229

230
    case VTK_PARSE_LONG_PTR:
231
232
233
234
235
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%li ");
        }
      break;
236

237
#ifdef VTK_USE_64BIT_IDS
238
    case VTK_PARSE_ID_TYPE_PTR:
239
240
241
242
243
244
245
246
247
248
      for (i = 0; i < currentFunction->HintSize; i++)
        {
#  if defined(_MSC_VER)
        fprintf(fp,"%%I64i ");
#  else
        fprintf(fp,"%%lli ");
#  endif
        }
      break;
#endif
249

250
    case VTK_PARSE_LONG_LONG_PTR:
251
252
253
254
255
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%lli ");
        }
      break;
256

257
    case VTK_PARSE___INT64_PTR:
258
259
260
261
262
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%I64i ");
        }
      break;
263

264
265
266
    case VTK_PARSE_UNSIGNED_CHAR_PTR:
    case VTK_PARSE_UNSIGNED_INT_PTR:
    case VTK_PARSE_UNSIGNED_SHORT_PTR:
267
#ifndef VTK_USE_64BIT_IDS
268
    case VTK_PARSE_UNSIGNED_ID_TYPE_PTR:
269
#endif
Ken Martin's avatar
Ken Martin committed
270
      for (i = 0; i < currentFunction->HintSize; i++)
Ken Martin's avatar
Ken Martin committed
271
272
273
        {
        fprintf(fp,"%%u ");
        }
Ken Martin's avatar
Ken Martin committed
274
      break;
275

276
    case VTK_PARSE_UNSIGNED_LONG_PTR:
277
278
279
280
281
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%lu ");
        }
      break;
282

283
#ifdef VTK_USE_64BIT_IDS
284
    case VTK_PARSE_UNSIGNED_ID_TYPE_PTR:
285
286
287
288
289
290
291
292
293
294
      for (i = 0; i < currentFunction->HintSize; i++)
        {
#  if defined(_MSC_VER)
        fprintf(fp,"%%I64u ");
#  else
        fprintf(fp,"%%llu ");
#  endif
        }
      break;
#endif
295

296
    case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
297
298
299
300
301
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%llu ");
        }
      break;
302

303
    case VTK_PARSE_UNSIGNED___INT64_PTR:
304
305
306
307
308
      for (i = 0; i < currentFunction->HintSize; i++)
        {
        fprintf(fp,"%%I64u ");
        }
      break;
309
    }
310

311
312
313
314
  if (((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
       != VTK_PARSE_FLOAT_PTR) &&
      ((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
       != VTK_PARSE_DOUBLE_PTR))
315
    {
316
317
318
319
320
321
    fprintf(fp,"\"");
    for (i = 0; i < currentFunction->HintSize; i++)
      {
      fprintf(fp,",temp%i[%i]",MAX_ARGS,i);
      }
    fprintf(fp,");\n");
322
323
324
325
326
327
    }

  fprintf(fp,INDENT "  Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
  fprintf(fp,INDENT "  }\n");
  fprintf(fp,INDENT "else\n");
  fprintf(fp,INDENT "  {\n");
328
  fprintf(fp,INDENT "  Tcl_SetResult(interp, const_cast<char *>(\"\"), TCL_VOLATILE);\n");
329
330
331
  fprintf(fp,INDENT "  }\n");

#undef INDENT
332
333
334
335
}

void return_result(FILE *fp)
{
336
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
337
    {
338
339
    case VTK_PARSE_VOID:
      fprintf(fp,"    Tcl_ResetResult(interp);\n");
340
      break;
341
342
    case VTK_PARSE_FLOAT:
    case VTK_PARSE_DOUBLE:
343
      fprintf(fp,"    char tempResult[1024];\n");
344
345
346
347
348
       /*
        * use tcl's print double function to support variable
        * precision at runtime
        */
      fprintf(fp,"    Tcl_PrintDouble(interp,temp%i,tempResult);\n",
349
              MAX_ARGS);
350
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
351
      break;
352
    case VTK_PARSE_INT:
353
#ifndef VTK_USE_64BIT_IDS
354
    case VTK_PARSE_ID_TYPE:
355
#endif
356
    case VTK_PARSE_SIGNED_CHAR:
357
358
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%i\",temp%i);\n",
359
              MAX_ARGS);
360
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
361
      break;
362
    case VTK_PARSE_BOOL:
363
364
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%i\",(int)temp%i);\n",
365
              MAX_ARGS);
366
367
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
368
    case VTK_PARSE_SHORT:
369
370
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%hi\",temp%i);\n",
371
              MAX_ARGS);
372
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
373
      break;
374
    case VTK_PARSE_LONG:
375
376
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%li\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
377
              MAX_ARGS);
378
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
379
      break;
380
#ifdef VTK_USE_64BIT_IDS
381
    case VTK_PARSE_ID_TYPE:
382
383
384
      fprintf(fp,"    char tempResult[1024];\n");
#  if defined(_MSC_VER)
      fprintf(fp,"    sprintf(tempResult,\"%%I64i\",temp%i);\n",
385
              MAX_ARGS);
386
387
#  else
      fprintf(fp,"    sprintf(tempResult,\"%%lli\",temp%i);\n",
388
              MAX_ARGS);
389
390
391
392
#  endif
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
#endif
393
    case VTK_PARSE_LONG_LONG:
394
395
396
397
398
      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;
399
    case VTK_PARSE___INT64:
400
401
402
403
404
      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;
405
    case VTK_PARSE_UNSIGNED_INT:
406
#ifndef VTK_USE_64BIT_IDS
407
    case VTK_PARSE_UNSIGNED_ID_TYPE:
408
#endif
409
410
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%u\",temp%i);\n",
411
              MAX_ARGS);
412
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
413
      break;
414
    case VTK_PARSE_UNSIGNED_SHORT:
415
416
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%hu\",temp%i);\n",
417
              MAX_ARGS);
418
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
419
      break;
420
    case VTK_PARSE_UNSIGNED_LONG:
421
422
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%lu\",temp%i);\n",
Ken Martin's avatar
Ken Martin committed
423
              MAX_ARGS);
424
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
425
      break;
426
    case VTK_PARSE_UNSIGNED_CHAR:
427
428
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%hu\",temp%i);\n",
429
              MAX_ARGS);
430
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
431
      break;
432
#ifdef VTK_USE_64BIT_IDS
433
    case VTK_PARSE_UNSIGNED_ID_TYPE:
434
435
436
      fprintf(fp,"    char tempResult[1024];\n");
#  if defined(_MSC_VER)
      fprintf(fp,"    sprintf(tempResult,\"%%I64u\",temp%i);\n",
437
              MAX_ARGS);
438
439
#  else
      fprintf(fp,"    sprintf(tempResult,\"%%llu\",temp%i);\n",
440
              MAX_ARGS);
441
442
443
444
#  endif
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
      break;
#endif
445
    case VTK_PARSE_UNSIGNED_LONG_LONG:
446
447
448
449
450
      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;
451
    case VTK_PARSE_UNSIGNED___INT64:
452
453
454
455
456
      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;
457
458
    case VTK_PARSE_CHAR_PTR:
      fprintf(fp,"    if (temp%i)\n      {\n      Tcl_SetResult(interp, const_cast<char *>(temp%i), TCL_VOLATILE);\n",MAX_ARGS,MAX_ARGS);
459
      fprintf(fp,"      }\n    else\n      {\n");
460
      fprintf(fp,"      Tcl_ResetResult(interp);\n      }\n");
461
      break;
462
    case VTK_PARSE_CHAR:
463
464
      fprintf(fp,"    char tempResult[1024];\n");
      fprintf(fp,"    sprintf(tempResult,\"%%c\",temp%i);\n",
465
              MAX_ARGS);
466
      fprintf(fp,"    Tcl_SetResult(interp, tempResult, TCL_VOLATILE);\n");
467
      break;
468
469
    case VTK_PARSE_VTK_OBJECT_REF:
    case VTK_PARSE_VTK_OBJECT_PTR:
470
      fprintf(fp,"      vtkTclGetObjectFromPointer(interp,(void *)(temp%i),\"%s\");\n",MAX_ARGS,currentFunction->ReturnClass);
471
472
473
474
      break;

    /* handle functions returning vectors */
    /* this is done by looking them up in a hint file */
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
    case VTK_PARSE_FLOAT_PTR:
    case VTK_PARSE_DOUBLE_PTR:
    case VTK_PARSE_INT_PTR:
    case VTK_PARSE_SHORT_PTR:
    case VTK_PARSE_LONG_PTR:
    case VTK_PARSE_ID_TYPE_PTR:
    case VTK_PARSE_LONG_LONG_PTR:
    case VTK_PARSE___INT64_PTR:
    case VTK_PARSE_SIGNED_CHAR_PTR:
    case VTK_PARSE_BOOL_PTR:
    case VTK_PARSE_UNSIGNED_CHAR_PTR:
    case VTK_PARSE_UNSIGNED_INT_PTR:
    case VTK_PARSE_UNSIGNED_SHORT_PTR:
    case VTK_PARSE_UNSIGNED_LONG_PTR:
    case VTK_PARSE_UNSIGNED_ID_TYPE_PTR:
    case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
    case VTK_PARSE_UNSIGNED___INT64_PTR:
492
493
494
      use_hints(fp);
      break;
    default:
495
      fprintf(fp,"    Tcl_SetResult(interp, const_cast<char *>(\"unable to return result.\"), TCL_VOLATILE);\n");
496
497
498
499
500
501
      break;
    }
}

void get_args(FILE *fp, int i)
{
Ken Martin's avatar
Ken Martin committed
502
503
  int j;
  int start_arg = 2;
504

Ken Martin's avatar
Ken Martin committed
505
506
507
  /* what arg do we start with */
  for (j = 0; j < i; j++)
    {
508
    start_arg = start_arg +
Ken Martin's avatar
Ken Martin committed
509
510
      (currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
    }
511

512
  /* handle VAR FUNCTIONS */
513
  if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
514
515
516
517
518
519
520
    {
    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 */
521
522
  if ((currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE)
      == VTK_PARSE_VOID)
523
524
525
    {
    return;
    }
526
527

  switch (currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE)
528
    {
529
530
    case VTK_PARSE_FLOAT:
    case VTK_PARSE_DOUBLE:
531
      fprintf(fp,
Ken Martin's avatar
Ken Martin committed
532
              "    if (Tcl_GetDouble(interp,argv[%i],&tempd) != TCL_OK) error = 1;\n",
533
              start_arg);
534
535
      fprintf(fp,"    temp%i = tempd;\n",i);
      break;
536
537
538
539
540
541
542
    case VTK_PARSE_INT:
    case VTK_PARSE_SHORT:
    case VTK_PARSE_LONG:
    case VTK_PARSE_ID_TYPE:
    case VTK_PARSE_LONG_LONG:
    case VTK_PARSE___INT64:
    case VTK_PARSE_SIGNED_CHAR:
543
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
544
              start_arg);
545
546
      fprintf(fp,"    temp%i = tempi;\n",i);
      break;
547
    case VTK_PARSE_BOOL:
548
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
549
              start_arg);
550
      fprintf(fp,"    temp%i = tempi ? true : false;\n",i);
551
      break;
552
    case VTK_PARSE_CHAR:
Ken Martin's avatar
Ken Martin committed
553
      fprintf(fp,"    temp%i = *(argv[%i]);\n",i,start_arg);
554
      break;
555
    case VTK_PARSE_UNSIGNED_CHAR:
556
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
557
              start_arg);
558
      fprintf(fp,"    temp%i = static_cast<unsigned char>(tempi);\n",i);
559
      break;
560
    case VTK_PARSE_UNSIGNED_INT: case VTK_PARSE_UNSIGNED_ID_TYPE:
561
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
562
              start_arg);
563
      fprintf(fp,"    temp%i = static_cast<unsigned int>(tempi);\n",i);
564
      break;
565
    case VTK_PARSE_UNSIGNED_SHORT:
566
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
567
              start_arg);
568
      fprintf(fp,"    temp%i = static_cast<unsigned short>(tempi);\n",i);
569
      break;
570
    case VTK_PARSE_UNSIGNED_LONG:
Ken Martin's avatar
Ken Martin committed
571
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
572
              start_arg);
573
      fprintf(fp,"    temp%i = static_cast<unsigned long>(tempi);\n",i);
Ken Martin's avatar
Ken Martin committed
574
      break;
575
    case VTK_PARSE_UNSIGNED_LONG_LONG: case VTK_PARSE_UNSIGNED___INT64:
576
      fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
577
              start_arg);
578
579
      fprintf(fp,"    temp%i = static_cast<unsigned long long>(tempi);\n",i);
      break;
580
    case VTK_PARSE_CHAR_PTR:
Ken Martin's avatar
Ken Martin committed
581
      fprintf(fp,"    temp%i = argv[%i];\n",i,start_arg);
582
      break;
583
584
    case VTK_PARSE_VTK_OBJECT_REF:
    case VTK_PARSE_VTK_OBJECT_PTR:
585
      fprintf(fp,"    temp%i = (%s *)(vtkTclGetPointerFromObject(argv[%i],const_cast<char *>(\"%s\"),interp,error));\n",i,currentFunction->ArgClasses[i],start_arg,
Ken Martin's avatar
Ken Martin committed
586
              currentFunction->ArgClasses[i]);
587
      break;
588
589
    case VTK_PARSE_VOID:
    case VTK_PARSE_VTK_OBJECT:
590
      break;
Ken Martin's avatar
Ken Martin committed
591
592
    default:
      if (currentFunction->ArgCounts[i] > 1)
Ken Martin's avatar
Ken Martin committed
593
594
595
        {
        for (j = 0; j < currentFunction->ArgCounts[i]; j++)
          {
596
          switch (currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
Ken Martin's avatar
Ken Martin committed
597
            {
598
599
            case VTK_PARSE_FLOAT:
            case VTK_PARSE_DOUBLE:
Ken Martin's avatar
Ken Martin committed
600
601
              fprintf(fp,
                      "    if (Tcl_GetDouble(interp,argv[%i],&tempd) != TCL_OK) error = 1;\n",
602
                      start_arg);
Ken Martin's avatar
Ken Martin committed
603
604
              fprintf(fp,"    temp%i[%i] = tempd;\n",i,j);
              break;
605
606
607
608
609
610
611
            case VTK_PARSE_INT:
            case VTK_PARSE_SHORT:
            case VTK_PARSE_LONG:
            case VTK_PARSE_ID_TYPE:
            case VTK_PARSE_LONG_LONG:
            case VTK_PARSE___INT64:
            case VTK_PARSE_SIGNED_CHAR:
Ken Martin's avatar
Ken Martin committed
612
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
613
                      start_arg);
Ken Martin's avatar
Ken Martin committed
614
615
              fprintf(fp,"    temp%i[%i] = tempi;\n",i,j);
              break;
616
            case VTK_PARSE_BOOL:
617
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
618
                      start_arg);
619
              fprintf(fp,"    temp%i[%i] = tempi ? true : false;\n",i,j);
620
              break;
621
            case VTK_PARSE_CHAR:
Ken Martin's avatar
Ken Martin committed
622
623
              fprintf(fp,"    temp%i[%i] = *(argv[%i]);\n",i,j,start_arg);
              break;
624
            case VTK_PARSE_UNSIGNED_CHAR:
Ken Martin's avatar
Ken Martin committed
625
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
626
                      start_arg);
627
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned char>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
628
              break;
629
630
            case VTK_PARSE_UNSIGNED_INT:
            case VTK_PARSE_UNSIGNED_ID_TYPE:
Ken Martin's avatar
Ken Martin committed
631
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
632
                      start_arg);
633
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned int>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
634
              break;
635
            case VTK_PARSE_UNSIGNED_SHORT:
Ken Martin's avatar
Ken Martin committed
636
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
637
                      start_arg);
638
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned short>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
639
              break;
640
            case VTK_PARSE_UNSIGNED_LONG:
Ken Martin's avatar
Ken Martin committed
641
              fprintf(fp,"    if (Tcl_GetInt(interp,argv[%i],&tempi) != TCL_OK) error = 1;\n",
642
                      start_arg);
643
              fprintf(fp,"    temp%i[%i] = static_cast<unsigned long>(tempi);\n",i,j);
Ken Martin's avatar
Ken Martin committed
644
645
646
647
648
              break;
            }
          start_arg++;
          }
        }
649

650
651
652
653
654
655
656
    }
}

void outputFunction(FILE *fp, FileInfo *data)
{
  int i;
  int args_ok = 1;
657

658
  /* some functions will not get wrapped no matter what else */
659
  if (currentFunction->IsOperator ||
660
      currentFunction->ArrayFailure ||
Ken Martin's avatar
Ken Martin committed
661
      !currentFunction->IsPublic ||
662
      !currentFunction->Name)
663
664
665
    {
    return;
    }
666

667
668
669
  /* check to see if we can handle the args */
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
670
671
    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE_UNKNOWN) args_ok = 0;
Ken Martin's avatar
Ken Martin committed
672
    /* if its a pointer arg make sure we have the ArgCount */
673
674
675
676
677
678
679
    if (((currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT) != 0) &&
        ((currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE)
         != VTK_PARSE_CHAR_PTR) &&
        ((currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE)
         != VTK_PARSE_VTK_OBJECT_PTR) &&
        ((currentFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE)
         != VTK_PARSE_VTK_OBJECT_REF))
Ken Martin's avatar
Ken Martin committed
680
681
      {
      if (currentFunction->NumberOfArguments > 1 ||
Ken Martin's avatar
Ken Martin committed
682
683
684
685
          !currentFunction->ArgCounts[i])
        {
        args_ok = 0;
        }
Ken Martin's avatar
Ken Martin committed
686
      }
687
688
689
690
691
692
693
    if (((currentFunction->ArgTypes[i] & VTK_PARSE_UNSIGNED) != 0) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_CHAR) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_INT) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_SHORT) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_LONG) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_ID_TYPE) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_LONG_LONG))
694
695
696
697
      {
      args_ok = 0;
      }
    }
698
  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_UNKNOWN)
699
700
    {
    args_ok = 0;
701
    }
702
703
704
705
706
  if (((currentFunction->ReturnType & VTK_PARSE_INDIRECT)
       != VTK_PARSE_POINTER) &&
      ((currentFunction->ReturnType & VTK_PARSE_INDIRECT)
       != VTK_PARSE_REF) &&
      ((currentFunction->ReturnType & VTK_PARSE_INDIRECT) != 0))
707
708
709
    {
    args_ok = 0;
    }
710
711
  if (currentFunction->NumberOfArguments &&
      (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION)
712
713
714
715
      &&(currentFunction->NumberOfArguments != 1))
    {
    args_ok = 0;
    }
716

Ken Martin's avatar
Ken Martin committed
717
  /* we can't handle void * return types */
718
719
  if ((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
      == VTK_PARSE_VOID_PTR)
Ken Martin's avatar
Ken Martin committed
720
721
722
    {
    args_ok = 0;
    }
723

724
  /* watch out for functions that dont have enough info */
725
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
726
    {
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
    case VTK_PARSE_FLOAT_PTR:
    case VTK_PARSE_DOUBLE_PTR:
    case VTK_PARSE_INT_PTR:
    case VTK_PARSE_SHORT_PTR:
    case VTK_PARSE_LONG_PTR:
    case VTK_PARSE_ID_TYPE_PTR:
    case VTK_PARSE_LONG_LONG_PTR:
    case VTK_PARSE___INT64_PTR:
    case VTK_PARSE_SIGNED_CHAR_PTR:
    case VTK_PARSE_BOOL_PTR:
    case VTK_PARSE_UNSIGNED_CHAR_PTR:
    case VTK_PARSE_UNSIGNED_INT_PTR:
    case VTK_PARSE_UNSIGNED_SHORT_PTR:
    case VTK_PARSE_UNSIGNED_LONG_PTR:
    case VTK_PARSE_UNSIGNED_ID_TYPE_PTR:
    case VTK_PARSE_UNSIGNED_LONG_LONG_PTR:
    case VTK_PARSE_UNSIGNED___INT64_PTR:
744
745
746
      args_ok = currentFunction->HaveHint;
      break;
    }
747

748
  /* if the args are OK and it is not a constructor or destructor */
749
  if (args_ok &&
750
751
752
      strcmp(data->ClassName,currentFunction->Name) &&
      strcmp(data->ClassName,currentFunction->Name + 1))
    {
Ken Martin's avatar
Ken Martin committed
753
    int required_args = 0;
754

Ken Martin's avatar
Ken Martin committed
755
756
757
    /* calc the total required args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
758
      required_args = required_args +
Ken Martin's avatar
Ken Martin committed
759
        (currentFunction->ArgCounts[i] ? currentFunction->ArgCounts[i] : 1);
Ken Martin's avatar
Ken Martin committed
760
      }
761

762
763
764
765
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#if !defined(VTK_LEGACY_REMOVE)\n");
      }
766
    fprintf(fp,"  if ((!strcmp(\"%s\",argv[1]))&&(argc == %i))\n    {\n",
Ken Martin's avatar
Ken Martin committed
767
            currentFunction->Name, required_args + 2);
768

769
770
771
772
    /* process the args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      output_temp(fp, i, currentFunction->ArgTypes[i],
773
                  currentFunction->ArgClasses[i],
Ken Martin's avatar
Ken Martin committed
774
                  currentFunction->ArgCounts[i]);
775
776
      }
    output_temp(fp, MAX_ARGS,currentFunction->ReturnType,
Ken Martin's avatar
Ken Martin committed
777
                currentFunction->ReturnClass, 0);
778

779
    /* only use the error variable if we have arguments to parse */
780
    if (currentFunction->NumberOfArguments)
781
      {
782
783
784
785
786
787
788
      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");
789
      }
790
791

    switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
792
      {
793
      case VTK_PARSE_VOID:
794
        fprintf(fp,"    op->%s(",currentFunction->Name);
Ken Martin's avatar
Ken Martin committed
795
        break;
796
      case VTK_PARSE_VTK_OBJECT_REF:
797
        fprintf(fp,"    temp%i = &(op)->%s(",MAX_ARGS,currentFunction->Name);
Ken Martin's avatar
Ken Martin committed
798
        break;
799
      default:
800
        fprintf(fp,"    temp%i = (op)->%s(",MAX_ARGS,currentFunction->Name);
801
802
803
804
      }
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      if (i)
Ken Martin's avatar
Ken Martin committed
805
806
807
        {
        fprintf(fp,",");
        }
808
      if (currentFunction->ArgTypes[i] == VTK_PARSE_VTK_OBJECT_REF)
Ken Martin's avatar
Ken Martin committed
809
810
811
        {
        fprintf(fp,"*(temp%i)",i);
        }
812
      else if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
Ken Martin's avatar
Ken Martin committed
813
        {
814
        fprintf(fp,"vtkTclVoidFunc,static_cast<void *>(temp%i)",i);
Ken Martin's avatar
Ken Martin committed
815
        }
816
      else
Ken Martin's avatar
Ken Martin committed
817
818
819
        {
        fprintf(fp,"temp%i",i);
        }
820
821
      }
    fprintf(fp,");\n");
822
823
    if (currentFunction->NumberOfArguments &&
        (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION))
824
      {
825
      fprintf(fp,"    op->%sArgDelete(vtkTclVoidFuncArgDelete);\n",
Ken Martin's avatar
Ken Martin committed
826
              currentFunction->Name);
827
828
      }
    return_result(fp);
829
    fprintf(fp,"    return TCL_OK;\n");
830

831
    /* close the if error */
832
833
834
835
    if (currentFunction->NumberOfArguments)
      {
      fprintf(fp,"    }\n");
      }
836

837
    fprintf(fp,"    }\n");
838
839
840
841
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#endif\n");
      }
842

843
844
845
846
847
848
849
850
    wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
    numberOfWrappedFunctions++;
    }
}

/* print the parsed structures */
void vtkParseOutput(FILE *fp, FileInfo *data)
{
851
  int i,j,k;
852

853
  fprintf(fp,"// tcl wrapper for %s object\n//\n",data->ClassName);
854
  fprintf(fp,"#define VTK_WRAPPING_CXX\n");
855
856
  if (strcmp("vtkObjectBase",data->ClassName) != 0)
    {
857
      /* Block inclusion of full streams. */
858
859
    fprintf(fp,"#define VTK_STREAMS_FWD_ONLY\n");
    }
Will Schroeder's avatar
Will Schroeder committed
860
  fprintf(fp,"#include \"vtkSystemIncludes.h\"\n");
861
862
  fprintf(fp,"#include \"%s.h\"\n\n",data->ClassName);
  fprintf(fp,"#include \"vtkTclUtil.h\"\n");
863
  fprintf(fp,"#include <vtkstd/stdexcept>\n");
864
  fprintf(fp,"#include <vtksys/ios/sstream>\n");
865
866
  if (data->IsConcrete)
    {
867
868
    if (strcmp(data->ClassName, "vtkRenderWindowInteractor") == 0)
      {
869
      fprintf(fp,"#include \"vtkToolkits.h\"\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
870
      fprintf(fp,"#if defined( VTK_USE_X ) && defined( VTK_USE_TK )\n");
871
      fprintf(fp,"# include \"vtkXRenderWindowTclInteractor.h\"\n");
Yves Starreveld's avatar
   
Yves Starreveld committed
872
      fprintf(fp,"#endif\n");
873
874
875

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

Andy Cedilnik's avatar
Andy Cedilnik committed
876
      fprintf(fp,"#if defined( VTK_USE_X ) && defined( VTK_USE_TK )\n");
877
      fprintf(fp,"  %s *temp = vtkXRenderWindowTclInteractor::New();\n",
Ken Martin's avatar
Ken Martin committed
878
              data->ClassName);
879
880
881
      fprintf(fp,"#else\n");
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
      fprintf(fp,"#endif\n");
882
      fprintf(fp,"  return static_cast<ClientData>(temp);\n}\n\n");
883
884
885
886
887
      }
    else
      {
      fprintf(fp,"\nClientData %sNewCommand()\n{\n",data->ClassName);
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
888
      fprintf(fp,"  return static_cast<ClientData>(temp);\n}\n\n");
889
      }
890
    }
891

892
893
894
895
896
897
  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);
898
  fprintf(fp,"  if ((argc == 2)&&(!strcmp(\"Delete\",argv[1]))&& !vtkTclInDelete(interp))\n    {\n");
899
900
  fprintf(fp,"    Tcl_DeleteCommand(interp,argv[0]);\n");
  fprintf(fp,"    return TCL_OK;\n    }\n");
901
  fprintf(fp,"   return %sCppCommand(static_cast<%s *>(static_cast<vtkTclCommandArgStruct *>(cd)->Pointer),interp, argc, argv);\n}\n",data->ClassName,data->ClassName);
902

903
904
905
906
907
  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");
908
909
910
911
  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");
912

913
  fprintf(fp,"  if (argc < 2)\n    {\n    Tcl_SetResult(interp,const_cast<char *>(\"Could not find requested method.\"), TCL_VOLATILE);\n    return TCL_ERROR;\n    }\n");
914
915
916
917
918

  /* 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
919
          data->ClassName);
920 </