File: ffprint.c

package info (click to toggle)
ncbi-tools6 6.1.20170106%2Bdfsg1-0%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 468,492 kB
  • sloc: ansic: 1,474,204; pascal: 6,740; cpp: 6,248; xml: 3,390; sh: 2,137; perl: 1,084; csh: 508; makefile: 427; ruby: 93; lisp: 81
file content (1168 lines) | stat: -rw-r--r-- 27,493 bytes parent folder | download | duplicates (12)
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
/*   ffprint.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
* ===========================================================================
*
* File Name:  ffprint.c
*
* Author:  Karl Sirotkin, Tom Madden, Tatiana Tatusov
*
* Version Creation Date:   7/15/95
*
* $Revision: 6.8 $
*
* File Description: 
*
* Modifications:  
* --------------------------------------------------------------------------
 * $Log: ffprint.c,v $
 * Revision 6.8  2003/07/15 14:35:56  dondosha
 * Added #defines for substitutes to fprintf and fflush, needed for gzip compression of Web BLAST results
 *
 * Revision 6.7  2002/08/26 22:06:57  kans
 * ff_RecalculateLinks (MS) to fix hotlink artifact
 *
 * Revision 6.6  1999/10/07 19:22:21  bazhin
 * Changed Int2 to Int4 for one variable. To prevent coredump.
 *
 * Revision 6.5  1999/10/06 20:25:28  bazhin
 * Removed memory leak.
 *
 * Revision 6.4  1999/08/31 14:36:06  tatiana
 * ff_print_string_mem() added
 *
 * Revision 6.3  1999/04/09 21:17:18  bazhin
 * Added functions "FFBSPrint()" and "BSAppend()" to parse ASN.1
 * data to ByteStore.
 *
 * Revision 6.2  1999/04/01 20:44:13  kans
 * Int2 lengths to Int4 to allow CountGapsInDeltaSeq with buffer > 32K
 *
 * Revision 6.1  1999/03/12 17:47:11  tatiana
 * file was lost
 *
 * Revision 6.7  1999/02/02 17:29:20  kans
 * added ff_MergeString
 *
 * Revision 6.6  1998/06/15 15:00:31  tatiana
 * UNIX compiler warnings fixed
 *
 * Revision 6.5  1998/02/26 22:43:46  madden
 * Used init_buff_ex in ff_StartPrint
 *
 * Revision 6.4  1998/02/19 01:54:51  ostell
 * added use of Thread Local Storage to make functions thread safe
 *
 * Revision 6.3  1997/10/23 20:21:31  madden
 * Fixed ByteStore leak
 *
 * Revision 6.2  1997/10/23 20:03:51  tatiana
 * allocates byte store in init_buf_ex()
 *
 * Revision 6.1  1997/09/16 16:56:04  tatiana
 * buffer reinit in ff_StartPrint
 *
 * Revision 6.0  1997/08/25 18:05:32  madden
 * Revision changed to 6.0
 *
 * Revision 5.8  1997/08/04 22:56:21  tatiana
 * init_buff_ex() added
 *
 * Revision 5.7  1997/07/18 20:11:16  vakatov
 * Restored NLM_EXTERNs which were lost in R5.5
 *
 * Revision 5.6  1997/07/16 21:24:12  tatiana
 * add AddPintLater
 *
Revision 5.3  1997/02/25  22:08:59  tatiana
ff_AddStringWithTildes(0 will print out "`~" as real tilde

Revision 5.2  1997/01/08  18:52:45  madden
Added LIBCALL's.

Revision 5.1  1996/09/10  14:26:56  tatiana
init_buff() added to ff_StartPrint

Revision 4.8  1996/04/15  18:44:41  tatiana
free_buff() added

Revision 4.4  1996/01/29  22:41:20  tatiana
ChangeStringWithTildes function added

Revision 4.3  1995/12/13  21:04:47  kans
descriptors now store entityID/itemID/itemtype in proper array

Revision 4.2  1995/12/13  16:34:06  tatiana
itemID added to FFPrintArray structure

Revision 4.1  1995/11/17  21:28:35  kans
asn2ff now uses gather (Tatiana)

Revision 1.5  1995/07/17  19:33:20  kans
parameters combined into Asn2ffJobPtr structure
*
* ==========================================================================
*/

