/*
* file printf.c
*/
#include <stdarg.h>
#include "printf.h"
/********************************************************************/
typedef struct {
int dest;
void (*func)(char);
char *loc;
} PRINTK_INFO;
int
printk(PRINTK_INFO *, const char *, va_list);
/********************************************************************/
#define DEST_CONSOLE (1)
#define DEST_STRING (2)
#define FLAGS_MINUS (0x01)
#define FLAGS_PLUS (0x02)
#define FLAGS_SPACE (0x04)
#define FLAGS_ZERO (0x08)
#define FLAGS_POUND (0x10)
#define IS_FLAG_MINUS(a) (a & FLAGS_MINUS)
#define IS_FLAG_PLUS(a) (a & FLAGS_PLUS)
#define IS_FLAG_SPACE(a) (a & FLAGS_SPACE)
#define IS_FLAG_ZERO(a) (a & FLAGS_ZERO)
#define IS_FLAG_POUND(a) (a & FLAGS_POUND)
#define LENMOD_h (0x01)
#define LENMOD_l (0x02)
#define LENMOD_L (0x04)
#define IS_LENMOD_h(a) (a & LENMOD_h)
#define IS_LENMOD_l(a) (a & LENMOD_l)
#define IS_LENMOD_L(a) (a & LENMOD_L)
#define FMT_d (0x0001)
#define FMT_o (0x0002)
#define FMT_x (0x0004)
#define FMT_X (0x0008)
#define FMT_u (0x0010)
#define FMT_c (0x0020)
#define FMT_s (0x0040)
#define FMT_p (0x0080)
#define FMT_n (0x0100)
#define IS_FMT_d(a) (a & FMT_d)
#define IS_FMT_o(a) (a & FMT_o)
#define IS_FMT_x(a) (a & FMT_x)
#define IS_FMT_X(a) (a & FMT_X)
#define IS_FMT_u(a) (a & FMT_u)
#define IS_FMT_c(a) (a & FMT_c)
#define IS_FMT_s(a) (a & FMT_s)
#define IS_FMT_p(a) (a & FMT_p)
#define IS_FMT_n(a) (a & FMT_n)
/********************************************************************/
static void printk_putc(int c, int *count, PRINTK_INFO *info) {
switch (info->dest) {
case DEST_CONSOLE:
info->func((char) c);
break;
case DEST_STRING:
*(info->loc) = (unsigned char) c;
++(info->loc);
break;
default:
break;
}
*count += 1;
}
/********************************************************************/
static int printk_mknumstr(char *numstr, void *nump, int neg, int radix) {
int a, b, c;
unsigned int ua, ub, uc;
int nlen;
char *nstrp;
nlen = 0;
nstrp = numstr;
*nstrp++ = '\0';
if (neg) {
a = *(int *) nump;
if (a == 0) {
*nstrp = '0';
++nlen;
goto done;
}
while (a != 0) {
b = (int) a / (int) radix;
c = (int) a - ((int) b * (int) radix);
if (c < 0) {
c = ~c + 1 + '0';
} else {
c = c + '0';
}
a = b;
*nstrp++ = (char) c;
++nlen;
}
} else {
ua = *(unsigned int *) nump;
if (ua == 0) {
*nstrp = '0';
++nlen;
goto done;
}
while (ua != 0) {
ub = (unsigned int) ua / (unsigned int) radix;
uc = (unsigned int) ua - ((unsigned int) ub * (unsigned int) radix);
if (uc < 10) {
uc = uc + '0';
} else {
uc = uc - 10 + 'A';
}
ua = ub;
*nstrp++ = (char) uc;
++nlen;
}
}
done: return nlen;
}
/********************************************************************/
static void printk_pad_zero(int curlen, int field_width, int *count,
PRINTK_INFO *info) {
int i;
for (i = curlen; i < field_width; i++) {
printk_putc('0', count, info);
}
}
/********************************************************************/
static void printk_pad_space(int curlen, int field_width, int *count,
PRINTK_INFO *info) {
int i;
for (i = curlen; i < field_width; i++) {
printk_putc(' ', count, info);
}
}
/********************************************************************/
int printk(PRINTK_INFO *info, const char *fmt, va_list ap) {
/* va_list ap; */
char *p;
int c;
char vstr[33];
char *vstrp;
int vlen;
int done;
int count = 0;
int flags_used;
int field_width;
#if 0
int precision_used;
int precision_width;
int length_modifier;
#endif
int ival;
int schar, dschar;
int *ivalp;
char *sval;
int cval;
unsigned int uval;
/*
* Start parsing apart the format string and display appropriate
* formats and data.
*/
for (p = (char *) fmt; (c = *p) != 0; p++) {
/*
* All formats begin with a '%' marker. Special chars like
* '\n' or '\t' are normally converted to the appropriate
* character by the __compiler__. Thus, no need for this
* routine to account for the '\' character.
*/
if (c != '%') {
/*
* This needs to be replaced with something like
* 'out_char()' or call an OS routine.
*/
#ifndef UNIX_DEBUG
if (c != '\n') {
printk_putc(c, &count, info);
} else {
printk_putc(0x0D /* CR */, &count, info);
printk_putc(0x0A /* LF */, &count, info);
}
#else
printk_putc(c, &count, info);
#endif
/*
* By using 'continue', the next iteration of the loop
* is used, skipping the code that follows.
*/
continue;
}
/*
* First check for specification modifier flags.
*/
flags_used = 0;
done = FALSE;
while (!done) {
switch (/* c = */*++p) {
case '-':
flags_used |= FLAGS_MINUS;
break;
case '+':
flags_used |= FLAGS_PLUS;
break;
case ' ':
flags_used |= FLAGS_SPACE;
break;
case '0':
flags_used |= FLAGS_ZERO;
break;
case '#':
flags_used |= FLAGS_POUND;
break;
default:
/* we've gone one char too far */
--p;
done = TRUE;
break;
}
}
/*
* Next check for minimum field width.
*/
field_width = 0;
done = FALSE;
while (!done) {
switch (c = *++p) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
field_width = (field_width * 10) + (c - '0');
break;
default:
/* we've gone one char too far */
--p;
done = TRUE;
break;
}
}
/*
* Next check for the width and precision field separator.
*/
if (/* (c = *++p) */*++p == '.') {
/* precision_used = TRUE; */
/*
* Must get precision field width, if present.
*/
/* precision_width = 0; */
done = FALSE;
while (!done) {
switch (/* c = uncomment if used below */*++p) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
#if 0
precision_width = (precision_width * 10) +
(c - '0');
#endif
break;
default:
/* we've gone one char too far */
--p;
done = TRUE;
break;
}
}
} else {
/* we've gone one char too far */
--p;
#if 0
precision_used = FALSE;
precision_width = 0;
#endif
}
/*
* Check for the length modifier.
*/
/* length_modifier = 0; */
switch (/* c = */*++p) {
case 'h':
/* length_modifier |= LENMOD_h; */
break;
case 'l':
/* length_modifier |= LENMOD_l; */
break;
case 'L':
/* length_modifier |= LENMOD_L; */
break;
default:
/* we've gone one char too far */
--p;
break;
}
/*
* Now we're ready to examine the format.
*/
switch (c = *++p) {
case 'd':
case 'i':
ival = (int) va_arg(ap, int);
vlen = printk_mknumstr(vstr, &ival, TRUE, 10);
vstrp = &vstr[vlen];
if (ival < 0) {
schar = '-';
++vlen;
} else {
if (IS_FLAG_PLUS(flags_used)) {
schar = '+';
++vlen;
} else {
if (IS_FLAG_SPACE(flags_used)) {
schar = ' ';
++vlen;
} else {
schar = 0;
}
}
}
dschar = FALSE;
/*
* do the ZERO pad.
*/
if (IS_FLAG_ZERO(flags_used)) {
if (schar)
printk_putc(schar, &count, info);
dschar = TRUE;
printk_pad_zero(vlen, field_width, &count, info);
vlen = field_width;
} else {
if (!IS_FLAG_MINUS(flags_used)) {
printk_pad_space(vlen, field_width, &count, info);
if (schar)
printk_putc(schar, &count, info);
dschar =
m0_48951476
- 粉丝: 0
- 资源: 4
最新资源
- Java毕业设计-springboot-vue-综合小区管理系统(源码+sql脚本+29页零基础部署图文详解+33页论文+环境工具+教程+视频+模板).zip
- Java毕业设计-springboot-vue-足球社区管理系统(源码+sql脚本+29页零基础部署图文详解+35页论文+环境工具+教程+视频+模板).zip
- Java毕业设计-springboot-vue-足球俱乐部管理系统(源码+sql脚本+29页零基础部署图文详解+31页论文+环境工具+教程+视频+模板).zip
- cursor使用学习研究ai-intelligent-assistant-master.zip
- Snipaste-1.16.2-x64-win 截图 贴图工具
- 信捷PLC与威纶通触摸屏控制非标设备机架旋铆机:多点位设定、运动控制与配方宏功能实现,信捷PLC程序 信捷PLC+威纶通触摸屏 非标设备机架旋铆机,可设定多点位,示教功能,配方功能
- python + nodejs实现数据采集
- Hypermesh与Dyna联合构建的整车碰撞模型仿真与实验对标研究报告:对标Honda Accord车型碰撞模型和报告分析(详尽版),Hypermesh+dyna整车碰撞模型( 仿真+试验对标) 国
- 基于Matlab与Yalmip求解器的智能软开关配电网重构模型:考虑二阶锥与多种约束条件的连通性和辐射性研究,智能软开关 配电网重构matlab 二阶锥 编程方法:matlab+yalmip(cple
- 计及条件风险价值的电气综合能源系统能量与备用调度分布鲁棒优化模型,matlab代码:计及条件风险价值的电气综合能源系统能量-备用分布鲁棒优化 关键词:wasserstein距离 CVAR条件风险价值
- 基于 DeepSeek-V2 和 LangGraph 为 Python 代码自动编写单元测试的源码
- 基于Moire光子晶体的大容量、高性能能带结构分析与研究-自由度高达300万次的独特挑战,Moire光子晶体能带 300w+自由度,需自己执行!!! ,核心关键词:Moire光子晶体;能带;300
- PowerBI svg 折线面积图
- 高频注入模型的脉振永磁同步电机无传感器矢量控制:可加载与转速辨识功能,高频注入模型,可加载,可加载,可加载 (1)基于脉振高频注入的永磁同步电机无速度传感器矢量控制MATLAB仿真模型; (2)模型可
- 新能源汽车车载充放电机系统MATLAB仿真模型:双向AC DC与双向DC DC技术结合V2G功能,新能源汽车车载双向OBC,PFC,LLC,V2G 双向 充电桩 电动汽车 车载充电机 充放电机 MAT
- 高频隔离型光伏离网单相逆变器控制算法:高频移相全桥升压+谐振控制器+SOGI双闭环dq解耦技术+仿真验证,高频隔离型光伏离网单相逆变器的控制算法的C代码+仿真模型,DC70~150V输入,AC220V
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