split.H 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. * */
  13. /*
  14. * split.H
  15. *
  16. * Created on: May 1, 2013
  17. * Author: xaxaxa
  18. */
  19. #ifndef SPLIT_H_
  20. #define SPLIT_H_
  21. #include <cpoll/basictypes.H>
  22. namespace cppsp
  23. {
  24. struct split
  25. {
  26. const char* s;
  27. const char* end;
  28. const char* s1;
  29. CP::String value;
  30. char delim;
  31. split(const char* s, int len, char delim) {
  32. if (len == -1) len = strlen(s);
  33. this->s = s;
  34. this->s1 = s;
  35. this->end = s + len;
  36. this->delim = delim;
  37. }
  38. bool read() {
  39. if (s == nullptr) return false;
  40. s = (const char*) memchr(s, delim, end - s);
  41. if (s == nullptr) {
  42. value= {s1, int(end - s1)};
  43. return true;
  44. }
  45. value= {s1, int(s - s1)};
  46. s1 = ++s;
  47. return true;
  48. }
  49. };
  50. }
  51. #endif /* SPLIT_H_ */