File: funtab.c

package info (click to toggle)
funtools 1.4.4+dfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,168 kB
  • ctags: 10,760
  • sloc: ansic: 87,238; sh: 9,727; lex: 4,595; asm: 3,281; ada: 1,681; makefile: 1,458; pascal: 1,089; cpp: 1,001; cs: 879; perl: 161; yacc: 64; sed: 32; csh: 10; tcl: 9
file content (1418 lines) | stat: -rw-r--r-- 38,256 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

#include <ctype.h>
#include <funtoolsP.h>

/*
 *
 * private routines
 *
 */

#ifdef ANSI_FUNC
static void *
swap(void *obuf, void *ibuf, int width, int type)
#else
static void *swap(obuf, ibuf, width, type)
     void *obuf;
     void *ibuf;
     int width;
     int type;
#endif
{
  switch ( type ) {
  case 'A':
  case 'B':
  case 'X':
  case 'L':
    memcpy(obuf, ibuf, width);
    break;
  case 'I':
  case 'U':
    swap2(obuf, ibuf, width);
    break;
  case 'J':
  case 'V':
  case 'E':
    swap4(obuf, ibuf, width);
    break;
  case 'K':
  case 'D':
    swap8(obuf, ibuf, width);
    break;
  default:
    memcpy(obuf, ibuf, width);
    break;
  }
  return obuf;
}

#ifdef ANSI_FUNC
static void
_FunTableSave(Fun fun, char *rows, char *ebuf, int nrow)
#else
static void _FunTableSave(fun, rows, ebuf, nrow)
     Fun fun;
     char *rows;
     char *ebuf;
     int nrow;
#endif
{
  SaveBuf new, cur;
  int n;

  /* allocate new data space */
  new = (SaveBuf)xcalloc(1, sizeof(SaveBufRec));
  /* this is how many rows we are saving */
  new->nrow = nrow;
  /* see if we can store in memory or if we have to shunt to a file */
  n = nrow * (fun->rowsize + fun->rawsize);
  if( (fun->head->lmem < 0) || ((fun->head->lmem-n)>0) ){
    new->type = 1;
    /* save alloc'ed raw buf null out buf so funtools does not free it */
    new->rbuf = fun->rawbuf;
    fun->rawbuf = NULL;
    /* if user passed in a row buffer, we save a copy */
    if( rows ){
      new->ebuf = xmalloc(nrow*fun->rawsize);
      memcpy(new->ebuf, ebuf, new->nrow*fun->rowsize);
    }
    /* otherwise just save funtools-allocated row buffer */
    else{
      new->ebuf  = ebuf;
    }
    /* decrease amount of memory we have left for saved data */
    if( fun->head->lmem > 0 )
      fun->head->lmem -= n;
  }
  else{
    new->type = 2;
    /* write user rows */
    fwrite(ebuf, fun->rowsize, nrow, fun->lefp);
    /* free up user row buffer unless user allocated it */
    if( !rows && ebuf ) xfree(ebuf);
    /* write raw rows */
    fwrite(fun->rawbuf, fun->rawsize, nrow, fun->lrfp);
    /* free up raw buffer */
    if( fun->rawbuf ) xfree(fun->rawbuf);
    fun->rawbuf = NULL;
  }
  /* add to end of list */
  if( fun->save == NULL ){
    fun->save = new;
  }
  else{
    for(cur=fun->save; cur->next!=NULL; cur=cur->next)
      ;
    cur->next = new;
  }
}

#ifdef ANSI_FUNC
static char *
_FunTableRestore(Fun fun, char *rows, int *nrow)
#else
static char *_FunTableRestore(fun, rows, nrow)
     Fun fun;
     char *rows;
     int *nrow;
#endif
{
  SaveBuf cur;
  char *ebuf=NULL;
  unsigned char *rbuf;

  /* start of pessimistically */
  *nrow = 0;
  /* make sure we have something to restore */
  if( !fun->save )
    return NULL;
  /* bump list to next */
  cur = fun->save;
  fun->save = cur->next;
  /* fill in the blanks */
  *nrow = cur->nrow;
  switch(cur->type){
  case 1:
    /* we might have to fill the passed row buffer */
    if( rows ){
      /* copy into user-supplied buffer */
      memcpy(rows, cur->ebuf, cur->nrow*fun->rowsize);
      /* we will return user-supplied buffer */
      ebuf = rows;
      /* free up temp buffer */
      if(cur->ebuf ) xfree(cur->ebuf);
    }
    /* otherwise, we just pass back the saved row buffer */
    else{
      ebuf = cur->ebuf;
    }
    /* move raw rows buf back into funtools struct */
    if( fun->rawbuf ) xfree(fun->rawbuf);
    fun->rawbuf = cur->rbuf;
    break;
  case 2:
    /* we might have to fill the passed row buffer */
    if( rows )
      ebuf = rows;
    else
      ebuf = xmalloc(fun->rowsize*cur->nrow);
    /* read into user-supplied buffer */
    fread(ebuf, fun->rowsize, cur->nrow, fun->lefp);
    /* allocate space for the raw buffer */
    rbuf = xmalloc(fun->rawsize*cur->nrow);
    /* read into raw buffer */
    fread(rbuf, fun->rawsize, cur->nrow, fun->lrfp);
    /* move raw rows buf back into funtools struct */
    if( fun->rawbuf ) xfree(fun->rawbuf);
    fun->rawbuf = rbuf;
    break;
  }
  /* this is the last file we "read" in the list */
  if( fun->head )
    fun->head->current = fun;
  else
    fun->current = fun;
  if( cur ) xfree(cur);
  /* return results */
  return ebuf;
}

#ifdef ANSI_FUNC
static void
_FunBlank(Fun fun, int type, char *buf)
#else
static void _FunBlank(fun, type, buf)
     Fun fun;
     int type;
     char *buf;
