]> rtime.felk.cvut.cz Git - sysless.git/blob - libs4c/cmdproc/cmd_io.c
C compilers are compliant enough that stdint.h can be used for integer types now.
[sysless.git] / libs4c / cmdproc / cmd_io.c
1 #include <cmd_proc.h>
2 #include <string.h>
3 #include <stdint.h>
4
5 /** 
6  * Blocking call to print a string.
7  * 
8  * @param cmd_io cmd_io structure.
9  * @param str Zero terminated string to print.
10  * 
11  * @return Upon successful completion, puts() shall return a
12  * non-negative number. In case of error, negative number is returned.
13  */
14 int cmd_io_puts(cmd_io_t *cmd_io, const char *str)
15 {
16   int ret;
17   unsigned len;
18   if (!str) return 0;
19   len = strlen(str);
20   do {
21       ret = cmd_io_write(cmd_io, str, len);
22       if (ret > 0) {
23           str+=ret;
24           len-=ret;
25       }
26   } while (ret>=0 && len>0);
27   return ret;
28 }
29
30 int cmd_io_write_bychar(cmd_io_t *cmd_io,const void *buf,int count)
31 {
32   int cn=0;
33   uint8_t* p=(uint8_t*)buf;
34   while(count--&&(*cmd_io->putc)(cmd_io,*p++)>=0){
35     cn++;
36   }
37   return cn;
38 }
39
40 int cmd_io_read_bychar(cmd_io_t *cmd_io,void *buf,int count)
41 {
42   int cn=0;
43   int ch;
44   uint8_t* p=(uint8_t*)buf;
45   while(count--&&(ch=(*cmd_io->getc)(cmd_io))>=0){
46     *p++=ch;
47     cn++;
48   }
49   return cn;
50 }
51