[gnoduino: 83/237] Moving write errors out of return value into separate API methods.



commit 608e35c237d7b625934296160b2c8cf670a73607
Author: David A. Mellis <d mellis arduino cc>
Date:   Fri Aug 26 16:08:14 2011 -0400

    Moving write errors out of return value into separate API methods.
    
    write(), print(), println() now return size_t (and don't use negative values to signal errors).
    Print adds writeError() for checking for write errors, clearWriteError() to reset the flag to false, and a protected setWriteError() for signalling errors.
    
    http://code.google.com/p/arduino/issues/detail?id=598

 arduino/cores/arduino/HardwareSerial.cpp |    2 +-
 arduino/cores/arduino/HardwareSerial.h   |    2 +-
 arduino/cores/arduino/Print.cpp          |  144 ++++++++++++++----------------
 arduino/cores/arduino/Print.h            |   66 ++++++++------
 arduino/cores/arduino/Printable.h        |    2 +-
 5 files changed, 105 insertions(+), 111 deletions(-)
---
diff --git a/arduino/cores/arduino/HardwareSerial.cpp b/arduino/cores/arduino/HardwareSerial.cpp
index 641c973..d6be218 100644
--- a/arduino/cores/arduino/HardwareSerial.cpp
+++ b/arduino/cores/arduino/HardwareSerial.cpp
@@ -352,7 +352,7 @@ void HardwareSerial::flush()
     ;
 }
 
-ssize_t HardwareSerial::write(uint8_t c)
+size_t HardwareSerial::write(uint8_t c)
 {
   int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
 	
diff --git a/arduino/cores/arduino/HardwareSerial.h b/arduino/cores/arduino/HardwareSerial.h
index 960d3f5..1895f08 100644
--- a/arduino/cores/arduino/HardwareSerial.h
+++ b/arduino/cores/arduino/HardwareSerial.h
@@ -55,7 +55,7 @@ class HardwareSerial : public Stream
     virtual int peek(void);
     virtual int read(void);
     virtual void flush(void);
-    virtual ssize_t write(uint8_t);
+    virtual size_t write(uint8_t);
     using Print::write; // pull in write(str) and write(buf, size) from Print
 };
 
diff --git a/arduino/cores/arduino/Print.cpp b/arduino/cores/arduino/Print.cpp
index 192d9a3..8190d4f 100755
--- a/arduino/cores/arduino/Print.cpp
+++ b/arduino/cores/arduino/Print.cpp
@@ -30,85 +30,80 @@
 // Public Methods //////////////////////////////////////////////////////////////
 
 /* default implementation: may be overridden */
-ssize_t Print::write(const char *str)
+size_t Print::write(const char *str)
 {
-  ssize_t n = 0;
+  size_t n = 0;
   while (*str) {
-    if (write(*str++) <= 0) break;
-    n++;
+    n += write(*str++);
   }
   return n;
 }
 
 /* default implementation: may be overridden */
-ssize_t Print::write(const uint8_t *buffer, size_t size)
+size_t Print::write(const uint8_t *buffer, size_t size)
 {
-  ssize_t n = 0;
+  size_t n = 0;
   while (size--) {
-    if (write(*buffer++) <= 0) break;
-    n++;
+    n += write(*buffer++);
   }
   return n;
 }
 
-ssize_t Print::print(const __FlashStringHelper *ifsh)
+size_t Print::print(const __FlashStringHelper *ifsh)
 {
   const prog_char *p = (const prog_char *)ifsh;
-  ssize_t n = 0;
+  size_t n = 0;
   while (1) {
     unsigned char c = pgm_read_byte(p++);
     if (c == 0) break;
-    if (write(c) <= 0) break;
-    n++;
+    n += write(c);
   }
   return n;
 }
 
-ssize_t Print::print(const String &s)
+size_t Print::print(const String &s)
 {
-  ssize_t n = 0;
+  size_t n = 0;
   for (int i = 0; i < s.length(); i++) {
-    if (write(s[i]) < 0) break;
-    n++;
+    n += write(s[i]);
   }
   return n;
 }
 
-ssize_t Print::print(const char str[])
+size_t Print::print(const char str[])
 {
   return write(str);
 }
 
-ssize_t Print::print(char c)
+size_t Print::print(char c)
 {
   return write(c);
 }
 
-ssize_t Print::print(unsigned char b, int base)
+size_t Print::print(unsigned char b, int base)
 {
   return print((unsigned long) b, base);
 }
 
-ssize_t Print::print(int n, int base)
+size_t Print::print(int n, int base)
 {
   return print((long) n, base);
 }
 
-ssize_t Print::print(unsigned int n, int base)
+size_t Print::print(unsigned int n, int base)
 {
   return print((unsigned long) n, base);
 }
 
-ssize_t Print::print(long n, int base)
+size_t Print::print(long n, int base)
 {
   if (base == 0) {
     return write(n);
   } else if (base == 10) {
     if (n < 0) {
       int t = print('-');
-      if (t <= 0) return t;
       n = -n;
-      return printNumber(n, 10) + 1;
+      return printNumber(n, 10) + t;
     }
     return printNumber(n, 10);
   } else {
@@ -116,110 +111,109 @@ ssize_t Print::print(long n, int base)
   }
 }
 
-ssize_t Print::print(unsigned long n, int base)
+size_t Print::print(unsigned long n, int base)
 {
   if (base == 0) return write(n);
   else return printNumber(n, base);
 }
 
-ssize_t Print::print(double n, int digits)
+size_t Print::print(double n, int digits)
 {
   return printFloat(n, digits);
 }
 
-ssize_t Print::println(const __FlashStringHelper *ifsh)
+size_t Print::println(const __FlashStringHelper *ifsh)
 {
-  ssize_t n = print(ifsh);
-  if (n >= 0) n += println();
+  size_t n = print(ifsh);
+  n += println();
   return n;
 }
 
-ssize_t Print::print(const Printable& x)
+size_t Print::print(const Printable& x)
 {
   return x.printTo(*this);
 }
 
-ssize_t Print::println(void)
+size_t Print::println(void)
 {
-  ssize_t t = print('\r');
-  if (t <= 0) return t;
-  if (print('\n') <= 0) return 1;
-  return 2;
+  size_t n = print('\r');
+  n += print('\n');
+  return n;
 }
 
