// send a karlpacket. Not exactly very general :) void xbee_send_16(uint16_t destination, kpacket packet) { PUT_CHAR(FRAME_DELIM); PUT_CHAR(0); // we never send more than 255 bytes // 5 = command, frame, destination + options, 2 = kpacket header // actually, packet is fixed length, nsensors is just for helping decode PUT_CHAR(5 + 2 + (MAX_SENSORS * sizeof(ksensor))); uint8_t checksum; // doesn't need to include escaping! woo checksum = escapePutChar(0x01); // tx 16 command id checksum += escapePutChar(1); // frame id, not used, but better to allow acks than explicitly disable them checksum += escapePut16(destination); checksum += escapePutChar(0); // options checksum += escapePutChar(packet.header); checksum += escapePutChar((packet.version << 4) | (packet.nsensors & 0x0f)); //checksum += escapePutChar(packet.nsensors); for (int i = 0; i < packet.nsensors; i++) { checksum += escapePutChar(packet.ksensors[i].type); checksum += escapePut32(packet.ksensors[i].value); } // remember to write out the rest of the packet! for (int i = packet.nsensors; i < MAX_SENSORS; i++) { checksum += escapePutChar(0); checksum += escapePut32(0); } PUT_CHAR(0xff - checksum); }
// write out value, inserting and escaping if needed. returns the input to add in checksumming uint8_t escapePutChar(uint8_t value) { if (needsEscaping(value)) { PUT_CHAR(ESCAPE); PUT_CHAR(value ^ 0x20); } else { PUT_CHAR(value); } return value; }
void string2chunk(char*src,chunk *dst){ uint32_t len=strlen(src); Warning(debug,"decoding \"%s\" (%d)",src,len); if (src[0]=='#' && src[len-1]=='#') { Warning(debug,"hex"); len=len/2 - 1; if (dst->len<len) Abort("chunk overflow"); dst->len=len; for(uint32_t i=0;i<len;i++){ dst->data[i]=(hex_decode(src[2*i+1])<<4)+hex_decode(src[2*i+2]); } } else if (src[0]=='"' && src[len-1]=='"') { Warning(debug,"quoted"); len=len-2; if (dst->len<len) Abort("chunk overflow"); dst->len=0; #define PUT_CHAR(c) { dst->data[dst->len]=c; dst->len++ ; } for(uint32_t i=0;i<len;i++) { if (!isprint((unsigned char)src[i+1])) Abort("non-printable character in source"); if (src[i+1]=='"') Abort("unquoted \" in string"); if (src[i+1]=='\\') { if (i+1==len) Abort("bad escape sequence"); switch(src[i+2]){ case '\\': PUT_CHAR('\\'); i++; continue; case '"': PUT_CHAR('"'); i++; continue; default: Abort("unknown escape sequence"); } } PUT_CHAR(src[i+1]); } #undef PUT_CHAR } else { Warning(debug,"verbatim"); if (dst->len<len) Abort("chunk overflow"); dst->len=len; for(uint32_t i=0;i<len;i++) { if (!isprint((unsigned char)src[i])) Abort("non-printable character in source"); if (isspace((unsigned char)src[i])) Abort("white space in source"); dst->data[i]=src[i]; } } Warning(debug,"return length is %d",dst->len); }
/* for %o octal representation */ static void octal(struct DATA *p, double d) { CWT_CHAR *tmp; tmp = otoa(d); p->width -= (int)cwt_str_ns()->strLen(tmp); PAD_RIGHT(p); if (p->square == FOUND) /* had prefix '0' for octal */ PUT_CHAR(_T('0'), p); while (*tmp) { /* octal */ PUT_CHAR(*tmp, p); tmp++; } PAD_LEFT(p); }
long SDMHello::Marshal(char* buf) { int cur = HEADER_SIZE; cur += source.Marshal(buf,cur); PUT_CHAR(&buf[cur], type); cur += sizeof(char); msg_length = cur - HEADER_SIZE; MarshalHeader(buf); return cur; }
int putchar_uart0(int ch) { /* Place your implementation of fputc here */ /* e.g. write a character to a UART, or to the */ /* debugger console with SWI WriteC */ while ( TX_READY(GET_STATUS(UART0_BASE))==0); PUT_CHAR(UART0_BASE, ch); return ch; }
/* for %x %X hexadecimal representation */ static void hexa(struct DATA *p, double d) { CWT_CHAR *tmp; tmp = htoa(d); p->width -= (int)cwt_str_ns()->strLen(tmp); PAD_RIGHT(p); if (p->square == FOUND) { /* prefix '0x' for hexa */ PUT_CHAR(_T('0'), p); PUT_CHAR(*p->pf, p); } while (*tmp) { /* hexa */ PUT_CHAR((*p->pf == _T('X') ? cwt_str_ns()->toUpper(*tmp) : *tmp), p); tmp++; } PAD_LEFT(p); }
void sendchar( char *ch ) { char Ch; unsigned int Status; Ch=*ch; do { Status = GET_STATUS(OS_COMPORT); } while (!TX_READY(Status)); // wait until ready PUT_CHAR(OS_COMPORT, Ch); if (Ch == '\n') { do { Status = GET_STATUS(OS_COMPORT); } while (!TX_READY(Status)); // wait until ready PUT_CHAR(OS_COMPORT, '\r'); } }
/* %f or %g floating point representation */ static void floating(struct DATA *p, double d) { CWT_CHAR *tmp, *tmp2; int i; DEF_PREC(p); d = ROUND(d, p); tmp = dtoa(d, p->precision, &tmp2); /* calculate the padding. 1 for the dot */ p->width = p->width - ((d > 0. && p->justify == RIGHT) ? 1:0) - ((p->space == FOUND) ? 1:0) - ((int)cwt_str_ns()->strLen(tmp)) - p->precision - 1; PAD_RIGHT(p); PUT_PLUS(d, p); PUT_SPACE(d, p); while (*tmp) { /* the integral */ PUT_CHAR(*tmp, p); tmp++; } if (p->precision != 0 || p->square == FOUND) PUT_CHAR(_T('.'), p); /* put the '.' */ if (*p->pf == _T('g') || *p->pf == _T('G')) /* smash the trailing zeros */ for (i = ((int)cwt_str_ns()->strLen(tmp2)) - 1; i >= 0 && tmp2[i] == _T('0'); i--) tmp2[i] = _T('\0'); for (; *tmp2; tmp2++) PUT_CHAR(*tmp2, p); /* the fraction */ PAD_LEFT(p); }
void cyg_hal_plf_serial_putc(void *__ch_data, char c) { channel_data_t* chan = (channel_data_t*)__ch_data; cyg_uint8* base = chan->base; cyg_uint32 status; CYGARC_HAL_SAVE_GP(); do { status = GET_STATUS(base); } while (!TX_READY(status)); // wait until ready PUT_CHAR(base, c); if (c == '\n') { do { status = GET_STATUS(base); } while (!TX_READY(status)); // wait until ready PUT_CHAR(base, '\r'); } CYGARC_HAL_RESTORE_GP(); }
/* for %d and friends, it puts in holder * the representation with the right padding */ static void decimal(struct DATA *p, double d) { CWT_CHAR *tmp; tmp = itoa(d); p->width -= (int)cwt_str_ns()->strLen(tmp); PAD_RIGHT(p); PUT_PLUS(d, p); PUT_SPACE(d, p); while (*tmp) { /* the integral */ PUT_CHAR(*tmp, p); tmp++; } PAD_LEFT(p); }
/* %s strings */ static void strings(struct DATA *p, CWT_CHAR *tmp) { int i; i = (int)cwt_str_ns()->strLen(tmp); if (p->precision != NOT_FOUND) /* the smallest number */ i = (i < p->precision ? i : p->precision); p->width -= i; PAD_RIGHT(p); while (i-- > 0) { /* put the sting */ PUT_CHAR(*tmp, p); tmp++; } PAD_LEFT(p); }
/* Encodes terminal modes for the terminal referenced by fd in a portable manner, and appends the modes to a buffer being constructed. Stores constructed buffers len to buf_len. This call always succeeds, but if an error happens during encoding, buf will be empty and buf_len will be 0 */ void ssh_encode_tty_flags(int fd, unsigned char **buf, size_t *buf_len) /*void tty_make_modes(int fd)*/ { SshBuffer buffer; #ifdef USING_TERMIOS struct termios tio; #endif #ifdef USING_SGTTY struct sgttyb tio; struct tchars tiotc; struct ltchars tioltc; int tiolm; #ifdef TIOCGSTAT struct tstatus tiots; #endif /* TIOCGSTAT */ #endif /* USING_SGTTY */ int baud; if (!isatty(fd)) { SSH_TRACE(2, ("Not a tty. (fd = %d)", fd)); *buf = ssh_xstrdup(""); *buf_len = 0; return; } ssh_buffer_init(&buffer); /* Get the modes. */ #ifdef USING_TERMIOS if (tcgetattr(fd, &tio) < 0) { PUT_CHAR(TTY_OP_END); ssh_warning("tcgetattr: %.100s", strerror(errno)); goto error; } #endif /* USING_TERMIOS */ #ifdef USING_SGTTY if (ioctl(fd, TIOCGETP, &tio) < 0) { PUT_CHAR(TTY_OP_END); ssh_warning("ioctl(fd, TIOCGETP, ...): %.100s", strerror(errno)); goto error; } if (ioctl(fd, TIOCGETC, &tiotc) < 0) { PUT_CHAR(TTY_OP_END); ssh_warning("ioctl(fd, TIOCGETC, ...): %.100s", strerror(errno)); goto error; } if (ioctl(fd, TIOCLGET, &tiolm) < 0) { PUT_CHAR(TTY_OP_END); ssh_warning("ioctl(fd, TIOCLGET, ...): %.100s", strerror(errno)); goto error; } if (ioctl(fd, TIOCGLTC, &tioltc) < 0) { PUT_CHAR(TTY_OP_END); ssh_warning("ioctl(fd, TIOCGLTC, ...): %.100s", strerror(errno)); goto error; } #ifdef TIOCGSTAT if (ioctl(fd, TIOCGSTAT, &tiots) < 0) { PUT_CHAR(TTY_OP_END); ssh_warning("ioctl(fd, TIOCGSTAT, ...): %.100s", strerror(errno)); goto error; } #endif /* TIOCGSTAT */ /* termio's ECHOE is really both LCRTBS and LCRTERA - so wire them together */ if (tiolm & LCRTBS) tiolm |= LCRTERA; #endif /* USING_SGTTY */ /* Store input and output baud rates. */ baud = speed_to_baud(cfgetospeed(&tio)); PUT_CHAR(TTY_OP_OSPEED); PUT_UINT32(baud); baud = speed_to_baud(cfgetispeed(&tio)); PUT_CHAR(TTY_OP_ISPEED); PUT_UINT32(baud); /* Store values of mode flags. */ #ifdef USING_TERMIOS #define TTYCHAR(NAME, OP) \ PUT_CHAR(OP); PUT_UINT32(tio.c_cc[NAME]); #define TTYMODE(NAME, FIELD, OP) \ PUT_CHAR(OP); PUT_UINT32((tio.FIELD & NAME) != 0); #define SGTTYCHAR(NAME, OP) #define SGTTYMODE(NAME, FIELD, OP) #define SGTTYMODEN(NAME, FIELD, OP) #endif /* USING_TERMIOS */ #ifdef USING_SGTTY #define TTYCHAR(NAME, OP) #define TTYMODE(NAME, FIELD, OP) #define SGTTYCHAR(NAME, OP) \ PUT_CHAR(OP); PUT_UINT32(NAME); #define SGTTYMODE(NAME, FIELD, OP) \ PUT_CHAR(OP); PUT_UINT32((FIELD & NAME) != 0); #define SGTTYMODEN(NAME, FIELD, OP) \ PUT_CHAR(OP); PUT_UINT32((FIELD & NAME) == 0); #endif /* USING_SGTTY */ #include "sshttyflagsi.h" #undef TTYCHAR #undef TTYMODE #undef SGTTYCHAR #undef SGTTYMODE #undef SGTTYMODEN /* Mark end of mode data. */ PUT_CHAR(TTY_OP_END); *buf_len = ssh_buffer_len(&buffer); *buf = ssh_xmemdup(ssh_buffer_ptr(&buffer), *buf_len); ssh_buffer_uninit(&buffer); SSH_DEBUG_HEXDUMP(5, ("encoded tty-flags buffer"), *buf, *buf_len); return; error: ssh_buffer_uninit(&buffer); *buf = ssh_xstrdup(""); *buf_len = 0; }
/*! Load, parse, and process a qdoc configuration file. This function is only called by the other load() function, but this one is recursive, i.e., it calls itself when it sees an \c{include} statement in the qdog configuration file. */ void Config::load(Location location, const QString& fileName) { QRegExp keySyntax("\\w+(?:\\.\\w+)*"); #define SKIP_CHAR() \ do { \ location.advance(c); \ ++i; \ c = text.at(i); \ cc = c.unicode(); \ } while (0) #define SKIP_SPACES() \ while (c.isSpace() && cc != '\n') \ SKIP_CHAR() #define PUT_CHAR() \ word += c; \ SKIP_CHAR(); if (location.depth() > 16) location.fatal(tr("Too many nested includes")); QFile fin(fileName); if (!fin.open(QFile::ReadOnly | QFile::Text)) { fin.setFileName(fileName + ".qdoc"); if (!fin.open(QFile::ReadOnly | QFile::Text)) location.fatal(tr("Cannot open file '%1': %2").arg(fileName).arg(fin.errorString())); } QString text = fin.readAll(); text += QLatin1String("\n\n"); text += QChar('\0'); fin.close(); location.push(fileName); location.start(); int i = 0; QChar c = text.at(0); uint cc = c.unicode(); while (i < (int) text.length()) { if (cc == 0) ++i; else if (c.isSpace()) { SKIP_CHAR(); } else if (cc == '#') { do { SKIP_CHAR(); } while (cc != '\n'); } else if (isMetaKeyChar(c)) { Location keyLoc = location; bool plus = false; QString stringValue; QStringList stringListValue; QString word; bool inQuote = false; bool prevWordQuoted = true; bool metWord = false; MetaStack stack; do { stack.process(c, location); SKIP_CHAR(); } while (isMetaKeyChar(c)); QStringList keys = stack.getExpanded(location); SKIP_SPACES(); if (keys.count() == 1 && keys.first() == "include") { QString includeFile; if (cc != '(') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); while (!c.isSpace() && cc != '#' && cc != ')') { includeFile += c; SKIP_CHAR(); } SKIP_SPACES(); if (cc != ')') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); if (cc != '#' && cc != '\n') location.fatal(tr("Trailing garbage")); /* Here is the recursive call. */ load(location, QFileInfo(QFileInfo(fileName).dir(), includeFile) .filePath()); } else { /* It wasn't an include statement, so it;s something else. */ if (cc == '+') { plus = true; SKIP_CHAR(); } if (cc != '=') location.fatal(tr("Expected '=' or '+=' after key")); SKIP_CHAR(); SKIP_SPACES(); for (;;) { if (cc == '\\') { int metaCharPos; SKIP_CHAR(); if (cc == '\n') { SKIP_CHAR(); } else if (cc > '0' && cc < '8') { word += QChar(c.digitValue()); SKIP_CHAR(); } else if ((metaCharPos = QString(QLatin1String("abfnrtv")).indexOf(c)) != -1) { word += "\a\b\f\n\r\t\v"[metaCharPos]; SKIP_CHAR(); } else { PUT_CHAR(); } } else if (c.isSpace() || cc == '#') { if (inQuote) { if (cc == '\n') location.fatal(tr("Unterminated string")); PUT_CHAR(); } else { if (!word.isEmpty()) { if (metWord) stringValue += QLatin1Char(' '); stringValue += word; stringListValue << word; metWord = true; word.clear(); prevWordQuoted = false; } if (cc == '\n' || cc == '#') break; SKIP_SPACES(); } } else if (cc == '"') { if (inQuote) { if (!prevWordQuoted) stringValue += QLatin1Char(' '); stringValue += word; if (!word.isEmpty()) stringListValue << word; metWord = true; word.clear(); prevWordQuoted = true; } inQuote = !inQuote; SKIP_CHAR(); } else if (cc == '$') { QString var; SKIP_CHAR(); while (c.isLetterOrNumber() || cc == '_') { var += c; SKIP_CHAR(); } if (!var.isEmpty()) { char *val = getenv(var.toLatin1().data()); if (val == 0) { location.fatal(tr("Environment variable '%1' undefined").arg(var)); } else { word += QString(val); } } } else { if (!inQuote && cc == '=') location.fatal(tr("Unexpected '='")); PUT_CHAR(); } } QStringList::ConstIterator key = keys.begin(); while (key != keys.end()) { if (!keySyntax.exactMatch(*key)) keyLoc.fatal(tr("Invalid key '%1'").arg(*key)); if (plus) { if (locMap[*key].isEmpty()) { locMap[*key] = keyLoc; } else { locMap[*key].setEtc(true); } if (stringValueMap[*key].isEmpty()) { stringValueMap[*key] = stringValue; } else { stringValueMap[*key] += QLatin1Char(' ') + stringValue; } stringListValueMap[*key] += stringListValue; } else { locMap[*key] = keyLoc; stringValueMap[*key] = stringValue; stringListValueMap[*key] = stringListValue; } ++key; } } } else { location.fatal(tr("Unexpected character '%1' at beginning of line") .arg(c)); } } }
/*! Load, parse, and process a qdoc configuration file. This function is only called by the other load() function, but this one is recursive, i.e., it calls itself when it sees an \c{include} statement in the qdoc configuration file. */ void Config::load(Location location, const QString& fileName) { QFileInfo fileInfo(fileName); QString path = fileInfo.canonicalPath(); pushWorkingDir(path); QDir::setCurrent(path); QRegExp keySyntax(QLatin1String("\\w+(?:\\.\\w+)*")); #define SKIP_CHAR() \ do { \ location.advance(c); \ ++i; \ c = text.at(i); \ cc = c.unicode(); \ } while (0) #define SKIP_SPACES() \ while (c.isSpace() && cc != '\n') \ SKIP_CHAR() #define PUT_CHAR() \ word += c; \ SKIP_CHAR(); if (location.depth() > 16) location.fatal(tr("Too many nested includes")); QFile fin(fileInfo.fileName()); if (!fin.open(QFile::ReadOnly | QFile::Text)) { if (!Config::installDir.isEmpty()) { int prefix = location.filePath().length() - location.fileName().length(); fin.setFileName(Config::installDir + QLatin1Char('/') + fileName.right(fileName.length() - prefix)); } if (!fin.open(QFile::ReadOnly | QFile::Text)) location.fatal(tr("Cannot open file '%1': %2").arg(fileName).arg(fin.errorString())); } QTextStream stream(&fin); #ifndef QT_NO_TEXTCODEC stream.setCodec("UTF-8"); #endif QString text = stream.readAll(); text += QLatin1String("\n\n"); text += QLatin1Char('\0'); fin.close(); location.push(fileName); location.start(); int i = 0; QChar c = text.at(0); uint cc = c.unicode(); while (i < (int) text.length()) { if (cc == 0) { ++i; } else if (c.isSpace()) { SKIP_CHAR(); } else if (cc == '#') { do { SKIP_CHAR(); } while (cc != '\n'); } else if (isMetaKeyChar(c)) { Location keyLoc = location; bool plus = false; QString stringValue; QStringList rhsValues; QString word; bool inQuote = false; bool prevWordQuoted = true; bool metWord = false; MetaStack stack; do { stack.process(c, location); SKIP_CHAR(); } while (isMetaKeyChar(c)); QStringList keys = stack.getExpanded(location); SKIP_SPACES(); if (keys.count() == 1 && keys.first() == QLatin1String("include")) { QString includeFile; if (cc != '(') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); while (!c.isSpace() && cc != '#' && cc != ')') { if (cc == '$') { QString var; SKIP_CHAR(); while (c.isLetterOrNumber() || cc == '_') { var += c; SKIP_CHAR(); } if (!var.isEmpty()) { const QByteArray val = qgetenv(var.toLatin1().data()); if (val.isNull()) { location.fatal(tr("Environment variable '%1' undefined").arg(var)); } else { includeFile += QString::fromLatin1(val); } } } else { includeFile += c; SKIP_CHAR(); } } SKIP_SPACES(); if (cc != ')') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); if (cc != '#' && cc != '\n') location.fatal(tr("Trailing garbage")); /* Here is the recursive call. */ load(location, QFileInfo(QDir(path), includeFile).filePath()); } else { /* It wasn't an include statement, so it's something else. We must see either '=' or '+=' next. If not, fatal error. */ if (cc == '+') { plus = true; SKIP_CHAR(); } if (cc != '=') location.fatal(tr("Expected '=' or '+=' after key")); SKIP_CHAR(); SKIP_SPACES(); for (;;) { if (cc == '\\') { int metaCharPos; SKIP_CHAR(); if (cc == '\n') { SKIP_CHAR(); } else if (cc > '0' && cc < '8') { word += QChar(c.digitValue()); SKIP_CHAR(); } else if ((metaCharPos = QString::fromLatin1("abfnrtv").indexOf(c)) != -1) { word += QLatin1Char("\a\b\f\n\r\t\v"[metaCharPos]); SKIP_CHAR(); } else { PUT_CHAR(); } } else if (c.isSpace() || cc == '#') { if (inQuote) { if (cc == '\n') location.fatal(tr("Unterminated string")); PUT_CHAR(); } else { if (!word.isEmpty()) { if (metWord) stringValue += QLatin1Char(' '); stringValue += word; #if 0 if (metWord) rhsValues << QString(" " + word); else #endif rhsValues << word; metWord = true; word.clear(); prevWordQuoted = false; } if (cc == '\n' || cc == '#') break; SKIP_SPACES(); } } else if (cc == '"') { if (inQuote) { if (!prevWordQuoted) stringValue += QLatin1Char(' '); stringValue += word; if (!word.isEmpty()) rhsValues << word; metWord = true; word.clear(); prevWordQuoted = true; } inQuote = !inQuote; SKIP_CHAR(); } else if (cc == '$') { QString var; SKIP_CHAR(); while (c.isLetterOrNumber() || cc == '_') { var += c; SKIP_CHAR(); } if (!var.isEmpty()) { const QByteArray val = qgetenv(var.toLatin1().constData()); if (val.isNull()) { location.fatal(tr("Environment variable '%1' undefined").arg(var)); } else { word += QString::fromLatin1(val); } } } else { if (!inQuote && cc == '=') location.fatal(tr("Unexpected '='")); PUT_CHAR(); } } QStringList::ConstIterator key = keys.constBegin(); while (key != keys.constEnd()) { if (!keySyntax.exactMatch(*key)) keyLoc.fatal(tr("Invalid key '%1'").arg(*key)); ConfigVarMultimap::Iterator i; i = configVars_.insert(*key, ConfigVar(*key, rhsValues, QDir::currentPath(), keyLoc)); i.value().plus_ = plus; ++key; } } } else { location.fatal(tr("Unexpected character '%1' at beginning of line").arg(c)); } } popWorkingDir(); if (!workingDirs_.isEmpty()) QDir::setCurrent(workingDirs_.top()); }
int cwt_vsnprintf(CWT_CHAR *string, size_t length, const CWT_CHAR *format, va_list args) { struct DATA data; CWT_CHAR conv_field[MAX_FIELD]; double d; /* temporary holder */ int state; int i; data.length = length - 1; /* leave room for '\0' */ data.holder = string; data.pf = format; data.counter = 0; /* sanity check, the string must be > 1 */ if (length < 1) return -1; for (; *data.pf && (data.counter < data.length); data.pf++) { if ( *data.pf == _T('%') ) { /* we got a magic % cookie */ conv_flag((CWT_CHAR *)0, &data); /* initialise format flags */ for (state = 1; *data.pf && state;) { switch (*(++data.pf)) { case _T('\0'): /* a NULL here ? ? bail out */ *data.holder = _T('\0'); return data.counter; break; case _T('f'): /* float, double */ STAR_ARGS(&data); d = va_arg(args, double); floating(&data, d); state = 0; break; case _T('g'): case _T('G'): STAR_ARGS(&data); DEF_PREC(&data); d = va_arg(args, double); i = log_10(d); /* for '%g|%G' ANSI: use f if exponent * is in the range or [-4,p] exclusively * else use %e|%E */ if (-4 < i && i < data.precision) floating(&data, d); else exponent(&data, d); state = 0; break; case _T('e'): case _T('E'): /* Exponent double */ STAR_ARGS(&data); d = va_arg(args, double); exponent(&data, d); state = 0; break; case _T('u'): case _T('d'): /* decimal */ STAR_ARGS(&data); if (data.a_long == FOUND) d = va_arg(args, long); else d = va_arg(args, int); decimal(&data, d); state = 0; break; case _T('o'): /* octal */ STAR_ARGS(&data); if (data.a_long == FOUND) d = va_arg(args, long); else d = va_arg(args, int); octal(&data, d); state = 0; break; case _T('x'): case _T('X'): /* hexadecimal */ STAR_ARGS(&data); if (data.a_long == FOUND) d = va_arg(args, long); else d = va_arg(args, int); hexa(&data, d); state = 0; break; case _T('c'): /* character */ d = va_arg(args, int); PUT_CHAR((CWT_CHAR)d, &data); state = 0; break; case _T('s'): /* string */ STAR_ARGS(&data); strings(&data, va_arg(args, CWT_CHAR *)); state = 0; break; case _T('n'): *(va_arg(args, int *)) = data.counter; /* what's the count ? */ state = 0; break; case _T('l'): data.a_long = FOUND; break; case _T('h'): break; case _T('%'): /* nothing just % */ PUT_CHAR(_T('%'), &data); state = 0; break; case _T('#'): case _T(' '): case _T('+'): case _T('*'): case _T('-'): case _T('.'): case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'): /* initialize width and precision */ for (i = 0; isflag(*data.pf); i++, data.pf++) if (i < MAX_FIELD - 1) conv_field[i] = *data.pf; conv_field[i] = _T('\0'); conv_flag(conv_field, &data); data.pf--; /* went to far go back */ break; default: /* is this an error ? maybe bail out */ state = 0; break; } /* end switch */
/* %e %E %g exponent representation */ static void exponent(struct DATA *p, double d) { CWT_CHAR *tmp, *tmp2; int j, i; DEF_PREC(p); j = log_10(d); d = d / pow_10(j); /* get the Mantissa */ d = ROUND(d, p); tmp = dtoa(d, p->precision, &tmp2); /* 1 for unit, 1 for the '.', 1 for 'e|E', * 1 for '+|-', 3 for 'exp' */ /* calculate how much padding need */ p->width = p->width - ((d > 0. && p->justify == RIGHT) ? 1:0) - ((p->space == FOUND) ? 1:0) - p->precision - 7; PAD_RIGHT(p); PUT_PLUS(d, p); PUT_SPACE(d, p); while (*tmp) {/* the integral */ PUT_CHAR(*tmp, p); tmp++; } if (p->precision != 0 || p->square == FOUND) PUT_CHAR(_T('.'), p); /* the '.' */ if (*p->pf == _T('g') || *p->pf == _T('G')) /* smash the trailing zeros */ for (i = ((int)cwt_str_ns()->strLen(tmp2)) - 1; i >= 0 && tmp2[i] == _T('0'); i--) tmp2[i] = _T('\0'); for (; *tmp2; tmp2++) PUT_CHAR(*tmp2, p); /* the fraction */ if (*p->pf == _T('g') || *p->pf == _T('e')) { /* the exponent put the 'e|E' */ PUT_CHAR(_T('e'), p); } else { PUT_CHAR(_T('E'), p); } if (j > 0) { /* the sign of the exp */ PUT_CHAR(_T('+'), p); } else { PUT_CHAR(_T('-'), p); j = -j; } tmp = itoa((double)j); if (j < 9) { /* need to pad the exponent with 0 '000' */ PUT_CHAR(_T('0'), p); PUT_CHAR(_T('0'), p); } else if (j < 99) { PUT_CHAR(_T('0'), p); } while (*tmp) { /* the exponent */ PUT_CHAR(*tmp, p); tmp++; } PAD_LEFT(p); }