#endif
{
  unsigned char bval;
  short sval;
  int ival;
  longlong lval;
  float fval;
  double dval;

  switch(type){
  case 'B':
    bval = fun->blank;
    memcpy(buf, &bval, sizeof(char));
    break;
  case 'I':
  case 'U':
    sval = fun->blank;
    memcpy(buf, &sval, sizeof(short));
    break;
  case 'J':
  case 'V':
    ival = fun->blank;
    memcpy(buf, &ival, sizeof(int));
    break;
  case 'K':
#if HAVE_LONG_LONG == 0
    gerror(stderr,
	   "long long support was not built into this program\n");
    exit(1);
#endif
    lval = fun->blank;
    memcpy(buf, &lval, sizeof(longlong));
    break;
  case 'E':
    fval = getnanf();
    memcpy(buf, &fval, sizeof(float));
    break;
  case 'D':
    dval = getnand();
    memcpy(buf, &dval, sizeof(double));
    break;
  default:
    memset(buf, 0, ft_sizeof(type));
    break;
  }
}
  
#ifdef ANSI_FUNC
static void *
_FunTextRead(Fun fun, char *buf, size_t size, size_t get,  size_t *got)
#else
static void *_FunTextRead(fun, buf, size, get,  got)
     Fun fun;
     char *buf;
     size_t size;
     size_t get;
     size_t *got;
#endif
{
  int p, t;
  int len;
  int dodata1;
  int xtype;
  int ntoken;
  int nline=0;
  char lbuf[SZ_LINE];
  char *bp;
  Parse parser=NULL;
  ParsedLine line;

  /* avoid -W unused parameter warning */
  if( 0 ) size = size;

  /* sanity checks
     NB: the order of ascii columns should be identical to the fitsy columns
     (since the latter was generated by the former) */
  if( !fun->header || !fun->header->table || !fun->header->table->col )
    return 0;

  /* if we have not read any bytes, use data1 */
  dodata1 = (fun->bytes==0);

  /* convert ASCII columns to binary columns */
  bp = buf;
  while( (size_t)nline < get ){
    /* first line should be in data1 */
    if( dodata1 ){
      /* look for the parser with data1 that has the most tokens */
      for(parser=NULL, ntoken=0, p=0; p<fun->nparser; p++){
	if( !(fun->parsers[p]->state & PARSE_STATE_BAD) && 
	    fun->parsers[p]->data1 ){
	  if( fun->parsers[p]->ntoken > ntoken ){
	    parser = fun->parsers[p];
	    ntoken = parser->ntoken;
	  }
	}
      }
      /* use the parser with the max number of tokens */
      if( parser ){
	line = parser->data1;
	if( PARSE_ISCOMMENT(line) ) continue;
	for(t=0; t<line->ntoken; t++){
	  switch(line->tokens[t].type){
	  case PARSE_FLOAT:
	  case PARSE_HEXINT:
	  case PARSE_INTEGER:
	    /* handle string type specially */
	    if( fun->header->table->col[t].type == 'A' ){
	      len = strlen(line->tokens[t].sval);
	      if( len >= fun->header->table->col[t].width){
		memmove(bp, line->tokens[t].sval, 
			fun->header->table->col[t].width);
	      }
	      else{
		memmove(bp, line->tokens[t].sval, len);
		memset(bp+len, 0, fun->header->table->col[t].width-len);
	      }
	    }
	    /* handle bit-field processing specially */
	    else if( fun->header->table->col[t].type == 'X' ){
	      switch(fun->header->table->col[t].width){
	      case 0:
	      case 1:
		xtype = 'B'; break;
	      case 2:
		xtype = 'U'; break;
	      case 4:
		xtype = 'V'; break;
	      default:
		xtype = 'B'; break;
	      }
	      if( (line->tokens[t].type == PARSE_INTEGER) ||
		  (line->tokens[t].type == PARSE_HEXINT)   )
		ft_acht2(xtype, bp, 'K', &line->tokens[t].lval, 1, 0, 0);
	      else
		ft_acht2(xtype, bp, 'D', &line->tokens[t].dval, 1, 0, 0);
	    }
	    else{
	      if( (line->tokens[t].type == PARSE_INTEGER) ||
		  (line->tokens[t].type == PARSE_HEXINT)   )
		ft_acht2(fun->header->table->col[t].type, bp, 
			 'K', &line->tokens[t].lval, 1, 0, 0);
	      else
		ft_acht2(fun->header->table->col[t].type, bp, 
			 'D', &line->tokens[t].dval, 1, 0, 0);
	    }
	    bp += fun->header->table->col[t].width;
	    break;
	  case PARSE_NULL:
	    _FunBlank(fun, fun->header->table->col[t].type, bp);
	    bp += fun->header->table->col[t].width;
	    break;
	  case PARSE_STRING:
	    len = strlen(line->tokens[t].sval);
	    if( len >= fun->header->table->col[t].width){
	      memmove(bp, line->tokens[t].sval, 
		      fun->header->table->col[t].width);
	    }
	    else{
	      memmove(bp, line->tokens[t].sval, len);
	      memset(bp+len, 0, fun->header->table->col[t].width-len);
	    }
	    bp += fun->header->table->col[t].width;
	    break;
	  default:
	    break;
	  }
	}
	/* processed data1 line */
	nline++;
      }
      /* went through all parsers looking for data1 */
      dodata1 = 0;
    }
    /* read and process the next line */
    else{
      /* get next line or be done */
      if( !ggets(fun->gio, lbuf, SZ_LINE) )
	break;
      /* analyze line and make sure one parser succeeded (even if its EOT) */
      if( !ParseAnalyze(fun->parsers, fun->nparser, lbuf) ){
	gerror(stderr, "text parser failure analyzing line: %s\n", lbuf);
	break;
      }
      /* look for valid parser with the most tokens */
      for(parser=NULL, ntoken=0, parser=NULL, p=0; p<fun->nparser; p++){
	if( fun->parsers[p]->state & PARSE_STATE_BAD) 
	  continue;
	else if( fun->parsers[p]->ntoken > ntoken ){
	  parser = fun->parsers[p];
	  ntoken = parser->ntoken;
	}
      }
      /* stop if no valid parser or valid parser found EOT */
      if( !parser ) break;
      if( parser->state & PARSE_STATE_EOT) break;
      /* use selected parser to fill up row buffer */
      line = parser->cur;
      if( PARSE_ISCOMMENT(line) ) continue;
      for(t=0; t<line->ntoken; t++){
	switch(line->tokens[t].type){
	case PARSE_FLOAT:
	case PARSE_HEXINT:
	case PARSE_INTEGER:
	  /* handle string type specially */
	  if( fun->header->table->col[t].type == 'A' ){
	    len = strlen(line->tokens[t].sval);
	    if( len >= fun->header->table->col[t].width){
	      memmove(bp, line->tokens[t].sval, 
		      fun->header->table->col[t].width);
	    }
	    else{
	      memmove(bp, line->tokens[t].sval, len);
	      memset(bp+len, 0, fun->header->table->col[t].width-len);
	    }
	  }
	  /* handle bit-field processing specially */
	  else if( fun->header->table->col[t].type == 'X' ){
	    switch(fun->header->table->col[t].width){
	    case 0:
	    case 1:
	      xtype = 'B'; break;
	    case 2:
	      xtype = 'U'; break;
	    case 4:
	      xtype = 'V'; break;
	    default:
	      xtype = 'B'; break;
	    }
	    if( (line->tokens[t].type == PARSE_INTEGER) ||
		(line->tokens[t].type == PARSE_HEXINT)   )
	      ft_acht2(xtype, bp, 'K', &line->tokens[t].lval, 1, 0, 0);
	    else
	      ft_acht2(xtype, bp, 'D', &line->tokens[t].dval, 1, 0, 0);
	  }
	  else{
	    if( (line->tokens[t].type == PARSE_INTEGER) ||
		(line->tokens[t].type == PARSE_HEXINT)   )
	      ft_acht2(fun->header->table->col[t].type, bp, 
		       'K', &line->tokens[t].lval, 1, 0, 0);
	    else
	      ft_acht2(fun->header->table->col[t].type, bp, 
		       'D', &line->tokens[t].dval, 1, 0, 0);
	  }
	  bp += fun->header->table->col[t].width;
	  break;
	case PARSE_STRING:
	  /* handle string type specially */
	  if( fun->header->table->col[t].type == 'A' ){
	    len = strlen(line->tokens[t].sval);
	    if( len >= fun->header->table->col[t].width){
	      memmove(bp, line->tokens[t].sval, 
		      fun->header->table->col[t].width);
	    }
	    else{
	      memmove(bp, line->tokens[t].sval, len);
	      memset(bp+len, 0, fun->header->table->col[t].width-len);
	    }
	  }
	  bp += fun->header->table->col[t].width;
	  break;
	case PARSE_NULL:
	  _FunBlank(fun, fun->header->table->col[t].type, bp);
	  bp += fun->header->table->col[t].width;
	  break;
	default:
	  break;
	}
      }
      /* processed another line */
      nline++;
    }
  }
  *got = nline;
  return buf;
}