#include <asn2ffp.h>
#include <ffprint.h>
#include <ncbithr.h>

int (*ff_fprintf)(FILE*, const char *, ...) = fprintf;
int (*ff_fflush)(FILE*) = fflush;

#define fprintf ff_fprintf
#define fflush ff_fflush

static TNlmTls ffprint_tls = NULL;

static void free_buff_contents(BuffStructPtr bfp)
{
	if (bfp == NULL) return;
	bfp->buffer = MemFree(bfp->buffer);
	bfp->line_prefix = MemFree(bfp->line_prefix);
	bfp->line_return = MemFree(bfp->line_return);
	if (bfp->byte_sp)
		bfp->byte_sp = BSFree(bfp->byte_sp);
	return;
}

static void BuffStructCleanup (TNlmTls tls, VoidPtr ptr)
{
	BuffStructPtr bfp;

	bfp = (BuffStructPtr)ptr;
	free_buff_contents(bfp);
	MemFree(bfp);
	return;
}


static BuffStruct PNTR NEAR GetBuffStruct ( void )
{
	BuffStructPtr bfp = NULL;

	if (NlmTlsGetValue( ffprint_tls, (VoidPtr *)(&bfp)))
	{
		if (bfp == NULL)
		{
			bfp = MemNew(sizeof(BuffStruct));
			NlmTlsSetValue(&ffprint_tls, bfp, BuffStructCleanup);
		}
	}
	return bfp;
}

/******************************************************************************
*
*	print utilities to handle the printing of the asn2ff system.
*	
*****************************************************************************/
NLM_EXTERN void LIBCALL init_buff(void)
{
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	bfp->buffer = (CharPtr) MemNew((MAX_BTP_BUF+2)*sizeof(Char));
	bfp->line_prefix = (CharPtr) MemNew(3*sizeof(Char));
	bfp->newline = NEWLINE;
}

NLM_EXTERN void LIBCALL init_buff_ex(Int2 init_size)
{
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	bfp->buffer = (CharPtr) MemNew((init_size+2)*sizeof(Char));
	bfp->line_prefix = (CharPtr) MemNew(3*sizeof(Char));
	bfp->newline = NEWLINE;
	bfp->byte_sp = BSNew(init_size+2);
}

NLM_EXTERN void LIBCALL free_buff(void)
{
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	free_buff_contents (bfp);
	return;
}

/***********************************************************************
*void asn2ff_set_output (FILE * fp, CharPtr line_return)
*
**************************************************************************/

NLM_EXTERN void LIBCALL asn2ff_set_output (FILE *fp, CharPtr line_return)
{
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	bfp->fp = fp;
	if (line_return)
	{
		if(bfp->line_return != NULL)
			MemFree(bfp->line_return);
		bfp->line_return = StringSave(line_return);
	}
}

NLM_EXTERN CharPtr LIBCALL ff_MergeString (void)

{
	ByteStorePtr byte_sp=NULL;
	CharPtr string=NULL;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	if (! bfp->fp)
	{
		byte_sp = bfp->byte_sp;

		string = BSMerge(byte_sp, NULL);
		bfp->byte_sp = BSFree(bfp->byte_sp);
	}

	return string;
}

NLM_EXTERN CharPtr LIBCALL FFPrint (FFPrintArrayPtr pap, Int4 index, Int4 pap_size)
{
	GBEntryPtr gbp;
	Asn2ffJobPtr ajp;
	ByteStorePtr byte_sp=NULL;
	CharPtr string=NULL;
	FFPrintArray pa=pap[index];
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	gbp = pa.gbp;
	ajp = pa.ajp;
	if (ajp == NULL) {
		return NULL;
	}
	ajp->pap_index = pa.index;
	ajp->pap_last = pa.last;

	if (index == 0 || index == (pap_size-1))
	{
		flat2asn_delete_locus_user_string();
		flat2asn_install_locus_user_string(gbp->locus);
		flat2asn_delete_accession_user_string();
		flat2asn_install_accession_user_string(gbp->accession);
	}

	bfp->n_links = 0;
	pa.fct(ajp, gbp);
	if (pa.descr == NULL) {
		pap[index].descr = gbp->descr;
	}
	if (pa.printxx == PRINTXX) {
		PrintXX();
	}
	if (! bfp->fp) {
		byte_sp = bfp->byte_sp;
		string = BSMerge(byte_sp, NULL);
		bfp->byte_sp = BSFree(bfp->byte_sp);
	}

	return string;
}

