Added {} to control structures for clarity (#600)

This commit is contained in:
Thomas Mørch
2016-10-16 12:46:42 +02:00
committed by Olivier
parent 80659ddb00
commit 54776a4520
34 changed files with 740 additions and 406 deletions
+16 -8
View File
@@ -120,7 +120,9 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
}
size_t EthernetClient::write(const char *str) {
if (str == NULL) return 0;
if (str == NULL) {
return 0;
}
return write((const uint8_t *)str, strlen(str));
}
size_t EthernetClient::write(const char *buffer, size_t size) {
@@ -165,8 +167,9 @@ void EthernetClient::flush() {
}
void EthernetClient::stop() {
if (_sock == -1)
if (_sock == -1) {
return;
}
// attempt to close the connection gracefully (send a FIN to other side)
shutdown(_sock, SHUT_RDWR);
@@ -178,21 +181,24 @@ void EthernetClient::stop() {
uint8_t s;
do {
s = status();
if (s == ETHERNETCLIENT_W5100_CLOSED)
if (s == ETHERNETCLIENT_W5100_CLOSED) {
break; // exit the loop
}
usleep(1000);
gettimeofday(&curTime, NULL);
} while (((curTime.tv_sec - startTime.tv_sec) * 1000000) + (curTime.tv_usec - startTime.tv_usec) < 1000000);
// if it hasn't closed, close it forcefully
if (s != ETHERNETCLIENT_W5100_CLOSED)
if (s != ETHERNETCLIENT_W5100_CLOSED) {
close(_sock);
}
_sock = -1;
}
uint8_t EthernetClient::status() {
if (_sock == -1) return ETHERNETCLIENT_W5100_CLOSED;
if (_sock == -1) {
return ETHERNETCLIENT_W5100_CLOSED;
}
struct tcp_info tcp_info;
int tcp_info_length = sizeof(tcp_info);
@@ -227,8 +233,10 @@ uint8_t EthernetClient::status() {
}
uint8_t EthernetClient::connected() {
if (_sock == -1) return 0;
if (_sock == -1) {
return 0;
}
if (peek() < 0) {
if (errno == EAGAIN) {
return 1;
+3 -1
View File
@@ -151,7 +151,9 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
size_t EthernetServer::write(const char *str)
{
if (str == NULL) return 0;
if (str == NULL) {
return 0;
}
return write((const uint8_t *)str, strlen(str));
}
+16 -8
View File
@@ -105,10 +105,12 @@ size_t Print::print(long n, int base) {
}
size_t Print::print(unsigned long n, int base) {
if(base == 0)
if(base == 0) {
return write(n);
else
}
else {
return printNumber(n, base);
}
}
size_t Print::print(double n, int digits) {
@@ -182,8 +184,9 @@ size_t Print::printNumber(unsigned long n, uint8_t base) {
*str = '\0';
// prevent crash if called with base == 1
if(base < 2)
if(base < 2) {
base = 10;
}
do {
unsigned long m = n;
@@ -198,14 +201,18 @@ size_t Print::printNumber(unsigned long n, uint8_t base) {
size_t Print::printFloat(double number, uint8_t digits) {
size_t n = 0;
if(std::isnan(number))
if(std::isnan(number)) {
return print("nan");
if(std::isinf(number))
}
if(std::isinf(number)) {
return print("inf");
if(number > 4294967040.0)
}
if(number > 4294967040.0) {
return print("ovf"); // constant determined empirically
if(number < -4294967040.0)
}
if(number < -4294967040.0) {
return print("ovf"); // constant determined empirically
}
// Handle negative numbers
if(number < 0.0) {
@@ -215,8 +222,9 @@ size_t Print::printFloat(double number, uint8_t digits) {
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for(uint8_t i = 0; i < digits; ++i)
for(uint8_t i = 0; i < digits; ++i) {
rounding /= 10.0;
}
number += rounding;
+3 -1
View File
@@ -212,7 +212,9 @@ int SerialPort::peek()
{
FILE * f = fdopen(sd, "r+");
int c = getc(f);
if (c == EOF) return -1;
if (c == EOF) {
return -1;
}
ungetc(c, f);
return c;
}
+53 -29
View File
@@ -33,8 +33,9 @@ int Stream::timedRead() {
_startMillis = millis();
do {
c = read();
if(c >= 0)
return c;
if(c >= 0) {
return c;
}
yield();
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
@@ -46,8 +47,9 @@ int Stream::timedPeek() {
_startMillis = millis();
do {
c = peek();
if(c >= 0)
if(c >= 0) {
return c;
}
yield();
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
@@ -59,12 +61,15 @@ int Stream::peekNextDigit() {
int c;
while(1) {
c = timedPeek();
if(c < 0)
if(c < 0) {
return c; // timeout
if(c == '-')
}
if(c == '-') {
return c;
if(c >= '0' && c <= '9')
}
if(c >= '0' && c <= '9') {
return c;
}
read(); // discard non-numeric
}
}
@@ -101,13 +106,14 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
size_t termIndex = 0;
int c;
if(*target == 0)
if(*target == 0) {
return true; // return true if target is a null string
}
while((c = timedRead()) > 0) {
if(c != target[index])
if(c != target[index]) {
index = 0; // reset index if any char does not match
}
if(c == target[index]) {
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
if(++index >= targetLen) { // return true if all chars in the target match
@@ -116,10 +122,12 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
}
if(termLen > 0 && c == terminator[termIndex]) {
if(++termIndex >= termLen)
if(++termIndex >= termLen) {
return false; // return false if terminate string found before target string
} else
}
} else {
termIndex = 0;
}
}
return false;
}
@@ -140,22 +148,27 @@ long Stream::parseInt(char skipChar) {
c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
if(c < 0) {
return 0; // zero returned if timeout
}
do {
if(c == skipChar)
; // ignore this charactor
else if(c == '-')
if(c == skipChar) {
// ignore this charactor
}
else if(c == '-') {
isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit?
}
else if(c >= '0' && c <= '9') { // is c a digit?
value = value * 10 + c - '0';
}
read(); // consume the character we got with peek
c = timedPeek();
} while((c >= '0' && c <= '9') || c == skipChar);
if(isNegative)
if(isNegative) {
value = -value;
}
return value;
}
@@ -175,31 +188,39 @@ float Stream::parseFloat(char skipChar) {
c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
if(c < 0) {
return 0; // zero returned if timeout
}
do {
if(c == skipChar)
; // ignore
else if(c == '-')
if(c == skipChar) {
// ignore
}
else if(c == '-') {
isNegative = true;
else if(c == '.')
}
else if(c == '.') {
isFraction = true;
}
else if(c >= '0' && c <= '9') { // is c a digit?
value = value * 10 + c - '0';
if(isFraction)
if(isFraction) {
fraction *= 0.1;
}
}
read(); // consume the character we got with peek
c = timedPeek();
} while((c >= '0' && c <= '9') || c == '.' || c == skipChar);
if(isNegative)
if(isNegative) {
value = -value;
if(isFraction)
}
if(isFraction) {
return value * fraction;
else
}
else {
return value;
}
}
// read characters from stream into buffer
@@ -211,8 +232,9 @@ size_t Stream::readBytes(char *buffer, size_t length) {
size_t count = 0;
while(count < length) {
int c = timedRead();
if(c < 0)
if(c < 0) {
break;
}
*buffer++ = (char) c;
count++;
}
@@ -224,13 +246,15 @@ size_t Stream::readBytes(char *buffer, size_t length) {
// returns the number of characters placed in the buffer (0 means no valid data found)
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) {
if(length < 1)
if(length < 1) {
return 0;
}
size_t index = 0;
while(index < length) {
int c = timedRead();
if(c < 0 || c == terminator)
if(c < 0 || c == terminator) {
break;
}
*buffer++ = (char) c;
index++;
}
+6 -4
View File
@@ -1,14 +1,14 @@
/*
noniso.cpp - replacements for non-ISO functions used by Arduino core
Copyright © 2016 Ivan Grokhotkov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
@@ -61,8 +61,9 @@ char* itoa(int value, char* result, int base) {
} while(quotient);
// Apply negative sign
if(value < 0)
if(value < 0) {
*out++ = '-';
}
reverse(result, out);
*out = 0;
@@ -112,8 +113,9 @@ char* ltoa(long value, char* result, int base) {
} while(quotient);
// Apply negative sign
if(value < 0)
if(value < 0) {
*out++ = '-';
}
reverse(result, out);
*out = 0;