Common.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  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, TERMINAL_COL_INFO "(%s:%4d) Info: %s\n"
  23. TERMINAL_COL_RESET, file, line, buffer);
  24. break;
  25. case 2:
  26. fprintf(stderr, TERMINAL_COL_ERROR "(%s:%4d) Error: %s\n"
  27. TERMINAL_COL_RESET, file, line, buffer);
  28. break;
  29. case 3:
  30. fprintf(stderr, TERMINAL_COL_WARNING "(%s:%4d) Warning: %s\n"
  31. TERMINAL_COL_RESET, file, line, buffer);
  32. break;
  33. };
  34. }
  35. //==============================================================================
  36. std::string replaceAllString(
  37. const std::string& str,
  38. const std::string& from,
  39. const std::string& to)
  40. {
  41. if(from.empty())
  42. {
  43. return str;
  44. }
  45. std::string out = str;
  46. size_t start_pos = 0;
  47. while((start_pos = out.find(from, start_pos)) != std::string::npos)
  48. {
  49. out.replace(start_pos, from.length(), to);
  50. start_pos += to.length();
  51. }
  52. return out;
  53. }
  54. //==============================================================================
  55. std::string getFilename(const std::string& path)
  56. {
  57. std::string out;
  58. const size_t last = path.find_last_of("/");
  59. if(std::string::npos != last)
  60. {
  61. out.insert(out.end(), path.begin() + last + 1, path.end());
  62. }
  63. else
  64. {
  65. out = path;
  66. }
  67. return out;
  68. }