1fb6c29d0Slogin // 2fb6c29d0Slogin // Created by longjin on 2022/1/22. 3fb6c29d0Slogin // 4fb6c29d0Slogin #include <common/kprint.h> 5fb6c29d0Slogin #include <common/printk.h> 6fb6c29d0Slogin 7fb6c29d0Slogin #include <common/spinlock.h> 8*abe3a6eaShanjiezhou #include <libs/lib_ui/textui.h> 9fb6c29d0Slogin #include <mm/mm.h> 10fb6c29d0Slogin 11fb6c29d0Slogin #include <common/math.h> 12fb6c29d0Slogin #include <common/string.h> 13fb6c29d0Slogin 14fb6c29d0Slogin static spinlock_t __printk_lock = {1}; 15fb6c29d0Slogin /** 16fb6c29d0Slogin * @brief 将数字按照指定的要求转换成对应的字符串(2~36进制) 17fb6c29d0Slogin * 18fb6c29d0Slogin * @param str 要返回的字符串 19fb6c29d0Slogin * @param num 要打印的数值 20fb6c29d0Slogin * @param base 基数 21fb6c29d0Slogin * @param field_width 区域宽度 22fb6c29d0Slogin * @param precision 精度 23fb6c29d0Slogin * @param flags 标志位 24fb6c29d0Slogin */ 25fb6c29d0Slogin static char *write_num(char *str, ul num, int base, int field_width, int precision, int flags); 26fb6c29d0Slogin 27fb6c29d0Slogin static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags); 28fb6c29d0Slogin 29fb6c29d0Slogin static int skip_and_atoi(const char **s) 30fb6c29d0Slogin { 31fb6c29d0Slogin /** 32fb6c29d0Slogin * @brief 获取连续的一段字符对应整数的值 33fb6c29d0Slogin * @param:**s 指向 指向字符串的指针 的指针 34fb6c29d0Slogin */ 35fb6c29d0Slogin int ans = 0; 36fb6c29d0Slogin while (is_digit(**s)) 37fb6c29d0Slogin { 38fb6c29d0Slogin ans = ans * 10 + (**s) - '0'; 39fb6c29d0Slogin ++(*s); 40fb6c29d0Slogin } 41fb6c29d0Slogin return ans; 42fb6c29d0Slogin } 43fb6c29d0Slogin 44fb6c29d0Slogin /** 45fb6c29d0Slogin * @brief 将字符串按照fmt和args中的内容进行格式化,当buf_size为-1时,字符串直接保存到buf中 46fb6c29d0Slogin * 否则将字符串前buf_size-1个字符放入,大小为buf_size的buf数组中 47fb6c29d0Slogin * 48fb6c29d0Slogin * @param buf 结果缓冲区 49fb6c29d0Slogin * @param fmt 格式化字符串 50fb6c29d0Slogin * @param args 内容 51fb6c29d0Slogin * @param buf_size buf_size为-1时,不指定buf的大小,否则buf大小为buf_size 52fb6c29d0Slogin * @return 最终字符串的长度 53fb6c29d0Slogin */ 54fb6c29d0Slogin static int __do_vsprintf(char *buf, const char *fmt, int buf_size, va_list args) 55fb6c29d0Slogin { 56fb6c29d0Slogin 57fb6c29d0Slogin // 当需要输出的字符串的指针为空时,使用该字符填充目标字符串的指针 58fb6c29d0Slogin static const char __end_zero_char = '\0'; 59fb6c29d0Slogin 60fb6c29d0Slogin char *str = NULL, *s = NULL, *end = NULL; 61fb6c29d0Slogin 62fb6c29d0Slogin str = buf; 63fb6c29d0Slogin 64fb6c29d0Slogin int flags; // 用来存储格式信息的bitmap 65fb6c29d0Slogin int field_width; //区域宽度 66fb6c29d0Slogin int precision; //精度 67fb6c29d0Slogin int qualifier; //数据显示的类型 68fb6c29d0Slogin int len; 69fb6c29d0Slogin 70fb6c29d0Slogin if (buf_size != -1) 71fb6c29d0Slogin { 72fb6c29d0Slogin end = buf + buf_size; 73fb6c29d0Slogin } 74fb6c29d0Slogin //开始解析字符串 75fb6c29d0Slogin for (; *fmt; ++fmt) 76fb6c29d0Slogin { 77fb6c29d0Slogin //内容不涉及到格式化,直接输出 78fb6c29d0Slogin if (*fmt != '%') 79fb6c29d0Slogin { 80fb6c29d0Slogin *str = *fmt; 81fb6c29d0Slogin ++str; 82fb6c29d0Slogin continue; 83fb6c29d0Slogin } 84fb6c29d0Slogin 85fb6c29d0Slogin //开始格式化字符串 86fb6c29d0Slogin 87fb6c29d0Slogin //清空标志位和field宽度 88fb6c29d0Slogin field_width = flags = 0; 89fb6c29d0Slogin 90fb6c29d0Slogin bool flag_tmp = true; 91fb6c29d0Slogin bool flag_break = false; 92fb6c29d0Slogin 93fb6c29d0Slogin ++fmt; 94fb6c29d0Slogin while (flag_tmp) 95fb6c29d0Slogin { 96fb6c29d0Slogin switch (*fmt) 97fb6c29d0Slogin { 98fb6c29d0Slogin case '\0': 99fb6c29d0Slogin //结束解析 100fb6c29d0Slogin flag_break = true; 101fb6c29d0Slogin flag_tmp = false; 102fb6c29d0Slogin break; 103fb6c29d0Slogin 104fb6c29d0Slogin case '-': 105fb6c29d0Slogin // 左对齐 106fb6c29d0Slogin flags |= LEFT; 107fb6c29d0Slogin ++fmt; 108fb6c29d0Slogin break; 109fb6c29d0Slogin case '+': 110fb6c29d0Slogin //在正数前面显示加号 111fb6c29d0Slogin flags |= PLUS; 112fb6c29d0Slogin ++fmt; 113fb6c29d0Slogin break; 114fb6c29d0Slogin case ' ': 115fb6c29d0Slogin flags |= SPACE; 116fb6c29d0Slogin ++fmt; 117fb6c29d0Slogin break; 118fb6c29d0Slogin case '#': 119fb6c29d0Slogin //在八进制数前面显示 '0o',在十六进制数前面显示 '0x' 或 '0X' 120fb6c29d0Slogin flags |= SPECIAL; 121fb6c29d0Slogin ++fmt; 122fb6c29d0Slogin break; 123fb6c29d0Slogin case '0': 124fb6c29d0Slogin //显示的数字之前填充‘0’来取代空格 125fb6c29d0Slogin flags |= PAD_ZERO; 126fb6c29d0Slogin ++fmt; 127fb6c29d0Slogin break; 128fb6c29d0Slogin default: 129fb6c29d0Slogin flag_tmp = false; 130fb6c29d0Slogin break; 131fb6c29d0Slogin } 132fb6c29d0Slogin } 133fb6c29d0Slogin if (flag_break) 134fb6c29d0Slogin break; 135fb6c29d0Slogin 136fb6c29d0Slogin //获取区域宽度 137fb6c29d0Slogin field_width = -1; 138fb6c29d0Slogin if (*fmt == '*') 139fb6c29d0Slogin { 140fb6c29d0Slogin field_width = va_arg(args, int); 141fb6c29d0Slogin ++fmt; 142fb6c29d0Slogin } 143fb6c29d0Slogin else if (is_digit(*fmt)) 144fb6c29d0Slogin { 145fb6c29d0Slogin field_width = skip_and_atoi(&fmt); 146fb6c29d0Slogin if (field_width < 0) 147fb6c29d0Slogin { 148fb6c29d0Slogin field_width = -field_width; 149fb6c29d0Slogin flags |= LEFT; 150fb6c29d0Slogin } 151fb6c29d0Slogin } 152fb6c29d0Slogin 153fb6c29d0Slogin //获取小数精度 154fb6c29d0Slogin precision = -1; 155fb6c29d0Slogin if (*fmt == '.') 156fb6c29d0Slogin { 157fb6c29d0Slogin ++fmt; 158fb6c29d0Slogin if (*fmt == '*') 159fb6c29d0Slogin { 160fb6c29d0Slogin precision = va_arg(args, int); 161fb6c29d0Slogin ++fmt; 162fb6c29d0Slogin } 163fb6c29d0Slogin else if (is_digit(*fmt)) 164fb6c29d0Slogin { 165fb6c29d0Slogin precision = skip_and_atoi(&fmt); 166fb6c29d0Slogin } 167fb6c29d0Slogin } 168fb6c29d0Slogin 169fb6c29d0Slogin //获取要显示的数据的类型 170fb6c29d0Slogin if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'Z') 171fb6c29d0Slogin { 172fb6c29d0Slogin qualifier = *fmt; 173fb6c29d0Slogin ++fmt; 174fb6c29d0Slogin } 175fb6c29d0Slogin //为了支持lld 176fb6c29d0Slogin if (qualifier == 'l' && *fmt == 'l', *(fmt + 1) == 'd') 177fb6c29d0Slogin ++fmt; 178fb6c29d0Slogin 179fb6c29d0Slogin //转化成字符串 180fb6c29d0Slogin long long *ip; 181fb6c29d0Slogin switch (*fmt) 182fb6c29d0Slogin { 183fb6c29d0Slogin //输出 % 184fb6c29d0Slogin case '%': 185fb6c29d0Slogin *str++ = '%'; 186fb6c29d0Slogin 187fb6c29d0Slogin break; 188fb6c29d0Slogin // 显示一个字符 189fb6c29d0Slogin case 'c': 190fb6c29d0Slogin //靠右对齐 191fb6c29d0Slogin if (!(flags & LEFT)) 192fb6c29d0Slogin { 193fb6c29d0Slogin while (--field_width > 0) 194fb6c29d0Slogin { 195fb6c29d0Slogin *str = ' '; 196fb6c29d0Slogin ++str; 197fb6c29d0Slogin } 198fb6c29d0Slogin } 199fb6c29d0Slogin 200fb6c29d0Slogin *str++ = (unsigned char)va_arg(args, int); 201fb6c29d0Slogin 202fb6c29d0Slogin while (--field_width > 0) 203fb6c29d0Slogin { 204fb6c29d0Slogin *str = ' '; 205fb6c29d0Slogin ++str; 206fb6c29d0Slogin } 207fb6c29d0Slogin 208fb6c29d0Slogin break; 209fb6c29d0Slogin 210fb6c29d0Slogin //显示一个字符串 211fb6c29d0Slogin case 's': 212fb6c29d0Slogin s = va_arg(args, char *); 213fb6c29d0Slogin if (!s) 214fb6c29d0Slogin s = &__end_zero_char; 215fb6c29d0Slogin len = strlen(s); 216fb6c29d0Slogin if (precision < 0) 217fb6c29d0Slogin { 218fb6c29d0Slogin //未指定精度 219fb6c29d0Slogin precision = len; 220fb6c29d0Slogin } 221fb6c29d0Slogin 222fb6c29d0Slogin else if (len > precision) 223fb6c29d0Slogin { 224fb6c29d0Slogin len = precision; 225fb6c29d0Slogin } 226fb6c29d0Slogin 227fb6c29d0Slogin //靠右对齐 228fb6c29d0Slogin if (!(flags & LEFT)) 229fb6c29d0Slogin while (len < field_width--) 230fb6c29d0Slogin { 231fb6c29d0Slogin *str = ' '; 232fb6c29d0Slogin ++str; 233fb6c29d0Slogin } 234fb6c29d0Slogin 235fb6c29d0Slogin for (int i = 0; i < len; i++) 236fb6c29d0Slogin { 237fb6c29d0Slogin *str = *s; 238fb6c29d0Slogin ++s; 239fb6c29d0Slogin ++str; 240fb6c29d0Slogin } 241fb6c29d0Slogin 242fb6c29d0Slogin while (len < field_width--) 243fb6c29d0Slogin { 244fb6c29d0Slogin *str = ' '; 245fb6c29d0Slogin ++str; 246fb6c29d0Slogin } 247fb6c29d0Slogin 248fb6c29d0Slogin break; 249fb6c29d0Slogin //以八进制显示字符串 250fb6c29d0Slogin case 'o': 251fb6c29d0Slogin flags |= SMALL; 252fb6c29d0Slogin case 'O': 253fb6c29d0Slogin flags |= SPECIAL; 254fb6c29d0Slogin if (qualifier == 'l') 255fb6c29d0Slogin str = write_num(str, va_arg(args, long long), 8, field_width, precision, flags); 256fb6c29d0Slogin else 257fb6c29d0Slogin str = write_num(str, va_arg(args, int), 8, field_width, precision, flags); 258fb6c29d0Slogin break; 259fb6c29d0Slogin 260fb6c29d0Slogin //打印指针指向的地址 261fb6c29d0Slogin case 'p': 262fb6c29d0Slogin if (field_width == 0) 263fb6c29d0Slogin { 264fb6c29d0Slogin field_width = 2 * sizeof(void *); 265fb6c29d0Slogin flags |= PAD_ZERO; 266fb6c29d0Slogin } 267fb6c29d0Slogin 268fb6c29d0Slogin str = write_num(str, (unsigned long)va_arg(args, void *), 16, field_width, precision, flags); 269fb6c29d0Slogin 270fb6c29d0Slogin break; 271fb6c29d0Slogin 272fb6c29d0Slogin //打印十六进制 273fb6c29d0Slogin case 'x': 274fb6c29d0Slogin flags |= SMALL; 275fb6c29d0Slogin case 'X': 276fb6c29d0Slogin // flags |= SPECIAL; 277fb6c29d0Slogin if (qualifier == 'l') 278fb6c29d0Slogin str = write_num(str, va_arg(args, ll), 16, field_width, precision, flags); 279fb6c29d0Slogin else 280fb6c29d0Slogin str = write_num(str, va_arg(args, int), 16, field_width, precision, flags); 281fb6c29d0Slogin break; 282fb6c29d0Slogin 283fb6c29d0Slogin //打印十进制有符号整数 284fb6c29d0Slogin case 'i': 285fb6c29d0Slogin case 'd': 286fb6c29d0Slogin 287fb6c29d0Slogin flags |= SIGN; 288fb6c29d0Slogin if (qualifier == 'l') 289fb6c29d0Slogin str = write_num(str, va_arg(args, long long), 10, field_width, precision, flags); 290fb6c29d0Slogin else 291fb6c29d0Slogin str = write_num(str, va_arg(args, int), 10, field_width, precision, flags); 292fb6c29d0Slogin break; 293fb6c29d0Slogin 294fb6c29d0Slogin //打印十进制无符号整数 295fb6c29d0Slogin case 'u': 296fb6c29d0Slogin if (qualifier == 'l') 297fb6c29d0Slogin str = write_num(str, va_arg(args, unsigned long long), 10, field_width, precision, flags); 298fb6c29d0Slogin else 299fb6c29d0Slogin str = write_num(str, va_arg(args, unsigned int), 10, field_width, precision, flags); 300fb6c29d0Slogin break; 301fb6c29d0Slogin 302fb6c29d0Slogin //输出有效字符数量到*ip对应的变量 303fb6c29d0Slogin case 'n': 304fb6c29d0Slogin 305fb6c29d0Slogin if (qualifier == 'l') 306fb6c29d0Slogin ip = va_arg(args, long long *); 307fb6c29d0Slogin else 308fb6c29d0Slogin ip = (ll *)va_arg(args, int *); 309fb6c29d0Slogin 310fb6c29d0Slogin *ip = str - buf; 311fb6c29d0Slogin break; 312fb6c29d0Slogin case 'f': 313fb6c29d0Slogin // 默认精度为3 314fb6c29d0Slogin // printk("1111\n"); 315fb6c29d0Slogin // va_arg(args, double); 316fb6c29d0Slogin // printk("222\n"); 317fb6c29d0Slogin 318fb6c29d0Slogin if (precision < 0) 319fb6c29d0Slogin precision = 3; 320fb6c29d0Slogin 321fb6c29d0Slogin str = write_float_point_num(str, va_arg(args, double), field_width, precision, flags); 322fb6c29d0Slogin 323fb6c29d0Slogin break; 324fb6c29d0Slogin 325fb6c29d0Slogin //对于不识别的控制符,直接输出 326fb6c29d0Slogin default: 327fb6c29d0Slogin *str++ = '%'; 328fb6c29d0Slogin if (*fmt) 329fb6c29d0Slogin *str++ = *fmt; 330fb6c29d0Slogin else 331fb6c29d0Slogin --fmt; 332fb6c29d0Slogin break; 333fb6c29d0Slogin } 334fb6c29d0Slogin } 335fb6c29d0Slogin //实现vsnprintf 的功能 336fb6c29d0Slogin if (buf_size > 0) 337fb6c29d0Slogin { 338fb6c29d0Slogin if (str < end) 339fb6c29d0Slogin { 340fb6c29d0Slogin *str = '\0'; 341fb6c29d0Slogin } 342fb6c29d0Slogin else 343fb6c29d0Slogin { 344fb6c29d0Slogin *(end-1) = '\0'; 345fb6c29d0Slogin } 346fb6c29d0Slogin return buf_size; 347fb6c29d0Slogin } 348fb6c29d0Slogin else 349fb6c29d0Slogin { 350fb6c29d0Slogin *str = '\0'; 351fb6c29d0Slogin } 352fb6c29d0Slogin 353fb6c29d0Slogin //返回缓冲区已有字符串的长度。 354fb6c29d0Slogin return str - buf; 355fb6c29d0Slogin } 356fb6c29d0Slogin 357fb6c29d0Slogin /** 358fb6c29d0Slogin * 将字符串按照fmt和args中的内容进行格式化,然后保存到buf中 359fb6c29d0Slogin * @param buf 结果缓冲区 360fb6c29d0Slogin * @param fmt 格式化字符串 361fb6c29d0Slogin * @param args 内容 362fb6c29d0Slogin * @return 最终字符串的长度 363fb6c29d0Slogin */ 364fb6c29d0Slogin int vsprintf(char *buf, const char *fmt, va_list args) 365fb6c29d0Slogin { 366fb6c29d0Slogin return __do_vsprintf(buf, fmt, -1, args); 367fb6c29d0Slogin } 368fb6c29d0Slogin 369fb6c29d0Slogin /** 370fb6c29d0Slogin * @brief 将字符串按照fmt和args中的内容进行格式化,截取字符串前buf_size-1,保存到buf中 371fb6c29d0Slogin * 372fb6c29d0Slogin * @param buf 结果缓冲区,大小为buf_size 373fb6c29d0Slogin * @param fmt 格式化字符串 374fb6c29d0Slogin * @param buf_size 缓冲区长度 375fb6c29d0Slogin * @param args 内容 376fb6c29d0Slogin * @return 最终字符串的长度 377fb6c29d0Slogin */ 378fb6c29d0Slogin int vsnprintf(char *buf, const char *fmt, int buf_size, va_list args) 379fb6c29d0Slogin { 380fb6c29d0Slogin return __do_vsprintf(buf, fmt, buf_size, args); 381fb6c29d0Slogin } 382fb6c29d0Slogin 383fb6c29d0Slogin static char *write_num(char *str, ul num, int base, int field_width, int precision, int flags) 384fb6c29d0Slogin { 385fb6c29d0Slogin /** 386fb6c29d0Slogin * @brief 将数字按照指定的要求转换成对应的字符串 387fb6c29d0Slogin * 388fb6c29d0Slogin * @param str 要返回的字符串 389fb6c29d0Slogin * @param num 要打印的数值 390fb6c29d0Slogin * @param base 基数 391fb6c29d0Slogin * @param field_width 区域宽度 392fb6c29d0Slogin * @param precision 精度 393fb6c29d0Slogin * @param flags 标志位 394fb6c29d0Slogin */ 395fb6c29d0Slogin 396fb6c29d0Slogin // 首先判断是否支持该进制 397fb6c29d0Slogin if (base < 2 || base > 36) 398fb6c29d0Slogin return 0; 399fb6c29d0Slogin char pad, sign, tmp_num[100]; 400fb6c29d0Slogin 401fb6c29d0Slogin const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 402fb6c29d0Slogin // 显示小写字母 403fb6c29d0Slogin if (flags & SMALL) 404fb6c29d0Slogin digits = "0123456789abcdefghijklmnopqrstuvwxyz"; 405fb6c29d0Slogin 406fb6c29d0Slogin if (flags & LEFT) 407fb6c29d0Slogin flags &= ~PAD_ZERO; 408fb6c29d0Slogin // 设置填充元素 409fb6c29d0Slogin pad = (flags & PAD_ZERO) ? '0' : ' '; 410fb6c29d0Slogin 411fb6c29d0Slogin sign = 0; 412fb6c29d0Slogin 413fb6c29d0Slogin if (flags & SIGN) 414fb6c29d0Slogin { 415fb6c29d0Slogin int64_t signed_num = (int64_t)num; 416fb6c29d0Slogin if (signed_num < 0) 417fb6c29d0Slogin { 418fb6c29d0Slogin sign = '-'; 419fb6c29d0Slogin num = -signed_num; 420fb6c29d0Slogin } 421fb6c29d0Slogin else 422fb6c29d0Slogin num = signed_num; 423fb6c29d0Slogin } 424fb6c29d0Slogin else 425fb6c29d0Slogin { 426fb6c29d0Slogin // 设置符号 427fb6c29d0Slogin sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0); 428fb6c29d0Slogin } 429fb6c29d0Slogin 430fb6c29d0Slogin // sign占用了一个宽度 431fb6c29d0Slogin if (sign) 432fb6c29d0Slogin --field_width; 433fb6c29d0Slogin 434fb6c29d0Slogin if (flags & SPECIAL) 435fb6c29d0Slogin if (base == 16) // 0x占用2个位置 436fb6c29d0Slogin field_width -= 2; 437fb6c29d0Slogin else if (base == 8) // O占用一个位置 438fb6c29d0Slogin --field_width; 439fb6c29d0Slogin 440fb6c29d0Slogin int js_num = 0; // 临时数字字符串tmp_num的长度 441fb6c29d0Slogin 442fb6c29d0Slogin if (num == 0) 443fb6c29d0Slogin tmp_num[js_num++] = '0'; 444fb6c29d0Slogin else 445fb6c29d0Slogin { 446fb6c29d0Slogin num = ABS(num); 447fb6c29d0Slogin //进制转换 448fb6c29d0Slogin while (num > 0) 449fb6c29d0Slogin { 450fb6c29d0Slogin tmp_num[js_num++] = digits[num % base]; // 注意这里,输出的数字,是小端对齐的。低位存低位 451fb6c29d0Slogin num /= base; 452fb6c29d0Slogin } 453fb6c29d0Slogin } 454fb6c29d0Slogin 455fb6c29d0Slogin if (js_num > precision) 456fb6c29d0Slogin precision = js_num; 457fb6c29d0Slogin 458fb6c29d0Slogin field_width -= precision; 459fb6c29d0Slogin 460fb6c29d0Slogin // 靠右对齐 461fb6c29d0Slogin if (!(flags & (LEFT + PAD_ZERO))) 462fb6c29d0Slogin while (field_width-- > 0) 463fb6c29d0Slogin *str++ = ' '; 464fb6c29d0Slogin 465fb6c29d0Slogin if (sign) 466fb6c29d0Slogin *str++ = sign; 467fb6c29d0Slogin if (flags & SPECIAL) 468fb6c29d0Slogin if (base == 16) 469fb6c29d0Slogin { 470fb6c29d0Slogin *str++ = '0'; 471fb6c29d0Slogin *str++ = digits[33]; 472fb6c29d0Slogin } 473fb6c29d0Slogin else if (base == 8) 474fb6c29d0Slogin *str++ = digits[24]; //注意这里是英文字母O或者o 475fb6c29d0Slogin if (!(flags & LEFT)) 476fb6c29d0Slogin while (field_width-- > 0) 477fb6c29d0Slogin *str++ = pad; 478fb6c29d0Slogin while (js_num < precision) 479fb6c29d0Slogin { 480fb6c29d0Slogin --precision; 481fb6c29d0Slogin *str++ = '0'; 482fb6c29d0Slogin } 483fb6c29d0Slogin 484fb6c29d0Slogin while (js_num-- > 0) 485fb6c29d0Slogin *str++ = tmp_num[js_num]; 486fb6c29d0Slogin 487fb6c29d0Slogin while (field_width-- > 0) 488fb6c29d0Slogin *str++ = ' '; 489fb6c29d0Slogin 490fb6c29d0Slogin return str; 491fb6c29d0Slogin } 492fb6c29d0Slogin 493fb6c29d0Slogin static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags) 494fb6c29d0Slogin { 495fb6c29d0Slogin /** 496fb6c29d0Slogin * @brief 将浮点数按照指定的要求转换成对应的字符串 497fb6c29d0Slogin * 498fb6c29d0Slogin * @param str 要返回的字符串 499fb6c29d0Slogin * @param num 要打印的数值 500fb6c29d0Slogin * @param field_width 区域宽度 501fb6c29d0Slogin * @param precision 精度 502fb6c29d0Slogin * @param flags 标志位 503fb6c29d0Slogin */ 504fb6c29d0Slogin 505fb6c29d0Slogin char pad, sign, tmp_num_z[100], tmp_num_d[350]; 506fb6c29d0Slogin 507fb6c29d0Slogin const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 508fb6c29d0Slogin // 显示小写字母 509fb6c29d0Slogin if (flags & SMALL) 510fb6c29d0Slogin digits = "0123456789abcdefghijklmnopqrstuvwxyz"; 511fb6c29d0Slogin 512fb6c29d0Slogin // 设置填充元素 513fb6c29d0Slogin pad = (flags & PAD_ZERO) ? '0' : ' '; 514fb6c29d0Slogin sign = 0; 515fb6c29d0Slogin if (flags & SIGN && num < 0) 516fb6c29d0Slogin { 517fb6c29d0Slogin sign = '-'; 518fb6c29d0Slogin num = -num; 519fb6c29d0Slogin } 520fb6c29d0Slogin else 521fb6c29d0Slogin { 522fb6c29d0Slogin // 设置符号 523fb6c29d0Slogin sign = (flags & PLUS) ? '+' : ((flags & SPACE) ? ' ' : 0); 524fb6c29d0Slogin } 525fb6c29d0Slogin 526fb6c29d0Slogin // sign占用了一个宽度 527fb6c29d0Slogin if (sign) 528fb6c29d0Slogin --field_width; 529fb6c29d0Slogin 530fb6c29d0Slogin int js_num_z = 0, js_num_d = 0; // 临时数字字符串tmp_num_z tmp_num_d的长度 531fb6c29d0Slogin uint64_t num_z = (uint64_t)(num); // 获取整数部分 532fb6c29d0Slogin uint64_t num_decimal = (uint64_t)(round(1.0 * (num - num_z) * pow(10, precision))); // 获取小数部分 533fb6c29d0Slogin 534fb6c29d0Slogin if (num == 0 || num_z == 0) 535fb6c29d0Slogin tmp_num_z[js_num_z++] = '0'; 536fb6c29d0Slogin else 537fb6c29d0Slogin { 538fb6c29d0Slogin //存储整数部分 539fb6c29d0Slogin while (num_z > 0) 540fb6c29d0Slogin { 541fb6c29d0Slogin tmp_num_z[js_num_z++] = digits[num_z % 10]; // 注意这里,输出的数字,是小端对齐的。低位存低位 542fb6c29d0Slogin num_z /= 10; 543fb6c29d0Slogin } 544fb6c29d0Slogin } 545fb6c29d0Slogin 546fb6c29d0Slogin while (num_decimal > 0) 547fb6c29d0Slogin { 548fb6c29d0Slogin tmp_num_d[js_num_d++] = digits[num_decimal % 10]; 549fb6c29d0Slogin num_decimal /= 10; 550fb6c29d0Slogin } 551fb6c29d0Slogin 552fb6c29d0Slogin field_width -= (precision + 1 + js_num_z); 553fb6c29d0Slogin 554fb6c29d0Slogin // 靠右对齐 555fb6c29d0Slogin if (!(flags & LEFT)) 556fb6c29d0Slogin while (field_width-- > 0) 557fb6c29d0Slogin *str++ = pad; 558fb6c29d0Slogin 559fb6c29d0Slogin if (sign) 560fb6c29d0Slogin *str++ = sign; 561fb6c29d0Slogin 562fb6c29d0Slogin // 输出整数部分 563fb6c29d0Slogin while (js_num_z > 0) 564fb6c29d0Slogin { 565fb6c29d0Slogin *str++ = tmp_num_z[js_num_z - 1]; 566fb6c29d0Slogin --js_num_z; 567fb6c29d0Slogin } 568fb6c29d0Slogin *str++ = '.'; 569fb6c29d0Slogin 570fb6c29d0Slogin // 输出小数部分 571fb6c29d0Slogin int total_dec_count = js_num_d; 572fb6c29d0Slogin for (int i = 0; i < precision && js_num_d-- > 0; ++i) 573fb6c29d0Slogin *str++ = tmp_num_d[js_num_d]; 574fb6c29d0Slogin 575fb6c29d0Slogin while (total_dec_count < precision) 576fb6c29d0Slogin { 577fb6c29d0Slogin ++total_dec_count; 578fb6c29d0Slogin *str++ = '0'; 579fb6c29d0Slogin } 580fb6c29d0Slogin 581fb6c29d0Slogin while (field_width-- > 0) 582fb6c29d0Slogin *str++ = ' '; 583fb6c29d0Slogin 584fb6c29d0Slogin return str; 585fb6c29d0Slogin } 586fb6c29d0Slogin 587fb6c29d0Slogin /** 588fb6c29d0Slogin * @brief 格式化打印字符串 589fb6c29d0Slogin * 590fb6c29d0Slogin * @param FRcolor 前景色 591fb6c29d0Slogin * @param BKcolor 背景色 592fb6c29d0Slogin * @param ... 格式化字符串 593fb6c29d0Slogin */ 594fb6c29d0Slogin int printk_color(unsigned int FRcolor, unsigned int BKcolor, const char *fmt, ...) 595fb6c29d0Slogin { 596fb6c29d0Slogin uint64_t rflags; 59740fe15e0SLoGin io_mfence(); 598fb6c29d0Slogin spin_lock_irqsave(&__printk_lock, rflags); 59940fe15e0SLoGin io_mfence(); 600fb6c29d0Slogin va_list args; 601fb6c29d0Slogin va_start(args, fmt); 602fb6c29d0Slogin char buf[4096]; // vsprintf()的缓冲区 603fb6c29d0Slogin int len = vsprintf(buf, fmt, args); 604fb6c29d0Slogin 605fb6c29d0Slogin va_end(args); 606fb6c29d0Slogin unsigned char current; 607fb6c29d0Slogin 608fb6c29d0Slogin int i; // 总共输出的字符数 609fb6c29d0Slogin for (i = 0; i < len; ++i) 610fb6c29d0Slogin { 611fb6c29d0Slogin current = *(buf + i); 612fb6c29d0Slogin // 输出 613*abe3a6eaShanjiezhou rs_textui_putchar(current, FRcolor, BKcolor); 614fb6c29d0Slogin } 61540fe15e0SLoGin io_mfence(); 616fb6c29d0Slogin spin_unlock_irqrestore(&__printk_lock, rflags); 61740fe15e0SLoGin io_mfence(); 618fb6c29d0Slogin return i; 619fb6c29d0Slogin } 620fb6c29d0Slogin 621fb6c29d0Slogin int sprintk(char *buf, const char *fmt, ...) 622fb6c29d0Slogin { 623fb6c29d0Slogin int count = 0; 624fb6c29d0Slogin va_list args; 625fb6c29d0Slogin 626fb6c29d0Slogin va_start(args, fmt); 627fb6c29d0Slogin count = vsprintf(buf, fmt, args); 628fb6c29d0Slogin va_end(args); 629fb6c29d0Slogin 630fb6c29d0Slogin return count; 631fb6c29d0Slogin } 632