/*
 *
 * semi-public routines
 *
 */

#ifdef ANSI_FUNC
void *
_FunRead(Fun fun, char *buf, size_t size, size_t get,  size_t *got)
#else
void *_FunRead(fun, buf, size, get,  got)
     Fun fun;
     char *buf;
     size_t size;
     size_t get;
     size_t *got;
#endif
{
  /* initialize */
  *got = 0;
  
  /* sanity check */
  if( !fun || !buf ) return NULL;

  /* text parsers are handled one line at a time */
  if( fun->parsers ){
    return _FunTextRead(fun, buf, size, get,  got);
  }
  /* binary reads */
  else{
    /* ordinary binary read */
    if( (fun->filt == NOFILTER) || (fun->filt->doidx != 1) ){
      return _gread(fun->gio, (void *)buf, size, get, got);
    }
    /* indexed read */
    else{
      return idxread(fun->filt->idx, fun->gio, fun->header,
		     (void *)buf, size, get, got, &fun->dofilt);
    }
  }
}

/*
 *
 * _FunFixNaxis2 -- change the value of an NAXIS2 card
 * used to correct a copied binary table after we filter
 *
 */
#ifdef ANSI_FUNC
int
_FunFixNaxis2(Fun fun)
#else
int _FunFixNaxis2(fun)
     Fun fun;
#endif
{
  if( (fun->ops & OP_WRHEAD) && (fun->headpos >=0) ){
    return FunParamPuti(fun,
			"NAXIS", 2, fun->io, "Number of entries in table", 0);
  }
  else{
    return(0);
  }
}

/*
 *
 * _FunTablePutHeader -- writes binary table header to file
 * used by FunTablePut and sometimes FunFlush (if no Put was done)
 *
 */
#ifdef ANSI_FUNC
int
_FunTablePutHeader(Fun fun)
#else
int _FunTablePutHeader(fun)
	Fun fun;
#endif
{
  /* write table header, if necessary */
  if( !fun->ops ){
    /* if we have no columns yet, try to get some from the assoc. input file */
    if( !fun->ncol ){
      /* if we have a header to copy from, that is */
      if( fun->ifun && fun->ifun->header && fun->ifun->header->table ){
	FunColumnSelect(fun, 0, "copy=select", NULL);
	/* grab the copied merge value  */
	fun->merge = fun->ifun->merge;
	/* and the organization value */
	fun->org = fun->ifun->org;
      }
      /* else we lose */
      else
	return 0;
    }

    /* write the dummy primary header, if necessary */
    _FunPrimaryExtension(fun);

    /* generate the fitsy header for specified columns */
    _FunColumnHeader(fun, fun->cols, fun->ncol);

    /* save file position for later updating */
    fun->headpos = gtell(fun->gio);

    /* write the binary table header */
    ft_headwrite(fun->gio, fun->header);

    /* we just wrote the header */
    fun->ops |= OP_WRHEAD;
  }
  return 1;
}