-ssize_t Print::println(const String &s)
+size_t Print::println(const String &s)
 {
-  ssize_t n = print(s);
-  if (n >= 0) n += println();
+  size_t n = print(s);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(const char c[])
+size_t Print::println(const char c[])
 {
-  ssize_t n = print(c);
-  if (n >= 0) n += println();
+  size_t n = print(c);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(char c)
+size_t Print::println(char c)
 {
-  ssize_t n = print(c);
-  if (n > 0) n += println();
+  size_t n = print(c);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(unsigned char b, int base)
+size_t Print::println(unsigned char b, int base)
 {
-  ssize_t n = print(b, base);
-  if (n >= 0) n += println();
+  size_t n = print(b, base);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(int num, int base)
+size_t Print::println(int num, int base)
 {
-  ssize_t n = print(num, base);
-  if (n >= 0) n += println();
+  size_t n = print(num, base);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(unsigned int num, int base)
+size_t Print::println(unsigned int num, int base)
 {
-  ssize_t n = print(num, base);
-  if (n >= 0) n += println();
+  size_t n = print(num, base);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(long num, int base)
+size_t Print::println(long num, int base)
 {
-  ssize_t n = print(num, base);
-  if (n >= 0) n += println();
+  size_t n = print(num, base);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(unsigned long num, int base)
+size_t Print::println(unsigned long num, int base)
 {
-  ssize_t n = print(num, base);
-  if (n >= 0) n += println();
+  size_t n = print(num, base);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(double num, int digits)
+size_t Print::println(double num, int digits)
 {
-  ssize_t n = print(num, digits);
-  if (n >= 0) n += println();
+  size_t n = print(num, digits);
+  n += println();
   return n;
 }
 
-ssize_t Print::println(const Printable& x)
+size_t Print::println(const Printable& x)
 {
-  ssize_t n = print(x);
-  if (n >= 0) n += println();
+  size_t n = print(x);
+  n += println();
   return n;
 }
 
 // Private Methods /////////////////////////////////////////////////////////////
 
-ssize_t Print::printNumber(unsigned long n, uint8_t base) {
+size_t Print::printNumber(unsigned long n, uint8_t base) {
   char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
   char *str = &buf[sizeof(buf) - 1];
 
@@ -238,14 +232,14 @@ ssize_t Print::printNumber(unsigned long n, uint8_t base) {
   return write(str);
 }
 
-ssize_t Print::printFloat(double number, uint8_t digits) 
+size_t Print::printFloat(double number, uint8_t digits) 
 { 
-  ssize_t n = 0, t;
+  size_t n = 0;
   
   // Handle negative numbers
   if (number < 0.0)
   {
-     if ((n = print('-')) <= 0) return n;
+     n += print('-');
      number = -number;
   }
 
@@ -259,15 +253,11 @@ ssize_t Print::printFloat(double number, uint8_t digits)
   // Extract the integer part of the number and print it
   unsigned long int_part = (unsigned long)number;
   double remainder = number - (double)int_part;
-  if ((t = print(int_part)) < 0) return n;
-  
-  n += t;
+  n += print(int_part);
 
   // Print the decimal point, but only if there are digits beyond
   if (digits > 0) {
-    t = print("."); 
-    if (t <= 0) return n;
-    n += t;
+    n += print("."); 
   }
 
   // Extract digits from the remainder one at a time
@@ -275,9 +265,7 @@ ssize_t Print::printFloat(double number, uint8_t digits)
   {
     remainder *= 10.0;
     int toPrint = int(remainder);
-    t = print(toPrint);
-    if (t <= 0) return n;
-    n += t;
+    n += print(toPrint);
     remainder -= toPrint; 
   } 
   
diff --git a/arduino/cores/arduino/Print.h b/arduino/cores/arduino/Print.h
index 3f303f3..fce302e 100755
--- a/arduino/cores/arduino/Print.h
+++ b/arduino/cores/arduino/Print.h
@@ -23,8 +23,6 @@
 #include <inttypes.h>
 #include <stdio.h> // for size_t
 
-typedef int ssize_t;
-
 #include "WString.h"
 #include "Printable.h"
 
@@ -36,37 +34,45 @@ typedef int ssize_t;
 class Print
 {
   private:
-    ssize_t printNumber(unsigned long, uint8_t);
-    ssize_t printFloat(double, uint8_t);
+    int write_error;
+    size_t printNumber(unsigned long, uint8_t);
+    size_t printFloat(double, uint8_t);
+  protected:
+    void setWriteError(int err = 1) { write_error = err; }
   public:
-    virtual ssize_t write(uint8_t) = 0;
-    virtual ssize_t write(const char *str);
-    virtual ssize_t write(const uint8_t *buffer, size_t size);
+    Print() : write_error(0) {}
+  
+    int writeError() { return write_error; }
+    void clearWriteError() { setWriteError(0); }
+  
+    virtual size_t write(uint8_t) = 0;
+    virtual size_t write(const char *str);
+    virtual size_t write(const uint8_t *buffer, size_t size);
     
-    ssize_t print(const __FlashStringHelper *);
-    ssize_t print(const String &);
-    ssize_t print(const char[]);
-    ssize_t print(char);
-    ssize_t print(unsigned char, int = DEC);
-    ssize_t print(int, int = DEC);
-    ssize_t print(unsigned int, int = DEC);
-    ssize_t print(long, int = DEC);
-    ssize_t print(unsigned long, int = DEC);
-    ssize_t print(double, int = 2);
-    ssize_t print(const Printable&);
+    size_t print(const __FlashStringHelper *);
+    size_t print(const String &);
+    size_t print(const char[]);
+    size_t print(char);
+    size_t print(unsigned char, int = DEC);
+    size_t print(int, int = DEC);
+    size_t print(unsigned int, int = DEC);
+    size_t print(long, int = DEC);
+    size_t print(unsigned long, int = DEC);
+    size_t print(double, int = 2);
+    size_t print(const Printable&);
 
-    ssize_t println(const __FlashStringHelper *);
-    ssize_t println(const String &s);
-    ssize_t println(const char[]);
-    ssize_t println(char);
-    ssize_t println(unsigned char, int = DEC);
-    ssize_t println(int, int = DEC);
-    ssize_t println(unsigned int, int = DEC);
-    ssize_t println(long, int = DEC);
-    ssize_t println(unsigned long, int = DEC);
-    ssize_t println(double, int = 2);
-    ssize_t println(const Printable&);
-    ssize_t println(void);
+    size_t println(const __FlashStringHelper *);
+    size_t println(const String &s);
+    size_t println(const char[]);
+    size_t println(char);
+    size_t println(unsigned char, int = DEC);
+    size_t println(int, int = DEC);
+    size_t println(unsigned int, int = DEC);
+    size_t println(long, int = DEC);
+    size_t println(unsigned long, int = DEC);
+    size_t println(double, int = 2);
+    size_t println(const Printable&);
+    size_t println(void);
 };
 
 #endif
diff --git a/arduino/cores/arduino/Printable.h b/arduino/cores/arduino/Printable.h
index 9065904..e22e87e 100644
--- a/arduino/cores/arduino/Printable.h
+++ b/arduino/cores/arduino/Printable.h
@@ -30,7 +30,7 @@ class Print;
 class Printable
 {
   public:
-    virtual ssize_t printTo(Print& p) const = 0;
+    virtual size_t printTo(Print& p) const = 0;
 };
 
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]