Common.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2009-2016, 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. //==============================================================================
  12. void log(const char* file, int line, unsigned type, const char* fmt, ...)
  13. {
  14. char buffer[1024];
  15. va_list args;
  16. va_start(args, fmt);
  17. vsnprintf(buffer, sizeof(buffer), fmt, args);
  18. va_end(args);
  19. switch(type)
  20. {
  21. case 1:
  22. fprintf(stdout,
  23. TERMINAL_COL_INFO "[I] %s (%s:%d)\n" TERMINAL_COL_RESET,
  24. buffer,
  25. file,
  26. line);
  27. break;
  28. case 2:
  29. fprintf(stderr,
  30. TERMINAL_COL_ERROR "[E] %s (%s:%d)\n" TERMINAL_COL_RESET,
  31. buffer,
  32. file,
  33. line);
  34. break;
  35. case 3:
  36. fprintf(stderr,
  37. TERMINAL_COL_WARNING "[W] %s (%s:%d)\n" TERMINAL_COL_RESET,
  38. buffer,
  39. file,
  40. line);
  41. break;
  42. };
  43. }
  44. //==============================================================================
  45. std::string replaceAllString(
  46. const std::string& str, const std::string& from, const std::string& to)
  47. {
  48. if(from.empty())
  49. {
  50. return str;
  51. }
  52. std::string out = str;
  53. size_t start_pos = 0;
  54. while((start_pos = out.find(from, start_pos)) != std::string::npos)
  55. {
  56. out.replace(start_pos, from.length(), to);
  57. start_pos += to.length();
  58. }
  59. return out;
  60. }
  61. //==============================================================================
  62. std::string getFilename(const std::string& path)
  63. {
  64. std::string out;
  65. const size_t last = path.find_last_of("/");
  66. if(std::string::npos != last)
  67. {
  68. out.insert(out.end(), path.begin() + last + 1, path.end());
  69. }
  70. else
  71. {
  72. out = path;
  73. }
  74. return out;
  75. }