Util.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #if !defined(Util_HPP)
  2. #define Util_HPP
  3. #include "inc.hpp"
  4. #define CATE_VERSION "v2.2 (Development)"
  5. #ifdef __WIN32
  6. #define ARGC_START 0
  7. #define OBJ_EXTENSION ".obj" //windows, why?
  8. #else
  9. #define ARGC_START 1
  10. #define OBJ_EXTENSION ".o"
  11. #endif // OS check
  12. extern int32_t lexer_line;
  13. #define BOLD "\033[1m"
  14. #define COLOR_RESET "\033[0m"
  15. #define RED "\033[31m"
  16. #define GREEN "\033[32m"
  17. #define YELLOW "\033[33m"
  18. #define BLUE "\033[34m"
  19. #define PURPLE "\033[35m"
  20. #define CYAN "\033[36m"
  21. #include <unistd.h>
  22. namespace Util
  23. {
  24. void error(string_view problem);
  25. void command_error(string_view problem);
  26. void lexer_error(std::string problem); //has the std here because flex /neg
  27. void error(int32_t line, string_view problem);
  28. void fatal_error(int32_t line, string_view problem);
  29. void build_error(string_view name, string_view problem);
  30. inline string remove_extension(string& s) {return s = s.substr(0, s.find_last_of("."));}
  31. string remove_quotes(string &s);
  32. //there are two of these because i'm lazy.
  33. void replace_all(string& s, string_view toReplace, string_view replaceWith);
  34. string replace_all_safe(string_view s, string_view toReplace, string_view replaceWith);
  35. /// @brief Returns the modified time of the given path
  36. /// @param path Path to check
  37. /// @return 0 if file doesn't exist, the modified time if it does.
  38. long long get_modified_time(const char *path);
  39. inline bool file_exists(const char* file_name) { return access(file_name, F_OK) != -1; }
  40. /// @brief Creates a folder if it doesn't already exist.
  41. /// @param path Path of the folder to create
  42. void create_folder(const char* path);
  43. /// @brief Like std::system, but exits if the command returns anything other than 0.
  44. /// @param command The command to execute.
  45. void system(string_view command);
  46. void user_system(int32_t line, string_view command);
  47. bool ends_with(string_view value, string_view ending); //written by tshepang from stackoverflow
  48. } // namespace Util
  49. #define highlight_func(x) YELLOW x COLOR_RESET
  50. #define highlight_var(x) PURPLE x COLOR_RESET
  51. /// @brief Returns if a file is a newer than another
  52. /// @param a Path to check
  53. /// @param b Other path to check
  54. #define newer_than(a, b) (Util::get_modified_time(a.c_str()) < Util::get_modified_time(b.c_str()))
  55. #endif