/**********************************************************/
static ByteStorePtr BSAppend(ByteStorePtr to, ByteStorePtr from)
{
    Nlm_BSUnitPtr tobup;
    Nlm_BSUnitPtr frombup;

    if(from == NULL)
        return(to);

    if(to == NULL)
    {
        to = MemNew(sizeof(Nlm_ByteStore));
        to->totlen = 0;
        to->chain = NULL;
    }

    to->seekptr = 0;
    to->chain_offset = 0;

    if(to->chain != NULL)
        for(tobup = to->chain; tobup->next != NULL; tobup = tobup->next)
            continue;
    else
        tobup = NULL;

    for(frombup = from->chain; frombup != NULL; frombup = frombup->next)
    {
        to->totlen += frombup->len;
        if(tobup == NULL)
        {
            to->chain = MemNew(sizeof(Nlm_BSUnit));
            tobup = to->chain;
        }
        else
        {
            tobup->next = MemNew(sizeof(Nlm_BSUnit));
            tobup = tobup->next;
        }
        tobup->str = MemNew(frombup->len);
        MemCpy(tobup->str, frombup->str, frombup->len);
        tobup->len = frombup->len;
        tobup->len_avail = frombup->len_avail;
    }
    to->curchain = to->chain;
    return(to);
}

/**********************************************************/
NLM_EXTERN void LIBCALL FFBSPrint(FFPrintArrayPtr pap, Int4 index,
                                  Int4 pap_size)
{
    GBEntryPtr    gbp;
    Asn2ffJobPtr  ajp;
    ByteStorePtr  byte_sp = NULL;
    FFPrintArray  pa = pap[index];
    BuffStructPtr bfp;

    bfp = GetBuffStruct();

    gbp = pa.gbp;
    ajp = pa.ajp;
    if(ajp == NULL)
        return;

    ajp->pap_index = pa.index;
    ajp->pap_last = pa.last;

    if(index == 0 || index == pap_size - 1)
    {
        flat2asn_delete_locus_user_string();
        flat2asn_install_locus_user_string(gbp->locus);
        flat2asn_delete_accession_user_string();
        flat2asn_install_accession_user_string(gbp->accession);
    }

    bfp->n_links = 0;
    pa.fct(ajp, gbp);
    if(pa.descr == NULL)
        pap[index].descr = gbp->descr;

    if(pa.printxx == PRINTXX)
    {
        PrintXX();
    }

    ajp->byte_st = BSAppend(ajp->byte_st, bfp->byte_sp);
    BSFree(bfp->byte_sp);
    bfp->byte_sp = NULL;
}

NLM_EXTERN void LIBCALL ff_print_string (FILE *fp, CharPtr string, CharPtr line_return)

{
	CharPtr s;
	Int2	i, l;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	if (! fp) {
		return;
	}
	if (! string || *string == *line_return) {
		ErrPostEx(SEV_WARNING, 1, 1, 
			"CAUTION: NULL String in ff_print_string\n\n");
		return;
	}
	for (s = string; (s = strchr(s, *line_return)) != NULL; s++) {
		*s = '\n';
	}
	for (i = 0, s = string; i < bfp->n_links; i++) {
		l = string - s + bfp->pos_links[i];
		if (s[l-1] == '\n') {
			for (; IS_WHITESP(s[l]); l++) ;
			fwrite(s, 1, l, fp);
		} else {
			fwrite(s, 1, l, fp);
		}
		fwrite(bfp->links[i], 1, StringLen(bfp->links[i]), fp);
		MemFree(bfp->links[i]);
		s += l;
	}
	fwrite(s, 1, StringLen(s), fp);
	BuffFree();
}

static CharPtr ff_print_insert(CharPtr str, CharPtr ins)
{
	CharPtr buff;
	
	buff = (CharPtr) MemNew(StringLen(str) + StringLen(ins) + 1);
	StringCpy(buff, ins);
	StringCat(buff, str);
	MemFree(str);
	return buff;
}

NLM_EXTERN CharPtr LIBCALL ff_print_string_mem (CharPtr string)

{
	CharPtr s, buff, bu;
	Int2	i, l;
	Int4	ll;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	if (! string) {
		ErrPostEx(SEV_WARNING, 1, 1, 
			"CAUTION: NULL String in ff_print_string\n\n");
		return NULL;
	}
	ll = StringLen(string);
	for (i = 0, s = string; i < bfp->n_links; i++) {
		ll += StringLen(bfp->links[i]);
	}
	buff = (CharPtr) MemNew(ll+1);
	bu = buff;
	for (i = 0, s = string; i < bfp->n_links; i++) {
		l = string - s + bfp->pos_links[i];
		if (s[l-1] == '\n') {
			for (; IS_WHITESP(s[l]); l++) ;
		}
		MemCpy(bu, s, l);
		s += l;
		bu += l;
		StringCpy(bu, bfp->links[i]);
		bu += StringLen(bfp->links[i]);
		MemFree(bfp->links[i]);
	}
	StringCpy(bu, s);
	BuffFree();
	MemFree(string);
	return buff;
}

NLM_EXTERN Int2 LIBCALL ff_StartPrint (Int2 init_indent, Int2 cont_indent, Int2 line_max, CharPtr line_prefix)

{

	CharPtr buffer; 
	Int2 indent_space;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	buffer = bfp->buffer;
	if (buffer == NULL) {
		if (line_max > MAX_BTP_BUF)
			init_buff_ex(line_max);
		else
			init_buff();
		buffer = bfp->buffer;
	}

	bfp->init_indent = init_indent;
	bfp->cont_indent = cont_indent;

	if (line_max > 0)
		bfp->line_max = line_max;
	else
		bfp->line_max = MAX_BTP_BUF;

	if (line_prefix) {
		StringCpy(bfp->line_prefix, line_prefix);
		*buffer = *line_prefix;
		buffer++;
		line_prefix++;
		*buffer = *line_prefix;
		buffer++;
		line_prefix++;
		indent_space = init_indent - StringLen(bfp->line_prefix);
	} else {
		bfp->line_prefix[0] = '\0'; 
		indent_space = init_indent;
	}

	if (indent_space > 0) {
		MemSet((VoidPtr) buffer, ' ', indent_space);
	}
	return init_indent;
}	/* ff_StartPrint */
		
NLM_EXTERN void LIBCALL ff_AddString (CharPtr string)

{
	Char newline;
	CharPtr buffer;
	Int2 increment_string=0, index, length_b/*, length_s -- UNUSED */, line_max;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	newline = bfp->newline;
	line_max = bfp->line_max;

	if (string == NULL)
		return;

	buffer = CheckBufferState(&increment_string, string[0]);

	if (buffer == NULL)
		return;

	length_b = StringLen(bfp->buffer);

	string += increment_string;
	index = length_b;
	while ((*buffer = *string) != '\0')
	{	
		if (*string == newline)
		{
			*buffer = '\0';
			*string = ' ';
			NewContLine();
			string++;
			ff_AddString(string);
			break;
		}
		else
		{
			buffer++;
			string++;
			index++;
			if (index == line_max)
			{ /* return value for ff_AddString??????? */
				ff_AddString(string);
				break;
			}
		}
	}

/*	length_s = index - length_b; -- NO EFFECT */

	return;
}	/* ff_AddString */	

NLM_EXTERN void LIBCALL AddLink (CharPtr str)
{
	CharPtr 	buffer;
	Int2 		increment_string=0;
	Int4		l;
	CharPtr PNTR buf_links;
	Int4 PNTR	buf_pos;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	
	buffer = CheckBufferState(&increment_string, str[0]);
	if (buffer == NULL) {
		return;
	}
	l = (bfp->byte_sp == NULL) ? 0 : BSLen(bfp->byte_sp);
	if (bfp->n_links >= bfp->buf_n_links) {
		buf_pos = MemNew((bfp->buf_n_links + LINKS)*sizeof(buf_pos[0]));
		buf_links = MemNew((bfp->buf_n_links + LINKS)*sizeof(buf_links[0]));
		if (bfp->buf_n_links > 0) {
			MemCpy(buf_pos, bfp->pos_links, bfp->buf_n_links*sizeof(buf_pos[0]));
			MemFree(bfp->pos_links);
			MemCpy(buf_links, bfp->links, bfp->buf_n_links*sizeof(buf_links[0]));
			MemFree(bfp->links);
		}
		bfp->pos_links = buf_pos;
		bfp->links = buf_links;
		bfp->buf_n_links += LINKS;
	}
	bfp->pos_links[bfp->n_links] = (buffer - bfp->buffer + l);
	bfp->links[bfp->n_links] = StringSave(str);
	bfp->n_links++;
	return;
}

NLM_EXTERN void LIBCALL AddLinkLater (CharPtr str, Int2 prevlen)
{
	CharPtr 	buffer;
	Int2 		increment_string=0;
	Int4		l;
	CharPtr PNTR buf_links;
	Int4 PNTR	buf_pos;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	
	buffer = CheckBufferState(&increment_string, str[0]);
	if (buffer == NULL) {
		return;
	}
	l = (bfp->byte_sp == NULL) ? 0 : BSLen(bfp->byte_sp);
	if (bfp->n_links >= bfp->buf_n_links) {
		buf_pos = MemNew((bfp->buf_n_links + LINKS)*sizeof(buf_pos[0]));
		buf_links = MemNew((bfp->buf_n_links + LINKS)*sizeof(buf_links[0]));
		if (bfp->buf_n_links > 0) {
			MemCpy(buf_pos, bfp->pos_links, bfp->buf_n_links*sizeof(buf_pos[0]));
			MemFree(bfp->pos_links);
			MemCpy(buf_links, bfp->links, bfp->buf_n_links*sizeof(buf_links[0]));
			MemFree(bfp->links);
		}
		bfp->pos_links = buf_pos;
		bfp->links = buf_links;
		bfp->buf_n_links += LINKS;
	}
	bfp->pos_links[bfp->n_links] = buffer - bfp->buffer + l - prevlen;
	bfp->links[bfp->n_links] = StringSave(str);
	bfp->n_links++;
	return;
}

NLM_EXTERN void LIBCALL ff_AddChar (Char character)
{
	CharPtr buffer;
	Int2 increment_string=0;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	if (character == NULLB)
		return;

	if (character == bfp->newline)
	{
		NewContLine();
	}
	else
	{
		buffer = CheckBufferState(&increment_string, character);
		*buffer = character;
	}

	return ;
}	/* ff_AddChar */

NLM_EXTERN void LIBCALL PrintXX(void)
{
	ff_StartPrint(0, 0, ASN2FF_EMBL_MAX, "XX");
	ff_EndPrint();
}

NLM_EXTERN void LIBCALL ff_AddInteger (CharPtr fmt, long integer)
{
	Char buffer[10];

	/* Only one integer read in	*/
	sprintf(buffer, fmt, integer);
	ff_AddString(buffer);
	return;
}	/* ff_AddInteger */
		
/***********************************************************************
*void ff_AddStringWithTildes (CharPtr string)
*
*	This function prints out a string and replaces the tiles ("~")
*	by new lines (by calling NewContLine).  Before this function is
*	called, printing must be initialized by calling ff_StartPrint;
*	afterwards ff_EndPrint must be called!
*	
*	Use accent grave (ASCII 96) "`~" to print out real tilde
*
* Can't this be rewritten to use ff_AddString????  That would be faster! 
************************************************************************/

NLM_EXTERN void LIBCALL ff_AddStringWithTildes (CharPtr string)
{
/* One "~" is a  new line, "~~" or "~~ ~~" means 2 returns */       

	while (*string != '\0') {
		if (*string == '`' && *(string+1) == '~') {
			ff_AddChar('~');
			string += 2;
		} else if (*string == '~') {
			NewContLine();
			string++;
			if (*string == '~') {
				NewContLine();
				string++;
				if (*string == ' ' && *(string+1) == '~' && *(string+2) == '~')
					string += 3;
             }
		} else if (*string == '\"') {   
			*string = '\'';
			ff_AddChar(*string);
			string++;
		} else {  
			ff_AddChar(*string);
			string++;
		}
	}

}	/* ff_AddStringWithTildes */
	
NLM_EXTERN void LIBCALL ChangeStringWithTildes (CharPtr string)
{
                while (*string != '\0') {
                /* One "~" is a line return, "~~" or "~~ ~~" means 2 returns */       
					if (*string == '~') {
						*string = '\n';
						string++;
						if (*string == '~') {
							*string = '\n';
							string++;
							if (*string == ' ' && *(string+1) == '~' &&
                                 		*(string+2) == '~') {
								string += 3;
							}
                        }
					} else if (*string == '\"') {
						*string = '\'';
						string++;
                    } else {  
						string++;
                    }
                }

}	/* ChangeStringWithTildes */
	
NLM_EXTERN void LIBCALL ff_RecalculateLinks(Int4 indent) {
    BuffStructPtr bfp = GetBuffStruct();
    Int4 len = (bfp->byte_sp == NULL) ? 0 : BSLen(bfp->byte_sp);
    Int2 i;

    for ( i = bfp->n_links - 1; i >= 0; --i ) {
        if ( bfp->pos_links[i] + 1 >= len ) { 
            bfp->pos_links[i] += indent;
        } else {
            break;
        }
    }
}

NLM_EXTERN CharPtr LIBCALL CheckBufferState(Int2Ptr increment_string, Char next_char)

{
	CharPtr buffer, line_prefix;
	CharPtr buf_ptr_comma=NULL, buf_ptr_dash=NULL, buf_ptr_space=NULL, 
	buf_ptr_start, ptr_index;
	CharPtr temp_ptr, temp_ptr_start;
	Char temp[MAX_TO_RT_SIDE+1];
	Int2 length, cont_indent, indent_space; 
	Int2 line_max, line_index;
	BuffStructPtr bfp;
    Int2 IndentSize = 0;

	bfp = GetBuffStruct();
	cont_indent = bfp->cont_indent;
	line_max = bfp->line_max;

	*increment_string = 0;
	temp_ptr = temp_ptr_start = temp;

	buffer = bfp->buffer;
	length=StringLen(buffer);
	
	if (length < bfp->line_max) {
		return buffer+length;
	} else if (length == bfp->line_max && next_char == '\0') {
		return buffer+length;
	} else {
		if (StringLen(bfp->line_prefix) > 0) {
			line_prefix = bfp->line_prefix;
			indent_space = cont_indent - StringLen(line_prefix);
		} else {
			line_prefix = NULL;
			indent_space = cont_indent;
		}

		if (next_char == ' ') {
			buf_ptr_space = buffer+line_max+1;
		} else {
			for (ptr_index=buffer+line_max, line_index=line_max-cont_indent; 
				ptr_index>buffer+line_max-MAX_TO_RT_SIDE &&
					line_index > 0;
						ptr_index--, line_index--) {
				if (ptr_index[0] == ' ') {
					buf_ptr_space = ptr_index;
					break;
				}
			}
		
			for (ptr_index=buffer+line_max, line_index=line_max-cont_indent; 
				ptr_index>buffer+line_max-MAX_TO_RT_SIDE &&
					line_index > 0;
						ptr_index--, line_index--) {
				if (ptr_index[0] == ',') {
					buf_ptr_comma = ptr_index;
/* Add one on the next line, otherwise the comma is lost, as space is. */
					buf_ptr_comma++;
					break;
				}
			}
			for (ptr_index=buffer+line_max, line_index=line_max-cont_indent; 
				ptr_index>buffer+line_max-MAX_TO_RT_SIDE &&
					line_index > 0;
						ptr_index--, line_index--) {
				if (ptr_index[0] == '-') {
				 /* Don't put "-" on next line! */
					buf_ptr_dash = ptr_index + 1;
					break;
				}
			}
		}

		if (next_char != ' ' &&
			(buf_ptr_space || buf_ptr_comma || buf_ptr_dash)) {
			if (buf_ptr_space) {
				buf_ptr_start = buf_ptr_space;
				buf_ptr_space++;
				while((*temp_ptr = *buf_ptr_space) != '\0') {
					temp_ptr++;
					buf_ptr_space++;
				}
				buf_ptr_start[0] = '\0';
				if (*(buf_ptr_start-1) == ' ') {
				/* If there are two spaces in a row */
					buf_ptr_start--;
					buf_ptr_start[0] = '\0';
				}
			} else if (buf_ptr_comma) {
				buf_ptr_start = buf_ptr_comma;
/* Check if a space follows the comma; if so, delete it; otherwise it shows
up on the next line and really looks stupid.			*/
				if (*buf_ptr_comma == ' ') {
					buf_ptr_comma++;
				}
				while((*temp_ptr = *buf_ptr_comma) != '\0') {
					temp_ptr++;
					buf_ptr_comma++;
				}
				buf_ptr_start[0] = '\0';
			} else if (buf_ptr_dash) {
				buf_ptr_start = buf_ptr_dash;
				while((*temp_ptr = *buf_ptr_dash) != '\0') {
					temp_ptr++;
					buf_ptr_dash++;
				}
				buf_ptr_start[0] = '\0';
			}
            
            
			FlushBuffer();
			if (line_prefix != NULL) {
				*buffer = *line_prefix;
				buffer++;
				line_prefix++;
				*buffer = *line_prefix;
				buffer++;
				line_prefix++;
			}
			if (indent_space > 0)
				MemSet((VoidPtr) buffer, ' ', indent_space);
			buffer += indent_space;
            IndentSize = buffer - bfp->buffer + 1;
			temp_ptr = temp_ptr_start;
			while((*buffer = *temp_ptr) != '\0') {
				temp_ptr++;
				buffer++;
			}
		    
            ff_RecalculateLinks(IndentSize);
			return buffer;
		} else if (next_char == ' ') {
			FlushBuffer();
			if (line_prefix) {
				*buffer = *line_prefix;
				buffer++;
				line_prefix++;
				*buffer = *line_prefix;
				buffer++;
				line_prefix++;
			}
			if (indent_space-1 > 0) {
				MemSet((VoidPtr) buffer, ' ', indent_space-1);
                ff_RecalculateLinks(buffer+indent_space-1 - bfp->buffer);
				return buffer+indent_space-1;
			} else if (indent_space-1 == 0) {
			/* if there is one space indentation! */
                ff_RecalculateLinks(buffer - bfp->buffer);
				return buffer;
			} else if (indent_space-1 < 0) {
			/* if there is zero space indentation! */
				*increment_string = 1;
                ff_RecalculateLinks(buffer - bfp->buffer);
				return buffer;
			}
		} else { 
			FlushBuffer();
			if (line_prefix) {
				*buffer = *line_prefix;
				buffer++;
				line_prefix++;
				*buffer = *line_prefix;
				buffer++;
				line_prefix++;
			}
			if (indent_space > 0) {
				MemSet((VoidPtr) buffer, ' ', indent_space);
			}
            ff_RecalculateLinks(buffer +indent_space - bfp->buffer);
			return buffer+indent_space;
		}
	}
	return buffer;	/* never used!  Only put in to make CodeWarrior happy*/
}	/* CheckBufferState */

