Teensy 4.1 core updated for C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

66 lines
1.6KB

  1. /*
  2. IPAddress.cpp - Base class that provides IPAddress
  3. Copyright (c) 2011 Adrian McEwen. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include "Arduino.h"
  17. #include "IPAddress.h"
  18. size_t IPAddress::printTo(Print& p) const
  19. {
  20. int i=0;
  21. while (1) {
  22. p.print(_address.bytes[i], DEC);
  23. if (++i >= 4) return 4;
  24. p.write('.');
  25. }
  26. }
  27. bool IPAddress::fromString(const char *address)
  28. {
  29. unsigned int acc = 0; // Accumulator
  30. unsigned int dots = 0;
  31. while (*address) {
  32. char c = *address++;
  33. if (c >= '0' && c <= '9') {
  34. acc = acc * 10 + (c - '0');
  35. if (acc > 255) {
  36. // Value out of [0..255] range
  37. return false;
  38. }
  39. } else if (c == '.') {
  40. if (dots == 3) {
  41. // Too much dots (there must be 3 dots)
  42. return false;
  43. }
  44. _address.bytes[dots++] = acc;
  45. acc = 0;
  46. } else {
  47. // Invalid char
  48. return false;
  49. }
  50. }
  51. if (dots != 3) {
  52. // Too few dots (there must be 3 dots)
  53. return false;
  54. }
  55. _address.bytes[3] = acc;
  56. return true;
  57. }