Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2376 → Rev 2377

/branches/fs/uspace/fs/printing.c
0,0 → 1,149
/* Functions for printing on console. */
 
 
/* Methods:
* init_printing: connects to SERVICE_CONSOLE and initializes used data structures
* print_console: prints char* on console
* print_console_int: prints int argument with specified conversion inside input string
*/
 
 
#include <string.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include <async.h>
#include "fs.h"
#include "../console/console.h"
 
#define TOTAL_CONVERSIONS 5
#define BUFF_SIZE 10
 
 
/* Table of printing conversions for: d, i, u, c, %. */
char conversions[TOTAL_CONVERSIONS];
char format_string[3] = "%";
 
static int validate_string(char conv);
 
int init_printing()
{
/* Attempt to connect to SERVICE_CONSOLE fail. */
if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS))
return FALSE;
 
conversions[0] = 'd';
conversions[1] = 'i';
conversions[2] = 'u';
conversions[3] = 'c';
conversions[4] = '%';
}
 
int validate_string(char conv)
{
int i;
/* Check if the conversion character matches conversions table. */
for (i = 0; i < TOTAL_CONVERSIONS; i++){
if (conv == conversions[i]) {
return TRUE;
}
}
 
return FALSE;
}
 
int print_console(char *str)
{
int i, len;
char c;
/* Must be connected to the CONSOLE_SERVICE. */
if (con_phone < 0)
return FALSE;
len = strlen(str);
if (len == 0)
return TRUE;
for (i = 0; i < len; i++) {
c = *str++;
async_req_2(con_phone, CONSOLE_PUTCHAR, c, 0, NULL, NULL); /* Do the work. */
}
 
return TRUE;
}
 
int print_console_int(char *str, int arg)
{
int i, retval, len, front_side, offset;
char conversion;
char buffer[BUFF_SIZE];
char *front, *back;
 
len = strlen(str);
 
if (len == 0)
return TRUE;
front = malloc((len+1) * sizeof(char));
back = malloc((len+1) * sizeof(char));
front_side = TRUE;
/* Copy front side of source string. */
for (i = 0; i < len; i++) {
if (str[i] == '%') {
front_side = FALSE;
break;
}
front[i] = str[i];
}
/* Character '%' was not found. Print it in a normal way, without int argument. */
if (front_side) {
return (print_console(str));
}
/* Character '%' was the last in the input string - error. */
if (i == (len-1)) {
return FALSE;
}
front[i] = '\0';
conversion = str[++i];
retval = validate_string(conversion);
if (!retval) {
return FALSE;
}
offset = ++i;
/* Copy back side of source string. */
for (;i < len; i++) {
back[i-offset] = str[i];
}
 
back[i] = '\0';
 
/* Two characters '%' in source string. */
if (conversion == '%') {
print_console(front);
print_console(back);
return TRUE;
}
/* Other 'normal' case. */
print_console(front);
format_string[1] = conversion;
sprintf(buffer, format_string, arg);
print_console(buffer);
print_console(back);
 
return TRUE;
}