-
Notifications
You must be signed in to change notification settings - Fork 24
/
text.c
2513 lines (2170 loc) · 78.3 KB
/
text.c
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
/*-----------------------------------------------------------------------*/
/* text.c --- text processing routines for xcircuit */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*-----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /* for isprint() and isdigit() */
#ifndef _MSC_VER
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#endif
/*------------------------------------------------------------------------*/
/* Local includes */
/*------------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
#include <tk.h>
#endif
#include "colordefs.h"
#include "xcircuit.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*------------------------------------------------------------------------*/
/* External Variable definitions */
/*------------------------------------------------------------------------*/
extern Display *dpy;
extern XCWindowData *areawin;
extern Globaldata xobjs;
extern short fontcount;
extern fontinfo *fonts;
extern colorindex *colorlist;
extern char _STR[150];
#ifdef HAVE_CAIRO
extern const char *utf8encodings[][256];
#endif
/* Global value of distance between characters in the font catalog */
short del = 64;
#ifndef TCL_WRAPPER
/*----------------------------------------------------------------------*/
/* Evaluation of expression types in strings (non-Tcl version---these */
/* are PostScript expressions). */
/* */
/* For now, expressions are just copied as-is, without evaluation. */
/* An allocated string is returned, and it is the responsibility of the */
/* calling routine to free it. */
/*----------------------------------------------------------------------*/
char *evaluate_expr(objectptr thisobj, oparamptr ops, objinstptr pinst)
{
UNUSED(thisobj);
UNUSED(pinst);
if (ops->type != XC_EXPR) return NULL;
return strdup(ops->parameter.expr);
}
#endif
/*----------------------------------------------------------------------*/
/* Determine if a label contains a parameter. */
/*----------------------------------------------------------------------*/
Boolean hasparameter(labelptr curlabel)
{
stringpart *chrptr;
for (chrptr = curlabel->string; chrptr != NULL; chrptr = chrptr->nextpart)
if (chrptr->type == PARAM_START)
return True;
return False;
}
/*----------------------------------------------------------------------*/
/* Join selected labels together. */
/*----------------------------------------------------------------------*/
void joinlabels()
{
/* genericptr *genobj; (jdk) */
short *jl;
stringpart *endpart;
/* int tpos; (jdk) */
labelptr dest, source;
if (areawin->selects < 2) {
Wprintf("Not enough labels selected for joining");
return;
}
SetForeground(dpy, areawin->gc, BACKGROUND);
for (jl = areawin->selectlist; jl < areawin->selectlist +
areawin->selects; jl++) {
if (SELECTTYPE(jl) == LABEL) {
dest = SELTOLABEL(jl);
UDrawString(dest, DOFORALL, areawin->topinstance);
for (endpart = dest->string; endpart->nextpart != NULL; endpart =
endpart->nextpart);
break;
}
}
for (++jl; jl < areawin->selectlist + areawin->selects; jl++) {
if (SELECTTYPE(jl) == LABEL) {
source = SELTOLABEL(jl);
UDrawString(source, DOFORALL, areawin->topinstance);
endpart->nextpart = source->string;
for (; endpart->nextpart != NULL; endpart = endpart->nextpart);
free(source);
removep(jl, 0);
reviseselect(areawin->selectlist, areawin->selects, jl);
}
}
SetForeground(dpy, areawin->gc, dest->color);
UDrawString(dest, dest->color, areawin->topinstance);
incr_changes(topobject);
clearselects();
}
/*----------------------------------------------------------------------*/
/* Insert a new segment into a string */
/*----------------------------------------------------------------------*/
stringpart *makesegment(stringpart **strhead, stringpart *before)
{
stringpart *newptr, *lastptr, *nextptr;
newptr = (stringpart *)malloc(sizeof(stringpart));
newptr->data.string = NULL;
if (before == *strhead) { /* insert at beginning */
newptr->nextpart = *strhead;
*strhead = newptr;
}
else { /* otherwise */
for(lastptr = *strhead; lastptr != NULL;) {
nextptr = nextstringpart(lastptr, areawin->topinstance);
if (nextptr == before) {
if (lastptr->type == PARAM_START) {
oparamptr obs = NULL;
char *key = lastptr->data.string;
obs = find_param(areawin->topinstance, key);
if (obs == NULL) {
Wprintf("Error: Bad parameter \"%s\"!", key);
} else {
obs->parameter.string = newptr; /* ?? */
}
}
else {
lastptr->nextpart = newptr;
}
newptr->nextpart = nextptr;
break;
}
else if (lastptr->nextpart == before && lastptr->type == PARAM_START) {
lastptr->nextpart = newptr;
newptr->nextpart = before;
break;
}
lastptr = nextptr;
}
}
return newptr;
}
/*----------------------------------------------------------------------*/
/* Split a string across text segments */
/*----------------------------------------------------------------------*/
stringpart *splitstring(int tpos, stringpart **strtop, objinstptr localinst)
{
int locpos, slen;
stringpart *newpart, *ipart;
ipart = findstringpart(tpos, &locpos, *strtop, localinst);
if (locpos > 0) { /* split the string */
newpart = makesegment(strtop, ipart);
newpart->type = TEXT_STRING;
newpart->data.string = ipart->data.string;
slen = strlen(newpart->data.string) - locpos;
ipart->data.string = (u_char *)malloc(slen + 1);
strncpy(ipart->data.string, newpart->data.string + locpos, slen + 1);
*(newpart->data.string + locpos) = '\0';
}
else newpart = ipart;
return newpart;
}
/*----------------------------------------------------------------------*/
/* Get the next string part, linking to a parameter if necessary */
/*----------------------------------------------------------------------*/
stringpart *nextstringpartrecompute(stringpart *strptr, objinstptr thisinst)
{
stringpart *nextptr = strptr->nextpart;
if (strptr->type == PARAM_START)
nextptr = linkstring(thisinst, strptr, TRUE);
else if (strptr->type == PARAM_END) {
strptr->nextpart = NULL;
/* Parameters that have a non-NULL entry in data have */
/* been promoted from an expression or numerical value */
/* to a string. The memory allocated for this string */
/* should be free'd. */
if (strptr->data.string != (u_char *)NULL) {
fprintf(stderr, "Non-NULL data in PARAM_END segment\n");
free(strptr->data.string);
strptr->data.string = (u_char *)NULL;
}
}
return nextptr;
}
/*----------------------------------------------------------------------*/
/* Same as the above routine, but don't recompute expression parameters */
/* when encountered. Use the previously generated result. */
/*----------------------------------------------------------------------*/
stringpart *nextstringpart(stringpart *strptr, objinstptr thisinst)
{
stringpart *nextptr = strptr->nextpart;
if (strptr->type == PARAM_START)
nextptr = linkstring(thisinst, strptr, FALSE);
else if (strptr->type == PARAM_END) {
strptr->nextpart = NULL;
if (strptr->data.string != (u_char *)NULL) {
fprintf(stderr, "Non-NULL data in PARAM_END segment\n");
free(strptr->data.string);
strptr->data.string = (u_char *)NULL;
}
}
return nextptr;
}
/*----------------------------------------------------------------------*/
/* Remove a string part from the string */
/*----------------------------------------------------------------------*/
stringpart *deletestring0(stringpart *dstr, stringpart **strtop, objinstptr thisinst,
Boolean domerge)
{
stringpart *strptr = NULL, *nextptr;
char *key;
oparamptr ops;
if (dstr == *strtop)
*strtop = dstr->nextpart;
else {
strptr = *strtop;
while (strptr != NULL) {
nextptr = nextstringpart(strptr, thisinst);
if (nextptr == dstr) break;
strptr = nextptr;
}
if (strptr == NULL)
return NULL;
/* If this is the begining of a parameter, then we have to figure */
/* out if it's an instance or a default, and change the pointer */
/* to the parameter in the parameter list, accordingly. */
else if ((strptr->type == PARAM_START) && (thisinst != NULL)) {
key = strptr->data.string;
ops = find_param(thisinst, key);
if (ops == NULL) {
Fprintf(stderr, "Error in deletestring: Bad parameter %s found\n", key);
}
else {
switch(ops->type) {
case XC_STRING:
ops->parameter.string = dstr->nextpart;
break;
case XC_EXPR:
/* Deleting an expression result can only result */
/* in bad things happening. */
return NULL;
default:
/* What to be done here? */
break;
}
}
}
/* If this is the end of a parameter, we have to link the */
/* PARAM_START, not the PARAM_END, which has already been nulled. */
else if (strptr->type == PARAM_END) {
for (strptr = *strtop; strptr != NULL; strptr = strptr->nextpart) {
if (strptr->nextpart == dstr) {
strptr->nextpart = dstr->nextpart;
break;
}
}
}
else
strptr->nextpart = dstr->nextpart;
}
if (dstr->type == TEXT_STRING)
free(dstr->data.string);
free(dstr);
/* attempt to merge, if legal, and requested */
if (strptr && domerge)
mergestring(strptr);
return strptr;
}
/*----------------------------------------------------------------------*/
/* deletestring() is a wrapper for deletestring0() */
/*----------------------------------------------------------------------*/
stringpart *deletestring(stringpart *dstr, stringpart **strtop, objinstptr thisinst)
{
return deletestring0(dstr, strtop, thisinst, TRUE);
}
/*----------------------------------------------------------------------*/
/* Merge string parts at boundary, if parts can be legally merged */
/* If the indicated string part is text and the part following the */
/* indicated string part is also text, merge the two. The indicated */
/* string part is returned, and the following part is freed. */
/* */
/* (Fixes thanks to Petter Larsson 11/17/03) */
/*----------------------------------------------------------------------*/
stringpart *mergestring(stringpart *firststr)
{
stringpart *nextstr = NULL;
if (firststr) nextstr = firststr->nextpart;
if (nextstr != NULL) {
if (firststr->type == TEXT_STRING && nextstr->type == TEXT_STRING) {
firststr->nextpart = nextstr->nextpart;
firststr->data.string = (char *)realloc(firststr->data.string,
1 + strlen(firststr->data.string) + strlen(nextstr->data.string));
strcat(firststr->data.string, nextstr->data.string);
free(nextstr->data.string);
free(nextstr);
}
}
return firststr;
}
/*----------------------------------------------------------------------*/
/* Link a parameter to a string */
/* If compute_exprs is TRUE, then we should recompute any expression */
/* parameters encountered. If FALSE, then we assume that all */
/* expressions have been computed previously, and may use the recorded */
/* instance value. */
/* */
/* 11/20/06---changed to allow two different static strings to save */
/* promoted results. This is necessary because we may be comparing */
/* two promoted results in, e.g., stringcomprelaxed(), and we don't */
/* want to overwrite the first result with the second. */
/*----------------------------------------------------------------------*/
stringpart *linkstring(objinstptr localinst, stringpart *strstart,
Boolean compute_exprs)
{
char *key;
stringpart *tmpptr, *nextptr = NULL;
static stringpart *promote[2] = {NULL, NULL};
static unsigned char pidx = 0;
oparamptr ops;
if (strstart->type != PARAM_START) return NULL;
key = strstart->data.string;
/* In case of no calling instance, always get the default from the */
/* current page object. */
if (localinst == NULL) {
ops = match_param(topobject, key);
if (ops == NULL)
return NULL;
}
else {
ops = find_param(localinst, key);
if (ops == NULL) {
/* We get here in cases where the object definition is being read, */
/* and there is no instance of the object to link to. In that */
/* case, we ignore parameters and move on to the next part. */
return strstart->nextpart;
}
}
if (ops->type != XC_STRING) {
if (promote[pidx] == NULL) {
/* Generate static string for promoting numerical parameters */
tmpptr = makesegment(&promote[pidx], NULL);
tmpptr->type = TEXT_STRING;
tmpptr = makesegment(&promote[pidx], NULL);
tmpptr->type = PARAM_END;
}
else {
if (promote[pidx]->data.string != NULL) {
free(promote[pidx]->data.string);
promote[pidx]->data.string = NULL;
}
}
/* Promote numerical type to string */
if (ops->type == XC_INT) {
promote[pidx]->data.string = (char *)malloc(13);
sprintf(promote[pidx]->data.string, "%12d", ops->parameter.ivalue);
nextptr = promote[pidx++];
}
else if (ops->type == XC_FLOAT) {
promote[pidx]->data.string = (char *)malloc(13);
sprintf(promote[pidx]->data.string, "%g", (double)(ops->parameter.fvalue));
nextptr = promote[pidx++];
}
else { /* ops->type == XC_EXPR */
oparamptr ips;
if (!compute_exprs && (ips = match_instance_param(localinst, key))
!= NULL && (ips->type == XC_STRING)) {
nextptr = ips->parameter.string;
promote[pidx]->data.string = NULL;
}
else {
promote[pidx]->data.string = evaluate_expr(((localinst == NULL) ?
topobject : localinst->thisobject), ops, localinst);
if (promote[pidx]->data.string != NULL)
nextptr = promote[pidx++];
else
nextptr = NULL;
}
}
pidx &= 0x1; /* pidx toggles between 0 and 1 */
}
else
nextptr = ops->parameter.string;
/* If the parameter exists, link the end of the parameter back to */
/* the calling string. */
if (nextptr != NULL) {
tmpptr = nextptr;
while (tmpptr->type != PARAM_END)
if ((tmpptr = tmpptr->nextpart) == NULL)
return NULL;
tmpptr->nextpart = strstart->nextpart;
return nextptr;
}
return NULL;
}
/*----------------------------------------------------------------------*/
/* Find the last font used prior to the indicated text position */
/*----------------------------------------------------------------------*/
int findcurfont(int tpos, stringpart *strtop, objinstptr thisinst)
{
stringpart *curpos;
int cfont = -1;
stringpart *strptr;
curpos = findstringpart(tpos, NULL, strtop, thisinst);
for (strptr = strtop; (strptr != NULL) && (strptr != curpos);
strptr = nextstringpart(strptr, thisinst))
if (strptr->type == FONT_NAME)
cfont = strptr->data.font;
return cfont;
}
/*----------------------------------------------------------------------*/
/* Return a local position and stringpart for the first occurrence of */
/* "substring" in the the indicated xcircuit string. If non-NULL, */
/* "locpos" is set to the position of the substring in the stringpart, */
/* or -1 if the text was not found. Text cannot cross stringpart */
/* boundaries (although this should be allowed). */
/*----------------------------------------------------------------------*/
stringpart *findtextinstring(char *search, int *locpos, stringpart *strtop,
objinstptr localinst)
{
stringpart *strptr = strtop;
char *strstart;
for (strptr = strtop; strptr != NULL; strptr = nextstringpart(strptr, localinst)) {
if ((strptr->type == TEXT_STRING) && strptr->data.string) {
strstart = strstr(strptr->data.string, search);
if (strstart != NULL) {
if (locpos != NULL)
*locpos = (int)(strstart - (char *)strptr->data.string);
return strptr;
}
}
}
if (locpos != NULL) *locpos = -1;
return NULL;
}
/*----------------------------------------------------------------------*/
/* Return a local position and stringpart for "tpos" positions into */
/* the indicated string. Position and stringpart are for the character */
/* or command immediately preceding "tpos" */
/*----------------------------------------------------------------------*/
stringpart *findstringpart(int tpos, int *locpos, stringpart *strtop,
objinstptr localinst)
{
stringpart *strptr = strtop;
int testpos = 0, tmplen;
for (strptr = strtop; strptr != NULL; strptr = nextstringpart(strptr, localinst)) {
if ((strptr->type == TEXT_STRING) && strptr->data.string) {
tmplen = strlen(strptr->data.string);
if (testpos + tmplen > tpos) {
if (locpos != NULL) *locpos = (tpos - testpos);
return strptr;
}
else testpos += tmplen - 1;
}
if (locpos != NULL) *locpos = -1;
if (testpos >= tpos) return strptr;
testpos++;
}
return NULL;
}
/*----------------------------------------------------------------------*/
/* The following must be in an order matching the "Text string part */
/* types" defined in xcircuit.h. */
/*----------------------------------------------------------------------*/
static char *nonprint[] = {
"Text", "Subscript", "Superscript", "Normalscript",
"Underline", "Overline", "Noline",
"Tab_Stop", "Tab_Forward", "Tab_Backward",
"Halfspace", "Quarterspace", "<Return>",
"Font", "Scale", "Color", "Margin_Stop", "Kern",
"Parameter", ">", "Net_Name", "Error", NULL}; /* (jdk) */
/* Handling of certain text escapes (subscript, superscript, underline, */
/* and overline) in TeX (added by Fabian Inostroza) */
static char *nonprinttex[] = {
"", "_{", "^{", "}",
"\\underline{", "\\overline{", "}",
"Tab_Stop", "Tab_Forward", "Tab_Backward",
"Halfspace", "Quarterspace", "<Return>",
"Font", "Scale", "Color", "Margin_Stop", "Kern",
"Parameter", ">", "Net_Name", "Error", NULL};
/*----------------------------------------------------------------------*/
/* charprint(): */
/* Write a printable version of the character or command at the */
/* indicated string part and position. */
/*----------------------------------------------------------------------*/
void charprint(char *sout, stringpart *strptr, int locpos)
{
char sc;
switch (strptr->type) {
case TEXT_STRING:
if (strptr->data.string) {
if (locpos > (int) strlen(strptr->data.string)) {
strcpy(sout, "<ERROR>");
}
else sc = *(strptr->data.string + locpos);
if (isprint(sc))
sprintf(sout, "%c", sc);
else
sprintf(sout, "/%03o", (u_char)sc);
}
else
*sout = '\0';
break;
case FONT_NAME:
sprintf(sout, "Font=%s", (strptr->data.font >= fontcount) ?
"(unknown)" : fonts[strptr->data.font].psname);
break;
case FONT_SCALE:
sprintf(sout, "Scale=%3.2f", strptr->data.scale);
break;
case KERN:
sprintf(sout, "Kern=(%d,%d)", strptr->data.kern[0], strptr->data.kern[1]);
break;
case PARAM_START:
sprintf(sout, "Parameter(%s)<", strptr->data.string);
break;
default:
strcpy(sout, nonprint[strptr->type]);
break;
}
}
/*----------------------------------------------------------------------*/
/* Version of the above, for printing LaTeX strings */
/* added by Fabian Inostroza 7/14/2013 */
/*----------------------------------------------------------------------*/
void charprinttex(char *sout, stringpart *strptr, int locpos)
{
char sc;
switch (strptr->type) {
case TEXT_STRING:
if (strptr->data.string) {
if (locpos > (int) strlen(strptr->data.string)) {
strcpy(sout, "<ERROR>");
}
else sc = *(strptr->data.string + locpos);
if (isprint(sc))
sprintf(sout, "%c", sc);
else
sprintf(sout, "/%03o", (u_char)sc);
}
else
*sout = '\0';
break;
default:
*sout = '\0';
break;
}
}
/*----------------------------------------------------------------------*/
/* Print a string (allocates memory for the string; must be freed by */
/* the calling routine). */
/*----------------------------------------------------------------------*/
char *xcstringtostring(stringpart *strtop, objinstptr localinst, Boolean textonly)
{
stringpart *strptr;
int pos = 0, locpos;
char *sout;
sout = (char *)malloc(1);
sout[0] = '\0';
while ((strptr = findstringpart(pos++, &locpos, strtop, localinst)) != NULL) {
if (!textonly || strptr->type == TEXT_STRING) {
charprint(_STR, strptr, locpos);
sout = (char *)realloc(sout, strlen(sout) + strlen(_STR) + 1);
strcat(sout, _STR);
}
/* Overbar on schematic names is translated to logical-NOT ("!") */
else if (textonly && strptr->type == OVERLINE) {
sout = (char *)realloc(sout, strlen(sout) + 2);
strcat(sout, "!");
}
}
return sout;
}
/*----------------------------------------------------------------------*/
/* Version of the above, for printing LaTeX strings */
/* added by Fabian Inostroza 7/14/2013 */
/*----------------------------------------------------------------------*/
char *textprinttex(stringpart *strtop, objinstptr localinst)
{
stringpart *strptr;
int pos = 0, locpos;
char *sout;
sout = (char *)malloc(1);
sout[0] = '\0';
while ((strptr = findstringpart(pos++, &locpos, strtop, localinst)) != NULL) {
charprinttex(_STR, strptr, locpos);
sout = (char *)realloc(sout, strlen(sout) + strlen(_STR) + 1);
strcat(sout, _STR);
}
return sout;
}
/*----------------------------------------------------------------------*/
/* Wrappers for xcstringtostring(): */
/* stringprint() includes information on text controls appropriate */
/* for printing in the message window (charreport()) */
/*----------------------------------------------------------------------*/
char *stringprint(stringpart *strtop, objinstptr localinst)
{
return xcstringtostring(strtop, localinst, False);
}
/*----------------------------------------------------------------------*/
/* textprint() excludes text controls, resulting in a string */
/* appropriate for netlist information, or for promoting a string */
/* parameter to a numeric type. */
/*----------------------------------------------------------------------*/
char *textprint(stringpart *strtop, objinstptr localinst)
{
return xcstringtostring(strtop, localinst, True);
}
/*----------------------------------------------------------------------*/
/* textprintsubnet() is like textprint(), except that strings in bus */
/* notation are reduced to just the single subnet. */
/*----------------------------------------------------------------------*/
char *textprintsubnet(stringpart *strtop, objinstptr localinst, int subnet)
{
char *newstr, *busptr, *endptr, *substr;
newstr = xcstringtostring(strtop, localinst, True);
if (subnet >= 0) {
busptr = strchr(newstr, areawin->buschar);
if (busptr != NULL) {
endptr = find_delimiter(busptr);
if (endptr != NULL) {
if (busptr == newstr)
sprintf(newstr, "%d", subnet);
else {
substr = strdup(newstr);
busptr++;
sprintf(substr + (int)(busptr - newstr), "%d%s", subnet, endptr);
free(newstr);
return substr;
}
}
}
else {
/* Promote a non-bus label to a bus label */
substr = malloc(10 + strlen(newstr));
strcpy(substr, newstr);
endptr = substr;
while ((*endptr) != '\0') endptr++;
sprintf(endptr, "%c%d%c", areawin->buschar, subnet,
standard_delimiter_end(areawin->buschar));
free(newstr);
return substr;
}
}
return newstr;
}
/*----------------------------------------------------------------------*/
/* Another variant: Print a subnet list according to the entries in */
/* a netlist (Genericlist *) pointer. This includes the condition in */
/* which the list is a single wire, not a bus. If "pinstring" is non- */
/* NULL, it will be used as the name of the subnet. Otherwise, */
/* "prefix" will be used, and will be appended with the net ID of the */
/* first net in the sublist to make the identifier unique. */
/*----------------------------------------------------------------------*/
char *textprintnet(char *prefix, char *pinstring, Genericlist *sublist)
{
char *newstr, *sptr;
buslist *sbus;
int i;
UNUSED(pinstring);
if (sublist->subnets == 0) {
newstr = (char *)malloc(strlen(prefix) + 10);
sprintf(newstr, "%s%d", prefix, sublist->net.id);
}
else { /* just make a comma-separated list */
newstr = (char *)malloc(strlen(prefix) + 20 + 3 * sublist->subnets);
sbus = sublist->net.list;
sprintf(newstr, "%s%d%c", prefix, sbus->netid, areawin->buschar);
for (i = 0; i < sublist->subnets; i++) {
sbus = sublist->net.list + i;
sptr = newstr + strlen(newstr);
if (i != 0)
strcat(sptr++, ",");
sprintf(sptr, "%d", sbus->subnetid);
}
sptr = newstr + strlen(newstr);
sprintf(sptr, "%c", standard_delimiter_end(areawin->buschar));
}
return newstr;
}
/*----------------------------------------------------------------------*/
/* Test equivalence of the text string parts of a label with the */
/* indicated char * string. */
/* */
/* Return 0 if the strings match. Return 1 or the result of strcmp() */
/* on the first non-matching substring text segment if the strings */
/* don't match. */
/* */
/* If "exact" is True, requires an exact match, otherwise requires */
/* that the label only match text to the length of text. */
/*----------------------------------------------------------------------*/
int textcompx(stringpart *string, char *text, Boolean exact, objinstptr localinst)
{
stringpart *strptr;
char *tptr = text;
char *sptr;
int rval;
size_t llen = strlen(text), slen;
Boolean has_text = FALSE;
for (strptr = string; strptr != NULL; strptr = nextstringpart(strptr, localinst)) {
if (strptr->type == TEXT_STRING) {
has_text = TRUE;
sptr = strptr->data.string;
slen = min(strlen(sptr), llen);
llen -= slen;
if (!exact && (rval = strncmp(sptr, tptr, slen)))
return rval;
else if (exact && (rval = strcmp(sptr, tptr)))
return rval;
else if (!exact && (llen == 0))
return 0;
else
tptr += slen;
}
}
/* Check condition that no text was encountered in the xcircuit string */
return ((llen > 0) && !has_text) ? 1 : 0;
}
/*----------------------------------------------------------------------*/
/* Wrappers for textcompx(), equivalent to strcmp() and strncmp(). */
/*----------------------------------------------------------------------*/
int textcomp(stringpart *string, char *text, objinstptr localinst)
{
return textcompx(string, text, True, localinst);
}
/*----------------------------------------------------------------------*/
int textncomp(stringpart *string, char *text, objinstptr localinst)
{
return textcompx(string, text, False, localinst);
}
/*----------------------------------------------------------------------*/
/* Test equivalence of two label strings */
/*----------------------------------------------------------------------*/
int stringcomp(stringpart *string1, stringpart *string2)
{
stringpart *strptr1, *strptr2;
for (strptr1 = string1, strptr2 = string2; strptr1 != NULL && strptr2 != NULL;
strptr1 = strptr1->nextpart, strptr2 = strptr2->nextpart) {
if (strptr1->type != strptr2->type)
return 1;
else {
switch (strptr1->type) {
case TEXT_STRING:
if (strptr1->data.string && strptr2->data.string) {
if (strcmp(strptr1->data.string, strptr2->data.string))
return 1;
}
else if (strptr1->data.string || strptr2->data.string)
return 1;
break;
case FONT_SCALE:
if (strptr1->data.scale != strptr2->data.scale) return 1;
break;
case FONT_COLOR:
if (strptr1->data.color != strptr2->data.color) return 1;
break;
case FONT_NAME:
if (strptr1->data.font != strptr2->data.font) return 1;
break;
case KERN:
if (strptr1->data.kern[0] != strptr2->data.kern[0] ||
strptr1->data.kern[1] != strptr2->data.kern[1]) return 1;
break;
}
}
}
/* One string continues after the other ends. . . */
if (strptr1 != NULL || strptr2 != NULL) return 1;
return 0;
}
/*----------------------------------------------------------------------*/
/* Test if the specified font is in the "Symbol" font family. */
/*----------------------------------------------------------------------*/
Boolean issymbolfont(int fontnumber)
{
if (!strcmp(fonts[fontnumber].family, "Symbol")) return True;
return False;
}
/*----------------------------------------------------------------------*/
/* Test if the specified font is in the "Helvetica" font family. */
/* SVG uses this to make sure it scales down the font by 7/8 to match */
/* our internal vectors, and to use "oblique" for the style instead of */
/* "italic". */
/*----------------------------------------------------------------------*/
Boolean issansfont(int fontnumber)
{
if (!strcmp(fonts[fontnumber].family, "Helvetica")) return True;
return False;
}
/*----------------------------------------------------------------------*/
/* Test if the specified font is ISO-Latin1 encoding */
/*----------------------------------------------------------------------*/
Boolean isisolatin1(int fontnumber)
{
if ((fonts[fontnumber].flags & 0xf80) == 0x100) return True;
return False;
}
/*----------------------------------------------------------------------*/
/* For a label representing a single bus subnet, return the index of */
/* the subnet. */
/*----------------------------------------------------------------------*/
int sub_bus_idx(labelptr thislab, objinstptr thisinst)
{
stringpart *strptr;
char *busptr;
int busidx;
for (strptr = thislab->string; strptr != NULL; strptr =
nextstringpart(strptr, thisinst)) {
if (strptr->type == TEXT_STRING) {
if ((busptr = strchr(strptr->data.string, areawin->buschar)) != NULL) {
if (sscanf(++busptr, "%d", &busidx) == 1)
return busidx;
}
if (sscanf(strptr->data.string, "%d", &busidx) == 1)
return busidx;
}
}
return -1;
}
/*----------------------------------------------------------------------*/
/* The following routine is like sub_bus_idx but returns TRUE or FALSE */
/* depending on whether the label was determined to be in bus notation */
/* or not. Note that sub_bux_idx may be run on sub-bus names (those */
/* that are internally generated from labels in bus notation), but */
/* pin_is_bus should not, because pin numbers in bus notation get the */
/* bus delimiters stripped from them. */
/*----------------------------------------------------------------------*/
Boolean pin_is_bus(labelptr thislab, objinstptr thisinst)
{
stringpart *strptr;
char *busptr;
/* int busidx; (jdk) */
Boolean found_delimiter = FALSE;
for (strptr = thislab->string; strptr != NULL; strptr =
nextstringpart(strptr, thisinst)) {
if (strptr->type == TEXT_STRING) {
if ((busptr = strchr(strptr->data.string, areawin->buschar)) != NULL) {
if (isdigit(*(++busptr)))
return TRUE;
else
found_delimiter = TRUE;
}
else if (found_delimiter == TRUE) {
return (isdigit(*(strptr->data.string))) ? TRUE : FALSE;
}
}
}
return FALSE;
}
/*----------------------------------------------------------------------*/
/* When encountering a label with bus notation, create a list of */
/* subnets belonging to the bus. Return the list as a pointer to a */
/* Genericlist structure. This structure is statically allocated and */
/* is expected to have its contents copied into the target netlist */
/* element. */
/* */
/* Unlike the above routine, this routine prints the original string */
/* into a char* array using textprint() so that escape sequences are */
/* removed and will not affect the result. */
/* */
/* To speed things up, the calling routine should have already called */
/* pin_is_bus() to determine if the label does indeed represent a bus. */
/* break_up_bus() is much slower in determining this. If break_up_bus */