/*
 *
 * _FunTableRowGet -- get rows from a table
 *
*/
#ifdef ANSI_FUNC
void *
_FunTableRowGet(Fun fun, void *rows, int maxrow, char *plist, int *nrow)
#else
void *_FunTableRowGet(fun, rows, maxrow, plist, nrow)
     Fun fun;
     void *rows;
     int maxrow;
     char *plist;
     int *nrow;
#endif
{
  int i, j, k, l;		/* counters */
  int tival;			/* temp int value */
  int dofilt;			/* true if we can filter */
  int skip;			/* bytes to skip to end of extension */
  int *rbuf;			/* valid row flags */
  int rowbufsize;		/* alloc'ed output buffer size */
  int maxbufsize;		/* max alloc'ed output buffer size */
  int smax;			/* max elements in sbuf */
  int pad=sizeof(double);	/* double boundary pad bytes */
  size_t get;			/* number of rows to read */
  size_t got;			/* number of rows read */
  double *sbuf=NULL;		/* temp buffer for scaling */
  unsigned char *vheap;		/* vheap space for output vector allocation */
  unsigned char *rawbase=NULL;	/* base pointer into rawbuf */
  unsigned char *rawptr;	/* current pointer into rawbuf */
  unsigned char *rowbase=NULL;	/* base pointer to output row buf */
  unsigned char *rowptr;	/* current pointer into output row buf */
  char *rawoff;			/* temp pointer to input */
  char *rowoff;			/* temp pointer to output */
  char *pbuf;			/* temp plist buffer */
  char tbuf[SZ_LINE];		/* ever-present temp buffer */
  static char paint[SZ_LINE];	/* paint mode (like zhtools) */
  static char debug[SZ_LINE];
  static int rconvert;

  /* init result */
  *nrow = 0;
  rbuf = NULL;
  
  /* just in case ... */
  if( !_FunValid(fun) ){
    gerror(stderr, "invalid funtools handle\n");
    return NULL;
  }

  /* if we delayed the open before, we have to open now */
  if( !fun->header && strchr(fun->mode, 'r') )
    _FunFITSOpen(fun, fun->fname, "r");

  /* make sure something good happened */
  if( !fun->header || !fun->header->table ){
    gerror(stderr, "not a binary table\n");
    return NULL;
  }

  /* check plist values: convert, mask ... */
  if( fun->iconvert < 0 ){
    rconvert = 0;
    fun->transparent = 0;
    pbuf = xstrdup(plist);
    if( _FunKeyword(pbuf, "convert", NULL, tbuf, SZ_LINE) ){
      fun->iconvert = istrue(tbuf);
      if( fun->iconvert != is_bigendian() ){
	rconvert = 1;
      }
    }
    else{
      fun->iconvert = (fun->endian != is_bigendian());
    }
    if( _FunKeyword(pbuf, "mask", "FUN_MASK", tbuf, SZ_LINE) ){
      if( !strcasecmp(tbuf, "transparent") )
	fun->transparent = 1;
    }
    if( _FunKeyword(pbuf, "paint", "FILTER_PAINT", tbuf, SZ_LINE) ){
      strncpy(paint, tbuf, SZ_LINE);
    }
    else{
      *paint = '\0';
    }
    if( _FunKeyword(pbuf, "debug", "FILTER_DEBUG", tbuf, SZ_LINE) ){
      strncpy(debug, tbuf, SZ_LINE);
    }
    else{
      *debug = '\0';
    }
    if( pbuf ) xfree(pbuf);
  }

  /* check for no more row to read in this file */
  if( !fun->left ){
    /* if possible, set up to read next file in list */
    if((fun->head) && (fun->head->ltype != LIST_NONE) && fun->head->current){
      fun->head->current = fun->next;
      /* if there is a next file, set it up */
      if(  fun->head->current ){
	/* rewind save file pointers */
	rewind(fun->head->current->lefp);
	rewind(fun->head->current->lrfp);
	/* and if we read no rows but we have set up a next file,
	   try again immediately */
	return FunTableRowGet(fun->head, rows, maxrow, plist, nrow);
      }
    }
    return NULL;
  }

  /* open the filter, if its not already been done */
  if( !fun->filt ){
    if( fun->filter && *fun->filter ){
      /* create the mode string */
      if( fun->endian != is_bigendian() )
	strncpy(tbuf, "convert=true", SZ_LINE-1);
      else
	strncpy(tbuf, "convert=false", SZ_LINE-1);
      /* add the binning key */
      if( fun->bincols ){
	strncat(tbuf, ",", SZ_LINE-1);
	strncat(tbuf, fun->bincols, SZ_LINE-1);
      }
      /* add the index flag */
      if( fun->idx == 1 ){
	strncat(tbuf, ",idx=true", SZ_LINE-1);
      }
      else if( fun->idx == -1 ){
	strncat(tbuf, ",idx=false", SZ_LINE-1);
      }
      /* add paint mode */
      if( *paint ){
	strncat(tbuf, ",", SZ_LINE-1);
	strncat(tbuf, "paint=", SZ_LINE-1);
	strncat(tbuf, paint, SZ_LINE-1);
      }
      /* add debug flag */
      if( *debug ){
	strcat(tbuf, ",");
	strcat(tbuf, "debug=");
	strcat(tbuf, debug);
      }
      fun->filt = FilterOpen(fun->header, fun->filter, tbuf);
      if( fun->filt == NULL ){
	gerror(stderr, "can't open filter\n");
	return NULL;
      }
      else if( fun->filt == NOFILTER ){
	fun->dofilt = 0;
      }
    }
    else{
      fun->filt = NOFILTER;
      fun->dofilt = 0;
    }
  }

  /* sanity check (after setup, to allow setup without actually reading) */
  if( maxrow <=0 ) return NULL;

  /* allocate output buffer, if necessary */
  if( rows ){
    rowbase = rows;
  }
  else{
    maxbufsize = fun->maxbufsize;
    rowbufsize = fun->rowsize + pad;
    /* we might have to allocate space for pointers as well */
    for(j=0; j<fun->ncol; j++){
      if( fun->cols[j]->mode & COL_PTR ){
	if( fun->cols[j]->poff && (fun->cols[j]->mode & COL_READ) ){
	  gerror(stderr,
	  "FunTableRowGet: [poff] not yet supported for allocated pointers\n");
	}
	rowbufsize += (fun->cols[j]->width + pad);
      }
    }
    /* ensure that we don't try to allocate too large a buffer, or we might
       swap our brains out and then die anyway */
    if( maxrow * rowbufsize > maxbufsize ){
      if( rowbufsize > maxbufsize )
	maxrow = 1;
      else
	maxrow = maxbufsize / rowbufsize;
    }
    if( maxrow <= 0 ) maxrow = 1;
    /* don't call xcalloc (which has internal error checking) but instead
       call system calloc and handle failure by halving the row number */
    while( !(rowbase = (unsigned char *)malloc(maxrow*rowbufsize+pad)) ){
      maxrow /= 2;
      if( maxrow <= 0 ){
	gerror(stderr, "can't allocate row buffer\n");
	return NULL;
      }
    }
    /* clear buffer */
    memset(rowbase, 0, maxrow*rowbufsize+pad);
    /* the vheap for pointers starts after row buffer */
    vheap = rowbase + (maxrow * fun->rowsize);
    /* make sure its on a double boundary */
    vheap += (pad - ((long)vheap % pad));
    /* set up the allocated pointers in each record */
    for(j=0; j<fun->ncol; j++){
      if( fun->cols[j]->mode & COL_PTR ){
	for(i=0; i<maxrow; i++){
	  rowptr = rowbase + (i*fun->rowsize);
	  *((unsigned char **)(rowptr+fun->cols[j]->offset)) = vheap;
	  vheap += fun->cols[j]->width + (pad - (fun->cols[j]->width % pad));
	  if( vheap >= (rowbase + (maxrow*rowbufsize+pad)) ){
	    gerror(stderr, "internal funtools error: vheap too small\n");
	    return NULL;
	  }
	}
      }
    }
  }

  /* free previously saved buffer of raw input data */
  if( fun->rawbuf ) xfree(fun->rawbuf);
  /* allocate space for a pile of row records */
  if( fun->left > 0 )
    fun->rawbufsize = MIN(maxrow, fun->left);
  /* this is the case where we read til EOF */
  else
    fun->rawbufsize = maxrow;
  if( !(fun->rawbuf=(unsigned char *)xmalloc(fun->rawsize*fun->rawbufsize)) ||
      !(rbuf = (int *)xmalloc(sizeof(int)*fun->rawbufsize)) ){
    gerror(stderr, "can't allocate row buffer\n");
    return NULL;
  }

  /* allocate a scaling buffer for the largest input or output column */
  smax = -1;
  for(j=0; j<fun->ncol; j++){
    smax = MAX(fun->cols[j]->n, smax);
  }
  if( smax >0 )
    sbuf = (double *)malloc(smax*sizeof(double));

  /* seed output pointer to start of output buffer */
  rowptr = rowbase;

  /* while there are still rows to read and we have not read the max ... */ 
  while( (fun->left != 0) && (*nrow < maxrow) ){
    /* figure out how many to read this time */
    if( fun->left > 0 )
      get = MIN(maxrow - *nrow, fun->left);
    /* read to EOF */
    else
      get = maxrow - *nrow;
    /* read in a pile of rows AFTER the currently saved raw rows */
    rawbase = fun->rawbuf + (*nrow * fun->rawsize);
    _FunRead(fun, (void *)rawbase, fun->rawsize, get, &got);
    /* if we got what we asked for ... */
    if( got == get ){
      /* if we are reading a set number of rows ... */
      if( fun->left > 0 ){
	/* subtract the number we just got */
	fun->left -= got;
	/* just in case! */
	if( fun->left < 0 )
	  fun->left = 0;
      }
    }
    /* didn't get what we asked for ... */
    else{
      /* if we are reading a set number of rows ... */
      if( fun->left > 0 ){
	/* using an index => already filtering, so fewer rows are acceptable */
	if( (fun->filt == NOFILTER) || (fun->filt->doidx != 1) ){
	  /* otherwise its unexpected */
	  gerror(stderr, "unexpected EOF reading rows\n");
	}
	/* ensure we stop after this iteration */
	fun->left = 0;
      }
      /* if we are reading til EOF, we just got there */
      else{
	fun->left = 0;
      }
    }

    /* filter these rows through the co-process */
    if( fun->dofilt ){
      dofilt = FilterEvents(fun->filt, (void *)rawbase, fun->rawsize, (int)got,
			    (void *)rbuf);
      /* look for error on filter call */
      if( dofilt < 0 ){
	gerror(stderr, "event filter failed\n");
      }
    }
    else{
      dofilt = 0;
    }

    /* process each row in this batch */
    for(rawptr=rawbase, i=0; (size_t)i<got; i++, rawptr += fun->rawsize){
      /* if its not a valid row, skip it */
      if( dofilt && !fun->transparent && !rbuf[i] ){
	continue;
      }
      /* get specified columns, convert to type, and store in output */
      for(j=0; j<fun->ncol; j++){
	/* if this column is not active, or is not to be read, or if its
	   to be taken from the input data, skip it */
	if(!(fun->cols[j]->mode & COL_ACTIVE)	||
	   !(fun->cols[j]->mode & COL_READ)  	||
	    (fun->cols[j]->mode & COL_IBUF)  	)
	  continue;
	/* this is the (input) fitsy column number */
	k = fun->cols[j]->tcol;
	if( fun->cols[j]->mode & COL_PTR )
	  rowoff = *((char **)(((char *)rowptr)+fun->cols[j]->offset));
	else
	  rowoff = (char *)rowptr+fun->cols[j]->offset;
	rowoff += fun->cols[j]->poff;
	/* for soa, rowbuf (i.e. rowbase) is indexed by nrows */
	if( fun->org == FUN_ORG_SOA )
	  rowoff += *nrow * fun->cols[j]->width;
	/* process special values, or data values */
	switch(k){
        /* unknown means mode is "rw" but we don't actually have the col
	   in the input data -- do nothing */
	case COL_UNKNOWN_ID:
	  break;
        /* $region: the special region id */
	case COL_REGION_ID:
	case COL_NUM_ID:
	  if( k == COL_REGION_ID ){
	    /* we either use the return value or -1 (no region filtering) */
	    if( dofilt )
	      tival = rbuf[i];
	    else
	      tival = COL_REGION_ID;
	  }
	  if( k == COL_NUM_ID ){
	    tival = fun->io + i + 1;
	  }
	  /* common code */
	  /* if source and dest type diff, we must do full type convert */
	  if( fun->cols[j]->type != 'J' ){
	    ft_acht2(fun->cols[j]->type, rowoff, 'J', &tival, 1, rconvert, 0);
	  }
	  /* for same types, we might have to swap bytes */
	  else if( rconvert ){
	    swap(rowoff, &tival, ft_sizeof('J'), 'J');
	  }
	  /* otherwise its an easy copy */
	  else{
	    memcpy(rowoff, &tival, ft_sizeof('J'));
	  }
	  break;
        /* data column */
	default:
	  /* this is the offset into the input record */
	  rawoff = (char *)rawptr + fun->header->table->col[k].offset;
	  /* if source and dest type diff or we are scaling the column,
	     we must do full type convert */
	  if( (fun->cols[j]->type != fun->header->table->col[k].type)	||
	       fun->header->table->col[k].scaled			){
	    /* handle bit-field processing specially */
	    if( fun->header->table->col[k].type == 'X' ){
	      int xtype;
	      switch(fun->header->table->col[k].width/fun->cols[j]->n){
	      case 0:
	      case 1:
		xtype = 'B'; break;
	      case 2:
		xtype = 'U'; break;
	      case 4:
		xtype = 'V'; break;
	      default:
		xtype = 'B'; break;
	      }
	      ft_acht2(fun->cols[j]->type, rowoff,
		       xtype, rawoff,
		       fun->cols[j]->n, fun->iconvert, 0);
	    }
	    /* handle scaled input data */
	    else if( fun->header->table->col[k].scaled ){
	      /* convert raw to double */
	      ft_acht2('D', sbuf,
		       fun->header->table->col[k].type, rawoff,
		       fun->cols[j]->n, fun->iconvert, 0);
	      /* calculate scaled value */
	      for(l=0; l<fun->cols[j]->n; l++){
		sbuf[l] = sbuf[l] * fun->header->table->col[k].scale +
		          fun->header->table->col[k].zero;
	      }
	      /* convert double to type */
	      ft_acht2(fun->cols[j]->type, rowoff,
		       'D', sbuf, fun->cols[j]->n, 0, 0);
	      /* mark column as having been scaled */
	      fun->cols[j]->scaled |= FUN_SCALE_APPLIED;
	    }
	    /* handle straight-forward change of type */
	    else{
	      ft_acht2(fun->cols[j]->type, rowoff,
		       fun->header->table->col[k].type, rawoff,
		       fun->cols[j]->n, fun->iconvert, 0);
	    }
	  }
	  /* for same types, we might have to swap bytes */
	  else if( fun->iconvert ){
	    if( fun->header->table->col[k].width >= fun->cols[j]->width ){
	      swap(rowoff, rawoff, fun->cols[j]->width, fun->cols[j]->type);
	    }
	    else{
	      swap(rowoff, rawoff, fun->header->table->col[k].width,
		   fun->cols[j]->type);
	      memset(rowoff+fun->header->table->col[k].width, 0,
		     fun->cols[j]->width-fun->header->table->col[k].width);
	    }
	  }
	  /* otherwise its an easy copy */
	  else{
	    if( fun->header->table->col[k].width >= fun->cols[j]->width ){
	      memcpy(rowoff, rawoff, fun->cols[j]->width);
	    }
	    else{
	      memcpy(rowoff, rawoff, fun->header->table->col[k].width);
	      memset(rowoff+fun->header->table->col[k].width, 0,
		     fun->cols[j]->width-fun->header->table->col[k].width);
	    }
	  }
	  break;
	}
      }
      /* save the input data in rawbuf */
      if( (fun->rawbuf + (*nrow * fun->rawsize)) != rawptr)
	memcpy((fun->rawbuf + (*nrow * fun->rawsize)), rawptr, fun->rawsize);
      /* got another row */
      *nrow += 1;
      /* bump to next output pointer */
      if( fun->org == FUN_ORG_AOS )
	rowptr += fun->rowsize;
    }
    /* update how many input rows we have read */
    fun->io += got;
    fun->bytes += (got*fun->rawsize);
  }

  /* free up space */
  if( rbuf ) xfree(rbuf);
  if( sbuf ) xfree(sbuf); 

  /* we just read a table */
  fun->ops |= OP_RDTABLE;
  /* this is the last file we read in the list */
  if( fun->head )
    fun->head->current = fun;
  else
    fun->current = fun;

  /* if we have no more rows left, clean up */
  if( !fun->left ){
    /* skip to end of extension */
    if( fun->bytes ){
      /* skip any unread rows */
      skip = (fun->rawsize*fun->total) - fun->bytes;
      gskip(fun->gio, (off_t)skip);
      fun->bytes += skip;
      /* skip padding */
      if( (skip = FT_BLOCK - (fun->bytes % FT_BLOCK)) == FT_BLOCK )
	skip = 0;
      gskip(fun->gio, (off_t)skip);
      /* reset io flag */
      /* fun->io = 0; */
    }
    /* close filter if done with the filter process */
    if( fun->filt && (fun->filt != NOFILTER) ){
      FilterClose(fun->filt);
      fun->filt = NULL;
    }
  }

  /* return buffer */
  return rowbase;
}

