Subversion Repositories HelenOS-historic

Compare Revisions

Ignore whitespace Rev 1603 → Rev 1604

/kernel/trunk/test/print/print1/test.c
32,9 → 32,10
 
void test(void)
{
int retval;
__native nat = 0x12345678u;
unsigned char buffer[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
printf(" Printf test \n");
52,13 → 53,19
printf(" Print to NULL '%s'\n",NULL);
 
retval = snprintf(buffer, BUFFER_SIZE, "Short text without parameters.");
printf("Result is: '%s', retval = %d\n", buffer, retval);
 
retval = snprintf(buffer, BUFFER_SIZE, "Very very very long text without parameters.");
printf("Result is: '%s', retval = %d\n", buffer, retval);
printf("Print short text to %d char long buffer via snprintf.\n", BUFFER_SIZE);
snprintf(buffer, BUFFER_SIZE, "Short %s", "text");
printf("Result is: '%s'\n", buffer);
retval = snprintf(buffer, BUFFER_SIZE, "Short %s", "text");
printf("Result is: '%s', retval = %d\n", buffer, retval);
printf("Print long text to %d char long buffer via snprintf.\n", BUFFER_SIZE);
snprintf(buffer, BUFFER_SIZE, "Very long %s. This text`s length is more than %d. We are interested in the result.", "text" , BUFFER_SIZE);
printf("Result is: '%s'\n", buffer);
retval = snprintf(buffer, BUFFER_SIZE, "Very long %s. This text`s length is more than %d. We are interested in the result.", "text" , BUFFER_SIZE);
printf("Result is: '%s', retval = %d\n", buffer, retval);
return;
}
/kernel/trunk/generic/src/printf/vsnprintf.c
50,31 → 50,34
*/
int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
{
size_t i,j;
size_t i;
i = data->size - data->len;
j = count;
 
if (i > 0) {
if (i <= j) {
if (i == 1) {
/* We have only one free byte left in buffer => write there trailing zero */
data->string[data->size - 1] = 0;
data->len = data->size;
} else {
/* We have not enought space for whole string with the trailing zero => print only a part of string */
memcpy((void *)(data->string + data->len), (void *)str, i - 1);
data->string[data->size - 1] = 0;
data->len = data->size;
}
} else {
/* Buffer is big enought to print whole string */
memcpy((void *)(data->string + data->len), (void *)str, j);
data->len += j;
/* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
data->string[data->len] = 0;
}
if ((count == 0) || (i == 0)) {
return 0;
}
if (i == 1) {
/* We have only one free byte left in buffer => write there trailing zero */
data->string[data->size - 1] = 0;
data->len = data->size;
return 1;
}
if (i <= count) {
/* We have not enought space for whole string with the trailing zero => print only a part of string */
memcpy((void *)(data->string + data->len), (void *)str, i - 1);
data->string[data->size - 1] = 0;
data->len = data->size;
return i;
}
/* Buffer is big enought to print whole string */
memcpy((void *)(data->string + data->len), (void *)str, count);
data->len += count;
/* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
data->string[data->len] = 0;
 
return count;
}
 
/kernel/trunk/generic/src/printf/printf_core.c
100,21 → 100,17
* @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
* @param count
* @param ps output method and its data
* @return 0 on success, EOF on fail
* @return number or printed characters
*/
static int printf_putnchars(const char * buf, size_t count, struct printf_spec *ps)
{
if (ps->write((void *)buf, count, ps->data) == count) {
return 0;
}
return EOF;
return ps->write((void *)buf, count, ps->data);
}
 
/** Print string without added newline
* @param str string to print
* @param ps write function specification and support data
* @return 0 on success or EOF on fail
* @return number or printed characters
*/
static int printf_putstr(const char * str, struct printf_spec *ps)
{
126,27 → 122,19
 
count = strlen(str);
 
if (ps->write((void *) str, count, ps->data) == count) {
return 0;
}
return EOF;
return ps->write((void *) str, count, ps->data);
}
 
/** Print one character to output
* @param c one character
* @param ps output method
* @return printed character or EOF
* @return number or printed characters
*/
static int printf_putchar(int c, struct printf_spec *ps)
{
unsigned char ch = c;
if (ps->write((void *) &ch, 1, ps->data) == 1) {
return c;
}
return EOF;
return ps->write((void *) &ch, 1, ps->data);
}
 
/** Print one formatted character
153,7 → 141,7
* @param c character to print
* @param width
* @param flags
* @return number of printed characters or EOF
* @return number of printed characters, negative value on fail
*/
static int print_char(char c, int width, __u64 flags, struct printf_spec *ps)
{
162,18 → 150,17
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
while (--width > 0) { /* one space is consumed by character itself hence predecrement */
/* FIXME: painful slow */
printf_putchar(' ', ps);
++counter;
if (printf_putchar(' ', ps) > 0)
++counter;
}
}
if (printf_putchar(c, ps) == EOF) {
return EOF;
}
if (printf_putchar(c, ps) > 0)
counter++;
 
while (--width > 0) { /* one space is consumed by character itself hence predecrement */
printf_putchar(' ', ps);
++counter;
if (printf_putchar(' ', ps) > 0)
++counter;
}
return ++counter;
184,7 → 171,7
* @param width
* @param precision
* @param flags
* @return number of printed characters or EOF
* @return number of printed characters or negative value on fail
*/
static int print_string(char *s, int width, int precision, __u64 flags, struct printf_spec *ps)
191,6 → 178,7
{
int counter = 0;
size_t size;
int retval;
 
if (s == NULL) {
return printf_putstr("(NULL)", ps);
207,29 → 195,28
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
while (width-- > 0) {
printf_putchar(' ', ps);
counter++;
if (printf_putchar(' ', ps) == 1)
counter++;
}
}
 
while (precision > size) {
precision--;
printf_putchar(' ', ps);
++counter;
if (printf_putchar(' ', ps) == 1)
++counter;
}
if (printf_putnchars(s, precision, ps) == EOF) {
return EOF;
if ((retval = printf_putnchars(s, precision, ps)) < 0) {
return -counter;
}
counter += retval;
 
counter += precision;
 
while (width-- > 0) {
printf_putchar(' ', ps);
++counter;
if (printf_putchar(' ', ps) == 1)
++counter;
}
return ++counter;
return counter;
}
 
 
254,8 → 241,9
char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
int size = 0; /* size of number with all prefixes and signs */
int number_size; /* size of plain number */
int written = 0;
char sgn;
int retval;
int counter = 0;
if (flags & __PRINTF_FLAG_BIGCHARS)
digits = digits_big;
322,15 → 310,16
if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
while (width-- > 0) {
printf_putchar(' ', ps);
written++;
if (printf_putchar(' ', ps) == 1)
counter++;
}
}
/* print sign */
if (sgn) {
printf_putchar(sgn, ps);
written++;
if (printf_putchar(sgn, ps) == 1)
counter++;
}
/* print prefix */
338,26 → 327,30
if (flags & __PRINTF_FLAG_PREFIX) {
switch(base) {
case 2: /* Binary formating is not standard, but usefull */
printf_putchar('0', ps);
if (printf_putchar('0', ps) == 1)
counter++;
if (flags & __PRINTF_FLAG_BIGCHARS) {
printf_putchar('B', ps);
if (printf_putchar('B', ps) == 1)
counter++;
} else {
printf_putchar('b', ps);
if (printf_putchar('b', ps) == 1)
counter++;
}
written += 2;
break;
case 8:
printf_putchar('o', ps);
written++;
if (printf_putchar('o', ps) == 1)
counter++;
break;
case 16:
printf_putchar('0', ps);
if (printf_putchar('0', ps) == 1)
counter++;
if (flags & __PRINTF_FLAG_BIGCHARS) {
printf_putchar('X', ps);
if (printf_putchar('X', ps) == 1)
counter++;
} else {
printf_putchar('x', ps);
if (printf_putchar('x', ps) == 1)
counter++;
}
written += 2;
break;
}
}
365,23 → 358,25
/* print leading zeroes */
precision -= number_size;
while (precision-- > 0) {
printf_putchar('0', ps);
written++;
if (printf_putchar('0', ps) == 1)
counter++;
}
 
/* print number itself */
 
written += printf_putstr(++ptr, ps);
if ((retval = printf_putstr(++ptr, ps)) > 0) {
counter += retval;
}
/* print ending spaces */
while (width-- > 0) {
printf_putchar(' ', ps);
written++;
if (printf_putchar(' ', ps) == 1)
counter++;
}
 
return written;
return counter;
}
 
 
484,7 → 479,7
if (c == '%' ) {
/* print common characters if any processed */
if (i > j) {
if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) == EOF) { /* error */
if ((retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps)) < 0) { /* error */
counter = -counter;
goto out;
}
581,7 → 576,7
* String and character conversions.
*/
case 's':
if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) == EOF) {
if ((retval = print_string(va_arg(ap, char*), width, precision, flags, ps)) < 0) {
counter = -counter;
goto out;
};
591,7 → 586,7
goto next_char;
case 'c':
c = va_arg(ap, unsigned int);
if ((retval = print_char(c, width, flags, ps)) == EOF) {
if ((retval = print_char(c, width, flags, ps)) < 0) {
counter = -counter;
goto out;
};
692,7 → 687,7
}
}
 
if ((retval = print_number(number, width, precision, base, flags, ps)) == EOF ) {
if ((retval = print_number(number, width, precision, base, flags, ps)) < 0) {
counter = -counter;
goto out;
};
706,7 → 701,7
}
if (i > j) {
if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) == EOF) { /* error */
if ((retval = printf_putnchars(&fmt[j], (__native)(i - j), ps)) < 0) { /* error */
counter = -counter;
goto out;