string_util_numeric_sort_test.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SuperTux
  2. // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
  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. #include <algorithm>
  17. #include <fstream>
  18. #include <iostream>
  19. #include <vector>
  20. #include "util/string_util.hpp"
  21. int main(int argc, char** argv)
  22. {
  23. std::vector<std::string> lines;
  24. // read files from stdin or files
  25. std::string line;
  26. if (argc > 1)
  27. {
  28. for(int i = 1; i < argc; ++i)
  29. {
  30. std::ifstream in(argv[i]);
  31. while(std::getline(in, line))
  32. {
  33. lines.push_back(line);
  34. }
  35. }
  36. }
  37. else
  38. {
  39. while(std::getline(std::cin, line))
  40. {
  41. lines.push_back(line);
  42. }
  43. }
  44. // sort lines
  45. std::sort(lines.begin(), lines.end(), StringUtil::numeric_less);
  46. // output the sorted text
  47. for(std::vector<std::string>::iterator i = lines.begin(); i != lines.end(); ++i)
  48. {
  49. std::cout << *i << std::endl;
  50. }
  51. return 0;
  52. }
  53. /* EOF */