📄 util.c
字号:
case '\n': *dp++ = '\\'; *dp++ = 'n'; break; case '\t': *dp++ = '\\'; *dp++ = 't'; break; case '\r': *dp++ = '\\'; *dp++ = 'r'; break; case '\b': *dp++ = '\\'; *dp++ = 'b'; break; case '\f': *dp++ = '\\'; *dp++ = 'f'; break; default: /* Should not be possible to overflow, truncate if so */ (void) jio_snprintf(dp, CSTRLEN+1 - (dp - buf), "\\%X", c); dp += strlen(dp); break; } *dp++ = '"'; if (len >= 0 && c != 0) *dp++ = '.', *dp++ = '.', *dp++ = '.'; *dp++ = 0; return buf;}/* * WARNING: out_of_memory() aborts the runtime! It should not be used * except in the case of out of memory situations that are clearly not * survivable, like running out of memory before the runtime is initialized. * If the runtime has finished initializing and you run out of memory, you * should throw an OutOfMemoryError instead. */voidout_of_memory(){ jio_fprintf(stderr, "**Out of memory, exiting**\n"); exit(1);}voidpanic(const char *format, ...){ va_list ap; char buf[256]; printCurrentClassName(); va_start(ap, format); /* If buffer overflow, quietly truncate */ (void) jio_vsnprintf(buf, sizeof(buf), format, ap); jio_fprintf(stdout, "\nERROR: %s\n", buf); exit(1);}char *classname2string(char *src, char *dst, int size) { char *buf = dst; for (; (--size > 0) && (*src != '\0') ; src++, dst++) { if (*src == '/') { *dst = '.'; } else { *dst = *src; } } *dst = '\0'; return buf;}typedef struct InstanceData { char *buffer; char *end;} InstanceData;#define ERROR_RETVAL -1#undef SUCCESS#define SUCCESS 0#undef CheckRet#define CheckRet(x) { if ((x) == ERROR_RETVAL) return ERROR_RETVAL; }static int put_char(InstanceData *this, int c){ if (iscntrl(0xff & c) && c != '\n' && c != '\t') { c = '@' + (c & 0x1F); if (this->buffer >= this->end) { return ERROR_RETVAL; } *this->buffer++ = '^'; } if (this->buffer >= this->end) { return ERROR_RETVAL; } *this->buffer++ = c; return SUCCESS;}static intformat_string(InstanceData *this, char *str, int left_justify, int min_width, int precision){ int pad_length; char *p; if (str == 0) { return ERROR_RETVAL; } if ((int)strlen(str) < precision) { pad_length = min_width - strlen(str); } else { pad_length = min_width - precision; } if (pad_length < 0) pad_length = 0; if (left_justify) { while (pad_length > 0) { CheckRet(put_char(this, ' ')); --pad_length; } } for (p = str; *p != '\0' && --precision >= 0; p++) { CheckRet(put_char(this, *p)); } if (!left_justify) { while (pad_length > 0) { CheckRet(put_char(this, ' ')); --pad_length; } } return SUCCESS;}#define MAX_DIGITS 32static intformat_number(InstanceData *this, long value, int format_type, int left_justify, int min_width, int precision, bool_t zero_pad){ int sign_value = 0; unsigned long uvalue; char convert[MAX_DIGITS+1]; int place = 0; int pad_length = 0; static char digits[] = "0123456789abcdef"; int base = 0; bool_t caps = FALSE; bool_t add_sign = FALSE; switch (format_type) { case 'o': case 'O': base = 8; break; case 'd': case 'D': add_sign = TRUE; /* fall through */ case 'u': case 'U': base = 10; break; case 'X': caps = TRUE; /* fall through */ case 'x': base = 16; break; } sysAssert(base > 0 && base <= 16); uvalue = value; if (add_sign) { if (value < 0) { sign_value = '-'; uvalue = -value; } } do { convert[place] = digits[uvalue % (unsigned)base]; if (caps) { convert[place] = toupper(convert[place]); } place++; uvalue = (uvalue / (unsigned)base); if (place > MAX_DIGITS) { return ERROR_RETVAL; } } while(uvalue); convert[place] = 0; pad_length = min_width - place; if (pad_length < 0) { pad_length = 0; } if (left_justify) { if (zero_pad && pad_length > 0) { if (sign_value) { CheckRet(put_char(this, sign_value)); --pad_length; sign_value = 0; } while (pad_length > 0) { CheckRet(put_char(this, '0')); --pad_length; } } else { while (pad_length > 0) { CheckRet(put_char(this, ' ')); --pad_length; } } } if (sign_value) { CheckRet(put_char(this, sign_value)); } while (place > 0 && --precision >= 0) { CheckRet(put_char(this, convert[--place])); } if (!left_justify) { while (pad_length > 0) { CheckRet(put_char(this, ' ')); --pad_length; } } return SUCCESS;}intjio_vsnprintf(char *str, size_t count, const char *fmt, va_list args){ char *strvalue; long value; InstanceData this; bool_t left_justify, zero_pad, long_flag, add_sign, fPrecision; int min_width, precision, ch; if (str == NULL) { return ERROR_RETVAL; } str[0] = '\0'; this.buffer = str; this.end = str + count - 1; *this.end = '\0'; /* ensure null-termination in case of failure */ while ((ch = *fmt++) != 0) { if (ch == '%') { zero_pad = long_flag = add_sign = fPrecision = FALSE; left_justify = TRUE; min_width = 0; precision = this.end - this.buffer; next_char: ch = *fmt++; switch (ch) { case 0: return ERROR_RETVAL; case '-': left_justify = FALSE; goto next_char; case '0': /* set zero padding if min_width not set */ if (min_width == 0) zero_pad = TRUE; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (fPrecision == TRUE) { precision = precision * 10 + (ch - '0'); } else { min_width = min_width * 10 + (ch - '0'); } goto next_char; case '.': fPrecision = TRUE; precision = 0; goto next_char; case 'l': long_flag = TRUE; goto next_char; case 's': strvalue = va_arg(args, char *); CheckRet(format_string(&this, strvalue, left_justify, min_width, precision)); break; case 'c': ch = va_arg(args, int); CheckRet(put_char(&this, ch)); break; case '%': CheckRet(put_char(&this, '%')); break; case 'd': case 'D': case 'u': case 'U': case 'o': case 'O': case 'x': case 'X': value = long_flag ? va_arg(args, long) : va_arg(args, int); CheckRet(format_number(&this, value, ch, left_justify, min_width, precision, zero_pad)); break; default: return ERROR_RETVAL; } } else { CheckRet(put_char(&this, ch)); } } *this.buffer = '\0'; return strlen(str);}int jio_snprintf(char *str, size_t count, const char *fmt, ...){ va_list args; int len; va_start(args, fmt); len = jio_vsnprintf(str, count, fmt, args); va_end(args); return len;}/* Return true if the two classes are in the same class package */bool_tIsSameClassPackage(ClassClass *class1, ClassClass *class2) { if (cbLoader(class1) != cbLoader(class2)) return FALSE; else { char *name1 = cbName(class1); char *name2 = cbName(class2); char *last_slash1 = strrchr(name1, DIR_SEPARATOR); char *last_slash2 = strrchr(name2, DIR_SEPARATOR); if ((last_slash1 == NULL) || (last_slash2 == NULL)) { /* One of the two doesn't have a package. Only return true * if the other one also doesn't have a package. */ return (last_slash1 == last_slash2); } else { int length1, length2; if (*name1 == SIGNATURE_ARRAY) { do name1++; while (*name1 == SIGNATURE_ARRAY); if (*name1 != SIGNATURE_CLASS) { /* Something is terribly wrong. Shouldn't be here */ return FALSE; } name1++; } if (*name2 == SIGNATURE_ARRAY) { do name2++; while (*name2 == SIGNATURE_ARRAY); if (*name2 != SIGNATURE_CLASS) { /* Something is terribly wrong. Shouldn't be here */ return FALSE; } name2++; } length1 = last_slash1 - name1; length2 = last_slash2 - name2; return ((length1 == length2) && (strncmp(name1, name2, length1) == 0)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -