Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

output: Fixed bug in printnum and added comments

  • Loading branch information...
commit a9b2da339a849f470724549852dbf49e10bbbb42 1 parent b54094b
@bemk authored
Showing with 56 additions and 16 deletions.
  1. +2 −5 src/andromeda/core.c
  2. +54 −11 src/drivers/text/out.c
View
7 src/andromeda/core.c
@@ -96,11 +96,8 @@ void test_sprintf()
if (test == NULL)
panic("OUT OF MEMORY!");
memset(test, 0, 255);
- int ret = sprintf(test, "hello: %8Xschaap\n \n", 0xC0DEBABE);
- printf("%X\t%s\n", ret, test);
- int i = 0;
- for (; test[i] != '\0'; i++)
- printf("%X = %c\n", test[i], test[i]);
+ int ret = sprintf(test, "hello: %8X\n", 0xC0DE);
+ printf("%X\t%s", ret, test);
free(test);
}
View
65 src/drivers/text/out.c
@@ -406,54 +406,84 @@ void putc(uint8_t c)
reloc(cursor.x, cursor.y);
}
-int sprintnum(char* str, size_t min_size, int num, int base, bool capital, bool sign)
+#define PRINTNUM_PADDING '0'
+/**
+ * \fn sprintnum
+ * \brief Write integer numbers according to a format and base number.
+ * \param str
+ * \brief The string to write to
+ * \param min_size
+ * \brief How many chars must this fill?
+ * \param num
+ * \brief What number do we print?
+ * \param base
+ * \brief The base number for the printed number
+ * \param capital
+ * \brief Can we use capitals in the text?
+ * \param sign
+ * \brief Is the integer signed or not?
+ * \return The number of chars printed.
+ */
+int
+sprintnum(char* str, size_t min_size, int num, int base, bool capital, bool sign)
{
if (base > 36 || base < 2)
return -E_INVALID_ARG;
-
+ /** If num == 0, the result is always the same, so optimize that out */
if (num == 0)
{
int i = 0;
for (; i < min_size-1; i++)
{
- *(str++) = ' ';
+ *(str++) = PRINTNUM_PADDING;
}
*(str++) = '0';
return min_size;
}
int32_t idx = 0;
uint32_t unum = (uint32_t)num;
+ /** If signedness is allowed, check for signedness */
if (num < 0 && sign)
unum = -num;
char tmp_str[32];
memset(tmp_str, 0, sizeof(tmp_str));
+ /**
+ * Convert the integer into an ascii representation
+ * This unfortunately reverses the string order
+ */
for (;unum != 0; idx++)
{
- tmp_str[sizeof(tmp_str) - idx] = (capital) ? HEX[unum % base] : hex[unum % base];
+ tmp_str[sizeof(tmp_str) - idx] = (capital) ? HEX[unum % base] :
+ hex[unum % base];
unum /= base;
}
+ /** If signed and negative, append the - sign */
if (num < 0 && sign)
{
tmp_str[sizeof(tmp_str) - idx] = '-';
idx++;
}
int ret = idx;
+ /**
+ * If the string doesn't cut the minimal length requirements, make it a
+ * little longer by appending a couple of characters
+ */
if (idx < min_size)
{
int i = 0;
for (; i < min_size - idx; i++)
- *(str++) = ' ';
+ *(str++) = PRINTNUM_PADDING;
ret = min_size;
}
idx --;
+ /**
+ * Now take the temp string, reverse it and put it in the output string
+ * The reversal to get the correct order again.
+ */
for (; idx >= 0; idx--)
- {
- int i = 0;
- for (; i < 0x1FFFFFFF; i++);
*(str++) = tmp_str[sizeof(tmp_str) - idx];
- }
return ret;
}
@@ -470,14 +500,20 @@ int sprintnum(char* str, size_t min_size, int num, int base, bool capital, bool
*/
int sprintf(char* str, char* fmt, ...)
{
+ /** Check the preconditions first. */
+ if (str == NULL || fmt == NULL)
+ return 0;
int num = 0;
va_list list;
va_start(list, fmt);
+ /** Itterate through the string to put every character in place. */
for (; *fmt != '\0'; fmt++, str++, num++)
{
+ /** If formatted? */
if (*fmt == '%')
{
+ /** Interpret the format numbering. */
int pre = 0;
int post = 0;
bool dotted = false;
@@ -505,6 +541,7 @@ int sprintf(char* str, char* fmt, ...)
if (post == 0)
post = 1;
int inc = 0;
+ /** Now finally choose the type of format. */
switch(*(++fmt))
{
case 'x':
@@ -532,9 +569,15 @@ int sprintf(char* str, char* fmt, ...)
*str = *fmt;
continue;
}
- num += inc;
- str += inc;
+ /**
+ * Update the looping info.
+ * The -1 below compensates for the increment by the for
+ * loop.
+ */
+ num += inc - 1;
+ str += inc - 1;
}
+ /** Else just copy the character over. */
else
*str = *fmt;
}
Please sign in to comment.
Something went wrong with that request. Please try again.