vtkWrapTcl.c 45.6 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 "vtkConfigure.h"
22
23
24

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

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 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
42
    result = (char *)malloc((size_t)(maxlen+1));
43
44
45
46
47
48
49
50
51
52
53
    oldmaxlen = maxlen;
    }

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

  j = 0;

  n = (int)strlen(comment);
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  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;
81
      }
82
83
84
85
86
87
88
89
90
91
    else if (isprint(comment[i]))
      {
      result[j] = comment[i];
      j++;
      }
    else
      {
      sprintf(&result[j],"\\%3.3o",comment[i]);
      j += 4;
      }
92

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

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

  return result;
}
105

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

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

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

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

136
  switch ((aType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
137
    {
138
139
140
141
142
143
144
145
146
147
148
149
150
    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;
151
152
    case VTK_PARSE_STRING:      fprintf(fp,"vtkStdString "); break;
    case VTK_PARSE_UNICODE_STRING: fprintf(fp,"vtkUnicodeString "); break;
153
    case VTK_PARSE_UNKNOWN:     return;
154
155
    }

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

  switch (aType & VTK_PARSE_INDIRECT)
164
    {
165
166
167
168
169
    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;
170
    }
171

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

181
182
183
184
185
#define INDENT "    "

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#undef INDENT
333
334
335
336
}

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

    /* handle functions returning vectors */
    /* this is done by looking them up in a hint file */
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    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:
497
498
499
      use_hints(fp);
      break;
    default:
500
      fprintf(fp,"    Tcl_SetResult(interp, const_cast<char *>(\"unable to return result.\"), TCL_VOLATILE);\n");
501
502
503
504
505
506
      break;
    }
}

void get_args(FILE *fp, int i)
{
Ken Martin's avatar
Ken Martin committed
507
508
  int j;
  int start_arg = 2;
509

Ken Martin's avatar
Ken Martin committed
510
511
512
  /* what arg do we start with */
  for (j = 0; j < i; j++)
    {
513
    start_arg = start_arg +
Ken Martin's avatar
Ken Martin committed
514
515
      (currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
    }
516

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

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

659
660
661
662
663
664
665
    }
}

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

667
  /* some functions will not get wrapped no matter what else */
668
  if (currentFunction->IsOperator ||
669
      currentFunction->ArrayFailure ||
Ken Martin's avatar
Ken Martin committed
670
      !currentFunction->IsPublic ||
671
      !currentFunction->Name)
672
673
674
    {
    return;
    }
675

676
677
678
  /* check to see if we can handle the args */
  for (i = 0; i < currentFunction->NumberOfArguments; i++)
    {
679
680
    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE_UNKNOWN) args_ok = 0;
681
#ifndef VTK_TYPE_USE_LONG_LONG
682
683
684
685
686
    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE_LONG_LONG ||
        (currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE_UNSIGNED_LONG_LONG) args_ok = 0;
#endif
687
#ifndef VTK_TYPE_USE___INT64
688
689
690
691
692
693
    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE___INT64 ||
        (currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE_UNSIGNED___INT64) args_ok = 0;
#endif

694
695
696
697
698
699
700
701
702
    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE) ==
         VTK_PARSE_STRING &&
        (currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT) != 0 &&
        (currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT) !=
         VTK_PARSE_REF) args_ok = 0;

    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE) ==
         VTK_PARSE_UNICODE_STRING) args_ok = 0;

703
704
705
706
707
    if ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE)
        == VTK_PARSE_VTK_OBJECT &&
        (currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT)
        != VTK_PARSE_POINTER) args_ok = 0;

Ken Martin's avatar
Ken Martin committed
708
    /* if its a pointer arg make sure we have the ArgCount */
709
710
711
712
    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)
713
         != VTK_PARSE_VTK_OBJECT_PTR))
Ken Martin's avatar
Ken Martin committed
714
715
      {
      if (currentFunction->NumberOfArguments > 1 ||
Ken Martin's avatar
Ken Martin committed
716
717
718
719
          !currentFunction->ArgCounts[i])
        {
        args_ok = 0;
        }
Ken Martin's avatar
Ken Martin committed
720
      }
721
722
723
724
725
    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) &&
