1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-10 21:24:12 +01:00

Changing from long to ssize_t (int) for write(), print(), println() return.

This commit is contained in:
David A. Mellis 2011-08-26 14:20:41 -04:00
parent 3f0b7f21c2
commit 929597375b
23 changed files with 123 additions and 121 deletions

View File

@ -352,7 +352,7 @@ void HardwareSerial::flush()
; ;
} }
long HardwareSerial::write(uint8_t c) ssize_t HardwareSerial::write(uint8_t c)
{ {
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;

View File

@ -55,7 +55,7 @@ class HardwareSerial : public Stream
virtual int peek(void); virtual int peek(void);
virtual int read(void); virtual int read(void);
virtual void flush(void); virtual void flush(void);
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
using Print::write; // pull in write(str) and write(buf, size) from Print using Print::write; // pull in write(str) and write(buf, size) from Print
}; };

View File

@ -30,9 +30,9 @@
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */ /* default implementation: may be overridden */
long Print::write(const char *str) ssize_t Print::write(const char *str)
{ {
long n = 0; ssize_t n = 0;
while (*str) { while (*str) {
if (write(*str++) <= 0) break; if (write(*str++) <= 0) break;
n++; n++;
@ -41,9 +41,9 @@ long Print::write(const char *str)
} }
/* default implementation: may be overridden */ /* default implementation: may be overridden */
long Print::write(const uint8_t *buffer, size_t size) ssize_t Print::write(const uint8_t *buffer, size_t size)
{ {
long n = 0; ssize_t n = 0;
while (size--) { while (size--) {
if (write(*buffer++) <= 0) break; if (write(*buffer++) <= 0) break;
n++; n++;
@ -51,10 +51,10 @@ long Print::write(const uint8_t *buffer, size_t size)
return n; return n;
} }
long Print::print(const __FlashStringHelper *ifsh) ssize_t Print::print(const __FlashStringHelper *ifsh)
{ {
const prog_char *p = (const prog_char *)ifsh; const prog_char *p = (const prog_char *)ifsh;
long n = 0; ssize_t n = 0;
while (1) { while (1) {
unsigned char c = pgm_read_byte(p++); unsigned char c = pgm_read_byte(p++);
if (c == 0) break; if (c == 0) break;
@ -64,9 +64,9 @@ long Print::print(const __FlashStringHelper *ifsh)
return n; return n;
} }
long Print::print(const String &s) ssize_t Print::print(const String &s)
{ {
long n = 0; ssize_t n = 0;
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
if (write(s[i]) < 0) break; if (write(s[i]) < 0) break;
n++; n++;
@ -74,38 +74,38 @@ long Print::print(const String &s)
return n; return n;
} }
long Print::print(const char str[]) ssize_t Print::print(const char str[])
{ {
return write(str); return write(str);
} }
long Print::print(char c) ssize_t Print::print(char c)
{ {
return write(c); return write(c);
} }
long Print::print(unsigned char b, int base) ssize_t Print::print(unsigned char b, int base)
{ {
return print((unsigned long) b, base); return print((unsigned long) b, base);
} }
long Print::print(int n, int base) ssize_t Print::print(int n, int base)
{ {
return print((long) n, base); return print((long) n, base);
} }
long Print::print(unsigned int n, int base) ssize_t Print::print(unsigned int n, int base)
{ {
return print((unsigned long) n, base); return print((unsigned long) n, base);
} }
long Print::print(long n, int base) ssize_t Print::print(long n, int base)
{ {
if (base == 0) { if (base == 0) {
return write(n); return write(n);
} else if (base == 10) { } else if (base == 10) {
if (n < 0) { if (n < 0) {
long t = print('-'); int t = print('-');
if (t <= 0) return t; if (t <= 0) return t;
n = -n; n = -n;
return printNumber(n, 10) + 1; return printNumber(n, 10) + 1;
@ -116,110 +116,110 @@ long Print::print(long n, int base)
} }
} }
long Print::print(unsigned long n, int base) ssize_t Print::print(unsigned long n, int base)
{ {
if (base == 0) return write(n); if (base == 0) return write(n);
else return printNumber(n, base); else return printNumber(n, base);
} }
long Print::print(double n, int digits) ssize_t Print::print(double n, int digits)
{ {
return printFloat(n, digits); return printFloat(n, digits);
} }
long Print::println(const __FlashStringHelper *ifsh) ssize_t Print::println(const __FlashStringHelper *ifsh)
{ {
long n = print(ifsh); ssize_t n = print(ifsh);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::print(const Printable& x) ssize_t Print::print(const Printable& x)
{ {
return x.printTo(*this); return x.printTo(*this);
} }
long Print::println(void) ssize_t Print::println(void)
{ {
long t = print('\r'); ssize_t t = print('\r');
if (t <= 0) return t; if (t <= 0) return t;
if (print('\n') <= 0) return 1; if (print('\n') <= 0) return 1;
return 2; return 2;
} }
long Print::println(const String &s) ssize_t Print::println(const String &s)
{ {
long n = print(s); ssize_t n = print(s);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(const char c[]) ssize_t Print::println(const char c[])
{ {
long n = print(c); ssize_t n = print(c);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(char c) ssize_t Print::println(char c)
{ {
long n = print(c); ssize_t n = print(c);
if (n > 0) n += println(); if (n > 0) n += println();
return n; return n;
} }
long Print::println(unsigned char b, int base) ssize_t Print::println(unsigned char b, int base)
{ {
long n = print(b, base); ssize_t n = print(b, base);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(int num, int base) ssize_t Print::println(int num, int base)
{ {
long n = print(num, base); ssize_t n = print(num, base);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(unsigned int num, int base) ssize_t Print::println(unsigned int num, int base)
{ {
long n = print(num, base); ssize_t n = print(num, base);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(long num, int base) ssize_t Print::println(long num, int base)
{ {
long n = print(num, base); ssize_t n = print(num, base);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(unsigned long num, int base) ssize_t Print::println(unsigned long num, int base)
{ {
long n = print(num, base); ssize_t n = print(num, base);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(double num, int digits) ssize_t Print::println(double num, int digits)
{ {
long n = print(num, digits); ssize_t n = print(num, digits);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
long Print::println(const Printable& x) ssize_t Print::println(const Printable& x)
{ {
long n = print(x); ssize_t n = print(x);
if (n >= 0) n += println(); if (n >= 0) n += println();
return n; return n;
} }
// Private Methods ///////////////////////////////////////////////////////////// // Private Methods /////////////////////////////////////////////////////////////
long Print::printNumber(unsigned long n, uint8_t base) { ssize_t Print::printNumber(unsigned long n, uint8_t base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1]; char *str = &buf[sizeof(buf) - 1];
@ -238,9 +238,9 @@ long Print::printNumber(unsigned long n, uint8_t base) {
return write(str); return write(str);
} }
long Print::printFloat(double number, uint8_t digits) ssize_t Print::printFloat(double number, uint8_t digits)
{ {
long n = 0, t; ssize_t n = 0, t;
// Handle negative numbers // Handle negative numbers
if (number < 0.0) if (number < 0.0)

View File

@ -23,6 +23,8 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> // for size_t #include <stdio.h> // for size_t
typedef int ssize_t;
#include "WString.h" #include "WString.h"
#include "Printable.h" #include "Printable.h"
@ -34,37 +36,37 @@
class Print class Print
{ {
private: private:
long printNumber(unsigned long, uint8_t); ssize_t printNumber(unsigned long, uint8_t);
long printFloat(double, uint8_t); ssize_t printFloat(double, uint8_t);
public: public:
virtual long write(uint8_t) = 0; virtual ssize_t write(uint8_t) = 0;
virtual long write(const char *str); virtual ssize_t write(const char *str);
virtual long write(const uint8_t *buffer, size_t size); virtual ssize_t write(const uint8_t *buffer, size_t size);
long print(const __FlashStringHelper *); ssize_t print(const __FlashStringHelper *);
long print(const String &); ssize_t print(const String &);
long print(const char[]); ssize_t print(const char[]);
long print(char); ssize_t print(char);
long print(unsigned char, int = DEC); ssize_t print(unsigned char, int = DEC);
long print(int, int = DEC); ssize_t print(int, int = DEC);
long print(unsigned int, int = DEC); ssize_t print(unsigned int, int = DEC);
long print(long, int = DEC); ssize_t print(long, int = DEC);
long print(unsigned long, int = DEC); ssize_t print(unsigned long, int = DEC);
long print(double, int = 2); ssize_t print(double, int = 2);
long print(const Printable&); ssize_t print(const Printable&);
long println(const __FlashStringHelper *); ssize_t println(const __FlashStringHelper *);
long println(const String &s); ssize_t println(const String &s);
long println(const char[]); ssize_t println(const char[]);
long println(char); ssize_t println(char);
long println(unsigned char, int = DEC); ssize_t println(unsigned char, int = DEC);
long println(int, int = DEC); ssize_t println(int, int = DEC);
long println(unsigned int, int = DEC); ssize_t println(unsigned int, int = DEC);
long println(long, int = DEC); ssize_t println(long, int = DEC);
long println(unsigned long, int = DEC); ssize_t println(unsigned long, int = DEC);
long println(double, int = 2); ssize_t println(double, int = 2);
long println(const Printable&); ssize_t println(const Printable&);
long println(void); ssize_t println(void);
}; };
#endif #endif

View File

@ -30,7 +30,7 @@ class Print;
class Printable class Printable
{ {
public: public:
virtual long printTo(Print& p) const = 0; virtual ssize_t printTo(Print& p) const = 0;
}; };
#endif #endif

View File

@ -70,15 +70,15 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1; return 1;
} }
long Client::write(uint8_t b) { ssize_t Client::write(uint8_t b) {
return write(&b, 1); return write(&b, 1);
} }
long Client::write(const char *str) { ssize_t Client::write(const char *str) {
return write((const uint8_t *) str, strlen(str)); return write((const uint8_t *) str, strlen(str));
} }
long Client::write(const uint8_t *buf, size_t size) { ssize_t Client::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) return -1; if (_sock == MAX_SOCK_NUM) return -1;
if (!send(_sock, buf, size)) return -2; if (!send(_sock, buf, size)) return -2;
return size; return size;

View File

@ -12,9 +12,9 @@ public:
uint8_t status(); uint8_t status();
int connect(IPAddress ip, uint16_t port); int connect(IPAddress ip, uint16_t port);
int connect(const char *host, uint16_t port); int connect(const char *host, uint16_t port);
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
virtual long write(const char *str); virtual ssize_t write(const char *str);
virtual long write(const uint8_t *buf, size_t size); virtual ssize_t write(const uint8_t *buf, size_t size);
virtual int available(); virtual int available();
virtual int read(); virtual int read();
virtual int read(uint8_t *buf, size_t size); virtual int read(uint8_t *buf, size_t size);

View File

@ -42,9 +42,9 @@ bool IPAddress::operator==(const uint8_t* addr)
return memcmp(addr, _address, sizeof(_address)) == 0; return memcmp(addr, _address, sizeof(_address)) == 0;
} }
long IPAddress::printTo(Print& p) const ssize_t IPAddress::printTo(Print& p) const
{ {
long n = 0, t; ssize_t n = 0, t;
for (int i =0; i < 3; i++) for (int i =0; i < 3; i++)
{ {
if ((t = p.print(_address[i], DEC)) > 0) n += t; if ((t = p.print(_address[i], DEC)) > 0) n += t;

View File

@ -60,7 +60,7 @@ public:
IPAddress& operator=(const uint8_t *address); IPAddress& operator=(const uint8_t *address);
IPAddress& operator=(uint32_t address); IPAddress& operator=(uint32_t address);
virtual long printTo(Print& p) const; virtual ssize_t printTo(Print& p) const;
friend class EthernetClass; friend class EthernetClass;
friend class UDP; friend class UDP;

View File

@ -67,19 +67,19 @@ Client Server::available()
return Client(MAX_SOCK_NUM); return Client(MAX_SOCK_NUM);
} }
long Server::write(uint8_t b) ssize_t Server::write(uint8_t b)
{ {
write(&b, 1); write(&b, 1);
} }
long Server::write(const char *str) ssize_t Server::write(const char *str)
{ {
write((const uint8_t *)str, strlen(str)); write((const uint8_t *)str, strlen(str));
} }
long Server::write(const uint8_t *buffer, size_t size) ssize_t Server::write(const uint8_t *buffer, size_t size)
{ {
long n = 0; ssize_t n = 0;
accept(); accept();

View File

@ -14,9 +14,9 @@ public:
Server(uint16_t); Server(uint16_t);
Client available(); Client available();
void begin(); void begin();
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
virtual long write(const char *str); virtual ssize_t write(const char *str);
virtual long write(const uint8_t *buf, size_t size); virtual ssize_t write(const uint8_t *buf, size_t size);
}; };
#endif #endif

View File

@ -102,18 +102,18 @@ int UDP::endPacket()
return sendUDP(_sock); return sendUDP(_sock);
} }
long UDP::write(uint8_t byte) ssize_t UDP::write(uint8_t byte)
{ {
return write(&byte, 1); return write(&byte, 1);
} }
long UDP::write(const char *str) ssize_t UDP::write(const char *str)
{ {
size_t len = strlen(str); size_t len = strlen(str);
return write((const uint8_t *)str, len); return write((const uint8_t *)str, len);
} }
long UDP::write(const uint8_t *buffer, size_t size) ssize_t UDP::write(const uint8_t *buffer, size_t size)
{ {
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size); uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
_offset += bytes_written; _offset += bytes_written;

View File

@ -67,11 +67,11 @@ public:
// Returns 1 if the packet was sent successfully, 0 if there was an error // Returns 1 if the packet was sent successfully, 0 if there was an error
int endPacket(); int endPacket();
// Write a single byte into the packet // Write a single byte into the packet
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
// Write a string of characters into the packet // Write a string of characters into the packet
virtual long write(const char *str); virtual ssize_t write(const char *str);
// Write size bytes from buffer into the packet // Write size bytes from buffer into the packet
virtual long write(const uint8_t *buffer, size_t size); virtual ssize_t write(const uint8_t *buffer, size_t size);
// Start processing the next available incoming packet // Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available // Returns the size of the packet in bytes, or 0 if no packets are available

View File

@ -258,7 +258,7 @@ inline void LiquidCrystal::command(uint8_t value) {
send(value, LOW); send(value, LOW);
} }
inline long LiquidCrystal::write(uint8_t value) { inline ssize_t LiquidCrystal::write(uint8_t value) {
send(value, HIGH); send(value, HIGH);
return 1; // assume sucess return 1; // assume sucess
} }

View File

@ -79,7 +79,7 @@ public:
void createChar(uint8_t, uint8_t[]); void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t); void setCursor(uint8_t, uint8_t);
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
void command(uint8_t); void command(uint8_t);
private: private:
void send(uint8_t, uint8_t); void send(uint8_t, uint8_t);

View File

@ -58,16 +58,16 @@ boolean File::isDirectory(void) {
} }
long File::write(uint8_t val) { ssize_t File::write(uint8_t val) {
return write(&val, 1); return write(&val, 1);
} }
long File::write(const char *str) { ssize_t File::write(const char *str) {
return write((const uint8_t *) str, strlen(str)); return write((const uint8_t *) str, strlen(str));
} }
long File::write(const uint8_t *buf, size_t size) { ssize_t File::write(const uint8_t *buf, size_t size) {
long t; ssize_t t;
if (!_file) return -1; if (!_file) return -1;
t = _file->write(buf, size); t = _file->write(buf, size);
if (t < 0) return t - 1; if (t < 0) return t - 1;

View File

@ -32,9 +32,9 @@ public:
File(SdFile f, char *name); // wraps an underlying SdFile File(SdFile f, char *name); // wraps an underlying SdFile
File(void); // 'empty' constructor File(void); // 'empty' constructor
~File(void); // destructor ~File(void); // destructor
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
virtual long write(const char *str); virtual ssize_t write(const char *str);
virtual long write(const uint8_t *buf, size_t size); virtual ssize_t write(const uint8_t *buf, size_t size);
virtual int read(); virtual int read();
virtual int peek(); virtual int peek();
virtual int available(); virtual int available();

View File

@ -283,9 +283,9 @@ class SdFile : public Print {
} }
/** \return SdVolume that contains this file. */ /** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;} SdVolume* volume(void) const {return vol_;}
long write(uint8_t b); ssize_t write(uint8_t b);
long write(const void* buf, uint16_t nbyte); ssize_t write(const void* buf, uint16_t nbyte);
long write(const char* str); ssize_t write(const char* str);
void write_P(PGM_P str); void write_P(PGM_P str);
void writeln_P(PGM_P str); void writeln_P(PGM_P str);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -1121,7 +1121,7 @@ uint8_t SdFile::truncate(uint32_t length) {
* for a read-only file, device is full, a corrupt file system or an I/O error. * for a read-only file, device is full, a corrupt file system or an I/O error.
* *
*/ */
long SdFile::write(const void* buf, uint16_t nbyte) { ssize_t SdFile::write(const void* buf, uint16_t nbyte) {
// convert void* to uint8_t* - must be before goto statements // convert void* to uint8_t* - must be before goto statements
const uint8_t* src = reinterpret_cast<const uint8_t*>(buf); const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
@ -1219,7 +1219,7 @@ long SdFile::write(const void* buf, uint16_t nbyte) {
* *
* Use SdFile::writeError to check for errors. * Use SdFile::writeError to check for errors.
*/ */
long SdFile::write(uint8_t b) { ssize_t SdFile::write(uint8_t b) {
return write(&b, 1); return write(&b, 1);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -1228,7 +1228,7 @@ long SdFile::write(uint8_t b) {
* *
* Use SdFile::writeError to check for errors. * Use SdFile::writeError to check for errors.
*/ */
long SdFile::write(const char* str) { ssize_t SdFile::write(const char* str) {
return write(str, strlen(str)); return write(str, strlen(str));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -440,7 +440,7 @@ int SoftwareSerial::available()
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF; return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
} }
long SoftwareSerial::write(uint8_t b) ssize_t SoftwareSerial::write(uint8_t b)
{ {
if (_tx_delay == 0) if (_tx_delay == 0)
return -1; return -1;

View File

@ -89,7 +89,7 @@ public:
bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; } bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
int peek(); int peek();
virtual long write(uint8_t byte); virtual ssize_t write(uint8_t byte);
virtual int read(); virtual int read();
virtual int available(); virtual int available();
virtual void flush(); virtual void flush();

View File

@ -124,7 +124,7 @@ uint8_t TwoWire::endTransmission(void)
// must be called in: // must be called in:
// slave tx event callback // slave tx event callback
// or after beginTransmission(address) // or after beginTransmission(address)
long TwoWire::write(uint8_t data) ssize_t TwoWire::write(uint8_t data)
{ {
if(transmitting){ if(transmitting){
// in master transmitter mode // in master transmitter mode
@ -148,7 +148,7 @@ long TwoWire::write(uint8_t data)
// must be called in: // must be called in:
// slave tx event callback // slave tx event callback
// or after beginTransmission(address) // or after beginTransmission(address)
long TwoWire::write(const uint8_t *data, size_t quantity) ssize_t TwoWire::write(const uint8_t *data, size_t quantity)
{ {
if(transmitting){ if(transmitting){
// in master transmitter mode // in master transmitter mode
@ -166,7 +166,7 @@ long TwoWire::write(const uint8_t *data, size_t quantity)
// must be called in: // must be called in:
// slave tx event callback // slave tx event callback
// or after beginTransmission(address) // or after beginTransmission(address)
long TwoWire::write(const char *data) ssize_t TwoWire::write(const char *data)
{ {
return write((uint8_t*)data, strlen(data)); return write((uint8_t*)data, strlen(data));
} }

View File

@ -52,9 +52,9 @@ class TwoWire : public Stream
uint8_t endTransmission(void); uint8_t endTransmission(void);
uint8_t requestFrom(uint8_t, uint8_t); uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(int, int); uint8_t requestFrom(int, int);
virtual long write(uint8_t); virtual ssize_t write(uint8_t);
virtual long write(const char *); virtual ssize_t write(const char *);
virtual long write(const uint8_t *, size_t); virtual ssize_t write(const uint8_t *, size_t);
virtual int available(void); virtual int available(void);
virtual int read(void); virtual int read(void);
virtual int peek(void); virtual int peek(void);