/*
 *
 * public routines
 *
 */

/*
 *
 * FunTableRowGet -- get rows from a table
 *
*/
#ifdef ANSI_FUNC
void *
FunTableRowGet(Fun fun, void *rows, int maxrow, char *plist, int *nrow)
#else
void *FunTableRowGet(fun, rows, maxrow, plist, nrow)
     Fun fun;
     void *rows;
     int maxrow;
     char *plist;
     int *nrow;
#endif
{
  int got;
  fd_set readfds;		/* read fds for select() */
  char *obuf;
  Fun cur, wrap, mark=NULL;

  /* just in case ... */
  *nrow = 0;
  if( !_FunValid(fun) ){
    gerror(stderr, "invalid data handle\n");
    return NULL;
  }
  
  /* process list type */
  switch(fun->ltype){
  case LIST_NONE:
    return _FunTableRowGet(fun, rows, maxrow, plist, nrow);
  case LIST_FILEORDER:
    return _FunTableRowGet(fun->current, rows, maxrow, plist, nrow);
  case LIST_SORT:
  case LIST_UNSORT:
  case LIST_TRYSORT:
again:
    /* if we are sorting -- look for first valid file before we read */
    if( fun->ltype == LIST_SORT ){
      for(mark=fun->head; mark; mark=mark->next ){
	if( mark->left )
	  break;
      }
      /* if we have no valid files left, we are done */
      if( !mark ){
	return NULL;
      }
      /* if we are sorting and we have current buffers, use one of them */
      else if( mark->save ){
	return _FunTableRestore(mark, rows, nrow);
      }
    }
    /* if we have no more active fds to process, select() for more */
    FD_ZERO(&readfds);
    for(got=0, cur=fun; cur; cur=cur->next){
      if( (cur->ifd >=0) && cur->left ){
	FD_SET(cur->ifd, &readfds);
	got++;
      }
    }
    /* if we have no more files, we are done */
    if( got == 0 ) return NULL;
    /* find out who is ready to be read */
    got = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
    if( got >0 ){
      /* get current starting place in list */
      if( fun->current && fun->current->next )
	cur = fun->current->next;
      else
	cur = fun;
      wrap = cur;
      while( !FD_ISSET(cur->ifd, &readfds) ){
	/* next file in list (with wrap around) */
	if( cur->next )
	  cur = cur->next;
	else
	  cur = fun;
	/* should not happen -- we went through the whole list unsucessfully */
	if( cur == wrap ){
	  return NULL;
	}
      }
      /* read rows */
      obuf = _FunTableRowGet(cur, rows, maxrow, plist, nrow);
      /* return if not sorting, or if we are and this file is current */
      if( fun->ltype != LIST_SORT ){
	return obuf;
      }
      else{
	/* if the currently accessed file is the one we want, return it */
	if( fun->current == mark ){
	  return obuf;
	}
	/* else we must save these rows for when this file is current */
	else{
	  /* store these rows */
	  _FunTableSave(fun->current, rows, obuf, *nrow);
	  /* reset, go back for current */
	  *nrow = 0;
	  goto again;
	}
      }
    }
    else
      return NULL;
  default:
    return NULL;
  }
}

