Common.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "Common.h"
  6. #include <cstdarg>
  7. #define TERMINAL_COL_INFO "\033[0;32m"
  8. #define TERMINAL_COL_ERROR "\033[1;31m"
  9. #define TERMINAL_COL_WARNING "\033[0;33m"
  10. #define TERMINAL_COL_RESET "\033[0m"
  11. void log(const char* file, int line, unsigned type, const char* fmt, ...)
  12. {
  13. char buffer[1024];
  14. va_list args;
  15. va_start(args, fmt);
  16. vsnprintf(buffer, sizeof(buffer), fmt, args);
  17. va_end(args);
  18. switch(type)
  19. {
  20. case 1:
  21. fprintf(stdout, TERMINAL_COL_INFO "[I] %s (%s:%d)\n" TERMINAL_COL_RESET, buffer, file, line);
  22. break;
  23. case 2:
  24. fprintf(stderr, TERMINAL_COL_ERROR "[E] %s (%s:%d)\n" TERMINAL_COL_RESET, buffer, file, line);
  25. break;
  26. case 3:
  27. fprintf(stderr, TERMINAL_COL_WARNING "[W] %s (%s:%d)\n" TERMINAL_COL_RESET, buffer, file, line);
  28. break;
  29. };
  30. }
  31. std::string replaceAllString(const std::string& str, const std::string& from, const std::string& to)
  32. {
  33. if(from.empty())
  34. {
  35. return str;
  36. }
  37. std::string out = str;
  38. size_t start_pos = 0;
  39. while((start_pos = out.find(from, start_pos)) != std::string::npos)
  40. {
  41. out.replace(start_pos, from.length(), to);
  42. start_pos += to.length();
  43. }
  44. return out;
  45. }
  46. std::string getFilename(const std::string& path)
  47. {
  48. std::string out;
  49. const size_t last = path.find_last_of("/");
  50. if(std::string::npos != last)
  51. {
  52. out.insert(out.end(), path.begin() + last + 1, path.end());
  53. }
  54. else
  55. {
  56. out = path;
  57. }
  58. return out;
  59. }