You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 7 години
преди 11 години
преди 9 години
преди 11 години
преди 8 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }