utils.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
  2. /*
  3. * utils.cc
  4. * Copyright (C) 2016 Unknown <francois@gautrais.eu>
  5. *
  6. * histodex is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * histodex is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "utils.h"
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <sstream>
  24. std::string tstr(const char* x)
  25. {
  26. return std::string(x);
  27. }
  28. std::string tstr(int x)
  29. {
  30. char tmp[64];
  31. sprintf(tmp, "%d", x);
  32. return std::string(tmp);
  33. }
  34. void split(const std::string &s, char delim, std::vector<std::string> &elems) {
  35. std::stringstream ss;
  36. ss.str(s);
  37. std::string item;
  38. while (std::getline(ss, item, delim)) {
  39. elems.push_back(item);
  40. }
  41. }
  42. std::string tstr(double x, int n)
  43. {
  44. char sn[16], tmp[64];
  45. sprintf(sn, "%%0.%df", n);
  46. sprintf(tmp, sn, x);
  47. return tstr(tmp);
  48. }
  49. std::vector<std::string> split(const std::string &s, char delim) {
  50. std::vector<std::string> elems;
  51. split(s, delim, elems);
  52. return elems;
  53. }