726
727
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_LONG_LONG) &&
        (currentFunction->ArgTypes[i] != VTK_PARSE_UNSIGNED_ID_TYPE))
728
729
730
731
      {
      args_ok = 0;
      }
    }
732
733
  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE_UNKNOWN) args_ok = 0;
734
#ifndef VTK_TYPE_USE_LONG_LONG
735
736
737
738
739
  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE_LONG_LONG ||
      (currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE_UNSIGNED_LONG_LONG) args_ok = 0;
#endif
740
#ifndef VTK_TYPE_USE___INT64
741
742
743
744
745
746
  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE___INT64 ||
      (currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE_UNSIGNED___INT64) args_ok = 0;
#endif

747
748
749
750
751
752
753
754
755
  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE_STRING &&
      (currentFunction->ReturnType & VTK_PARSE_INDIRECT) != 0 &&
      (currentFunction->ReturnType & VTK_PARSE_INDIRECT) !=
       VTK_PARSE_REF) args_ok = 0;

  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE) ==
       VTK_PARSE_UNICODE_STRING) args_ok = 0;

756
757
758
759
760
  if ((currentFunction->ReturnType & VTK_PARSE_BASE_TYPE)
      == VTK_PARSE_VTK_OBJECT &&
      (currentFunction->ReturnType & VTK_PARSE_INDIRECT)
      != VTK_PARSE_POINTER) args_ok = 0;

761
  if (((currentFunction->ReturnType & VTK_PARSE_INDIRECT)
762
       != VTK_PARSE_POINTER) &&
763
764
765
      ((currentFunction->ReturnType & VTK_PARSE_INDIRECT)
       != VTK_PARSE_REF) &&
      ((currentFunction->ReturnType & VTK_PARSE_INDIRECT) != 0))
766
767
768
    {
    args_ok = 0;
    }
769
770
  if (currentFunction->NumberOfArguments &&
      (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION)
771
772
773
774
      &&(currentFunction->NumberOfArguments != 1))
    {
    args_ok = 0;
    }
775

Ken Martin's avatar
Ken Martin committed
776
  /* we can't handle void * return types */
777
778
  if ((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
      == VTK_PARSE_VOID_PTR)
Ken Martin's avatar
Ken Martin committed
779
780
781
    {
    args_ok = 0;
    }
782

783
  /* watch out for functions that dont have enough info */
784
  switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
785
    {
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
    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:
803
804
805
      args_ok = currentFunction->HaveHint;
      break;
    }
806

807
  /* if the args are OK and it is not a constructor or destructor */
808
  if (args_ok &&
809
810
811
      strcmp(data->ClassName,currentFunction->Name) &&
      strcmp(data->ClassName,currentFunction->Name + 1))
    {
Ken Martin's avatar
Ken Martin committed
812
    int required_args = 0;
813

Ken Martin's avatar
Ken Martin committed
814
815
816
    /* calc the total required args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
817
      required_args = required_args +
Ken Martin's avatar
Ken Martin committed
818
        (currentFunction->ArgCounts[i] ? currentFunction->ArgCounts[i] : 1);
Ken Martin's avatar
Ken Martin committed
819
      }
820

821
822
823
824
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#if !defined(VTK_LEGACY_REMOVE)\n");
      }
825
    fprintf(fp,"  if ((!strcmp(\"%s\",argv[1]))&&(argc == %i))\n    {\n",
Ken Martin's avatar
Ken Martin committed
826
            currentFunction->Name, required_args + 2);
827

828
829
830
831
    /* process the args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      output_temp(fp, i, currentFunction->ArgTypes[i],
832
                  currentFunction->ArgClasses[i],
Ken Martin's avatar
Ken Martin committed
833
                  currentFunction->ArgCounts[i]);
834
835
      }
    output_temp(fp, MAX_ARGS,currentFunction->ReturnType,
Ken Martin's avatar
Ken Martin committed
836
                currentFunction->ReturnClass, 0);
837

838
    /* only use the error variable if we have arguments to parse */
839
    if (currentFunction->NumberOfArguments)
840
      {
841
842
843
844
845
846
847
      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");
848
      }
849
850

    switch (currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE)
851
      {
852
      case VTK_PARSE_VOID:
853
        fprintf(fp,"    op->%s(",currentFunction->Name);
Ken Martin's avatar
Ken Martin committed
854
        break;
855
      default:
856
        fprintf(fp,"    temp%i = (op)->%s(",MAX_ARGS,currentFunction->Name);
857
858
859
860
      }
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
      {
      if (i)
Ken Martin's avatar
Ken Martin committed
861
862
863
        {
        fprintf(fp,",");
        }
864
      if (currentFunction->ArgTypes[i] == VTK_PARSE_FUNCTION)
Ken Martin's avatar
Ken Martin committed
865
        {
866
        fprintf(fp,"vtkTclVoidFunc,static_cast<void *>(temp%i)",i);
Ken Martin's avatar
Ken Martin committed
867
        }
868
      else
Ken Martin's avatar
Ken Martin committed
869
870
871
        {
        fprintf(fp,"temp%i",i);
        }
872
873
      }
    fprintf(fp,");\n");
874
875
    if (currentFunction->NumberOfArguments &&
        (currentFunction->ArgTypes[0] == VTK_PARSE_FUNCTION))
876
      {
877
      fprintf(fp,"    op->%sArgDelete(vtkTclVoidFuncArgDelete);\n",
Ken Martin's avatar
Ken Martin committed
878
              currentFunction->Name);
879
880
      }
    return_result(fp);
881
    fprintf(fp,"    return TCL_OK;\n");
882

883
    /* close the if error */
884
885
886
887
    if (currentFunction->NumberOfArguments)
      {
      fprintf(fp,"    }\n");
      }
888

889
    fprintf(fp,"    }\n");
890
891
892
893
    if(currentFunction->IsLegacy)
      {
      fprintf(fp,"#endif\n");
      }
894

895
896
897
898
899
900
901
902
    wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
    numberOfWrappedFunctions++;
    }
}

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

