string_converter.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_FLEXLAY_STRING_CONVERTER_HPP
  17. #define HEADER_FLEXLAY_STRING_CONVERTER_HPP
  18. #include <stdexcept>
  19. #include <sstream>
  20. #include "config.h"
  21. template <class T>
  22. std::string to_string(const T& any)
  23. {
  24. std::ostringstream oss;
  25. oss << any;
  26. return oss.str();
  27. }
  28. template <class T>
  29. bool from_string(const std::string& rep, T& x)
  30. {
  31. // this is necessary so that if "x" is not modified if the conversion fails
  32. T temp;
  33. std::istringstream iss(rep);
  34. iss >> temp;
  35. if (iss.fail()) {
  36. return false;
  37. } else {
  38. x = temp;
  39. return true;
  40. }
  41. }
  42. inline bool has_suffix(const std::string& data, const std::string& suffix)
  43. {
  44. if (data.length() >= suffix.length())
  45. return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
  46. else
  47. return false;
  48. }
  49. #endif
  50. /* EOF */