|
|
1.1 ! root 1: #include "univ.h" ! 2: #include "format.pri" ! 3: #include <ctype.h> ! 4: #include "symtab.pub" ! 5: #include "symbol.h" ! 6: SRCFILE("format.c") ! 7: ! 8: char *Bls::af(PRINTF_ARGS) ! 9: { ! 10: char x[1024], *q = x; ! 11: ! 12: trace( "%d.af(%s ...) %s", this, fmt, text ); ! 13: IF_LIVE(!this) return "Bls::af"; ! 14: if( p<text+TBLS && fmt ){ ! 15: sprintf(x, PRINTF_COPY); ! 16: while( *q && p<text+TBLS ) *p++ = *q++; ! 17: *p = 0; ! 18: } ! 19: return text; ! 20: } ! 21: ! 22: Bls::Bls(PRINTF_ARGS) ! 23: { ! 24: clear(); ! 25: af(PRINTF_COPY); ! 26: } ! 27: ! 28: Bls::Bls(Bls &b) ! 29: { ! 30: clear(); ! 31: af("%s", b.text); ! 32: } ! 33: ! 34: Format::Format(long f, SymTab *s) { format = f; stab = s; } ! 35: ! 36: char *FmtByte(int c) ! 37: { static char buf[8]; ! 38: ! 39: switch( c &= 0xFF ){ ! 40: case '\0' : return "\\0"; ! 41: case '\b' : return "\\b"; ! 42: case '\f' : return "\\f"; ! 43: case '\n' : return "\\n"; ! 44: case '\r' : return "\\r"; ! 45: case '\t' : return "\\t"; ! 46: case '\v' : return "\\v"; ! 47: case ' ' : return " "; /* see ikeya!<ctype.h> */ ! 48: case '\'' : return "\\\'"; ! 49: case '\"' : return "\\\""; ! 50: case '\\' : return "\\\\"; ! 51: } ! 52: sprintf( buf, isascii(c)&&isprint(c)?"%c":"\\%03o", c ); ! 53: return buf; ! 54: } ! 55: ! 56: char *FmtAscii(unsigned long c) ! 57: { ! 58: static char buf[32]; ! 59: ! 60: if( c == 0 ) return "0"; ! 61: strcpy(buf, "'"); ! 62: int lead = 1; int byte = 4; // cfront bug ! 63: for( ; byte; --byte, c<<=8 ){ ! 64: if( (c&0xFF000000) || !lead ){ ! 65: strcat(buf, FmtByte(c>>24)); ! 66: lead = 0; ! 67: } ! 68: } ! 69: return strcat(buf, "'"); ! 70: } ! 71: ! 72: void Format::grow(char *b) ! 73: { ! 74: accum.af("%s%s", sep, b); ! 75: sep = "="; ! 76: } ! 77: ! 78: void Format::growtime(long t) ! 79: { ! 80: char *time(long*); ! 81: grow(ctime(&t)); ! 82: } ! 83: ! 84: void Format::grow(char *fmt, long l) ! 85: { ! 86: char buf[32]; ! 87: ! 88: sprintf(buf, fmt, l); ! 89: grow(buf); ! 90: } ! 91: ! 92: void Format::grow(char *fmt, double d) ! 93: { ! 94: char buf[32]; ! 95: ! 96: trace( "%d.grow(%s,%g)", this, fmt, d ); ! 97: sprintf(buf, fmt, d); ! 98: if( fmt[1]=='g' && !strcmp(buf, "0") ) ! 99: grow("0.0"); ! 100: else ! 101: grow(buf); ! 102: } ! 103: ! 104: char *Format::f(long lng, double dbl) ! 105: { ! 106: Symbol *s; ! 107: ! 108: if( !this ) return "Format::f"; ! 109: ! 110: accum.clear(); ! 111: sep = ""; ! 112: ! 113: if( !format ) format = F_HEX; ! 114: ! 115: if( format&F_MASK8 ) lng &= 0xFF; ! 116: if( format&F_MASK16 ) lng &= 0xFFFF; ! 117: ! 118: if( format&F_FLOAT ) grow("%g", dbl); ! 119: if( format&F_DOUBLE ) grow("%g", dbl); ! 120: if( format&F_DBLHEX ) grow("0x%X_%X", dbl); ! 121: ! 122: if( format&F_SYMBOLIC ! 123: && lng ! 124: && stab ! 125: && (s = stab->loctosym(SSet(U_FUNC,U_GLB,U_STA,U_STMT), lng)) ){ ! 126: lng -= s->range.lo; ! 127: grow(s->text()); ! 128: if( !lng ) ! 129: format = 0; ! 130: else { ! 131: sep = "+"; ! 132: if( !(format&F_HOAD) ) format |= F_HEX; ! 133: } ! 134: } ! 135: ! 136: if( format&F_TIME ) growtime(lng); ! 137: if( (format&F_HOAD) && lng>=0 && lng<=7 ) grow("%d", lng); ! 138: else { ! 139: if( format&F_OCTAL ) grow("0%o", lng); ! 140: if( format&F_DECIMAL ) grow("%u", lng); ! 141: if( format&F_HEX ) grow("0x%X", lng); ! 142: if( format&F_ASCII ) grow(FmtAscii(lng)); ! 143: ! 144: if( format&F_EXT8 ) lng = (char ) lng; ! 145: if( format&F_EXT16) lng = (short) lng; ! 146: ! 147: if( format&F_SIGNED ! 148: && (lng<0 || !(format&F_DECIMAL)) ) grow("%d", lng); ! 149: } ! 150: return accum.text; ! 151: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.