OS.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <cstdarg>
  25. #include "Config.h"
  26. #include "Types.h"
  27. #include "Vector.h"
  28. #include "DynamicString.h"
  29. #include "EventBuffer.h"
  30. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. #if defined(LINUX)
  34. const size_t MAX_PATH_LENGTH = 1024;
  35. const char PATH_SEPARATOR = '/';
  36. #elif defined(WINDOWS)
  37. const size_t MAX_PATH_LENGTH = 1024;
  38. const char PATH_SEPARATOR = '\\';
  39. #define snprintf _snprintf
  40. #undef MK_SHIFT
  41. #undef MK_ALT
  42. #elif defined(ANDROID)
  43. const size_t MAX_PATH_LENGTH = 1024;
  44. const char PATH_SEPARATOR = '/';
  45. #else
  46. #error "Oops, invalid platform!"
  47. #endif
  48. namespace os
  49. {
  50. //-----------------------------------------------------------------------------
  51. // Print and log functions
  52. //-----------------------------------------------------------------------------
  53. void printf(const char* string, ...); //!< Printf wrapper
  54. void vprintf(const char* string, va_list arg); //!< VPrintf wrapper
  55. void log_debug(const char* string, va_list arg); //!< Print debug message
  56. void log_error(const char* string, va_list arg); //!< Print error message
  57. void log_warning(const char* string, va_list arg); //!< Print warning message
  58. void log_info(const char* string, va_list arg); //!< Print info message
  59. //-----------------------------------------------------------------------------
  60. // Paths
  61. //-----------------------------------------------------------------------------
  62. bool is_root_path(const char* path);
  63. bool is_absolute_path(const char* path);
  64. //-----------------------------------------------------------------------------
  65. // File management
  66. //-----------------------------------------------------------------------------
  67. /// Returns whether the path is a file or directory on the disk
  68. bool exists(const char* path);
  69. /// Returns whether the path is a directory. (May not resolve symlinks.)
  70. bool is_directory(const char* path);
  71. /// Returns whether the path is a regular file. (May not resolve symlinks.)
  72. bool is_file(const char* path);
  73. /// Creates a regular file. Returns true if success, false if not
  74. bool create_file(const char* path);
  75. /// Deletes a regular file. Returns true if success, false if not
  76. bool delete_file(const char* path);
  77. /// Creates a directory. Returns true if success, false if not
  78. bool create_directory(const char* path);
  79. /// Deletes a directory. Returns true if success, false if not
  80. bool delete_directory(const char* path);
  81. /// Returns the list of @a files in the given @a dir directory. Optionally walks into
  82. /// subdirectories whether @a recursive is true.
  83. /// @note
  84. /// Does not follow symbolic links.
  85. void list_files(const char* path, Vector<DynamicString>& files);
  86. /// Returns os-dependent path from os-indipendent @a path
  87. const char* normalize_path(const char* path);
  88. //-----------------------------------------------------------------------------
  89. // OS ambient variables
  90. //-----------------------------------------------------------------------------
  91. /// Fills ret with the path of the current working directory. Returns true if success, false if not
  92. const char* get_cwd();
  93. /// Fills ret with the path of the user home directory
  94. const char* get_home();
  95. /// Returns the content of the 'env' environment variable or the empty string
  96. const char* get_env(const char* env);
  97. //-----------------------------------------------------------------------------
  98. // Render window and input management
  99. //-----------------------------------------------------------------------------
  100. CE_EXPORT void init_os();
  101. void get_cursor_xy(int32_t& x, int32_t& y);
  102. void set_cursor_xy(int32_t x, int32_t y);
  103. //-----------------------------------------------------------------------------
  104. // Timing
  105. //-----------------------------------------------------------------------------
  106. uint64_t milliseconds();
  107. uint64_t microseconds();
  108. //-----------------------------------------------------------------------------
  109. // Dynamic libraries
  110. //-----------------------------------------------------------------------------
  111. void* open_library(const char* path);
  112. void close_library(void* library);
  113. void* lookup_symbol(void* library, const char* name);
  114. //-----------------------------------------------------------------------------
  115. // Process execution
  116. //-----------------------------------------------------------------------------
  117. /// Executes a process.
  118. /// @a args is an array of arguments where:
  119. /// @a args[0] is the path to the program executable,
  120. /// @a args[1, 2, ..., n-1] is a list of arguments to pass to the executable,
  121. /// @a args[n] is NULL.
  122. void execute_process(const char* args[]);
  123. //-----------------------------------------------------------------------------
  124. } // namespace os
  125. EventBuffer* os_event_buffer();
  126. } // namespace crown