utils.cpp 792 B

123456789101112131415161718192021222324252627282930313233
  1. #include "utils.h"
  2. #include <algorithm>
  3. bool replace(std::string &str, const std::string &from, const std::string &to) {
  4. size_t start_pos = str.find(from);
  5. if (start_pos == std::string::npos)
  6. return false;
  7. str.replace(start_pos, from.length(), to);
  8. return true;
  9. }
  10. bool replace(std::string &str, const char *from, const char *to) {
  11. size_t start_pos = str.find(from);
  12. if (start_pos == std::string::npos)
  13. return false;
  14. str.replace(start_pos, strlen(from), to);
  15. return true;
  16. }
  17. bool file_exists(const std::string &name) {
  18. std::ifstream f(name.c_str());
  19. return f.good();
  20. }
  21. bool file_exists(const char *name) {
  22. std::ifstream f(name);
  23. return f.good();
  24. }
  25. void string_toupper(std::string &str) {
  26. std::transform(str.begin(), str.end(), str.begin(), ::toupper);
  27. }