[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
precision formatting in grub_printf
From: |
Deepak Vankadaru |
Subject: |
precision formatting in grub_printf |
Date: |
Sun, 17 Aug 2008 23:28:38 +0530 |
Hi
I have implemented precision formatting support in grub_printf. Following is the diff (of kern/misc.c)
Thanks
Deepak
###############start of diff
*** kern/misc.c 2008-08-18 04:46:36.000000000 +0530
--- /home/deepakv/orig/grub2-1.96+20080724/kern/misc.c 2008-06-16 06:12:48.000000000 +0530
***************
*** 565,572 ****
*p++ = (d > 9) ? d + 'a' - 10 : d + '0';
}
while (n /= base);
- if ( p == str)
- *p++ = '0';
*p = 0;
grub_reverse (str);
--- 565,570 ----
***************
*** 682,693 ****
write_char (*s++);
}
- void write_str_limit (const char *s, int count)
- {
- while (*s && count--)
- write_char (*s++);
- }
-
void write_fill (const char ch, int n)
{
int i;
--- 680,685 ----
***************
*** 704,711 ****
char tmp[32];
char *p;
unsigned int format1 = 0;
! unsigned int format2 = 1;
! grub_bool_t format2_default = TRUE;
char zerofill = ' ';
int rightfill = 0;
int n;
--- 696,702 ----
char tmp[32];
char *p;
unsigned int format1 = 0;
! unsigned int format2 = 3;
char zerofill = ' ';
int rightfill = 0;
int n;
***************
*** 723,737 ****
while (*p && grub_isdigit (*p))
p++;
! if (p > fmt || *p == '.')
{
char s[p - fmt + 1];
grub_strncpy (s, fmt, p - fmt);
s[p - fmt] = 0;
if (s[0] == '0')
zerofill = '0';
! if (p - fmt)
! format1 = grub_strtoul (s, 0, 10);
fmt = p;
if (*p && *p == '.')
{
--- 714,727 ----
while (*p && grub_isdigit (*p))
p++;
! if (p > fmt)
{
char s[p - fmt + 1];
grub_strncpy (s, fmt, p - fmt);
s[p - fmt] = 0;
if (s[0] == '0')
zerofill = '0';
! format1 = grub_strtoul (s, 0, 10);
fmt = p;
if (*p && *p == '.')
{
***************
*** 747,755 ****
format2 = grub_strtoul (fstr, 0, 10);
fmt = p;
}
- else
- format2 = 0;
- format2_default = FALSE;
}
}
--- 737,742 ----
***************
*** 790,806 ****
n = va_arg (args, int);
grub_itoa (tmp, c, n);
}
! if ( format2 == 0 && grub_strcmp (tmp, "0") == 0)
! break;
! if ( grub_strlen (tmp) > format2)
! format2 = grub_strlen (tmp);
! if (! rightfill && format2 < format1)
! write_fill (zerofill, format1 - format2);
! if ( grub_strlen (tmp) < format2)
! write_fill ('0', format2 - grub_strlen (tmp));
write_str (tmp);
! if (rightfill && format2 < format1)
! write_fill (zerofill, format1 - format2);
break;
case 'c':
--- 777,787 ----
n = va_arg (args, int);
grub_itoa (tmp, c, n);
}
! if (! rightfill && grub_strlen (tmp) < format1)
! write_fill (zerofill, format1 - grub_strlen (tmp));
write_str (tmp);
! if (rightfill && grub_strlen (tmp) < format1)
! write_fill (zerofill, format1 - grub_strlen (tmp));
break;
case 'c':
***************
*** 862,895 ****
p = va_arg (args, char *);
if (p)
{
! if ( format2_default )
! {
! if (!rightfill && grub_strlen (p) < format1)
! write_fill (' ', format1 - grub_strlen (p));
! write_str (p);
! if (rightfill && grub_strlen (p) < format1)
! write_fill (' ', format1 - grub_strlen (p));
! }
! else
! {
! if (!rightfill && format2 < format1)
! write_fill (' ', format1 - format2);
!
! if ( grub_strlen (p) < format2)
! {
! if ( format2 <= format1)
! write_fill (' ', format2 - grub_strlen (p));
! else
! write_fill (' ', format1 - grub_strlen (p));
! }
!
! write_str_limit (p, format2);
!
! if (rightfill && format2 < format1)
! write_fill (' ', format1 - format2);
! }
}
else
write_str ("(null)");
--- 843,855 ----
p = va_arg (args, char *);
if (p)
{
! if (!rightfill && grub_strlen (p) < format1)
! write_fill (zerofill, format1 - grub_strlen (p));
! write_str (p);
! if (rightfill && grub_strlen (p) < format1)
! write_fill (zerofill, format1 - grub_strlen (p));
}
else
write_str ("(null)");
###############end of diff
######start of test case
int main()
{
typedef int (*printf_ptr)(char const *str, ...);
printf_ptr p;
if(getenv("GRUB_PRINTF"))
{
printf("using grub_printf\n");
p = grub_printf;
}
else
{
printf("using printf\n");
p = printf;
}
p("##%4.2d##\n",1);
p("##%4.2d##\n",12);
p("##%4.2d##\n",123);
p("##%4.2d##\n",1234);
p("##%4.2d##\n",12345);
p("##%3.3d##\n",12);
p("##%3.3d##\n",123);
p("##%3.3d##\n",1234);
p("##%2.4d##\n",12345);
p("##%2.4d##\n",1234);
p("##%2.4d##\n",123);
p("##%2.4d##\n",12);
p("##%2.4d##\n",1);
p("##%.0d##\n",0);
p("##%.0d##\n",1);
p("##%4.2s##\n","1");
p("##%4.2s##\n","12");
p("##%4.2s##\n","123");
p("##%4.2s##\n","1234");
p("##%4.2s##\n","12345");
p("##%3.3s##\n","12");
p("##%3.3s##\n","123");
p("##%3.3s##\n","1234");
p("##%2.4s##\n","12345");
p("##%2.4s##\n","1234");
p("##%2.4s##\n","123");
p("##%2.4s##\n","12");
p("##%2.4s##\n","1");
return 0;
}
- precision formatting in grub_printf,
Deepak Vankadaru <=