1: #include "../h/rt.h" 2: 3: /* 4: * center(s1,n,s2) - pad s1 on left and right with s2 to length n. 5: */ 6: 7: Xcenter(nargs, arg3, arg2, arg1, arg0) 8: int nargs; 9: struct descrip arg3, arg2, arg1, arg0; 10: { 11: register char *s, *st; 12: int cnt, slen, hcnt; 13: char *sbuf, *s3; 14: char sbuf1[MAXSTRING], sbuf2[MAXSTRING]; 15: extern char *alcstr(); 16: 17: /* 18: * s1 must be a string. n must be a non-negative integer and defaults 19: * to 1. s2 must be a string and defaults to a blank. 20: */ 21: if (cvstr(&arg1, sbuf1) == NULL) 22: runerr(103, &arg1); 23: defshort(&arg2, 1); 24: if ((cnt = INTVAL(arg2)) < 0) 25: runerr(205, &arg2); 26: defstr(&arg3, sbuf2, &blank); 27: 28: sneed(cnt); 29: 30: if (STRLEN(arg3) == 0) { 31: /* 32: * The padding string is null, make it a blank. 33: */ 34: slen = 1; 35: s3 = " "; 36: } 37: else { 38: slen = STRLEN(arg3); 39: s3 = STRLOC(arg3); 40: } 41: 42: /* 43: * Get n bytes of string space for the new string. Start at the right 44: * of the new string and copy s2 into it from right to left as 45: * many times as will fit in the right half of the new string. 46: */ 47: sbuf = alcstr(NULL, cnt); 48: hcnt = cnt / 2; 49: s = sbuf + cnt; 50: while (s > sbuf + hcnt) { 51: st = s3 + slen; 52: while (st > s3 && s > sbuf + hcnt) 53: *--s = *--st; 54: } 55: 56: /* 57: * Start at the left end of the new string and copy s1 into it from 58: * left to right as many time as will fit in the left half of the 59: * new string. 60: */ 61: s = sbuf; 62: while (s < sbuf + hcnt) { 63: st = s3; 64: while (st < s3 + slen && s < sbuf + hcnt) 65: *s++ = *st++; 66: } 67: 68: slen = STRLEN(arg1); 69: if (cnt < slen) { 70: /* 71: * s1 is larger than the field to center it in. The source for the 72: * copy starts at the appropriate point in s1 and the destination 73: * starts at the left end of of the new string. 74: */ 75: s = sbuf; 76: st = STRLOC(arg1) + slen/2 - hcnt + (~cnt&slen&1); 77: } 78: else { 79: /* 80: * s1 is smaller than the field to center it in. The source for the 81: * copy starts at the left end of s1 and the destination starts at 82: * the appropriate point in the new string. 83: */ 84: s = sbuf + hcnt - slen/2 - (~cnt&slen&1); 85: st = STRLOC(arg1); 86: } 87: /* 88: * Perform the copy, moving min(*s1,n) bytes from st to s. 89: */ 90: if (slen > cnt) 91: slen = cnt; 92: while (slen-- > 0) 93: *s++ = *st++; 94: 95: /* 96: * Return the new string. 97: */ 98: STRLEN(arg0) = cnt; 99: STRLOC(arg0) = sbuf; 100: } 101: 102: Procblock(center,3)