NLM_EXTERN Int2 LIBCALL NewContLine (void)

{
	Int2 cont_indent, indent_space;
	CharPtr buffer, line_prefix=NULL;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	cont_indent = bfp->cont_indent;

	FlushBuffer();
	buffer=bfp->buffer;

	if (StringLen(bfp->line_prefix) > 0)
	{
		line_prefix = bfp->line_prefix;
		indent_space = cont_indent - StringLen(line_prefix);
	}
	else
	{
		indent_space = cont_indent;
	}

	if (line_prefix)
	{
		*buffer = *line_prefix;
		buffer++;
		line_prefix++;
		*buffer = *line_prefix;
		buffer++;
		line_prefix++;
	}

	if (indent_space > 0)
		MemSet((VoidPtr) buffer, ' ', indent_space);
	return cont_indent;
}	/* NewContLine */


NLM_EXTERN Int2 LIBCALL TabToColumn (Int2 column)

{
	CharPtr start_buffer, buffer;
	Int2 increment_string=0, length;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	start_buffer = bfp->buffer;

	if ((length=StringLen(start_buffer)) > column)
		return -1;

	buffer = CheckBufferState(&increment_string, '\0');

	if ((column-length-1) > 0)
		MemSet((VoidPtr) buffer, ' ', (column-length-1));
		
	return length;
}

NLM_EXTERN void LIBCALL ff_EndPrint (void)

{
	FlushBuffer();
}

