📄 pputf.cxx
字号:
/* * * C++ Portable Types Library (PTypes) * Version 1.7.5 Released 9-Mar-2003 * * Copyright (c) 2001, 2002, 2003 Hovik Melikyan * * http://www.melikyan.com/ptypes/ * http://ptypes.sourceforge.net/ * */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <stdarg.h>#include "ptypes.h"#include "pstreams.h"#include "pasync.h" // for hashed mutex lockingPTYPES_BEGINstatic cset fmtopts = " #+~-0-9.";enum fmt_type_t{ FMT_NONE, FMT_CHAR, FMT_SHORT, FMT_INT, FMT_LONG, FMT_LARGE, FMT_STR, FMT_PTR, FMT_DOUBLE, FMT_LONG_DOUBLE};void outstm::vputf(const char* fmt, va_list va){ pmemlock* lock = pgetmemlock(this); pmementer(lock); try { const char* p = fmt; while (*p != 0) { // write out raw data between format specifiers const char* e = strchr(p, '%'); if (e == 0) e = p + strlen(p); if (e > p) write(p, e - p); if (*e != '%') break; e++; if (*e == '%') { // write out a single '%' put('%'); p = e + 1; continue; } // build a temporary buffer for the conversion specification char fbuf[128]; fbuf[0] = '%'; char* f = fbuf + 1; bool modif = false; // formatting flags and width specifiers while (*e & fmtopts && uint(f - fbuf) < sizeof(fbuf) - 5) { *f++ = *e++; modif = true; } // prefixes fmt_type_t fmt_type = FMT_NONE; switch(*e) { case 'h': fmt_type = FMT_SHORT; *f++ = *e++; break; case 'L': fmt_type = FMT_LONG_DOUBLE; *f++ = *e++; break; case 'l': e++; if (*e == 'l') {#ifdef WIN32 *f++ = 'I'; // Microsoft's Preaking I64 for __int64... *f++ = '6'; // not to be confused with 57-bit and 91-bit integers *f++ = '4'; // which are planned for the future Windows systems#else *f++ = 'l'; *f++ = 'l';#endif e++; fmt_type = FMT_LARGE; } else { *f++ = 'l'; fmt_type = FMT_LONG; } break; } // format specifier switch(*e) { case 'c': fmt_type = FMT_CHAR; *f++ = *e++; break; case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': if (fmt_type < FMT_SHORT || fmt_type > FMT_LARGE) fmt_type = FMT_INT; *f++ = *e++; break; case 'e': case 'E': case 'f': case 'g': case 'G': if (fmt_type != FMT_LONG_DOUBLE) fmt_type = FMT_DOUBLE; *f++ = *e++; break; case 's': fmt_type = FMT_STR; *f++ = *e++; break; case 'p': fmt_type = FMT_PTR; *f++ = *e++; break; } if (fmt_type == FMT_NONE) break; *f = 0; // some formatters are processed here 'manually', // while others are passed to snprintf char buf[4096]; int s = 0; switch(fmt_type) { case FMT_NONE: break; // to avoid compiler warning case FMT_CHAR: if (modif) s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,int)); else put(char(va_arg(va,int))); break; case FMT_SHORT: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,int)); break; case FMT_INT: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,int)); break; case FMT_LONG: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,long)); break; case FMT_LARGE: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,large)); break; case FMT_STR: if (modif) s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,char*)); else put(va_arg(va,const char*)); break; case FMT_PTR: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,void*)); break; case FMT_DOUBLE: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,double)); break; case FMT_LONG_DOUBLE: s = snprintf(buf, sizeof(buf), fbuf, va_arg(va,long double)); break; } if (s > 0) write(buf, s); p = e; } } catch(estream*) { pmemleave(lock); throw; } pmemleave(lock);}void outstm::putf(const char* fmt, ...){ va_list va; va_start(va, fmt); vputf(fmt, va); va_end(va);}void fdxstm::putf(const char* fmt, ...){ va_list va; va_start(va, fmt); out.vputf(fmt, va); va_end(va);}PTYPES_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -