Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
Software for White Rabbit PTP Core
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
31
Issues
31
List
Board
Labels
Milestones
Merge Requests
4
Merge Requests
4
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
image/svg+xml
Discourse
Discourse
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
Software for White Rabbit PTP Core
Commits
a89b3443
Commit
a89b3443
authored
Apr 08, 2011
by
Tomasz Wlostowski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added library routines
parent
0e8f8909
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
251 additions
and
0 deletions
+251
-0
mprintf.c
lib/mprintf.c
+251
-0
No files found.
lib/mprintf.c
0 → 100644
View file @
a89b3443
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "uart.h"
int
vprintf
(
char
const
*
format
,
va_list
ap
)
{
unsigned
char
scratch
[
16
];
unsigned
char
format_flag
;
unsigned
int
u_val
=
0
;
unsigned
char
base
;
unsigned
char
*
ptr
;
unsigned
char
width
=
0
;
unsigned
char
fill
;
while
(
1
)
{
width
=
0
;
fill
=
' '
;
while
((
format_flag
=
*
format
++
)
!=
'%'
)
{
if
(
!
format_flag
)
{
va_end
(
ap
);
return
(
0
);
}
uart_write_byte
(
format_flag
);
}
// check for zero pad
format_flag
=
*
format
-
'0'
;
if
(
format_flag
==
0
)
// zero pad
{
fill
=
'0'
;
format
++
;
}
// check for width spec
format_flag
=
*
format
-
'0'
;
if
(
format_flag
>
0
&&
format_flag
<=
9
)
// width set
{
width
=
format_flag
;
format
++
;
}
switch
(
format_flag
=
*
format
++
)
{
case
'c'
:
format_flag
=
va_arg
(
ap
,
int
);
//fall through
default:
uart_write_byte
(
format_flag
);
continue
;
case
'S'
:
case
's'
:
ptr
=
(
unsigned
char
*
)
va_arg
(
ap
,
char
*
);
while
(
*
ptr
)
uart_write_byte
(
*
ptr
++
);
continue
;
case
'd'
:
base
=
10
;
goto
CONVERSION_LOOP
;
case
'u'
:
base
=
10
;
goto
CONVERSION_LOOP
;
case
'x'
:
base
=
16
;
CONVERSION_LOOP:
u_val
=
va_arg
(
ap
,
unsigned
int
);
if
((
format_flag
==
'd'
)
&&
(
u_val
&
0x80000000
))
{
uart_write_byte
(
'-'
);
u_val
=-
u_val
;
}
ptr
=
scratch
+
16
;
*--
ptr
=
0
;
do
{
char
ch
=
(
u_val
%
base
)
+
'0'
;
if
(
ch
>
'9'
)
ch
+=
'a'
-
'9'
-
1
;
*--
ptr
=
ch
;
u_val
/=
base
;
if
(
width
)
width
--
;
}
while
(
u_val
>
0
);
// while (width--)
// *--ptr = fill;
while
(
*
ptr
)
uart_write_byte
(
*
ptr
++
);
}
}
return
0
;
}
static
int
_p_vsprintf
(
char
const
*
format
,
va_list
ap
,
char
*
dst
)
{
unsigned
char
scratch
[
16
];
unsigned
char
format_flag
;
unsigned
int
u_val
=
0
;
unsigned
char
base
;
unsigned
char
*
ptr
;
unsigned
char
width
=
0
;
unsigned
char
fill
;
while
(
1
)
{
width
=
0
;
fill
=
' '
;
while
((
format_flag
=
*
format
++
)
!=
'%'
)
{
if
(
!
format_flag
)
{
va_end
(
ap
);
*
dst
++=
0
;
return
(
0
);
}
*
dst
++=
format_flag
;
}
// check for zero pad
format_flag
=
*
format
-
'0'
;
if
(
format_flag
==
0
)
// zero pad
{
fill
=
'0'
;
format
++
;
}
// check for width spec
format_flag
=
*
format
-
'0'
;
if
(
format_flag
>
0
&&
format_flag
<=
9
)
// width set
{
width
=
format_flag
;
format
++
;
}
switch
(
format_flag
=
*
format
++
)
{
case
'c'
:
format_flag
=
va_arg
(
ap
,
int
);
//fall through
default:
*
dst
++=
format_flag
;
continue
;
case
'S'
:
case
's'
:
ptr
=
(
unsigned
char
*
)
va_arg
(
ap
,
char
*
);
while
(
*
ptr
)
*
dst
++=*
ptr
++
;
continue
;
case
'd'
:
case
'u'
:
base
=
10
;
goto
CONVERSION_LOOP
;
case
'x'
:
base
=
16
;
CONVERSION_LOOP:
u_val
=
va_arg
(
ap
,
unsigned
int
);
ptr
=
scratch
+
16
;
*--
ptr
=
0
;
do
{
char
ch
=
(
u_val
%
base
)
+
'0'
;
if
(
ch
>
'9'
)
ch
+=
'a'
-
'9'
-
1
;
*--
ptr
=
ch
;
u_val
/=
base
;
if
(
width
)
width
--
;
}
while
(
u_val
>
0
);
// while (width--)
// *--ptr = fill;
while
(
*
ptr
)
*
dst
++=*
ptr
++
;
}
}
*
dst
++=
0
;
return
0
;
}
int
mprintf
(
char
const
*
format
,
...)
{
int
rval
;
va_list
ap
;
va_start
(
ap
,
format
);
rval
=
vprintf
(
format
,
ap
);
va_end
(
ap
);
return
rval
;
}
int
sprintf
(
char
*
dst
,
char
const
*
format
,
...)
{
va_list
ap
;
va_start
(
ap
,
format
);
int
r
=
_p_vsprintf
(
format
,
ap
,
dst
);
return
r
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment