Annotation of 43BSD/contrib/icon/rt/cvstr.c, revision 1.1.1.1

1.1       root        1: #include "../h/rt.h"
                      2: 
                      3: /*
                      4:  * cvstr(d,s) - convert d (in place) into a string, using s as buffer
                      5:  *  if necessary.  cvstr returns 0 if the conversion fails, 1 if d
                      6:  *  wasn't a string but was converted into one, and 2 if d was already
                      7:  *  a string.  When a string conversion takes place, sbuf gets the
                      8:  *  resulting string.
                      9:  */
                     10: 
                     11: cvstr(d, sbuf)
                     12: register struct descrip *d;
                     13: char *sbuf;
                     14:    {
                     15:    
                     16:    /*
                     17:     * Dereference d if necessary.
                     18:     */
                     19:       DeRef(*d)
                     20: 
                     21:    if (QUAL(*d)) {
                     22:       /*
                     23:        * d is already a string.
                     24:        */
                     25:       if (NULLDESC(*d))
                     26:          return (NULL);
                     27:       return (2);
                     28:       }
                     29: 
                     30:    switch (TYPE(*d)) {
                     31:       /*
                     32:        * For types that can be converted into strings, call the appropriate
                     33:        *  conversion routine and return their result.  Note that the
                     34:        *  conversion routines write over d.
                     35:        */
                     36:       case T_INTEGER:
                     37:          return (itos((long)INTVAL(*d), d, sbuf));
                     38: 
                     39: #ifdef LONGS
                     40:       case T_LONGINT:
                     41:          return (itos(BLKLOC(*d)->longint.intval, d, sbuf));
                     42: #endif LONGS
                     43:       case T_REAL:
                     44:          return (rtos(BLKLOC(*d)->realblk.realval, d, sbuf));
                     45: 
                     46:       case T_CSET:
                     47:          return (cstos(BLKLOC(*d)->cset.bits, d, sbuf));
                     48: 
                     49:       default:
                     50:          /*
                     51:           * d can't be converted to a string.
                     52:           */
                     53:          return (NULL);
                     54:       }
                     55:    }
                     56: 
                     57: /*
                     58:  * itos - convert the integer num into a string using s as a buffer and
                     59:  *  making q a descriptor for the resulting string.
                     60:  */
                     61: static itos(num, q, s)
                     62: long num;
                     63: struct descrip *q;
                     64: char *s;
                     65:    {
                     66:    register char *p;
                     67:    long ival;
                     68: 
                     69:    p = s + MAXSTRING - 1;
                     70:    ival = num;
                     71: 
                     72:    *p = '\0';
                     73:    if (num >= 0L)
                     74:       do {
                     75:          *--p = ival % 10L + '0';
                     76:          ival /= 10L;
                     77:          } while (ival != 0L);
                     78:    else {
                     79:       do {
                     80:          *--p = '0' - (ival % 10L);
                     81:          ival /= 10L;
                     82:          } while (ival != 0L);
                     83:       *--p = '-';
                     84:       }
                     85:    
                     86:    STRLEN(*q) = s + MAXSTRING - 1 - p;
                     87:    STRLOC(*q) = p;
                     88:    return (1);
                     89:    }
                     90: 
                     91: /*
                     92:  * rtos - convert the real number n into a string using s as a buffer and
                     93:  *  making q a descriptor for the resulting string.
                     94:  */
                     95: rtos(n, q, s)
                     96: double n;
                     97: struct descrip *q;
                     98: char *s;
                     99:    {
                    100:    /*
                    101:     * gcvt does all the work.
                    102:     */
                    103:    gcvt(n, 8, s);
                    104:    STRLEN(*q) = strlen(s);
                    105:    STRLOC(*q) = s;
                    106:    return (1);
                    107:    }
                    108: 
                    109: /*
                    110:  * cstos - convert the cset bit array pointed at by cs into a string using
                    111:  *  s as a buffer and making q a descriptor for the resulting string.
                    112:  */
                    113: static cstos(cs, q, s)
                    114: int *cs;
                    115: struct descrip *q;
                    116: char *s;
                    117:    {
                    118:    register char *p;
                    119:    register int i;
                    120: 
                    121:    p = s;
                    122:    for (i = 0; i < CSETSIZE*INTSIZE; i++) {
                    123:       if (tstb(i, cs))
                    124:          *p++ = (char)i;
                    125:       }
                    126:    *p = '\0';
                    127: 
                    128:    STRLEN(*q) = p - s;
                    129:    STRLOC(*q) = s;
                    130:    return (1);
                    131:    }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.