|
-
- #ifndef StdioStream_h
- #define StdioStream_h
-
- #include <limits.h>
- #include "FatFile.h"
-
-
- const uint8_t STREAM_BUF_SIZE = 64;
-
- const uint8_t UNGETC_BUF_SIZE = 2;
-
-
- #include <stdio.h>
- #undef clearerr
- #undef fclose
- #undef feof
- #undef ferror
- #undef fflush
- #undef fgetc
- #undef fgetpos
- #undef fgets
- #undef fopen
- #undef fprintf
- #undef fputc
- #undef fputs
- #undef fread
- #undef freopen
- #undef fscanf
- #undef fseek
- #undef fsetpos
- #undef ftell
- #undef fwrite
- #undef getc
- #undef getchar
- #undef gets
- #undef perror
- #undef printf
- #undef putc
- #undef putchar
- #undef puts
- #undef remove
- #undef rename
- #undef rewind
- #undef scanf
- #undef setbuf
- #undef setvbuf
- #undef sprintf
- #undef sscanf
- #undef tmpfile
- #undef tmpnam
- #undef ungetc
- #undef vfprintf
- #undef vprintf
- #undef vsprintf
-
-
- #ifndef EOF
-
- #define EOF (-1)
- #endif
- #ifndef NULL
-
- #define NULL 0
- #endif
- #ifndef SEEK_CUR
-
- #define SEEK_CUR 1
- #endif
- #ifndef SEEK_END
-
- #define SEEK_END 2
- #endif
- #ifndef SEEK_SET
-
- #define SEEK_SET 0
- #endif
-
-
- class StdioStream : private FatFile {
- public:
-
-
- StdioStream() {
- m_w = m_r = 0;
- m_p = m_buf;
- m_flags = 0;
- }
-
-
- void clearerr() {m_flags &= ~(F_ERR | F_EOF);}
-
-
-
- int fclose();
-
-
-
- int feof() {return (m_flags & F_EOF) != 0;}
-
-
-
- int ferror() {return (m_flags & F_ERR) != 0;}
-
-
-
- int fflush();
-
-
-
- int fgetc() {return m_r-- == 0 ? fillGet() : *m_p++;}
-
-
-
- char* fgets(char* str, size_t num, size_t* len = 0);
-
-
-
- bool fopen(const char* filename, const char * mode);
-
-
-
- int fputc(int c) {return m_w-- == 0 ? flushPut(c) : *m_p++ = c;}
-
-
-
- int fputs(const char* str);
-
-
-
- int fputs_P(PGM_P str);
-
-
-
- size_t fread(void* ptr, size_t size, size_t count);
-
-
-
- int fseek(int32_t offset, int origin);
-
-
-
- int32_t ftell();
-
-
-
- size_t fwrite(const void * ptr, size_t size, size_t count);
-
-
-
- inline __attribute__((always_inline))
- int getc() {return m_r-- == 0 ? fillGet() : *m_p++;}
-
-
-
- inline __attribute__((always_inline))
- int putc(int c) {return m_w-- == 0 ? flushPut(c) : *m_p++ = c;}
-
-
-
- inline __attribute__((always_inline))
- int putCRLF() {
- if (m_w < 2) {
- if (!flushBuf()) return -1;
- }
- *m_p++ = '\r';
- *m_p++ = '\n';
- m_w -= 2;
- return 2;
- }
-
-
-
- size_t print(char c) {
- return putc(c) < 0 ? 0 : 1;
- }
-
-
-
- size_t print(const char* str) {
- int n = fputs(str);
- return n < 0 ? 0 : n;
- }
-
-
-
- size_t print(const __FlashStringHelper *str);
-
-
-
- size_t print(double val, uint8_t prec = 2) {
- return print(static_cast<float>(val), prec);
- }
-
-
-
- size_t print(float val, uint8_t prec = 2) {
- int n = printDec(val, prec);
- return n > 0 ? n : 0;
- }
-
-
-
- template <typename T>
- size_t print(T val) {
- int n = printDec(val);
- return n > 0 ? n : 0;
- }
-
-
-
- size_t println() {
- return putCRLF() > 0 ? 2 : 0;
- }
-
-
-
- size_t println(double val, uint8_t prec = 2) {
- return println(static_cast<float>(val), prec);
- }
-
-
-
- size_t println(float val, uint8_t prec = 2) {
- int n = printDec(val, prec);
- return n > 0 && putCRLF() > 0 ? n + 2 : 0;
- }
-
-
-
- template <typename T>
- size_t println(T val) {
- int n = print(val);
- return putCRLF() > 0 ? n + 2 : 0;
- }
-
-
-
- int printDec(char n) {
- if (CHAR_MIN == 0) {
- return printDec((unsigned char)n);
- } else {
- return printDec((signed char)n);
- }
- }
-
-
-
- int printDec(signed char n);
-
-
-
- int printDec(unsigned char n) {
- return printDec((uint16_t)n);
- }
-
-
-
- int printDec(int16_t n);
-
-
-
- int printDec(uint16_t n);
-
-
-
- int printDec(int32_t n);
-
-
-
- int printDec(uint32_t n);
-
-
-
- int printDec(double value, uint8_t prec) {
- return printDec(static_cast<float>(value), prec);
- }
-
-
-
- int printDec(float value, uint8_t prec);
-
-
- int printField(double value, char term, uint8_t prec = 2) {
- return printField(static_cast<float>(value), term, prec) > 0;
- }
-
-
- int printField(float value, char term, uint8_t prec = 2) {
- int rtn = printDec(value, prec);
- return rtn < 0 || putc(term) < 0 ? -1 : rtn + 1;
- }
-
-
- template <typename T>
- int printField(T value, char term) {
- int rtn = printDec(value);
- return rtn < 0 || putc(term) < 0 ? -1 : rtn + 1;
- }
-
-
-
- int printHex(uint32_t n);
-
-
-
- int printHexln(uint32_t n) {
- int rtn = printHex(n);
- return rtn < 0 || putCRLF() != 2 ? -1 : rtn + 2;
- }
-
-
-
- bool rewind();
-
-
-
- int ungetc(int c);
-
- private:
- bool fillBuf();
- int fillGet();
- bool flushBuf();
- int flushPut(uint8_t c);
- char* fmtSpace(uint8_t len);
- int write(const void* buf, size_t count);
-
-
- static const uint8_t F_SRD = 0x01;
- static const uint8_t F_SWR = 0x02;
- static const uint8_t F_SRW = 0x04;
- static const uint8_t F_EOF = 0x10;
- static const uint8_t F_ERR = 0x20;
-
- uint8_t m_flags;
- uint8_t* m_p;
- uint8_t m_r;
- uint8_t m_w;
- uint8_t m_buf[STREAM_BUF_SIZE];
- };
-
- #endif
|