/*
 *
 * FunTableRowPut -- put rows to a table
 *
*/
#ifdef ANSI_FUNC
int
FunTableRowPut(Fun fun, void *rows, int nrow, int idx, char *plist)
#else
int FunTableRowPut(fun, rows, nrow, idx, plist)
     Fun fun;
     void *rows;
     int nrow;
     int idx;
     char *plist;
#endif
{
  int i;
  int ieoff1, ieoff2;
  int to, from;
  int width;
  int convert;
  int shortcut;
  char *rawptr;
  char *oebuf;
  char *rowptr;
  char *pbuf;
  char tbuf[SZ_LINE];
  Fun ifun;

  /* gotta have it */
  if( !_FunValid(fun) )
    return 0;

  /* this extension is a table */
  fun->type = FUN_TABLE;

  /* temp switch this fun's ifun to be the last fun we read in the ifun list */
  /* we need to do this to pick up the correct raw rawbuf */
  if( (ifun = fun->ifun) ){
    if( ifun->current )
      ifun = ifun->current;
  }
  /* no reference fun handle -- rely on the output fun */
  else
    ifun = fun;

  /* we have to convert from native if the data is the big-endian */
  if( fun->oconvert < 0 ){
    pbuf = xstrdup(plist);
    if( _FunKeyword(pbuf, "convert", NULL, tbuf, SZ_LINE) )
      fun->oconvert = istrue(tbuf);
    else
      fun->oconvert = !is_bigendian();
    if( pbuf ) xfree(pbuf);
  }

  /* initialize for writing */
  if( !_FunTablePutHeader(fun) )
    return(0);

  /* exit if we are not actually writing any events */
  if( !nrow )
    return(0);

  /* allocate temp buffer for rows */
  shortcut = 1;
  for(width=0, i=0; i<fun->ncol; i++){
    if( (fun->cols[i]->mode  & COL_ACTIVE)     &&
	(fun->cols[i]->mode  & COL_WRITE)      &&
        !(fun->cols[i]->mode & COL_REPLACEME) ){
      /* if we are writing anything from the user buffer, no shortcuts */
      if( !(fun->cols[i]->mode & COL_IBUF) || (i != fun->cols[i]->from) )
	shortcut = 0;
      /* add to the size of the output row record */
      width += fun->cols[i]->width;
    }
    /* if we are not writing anything from the input buffer, no shortcuts */
    else if( (fun->cols[i]->mode & COL_IBUF) )
      shortcut = 0;
  }

  /* make sure we have something to write */
  if( !width ){
    gerror(stderr, "No columns defined for FunTableRowPut()\n");
  }

  /* if we are just writing the input rows, we can do it much faster */
  if( shortcut ){
    oebuf = (char *)ifun->rawbuf + (idx * ifun->rawsize); 
  }
  /* normally, we have to move all columns to a temp buffer and then write */
  else{
    /* write rows (might have to convert to big_endian) */
    oebuf = xmalloc(width*nrow);
    rowptr = oebuf;
    ieoff1 = idx * ifun->rawsize;
    ieoff2 = 0;
    /* process rows */
    for(i=0; i<nrow; i++){
      /* process columns in a row */
      for(to=0; to<fun->ncol; to++){
	/* skip if necessary */
	if( !(fun->cols[to]->mode & COL_ACTIVE)    ||
	    !(fun->cols[to]->mode & COL_WRITE)     ||
	     (fun->cols[to]->mode & COL_REPLACEME) )
	  continue;
	/* if from value is -1, that means "use value from this column" */
	if( fun->cols[to]->from <0 )
	  from = to;
	/* otherwise we use the value from some other column */
	else
	  from = fun->cols[to]->from;
	/* raw input data */
	if( (fun->cols[from]->mode & COL_IBUF) ){
	  rawptr = (char *)ifun->rawbuf+ieoff1;
	  convert = !ifun->endian;
	}
	/* user data */
	else{
	  rawptr = (char *)rows+ieoff2;
	  convert = fun->oconvert;
	}
	if( fun->cols[from]->mode & COL_PTR ){
	  rawptr = *((char **)(((char *)rawptr)+fun->cols[from]->offset));
	}
	else{
	  rawptr = rawptr + fun->cols[from]->offset;
	}
	rawptr += fun->cols[from]->poff;
	/* for soa, rowbuf (i.e. rowbase) is indexed by nrows */
	if( fun->org == FUN_ORG_SOA )
	  rawptr += i * fun->cols[from]->width;
	/* if source and dest type are different, we do full type convert */
	if( fun->cols[to]->type != fun->cols[from]->type ){
	  /* but try to handle writing int to bit-field specially */
	  if( fun->cols[to]->type == 'X' ){
	    int xtype;
	    switch(fun->cols[to]->width/fun->cols[from]->n){
	    case 0:
	    case 1:
	      xtype = 'B'; break;
	    case 2:
	      xtype = 'U'; break;
	    case 4:
	      xtype = 'V'; break;
	    default:
	      xtype = 'B'; break;
	    }
	    ft_acht2(xtype, rowptr,
		     fun->cols[from]->type, rawptr,
		     fun->cols[from]->n, convert, 1);
	  }
	  else{
	    ft_acht2(fun->cols[to]->type, rowptr, 
		     fun->cols[from]->type, rawptr,
		     fun->cols[from]->n, convert, 1);
	  }
	}
	/* for same types, we might have to swap bytes */
	else if( convert ){
	  swap(rowptr, rawptr, fun->cols[to]->width, fun->cols[to]->type);
	}
	/* otherwise its an easy copy */
	else{
	  memcpy(rowptr, rawptr, fun->cols[to]->width);
	}
	/* bump output pointer */
	rowptr += fun->cols[to]->width;
      }
      /* bump raw and user row pointers */
      ieoff1 += ifun->rawsize;
      if( fun->org == FUN_ORG_AOS )
	ieoff2 += fun->rowsize;
    }
  }

  /* write rows */
  if( gwrite(fun->gio, oebuf, width, nrow) != (size_t)nrow ){
    gerror(stderr, "unexpected error writing rows\n");
    return 0;
  }

  /* update bookkeeping */
  fun->io += nrow;
  fun->bytes += (width*nrow);

  /* free up allocated space */
  if( !shortcut && oebuf ) xfree(oebuf);

  /* we just wrote a table */
  fun->ops |= OP_WRTABLE;

  /* return the number of rows output */
  return nrow;
}

/*
 *
 * FunTableRowSeek -- seek to a specified rows in a table
 *
*/
#ifdef ANSI_FUNC
off_t
FunTableRowSeek(Fun fun, int nrow, char *plist)
#else
off_t FunTableRowSeek(fun, nrow, plist)
     Fun fun;
     int nrow;
     char *plist;
#endif
{
  off_t ipos, opos;

  /* avoid -W unused parameter warning */
  if( 0 ) plist = plist;

  /* gotta have it */
  if( !_FunValid(fun) )
    return (off_t)-1;
  /* get new position */
  ipos = fun->datastart + (fun->rawsize*(nrow-1));
  /* try to set position */
  opos = gseek(fun->gio, ipos, 0);
  /* if successful, reset the byte positon */
  if( opos >= 0 ){
    fun->bytes = opos;
    fun->io = nrow-1;
    fun->left = fun->total - fun->io;
    opos = ((opos - fun->datastart) / fun->rawsize) + 1;
    /* a negative record number only means we started from an odd place */
    if( opos < 0 ) opos = 0;
  }
  /* return results */
  return opos;
}