📄 misc.c
字号:
if (c == 'o') { if (f & flag_hash && value != 0) { put_ulong(&buf, value, 8, 0, T("0"), width, prec, f); } else { put_ulong(&buf, value, 8, 0, NULL, width, prec, f); } } else if (c == 'u') { put_ulong(&buf, value, 10, 0, NULL, width, prec, f); } else { if (f & flag_hash && value != 0) { if (c == 'x') { put_ulong(&buf, value, 16, 0, T("0x"), width, prec, f); } else { put_ulong(&buf, value, 16, 1, T("0X"), width, prec, f); } } else { put_ulong(&buf, value, 16, 0, NULL, width, prec, f); } } } else if (c == 'c') { char_t value = va_arg(arg, int); put_char(&buf, value); } else if (c == 's' || c == 'S') { char_t *value = va_arg(arg, char_t *); if (value == NULL) { put_string(&buf, T("(null)"), -1, width, prec, f); } else if (f & flag_hash) { put_string(&buf, value + 1, (char_t) *value, width, prec, f); } else { put_string(&buf, value, -1, width, prec, f); } } else if (c == 'p') { void *value = va_arg(arg, void *); put_ulong(&buf, (unsigned long int) value, 16, 0, T("0x"), width, prec, f); } else if (c == 'n') { if (f & flag_short) { short int *value = va_arg(arg, short int *); *value = buf.count; } else if (f & flag_long) { long int *value = va_arg(arg, long int *); *value = buf.count; } else { int *value = va_arg(arg, int *); *value = buf.count; } } else { put_char(&buf, c); } } } if (buf.s == NULL) { put_char(&buf, '\0'); }/* * If the user requested a dynamic buffer (*s == NULL), ensure it is returned. */ if (*s == NULL || msize != 0) { *s = buf.s; } if (*s != NULL && size > 0) { if (buf.count < size) { (*s)[buf.count] = '\0'; } else { (*s)[buf.size - 1] = '\0'; } } if (msize != 0) { return buf.size; } return buf.count;}/******************************************************************************//* * Add a character to a string buffer */static void put_char(strbuf_t *buf, char_t c){ if (buf->count >= (buf->size - 1)) { if (! (buf->flags & STR_REALLOC)) { return; } buf->size += STR_INC; if (buf->size > buf->max && buf->size > STR_INC) {/* * Caller should increase the size of the calling buffer */ buf->size -= STR_INC; return; } if (buf->s == NULL) { buf->s = balloc(B_L, buf->size * sizeof(char_t)); } else { buf->s = brealloc(B_L, buf->s, buf->size * sizeof(char_t)); } } buf->s[buf->count] = c; if (c != '\0') { ++buf->count; }}/******************************************************************************//* * Add a string to a string buffer */static void put_string(strbuf_t *buf, char_t *s, int len, int width, int prec, enum flag f){ int i; if (len < 0) { len = strnlen(s, prec >= 0 ? prec : ULONG_MAX); } else if (prec >= 0 && prec < len) { len = prec; } if (width > len && !(f & flag_minus)) { for (i = len; i < width; ++i) { put_char(buf, ' '); } } for (i = 0; i < len; ++i) { put_char(buf, s[i]); } if (width > len && f & flag_minus) { for (i = len; i < width; ++i) { put_char(buf, ' '); } }}/******************************************************************************//* * Add a long to a string buffer */static void put_ulong(strbuf_t *buf, unsigned long int value, int base, int upper, char_t *prefix, int width, int prec, enum flag f){ unsigned long x, x2; int len, zeros, i; for (len = 1, x = 1; x < ULONG_MAX / base; ++len, x = x2) { x2 = x * base; if (x2 > value) { break; } } zeros = (prec > len) ? prec - len : 0; width -= zeros + len; if (prefix != NULL) { width -= strnlen(prefix, ULONG_MAX); } if (!(f & flag_minus)) { if (f & flag_zero) { for (i = 0; i < width; ++i) { put_char(buf, '0'); } } else { for (i = 0; i < width; ++i) { put_char(buf, ' '); } } } if (prefix != NULL) { put_string(buf, prefix, -1, 0, -1, flag_none); } for (i = 0; i < zeros; ++i) { put_char(buf, '0'); } for ( ; x > 0; x /= base) { int digit = (value / x) % base; put_char(buf, (char) ((digit < 10 ? '0' : (upper ? 'A' : 'a') - 10) + digit)); } if (f & flag_minus) { for (i = 0; i < width; ++i) { put_char(buf, ' '); } }}/******************************************************************************//* * Convert an ansi string to a unicode string. On an error, we return the * original ansi string which is better than returning NULL. nBytes is the * size of the destination buffer (ubuf) in _bytes_. */char_t *ascToUni(char_t *ubuf, char *str, int nBytes){#if UNICODE if (MultiByteToWideChar(CP_ACP, 0, str, nBytes / sizeof(char_t), ubuf, nBytes / sizeof(char_t)) < 0) { return (char_t*) str; }#else memcpy(ubuf, str, nBytes);#endif return ubuf;}/******************************************************************************//* * Convert a unicode string to an ansi string. On an error, return the * original unicode string which is better than returning NULL. * N.B. nBytes is the number of _bytes_ in the destination buffer, buf. */char *uniToAsc(char *buf, char_t *ustr, int nBytes){#if UNICODE if (WideCharToMultiByte(CP_ACP, 0, ustr, nBytes, buf, nBytes, NULL, NULL) < 0) { return (char*) ustr; }#else memcpy(buf, ustr, nBytes);#endif return (char*) buf;}/******************************************************************************//* * allocate (balloc) a buffer and do ascii to unicode conversion into it. * cp points to the ascii buffer. alen is the length of the buffer to be * converted not including a terminating NULL. Return a pointer to the * unicode buffer which must be bfree'd later. Return NULL on failure to * get buffer. The buffer returned is NULL terminated. */char_t *ballocAscToUni(char *cp, int alen){ char_t *unip; int ulen; ulen = (alen + 1) * sizeof(char_t); if ((unip = balloc(B_L, ulen)) == NULL) { return NULL; } ascToUni(unip, cp, ulen); unip[alen] = 0; return unip;}/******************************************************************************//* * allocate (balloc) a buffer and do unicode to ascii conversion into it. * unip points to the unicoded string. ulen is the number of characters * in the unicode string not including a teminating null. Return a pointer * to the ascii buffer which must be bfree'd later. Return NULL on failure * to get buffer. The buffer returned is NULL terminated. */char *ballocUniToAsc(char_t *unip, int ulen){ char * cp; if ((cp = balloc(B_L, ulen+1)) == NULL) { return NULL; } uniToAsc(cp, unip, ulen); cp[ulen] = '\0'; return cp;}/******************************************************************************//* * convert a hex string to an integer. The end of the string or a non-hex * character will indicate the end of the hex specification. */unsigned int hextoi(char_t *hexstring){ register char_t *h; register unsigned int c, v; v = 0; h = hexstring; if (*h == '0' && (*(h+1) == 'x' || *(h+1) == 'X')) { h += 2; } while ((c = (unsigned int)*h++) != 0) { if (c >= '0' && c <= '9') { c -= '0'; } else if (c >= 'a' && c <= 'f') { c = (c - 'a') + 10; } else if (c >= 'A' && c <= 'F') { c = (c - 'A') + 10; } else { break; } v = (v * 0x10) + c; } return v;}/******************************************************************************//* * convert a string to an integer. If the string starts with "0x" or "0X" * a hexidecimal conversion is done. */unsigned int gstrtoi(char_t *s){ if (*s == '0' && (*(s+1) == 'x' || *(s+1) == 'X')) { s += 2; return hextoi(s); } return gatoi(s);}/******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -