COSA
An Object-Oriented Platform for Arduino Programming
IOStream.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_IOSTREAM_HH
22 #define COSA_IOSTREAM_HH
23 
24 #include "Cosa/Types.h"
25 
30 class IOStream {
31 public:
33  static const int EOF = -1;
34 
36  static const char CR[] PROGMEM;
37  static const char LF[] PROGMEM;
38  static const char CRLF[] PROGMEM;
39 
43  enum Base {
44  bcd = 0,
45  bin = 2,
46  oct = 8,
47  dec = 10,
48  hex = 16
49  } __attribute__((packed));
50 
54  enum Mode {
55  CR_MODE = 0,
56  LF_MODE = 1,
58  } __attribute__((packed));
59 
63  class Device {
64  public:
69  Device() :
70  m_blocking(false),
71  m_eol(CR_MODE)
72  {}
73 
77  void non_blocking()
78  {
79  m_blocking = false;
80  }
81 
85  void blocking()
86  {
87  m_blocking = true;
88  }
89 
94  bool is_blocking() const
95  {
96  return (m_blocking);
97  }
98 
103  void eol(Mode mode)
104  {
105  m_eol = mode;
106  }
107 
112  Mode eol() const
113  {
114  return (m_eol);
115  }
116 
122  virtual int available();
123 
129  virtual int room();
130 
137  virtual int putchar(char c);
138 
146  virtual int puts(const char* s);
147 
155  virtual int puts(str_P s);
156 
164  virtual int write(const void* buf, size_t size);
165 
173  virtual int write_P(const void* buf, size_t size);
174 
181  virtual int write(const iovec_t* vec);
182 
188  virtual int peekchar();
189 
197  virtual int peekchar(char c);
198 
204  virtual int getchar();
205 
214  virtual char* gets(char *s, size_t count);
215 
223  virtual int read(void* buf, size_t size);
224 
231  virtual int read(iovec_t* vec);
232 
238  virtual int flush();
239 
244  virtual void empty();
245 
246  protected:
249 
252  };
253 
258  IOStream(Device* dev);
259  IOStream();
260 
265  Device* device() const
266  {
267  return (m_dev);
268  }
269 
276  {
277  Device* previous = m_dev;
278  m_dev = dev;
279  return (previous);
280  }
281 
286  str_P eol() const
287  {
288  return (m_eols);
289  }
290 
295  str_P EOL() const
296  {
297  return (m_eols);
298  }
299 
304  void eol(str_P s)
305  {
306  m_eols = s;
307  }
308 
315  int8_t width(int8_t value)
316  {
317  int8_t res = m_width;
318  m_width = value;
319  return (res);
320  }
321 
327  uint8_t precision(uint8_t value)
328  {
329  uint8_t res = m_prec;
330  m_prec = value;
331  return (res);
332  }
333 
339  void print(int value, Base base = dec);
340 
346  void print(long int value, Base base = dec);
347 
353  void print(unsigned int value, Base base = dec);
354 
360  void print(unsigned long int value, Base base = dec);
361 
368  void print(unsigned int value, uint8_t digits, Base base);
369 
376  void print(unsigned long int value, uint8_t digits, Base base);
377 
388  void print(double value, int8_t width, uint8_t prec);
389 
398  void print(uint32_t src, const void *ptr, size_t size,
399  Base base = dec, uint8_t max = 16);
400 
408  void print(const void *ptr, size_t size,
409  Base base = dec, uint8_t max = 16)
410  {
411  print((uint32_t) ptr, ptr, size, base, max);
412  }
413 
418  void print(void *ptr)
419  {
420  print((unsigned int) ptr, hex);
421  }
422 
428  void print(const void *ptr)
429  {
430  print((unsigned int) ptr, hex);
431  }
432 
437  void print(char c)
438  __attribute__((always_inline))
439  {
440  if (m_dev != NULL) m_dev->putchar(c);
441  }
442 
447  void print(const char* s)
448  __attribute__((always_inline))
449  {
450  if (m_dev != NULL) m_dev->puts(s);
451  }
452 
458  void print(str_P s)
459  __attribute__((always_inline))
460  {
461  if (m_dev != NULL) m_dev->puts(s);
462  }
463 
467  void println()
468  __attribute__((always_inline))
469  {
470  if (m_dev != NULL) m_dev->puts(m_eols);
471  }
472 
479  void vprintf(str_P format, va_list args);
480 
487  void printf(str_P format, ...)
488  {
489  va_list args;
490  va_start(args, format);
491  vprintf(format, args);
492  va_end(args);
493  }
494 
499  void print(IOStream::Device* buffer);
500 
504  void flush()
505  __attribute__((always_inline))
506  {
507  if (m_dev != NULL) m_dev->flush();
508  }
509 
517  char* scan(char *s, size_t count);
518 
530  char* readline(char* buf, size_t size, bool echo = true);
531 
538  typedef IOStream& (*Manipulator)(IOStream&);
539 
546  {
547  return (func(*this));
548  }
549 
557  {
558  print(n, m_base);
559  m_base = dec;
560  return (*this);
561  }
562 
569  IOStream& operator<<(long int n)
570  {
571  print(n, m_base);
572  m_base = dec;
573  return (*this);
574  }
575 
583  IOStream& operator<<(double n)
584  {
585  print(n, m_width, m_prec);
586  m_base = dec;
587  return (*this);
588  }
589 
596  IOStream& operator<<(unsigned int n)
597  {
598  print(n, m_base);
599  m_base = dec;
600  return (*this);
601  }
602 
609  IOStream& operator<<(unsigned long int n)
610  {
611  print(n, m_base);
612  m_base = dec;
613  return (*this);
614  }
615 
621  IOStream& operator<<(void* ptr)
622  {
623  print(ptr);
624  m_base = dec;
625  return (*this);
626  }
627 
634  IOStream& operator<<(const void* ptr)
635  {
636  print(ptr);
637  m_base = dec;
638  return (*this);
639  }
640 
647  {
648  print(c);
649  return (*this);
650  }
651 
658  {
659  print(s);
660  return (*this);
661  }
662 
668  IOStream& operator<<(const char* s)
669  {
670  print(s);
671  return (*this);
672  }
673 
680  {
681  print(s);
682  return (*this);
683  }
684 
691  {
692  print(&buffer);
693  return (*this);
694  }
695 
702  {
703  if (m_dev != NULL) m_dev->write(vec);
704  return (*this);
705  }
706 
707 #if !defined(COSA_IOSTREAM_STDLIB_DTOA)
708  /* Faster version of standard number to string conversion */
709  static char* ultoa(unsigned long __val, char *__s, int base);
710  static char* ltoa(long __val, char *__s, int base);
711  static char* utoa(unsigned int __val, char *__s, int base);
712  static char* itoa(int __val, char *__s, int base);
713 #endif
714 
715  friend IOStream& bcd(IOStream& outs);
716  friend IOStream& bin(IOStream& outs);
717  friend IOStream& oct(IOStream& outs);
718  friend IOStream& dec(IOStream& outs);
719  friend IOStream& hex(IOStream& outs);
720  friend IOStream& flush(IOStream& outs);
721 
722 protected:
725  int8_t m_width;
726  uint8_t m_prec;
728 
730  static const size_t BUF_MAX = sizeof(uint32_t) * CHARBITS + 1;
731 
736  void print_prefix(Base base);
737 };
738 
744 inline IOStream&
745 bcd(IOStream& outs)
746 {
747  outs.m_base = IOStream::bcd;
748  return (outs);
749 }
750 
756 inline IOStream&
757 bin(IOStream& outs)
758 {
759  outs.m_base = IOStream::bin;
760  return (outs);
761 }
762 
768 inline IOStream&
769 oct(IOStream& outs)
770 {
771  outs.m_base = IOStream::oct;
772  return (outs);
773 }
774 
780 inline IOStream&
781 dec(IOStream& outs)
782 {
783  outs.m_base = IOStream::dec;
784  return (outs);
785 }
786 
792 inline IOStream&
793 hex(IOStream& outs)
794 {
795  outs.m_base = IOStream::hex;
796  return (outs);
797 }
798 
804 inline IOStream&
805 tab(IOStream& outs)
806 {
807  outs.print('\t');
808  return (outs);
809 }
810 
816 inline IOStream&
818 {
819  outs.println();
820  return (outs);
821 }
822 
828 inline IOStream&
830 {
831  outs.print('\0');
832  return (outs);
833 }
834 
840 inline IOStream&
842 {
843  outs.print('\f');
844  return (outs);
845 }
846 
852 inline IOStream&
854 {
855  if (outs.m_dev != NULL) outs.m_dev->flush();
856  return (outs);
857 }
858 #endif
IOStream &(* Manipulator)(IOStream &)
Definition: IOStream.hh:538
virtual int write_P(const void *buf, size_t size)
void print(str_P s)
Definition: IOStream.hh:458
char * scan(char *s, size_t count)
Definition: IOStream.cpp:224
virtual int putchar(char c)
virtual int puts(const char *s)
IOStream & ends(IOStream &outs)
Definition: IOStream.hh:829
IOStream & operator<<(const iovec_t *vec)
Definition: IOStream.hh:701
IOStream & operator<<(IOStream &buffer)
Definition: IOStream.hh:690
void println()
Definition: IOStream.hh:467
void print(int value, Base base=dec)
Definition: IOStream.cpp:46
int8_t m_width
Minimum width of output string.
Definition: IOStream.hh:725
virtual int write(const void *buf, size_t size)
virtual int read(void *buf, size_t size)
#define NULL
Definition: Types.h:101
virtual int room()
IOStream & operator<<(void *ptr)
Definition: IOStream.hh:621
virtual void empty()
IOStream & operator<<(str_P s)
Definition: IOStream.hh:679
IOStream & operator<<(int n)
Definition: IOStream.hh:556
static const char CR[]
Definition: IOStream.hh:36
IOStream & operator<<(Manipulator func)
Definition: IOStream.hh:545
void eol(Mode mode)
Definition: IOStream.hh:103
IOStream & operator<<(const void *ptr)
Definition: IOStream.hh:634
IOStream & tab(IOStream &outs)
Definition: IOStream.hh:805
IOStream()
Definition: IOStream.cpp:37
Definition: Types.h:391
uint8_t precision(uint8_t value)
Definition: IOStream.hh:327
static char * ltoa(long __val, char *__s, int base)
void print(const void *ptr, size_t size, Base base=dec, uint8_t max=16)
Definition: IOStream.hh:408
IOStream & operator<<(unsigned long int n)
Definition: IOStream.hh:609
void printf(str_P format,...)
Definition: IOStream.hh:487
void flush()
Definition: IOStream.hh:504
int8_t width(int8_t value)
Definition: IOStream.hh:315
void print(void *ptr)
Definition: IOStream.hh:418
void print_prefix(Base base)
Definition: IOStream.cpp:125
IOStream & operator<<(unsigned int n)
Definition: IOStream.hh:596
char * readline(char *buf, size_t size, bool echo=true)
Definition: IOStream.cpp:262
IOStream & operator<<(const char *s)
Definition: IOStream.hh:668
#define CHARBITS
Definition: Types.h:57
const class prog_str * str_P
Definition: Types.h:187
static const char CRLF[]
Definition: IOStream.hh:38
static char * itoa(int __val, char *__s, int base)
Mode eol() const
Definition: IOStream.hh:112
void eol(str_P s)
Definition: IOStream.hh:304
static char * utoa(unsigned int __val, char *__s, int base)
IOStream & operator<<(long int n)
Definition: IOStream.hh:569
Base m_base
Base for next output operator.
Definition: IOStream.hh:724
static const int EOF
Definition: IOStream.hh:33
virtual int flush()
IOStream & endl(IOStream &outs)
Definition: IOStream.hh:817
void blocking()
Definition: IOStream.hh:85
str_P EOL() const
Definition: IOStream.hh:295
void print(char c)
Definition: IOStream.hh:437
Device * device() const
Definition: IOStream.hh:265
IOStream & operator<<(char c)
Definition: IOStream.hh:646
virtual int available()
virtual char * gets(char *s, size_t count)
str_P eol() const
Definition: IOStream.hh:286
virtual int getchar()
virtual int peekchar()
bool is_blocking() const
Definition: IOStream.hh:94
IOStream & operator<<(char *s)
Definition: IOStream.hh:657
void print(const char *s)
Definition: IOStream.hh:447
IOStream & clear(IOStream &outs)
Definition: IOStream.hh:841
IOStream & operator<<(double n)
Definition: IOStream.hh:583
Device * m_dev
Delegated device.
Definition: IOStream.hh:723
Device * device(Device *dev)
Definition: IOStream.hh:275
static const size_t BUF_MAX
Definition: IOStream.hh:730
void non_blocking()
Definition: IOStream.hh:77
static const char LF[]
Definition: IOStream.hh:37
void print(const void *ptr)
Definition: IOStream.hh:428
void vprintf(str_P format, va_list args)
Definition: IOStream.cpp:163
str_P m_eols
End of line string (program memory).
Definition: IOStream.hh:727
uint8_t m_prec
Number of digits after decimal sign.
Definition: IOStream.hh:726
static char * ultoa(unsigned long __val, char *__s, int base)