905
  fprintf(fp,"// tcl wrapper for %s object\n//\n",data->ClassName);
906
  fprintf(fp,"#define VTK_WRAPPING_CXX\n");
907
908
  if (strcmp("vtkObjectBase",data->ClassName) != 0)
    {
909
      /* Block inclusion of full streams. */
910
911
    fprintf(fp,"#define VTK_STREAMS_FWD_ONLY\n");
    }
Will Schroeder's avatar
Will Schroeder committed
912
  fprintf(fp,"#include \"vtkSystemIncludes.h\"\n");
913
914
  fprintf(fp,"#include \"%s.h\"\n\n",data->ClassName);
  fprintf(fp,"#include \"vtkTclUtil.h\"\n");
915
  fprintf(fp,"#include \"vtkStdString.h\"\n");
916
  fprintf(fp,"#include <vtkstd/stdexcept>\n");
917
  fprintf(fp,"#include <vtksys/ios/sstream>\n");
918
919
  if (data->IsConcrete)
    {
920
921
    if (strcmp(data->ClassName, "vtkRenderWindowInteractor") == 0)
      {
922
      fprintf(fp,"#include \"vtkToolkits.h\"\n");
Andy Cedilnik's avatar
Andy Cedilnik committed
923
      fprintf(fp,"#if defined( VTK_USE_X ) && defined( VTK_USE_TK )\n");
924
      fprintf(fp,"# include \"vtkXRenderWindowTclInteractor.h\"\n");
Yves Starreveld's avatar
   
Yves Starreveld committed
925
      fprintf(fp,"#endif\n");
926
927
928

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

Andy Cedilnik's avatar
Andy Cedilnik committed
929
      fprintf(fp,"#if defined( VTK_USE_X ) && defined( VTK_USE_TK )\n");
930
      fprintf(fp,"  %s *temp = vtkXRenderWindowTclInteractor::New();\n",
Ken Martin's avatar
Ken Martin committed
931
              data->ClassName);
932
933
934
      fprintf(fp,"#else\n");
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
      fprintf(fp,"#endif\n");
935
      fprintf(fp,"  return static_cast<ClientData>(temp);\n}\n\n");
936
937
938
939
940
      }
    else
      {
      fprintf(fp,"\nClientData %sNewCommand()\n{\n",data->ClassName);
      fprintf(fp,"  %s *temp = %s::New();\n",data->ClassName,data->ClassName);
941
      fprintf(fp,"  return static_cast<ClientData>(temp);\n}\n\n");
Jim Miller's avatar