NLM_EXTERN void LIBCALL FlushBuffer (void)

{
	ByteStorePtr byte_sp=NULL;
	CharPtr line_ret;
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	line_ret = bfp->line_return;

	if (bfp->fp)
	{
		fflush(bfp->fp);
		fprintf(bfp->fp, "%s\n", bfp->buffer);
	}
	else
	{
		byte_sp=bfp->byte_sp;
		if (! byte_sp) {
			bfp->byte_sp = byte_sp = BSNew(MAX_BTP_BUF);
		}
		BSWrite(byte_sp, bfp->buffer, StringLen(bfp->buffer));
		BSWrite(byte_sp, line_ret, StringLen(line_ret));
	}
	MemSet((VoidPtr) bfp->buffer, '\0', bfp->line_max);
}


/**************************************************************************
*CheckEndPunctuation
*
*	This code checks to ensure that the last character of a string
*	is the character passed.  Any extra spaces or tabs at the end of
*	the string are eliminated.
*
**************************************************************************/

NLM_EXTERN CharPtr LIBCALL CheckEndPunctuation (CharPtr string, Char end)

{
	CharPtr stringptr, newstring;
	Int4 length;

	if (string == NULL)
		return NULL;

	length = StringLen(string);
	newstring = (CharPtr) MemNew((length+2)*sizeof(Char));

	if (length > 0)
	{
		newstring = StringCpy(newstring, string);
		for (stringptr=newstring+length-1; stringptr > newstring; stringptr--)
		{
			if (*stringptr == ' ' || *stringptr == '\t' || *stringptr == '~')
			{
				*stringptr = '\0';
			}
			else
			{
				break;
			}
		}
	
		if (*stringptr != end)
		{
			stringptr++;
			*stringptr = end;
			stringptr++;
			*stringptr = '\0';
		}	
	} 
	else
	{
		newstring[0] = end;
		newstring[1] = '\0';
	}

	return newstring;
}

NLM_EXTERN void LIBCALL BuffFree(void)
{
	BuffStructPtr bfp;

	bfp = GetBuffStruct();
	bfp->buffer[0] = '\0';
	bfp->n_links = 0;

}

NLM_EXTERN CharPtr LIBCALL ReportPrint (FFPrintArrayPtr pap, Int4 index, Int4 pap_size)

{
	GBEntryPtr gbp;
	Asn2ffJobPtr ajp;
	ByteStorePtr byte_sp=NULL;
	CharPtr string=NULL;
	FFPrintArray pa=pap[index];
	BuffStructPtr bfp;

	bfp = GetBuffStruct();

	gbp = pa.gbp;
	ajp = pa.ajp;
	ajp->pap_index = pa.index;
	ajp->pap_last = pa.last;

	if (index == 0 || index == (pap_size-1))
	{
		flat2asn_delete_locus_user_string();
		flat2asn_install_locus_user_string(gbp->locus);
		flat2asn_delete_accession_user_string();
		flat2asn_install_accession_user_string(gbp->accession);
	}

	pa.fct(ajp, gbp);

	if (! bfp->fp)
	{
		byte_sp = bfp->byte_sp;

		string = BSMerge(byte_sp, NULL);
		bfp->byte_sp = BSFree(bfp->byte_sp);
	}

	return string;
}

/*****************************************************************************
*Boolean DoSpecialLineBreak (BiotablePtr btp, CharPtr string, Int2 indent)
*
*	Look for line breaks in special cases, such as KEYWORDS, when
*	line breaks are only allowed after semi-colons.
*
*	"length" is reset to zero if string is NULL, "indent" tells how
*	many spaces the line is indented from the left side.
*****************************************************************************/
 
NLM_EXTERN Boolean LIBCALL DoSpecialLineBreak (CharPtr string, Int2 indent)

{
	Boolean retval;
	static Int2 length=0;

	if (string == NULL)
	{
		length = 0;
		retval = FALSE;
	}
	else
	{
		length += StringLen(string);
		length += 2;
		if (length < (ASN2FF_GB_MAX-indent))
		{
			retval = FALSE;
		}
		else
		{
			length = StringLen(string);
			length += 2;
			retval = TRUE;
		}
	}
	return retval;

}	/* DoSpecialLineBreak */