1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */ 2: /* $Header: B1tex.c,v 1.1 84/06/28 00:48:59 timo Exp $ */ 3: 4: /* B texts */ 5: #include "b.h" 6: #include "b1obj.h" 7: #include "B1tlt.h" /* for Cts */ 8: 9: Visible value mk_text(m) string m; { 10: value v; intlet len= strlen(m); 11: v= grab_tex(len); 12: strcpy(Str(v), m); 13: return v; 14: } 15: 16: Visible bool character(v) value v; { 17: if (Is_text(v) && Length(v) == 1) return Yes; 18: else return No; 19: } 20: 21: Visible char charval(v) value v; { 22: if (!Is_text(v) || Length(v) != 1) error("value not a character"); 23: return *Str(v); 24: } 25: 26: Visible string strval(v) value v; { 27: return Str(v); 28: } 29: 30: Visible value concat(s, t) value s, t; { 31: value c; 32: if (s->type != Tex) error("in t^u, t is not a text"); 33: else if (t->type != Tex) error("in t^u, t is a text, but u is not"); 34: c= grab_tex(Length(s)+Length(t)); 35: strcpy(Str(c), Str(s)); strcpy(Str(c)+Length(s), Str(t)); 36: return c; 37: } 38: 39: Hidden Procedure concato(s, t) value *s; string t; { 40: if ((*s)->type != Tex) error("attempt to join text with non-text"); 41: xtndtex(s, strlen(t)); 42: strcat(Str(*s), t); 43: } 44: 45: Visible value trim(v, B, C) value v; intlet B, C; { 46: intlet len= Length(v), k; value w; 47: string vp= Str(v)+B, wp; 48: if (v->type != Tex) error("trim (@ or |) applied to non-text"); 49: if (B < 0 || C < 0 || B+C > len) 50: error("trim (@ or |) out of bounds"); 51: w= grab_tex(len-=(B+C)); wp= Str(w); 52: Overall *wp++= *vp++; *wp= '\0'; 53: return w; 54: } 55: 56: Visible value repeat(x, y) value x, y; { 57: value r; intlet i= propintlet(intval(y)); intlet xl= Length(x), p, q; 58: string rp, xp; 59: if (x->type != Tex) error("in t^^n, t is not a text"); 60: if (i < 0) error("in t^^n, n is negative"); 61: r= grab_tex(propintlet(i*xl)); rp= Str(r); 62: for (p= 0; p < i; p++) { 63: xp= Str(x); 64: for (q= 0; q < xl; q++) *rp++= *xp++; 65: } 66: *rp= '\0'; 67: return r; 68: } 69: 70: #define Left 'L' 71: #define Right 'R' 72: #define Centre 'C' 73: 74: Hidden value adj(x, y, side) value x, y; literal side; { 75: value r, v= convert(x, Yes, Yes); int i= intval(y); 76: intlet lv= Length(v), la, k, ls, rs; 77: string rp, vp; 78: la= propintlet(i) - lv; 79: if (la <= 0) return v; 80: r= grab_tex(lv+la); rp= Str(r); vp= Str(v); 81: 82: if (side == Left) { ls= 0; rs= la; } 83: else if (side == Centre) { ls= la/2; rs= (la+1)/2; } 84: else { ls= la; rs= 0; } 85: 86: for (k= 0; k < ls; k++) *rp++= ' '; 87: for (k= 0; k < lv; k++) *rp++= *vp++; 88: for (k= 0; k < rs; k++) *rp++= ' '; 89: *rp= 0; 90: release(v); 91: return r; 92: } 93: 94: Visible value adjleft(x, y) value x, y; { 95: return adj(x, y, Left); 96: } 97: 98: Visible value centre(x, y) value x, y; { 99: return adj(x, y, Centre); 100: } 101: 102: Visible value adjright(x, y) value x, y; { 103: return adj(x, y, Right); 104: } 105: 106: /* For reasons of efficiency, wri does not always call convert but writes 107: directly on the standard output. Modifications in convert should 108: be mirrored by changes in wri and vice versa. */ 109: 110: Visible value convert(v, coll, outer) value v; bool coll, outer; { 111: literal type= v->type; intlet len= Length(v), k; value *vp= Ats(v); 112: value t, cv; 113: switch (type) { 114: case Num: 115: return mk_text(convnum(v)); 116: case Tex: 117: if (outer) return copy(v); 118: else {string tp= (string) vp; char cs[2]; 119: cs[1]= '\0'; 120: t= mk_text("'"); 121: Overall { 122: cs[0]= *tp++; 123: concato(&t, cs); 124: if (cs[0] == '\'' || cs[0] == '`') 125: concato(&t, cs); 126: } 127: concato(&t, "'"); 128: return t; 129: } 130: case Com: 131: outer&= coll; 132: t= mk_text(coll ? "" : "("); 133: Overall { 134: concato(&t, Str(cv= convert(*vp++, No, outer))); 135: release(cv); 136: if (k != len-1) concato(&t, outer ? " " : ", "); 137: } 138: if (!coll) concato(&t, ")"); 139: return t; 140: case Lis: case ELT: 141: t= mk_text("{"); 142: Overall { 143: concato(&t, Str(cv= convert(*vp++, No, No))); 144: release(cv); 145: if (k != len-1) concato(&t, "; "); 146: } 147: concato(&t, "}"); 148: return t; 149: case Tab: 150: t= mk_text("{"); 151: Overall { 152: concato(&t, "["); 153: concato(&t, Str(cv= convert(Cts(*vp), Yes, No))); 154: release(cv); 155: concato(&t, "]: "); 156: concato(&t, Str(cv= convert(Dts(*vp++), No, No))); 157: release(cv); 158: if (k != len-1) concato(&t, "; "); 159: } 160: concato(&t, "}"); 161: return t; 162: default: 163: syserr("converting value of unknown type"); 164: return (value) Dummy; 165: } 166: }