OS.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #define Thread os::Thread
  41. #undef MK_SHIFT
  42. #undef MK_ALT
  43. #elif defined(ANDROID)
  44. const size_t MAX_PATH_LENGTH = 1024;
  45. const char PATH_SEPARATOR = '/';
  46. #else
  47. #error "Oops, invalid platform!"
  48. #endif
  49. namespace os
  50. {
  51. //-----------------------------------------------------------------------------
  52. // Print and log functions
  53. //-----------------------------------------------------------------------------
  54. void printf(const char* string, ...); //!< Printf wrapper
  55. void vprintf(const char* string, va_list arg); //!< VPrintf wrapper
  56. void log_debug(const char* string, va_list arg); //!< Print debug message
  57. void log_error(const char* string, va_list arg); //!< Print error message
  58. void log_warning(const char* string, va_list arg); //!< Print warning message
  59. void log_info(const char* string, va_list arg); //!< Print info message
  60. //-----------------------------------------------------------------------------
  61. // Paths
  62. //-----------------------------------------------------------------------------
  63. bool is_root_path(const char* path);
  64. bool is_absolute_path(const char* path);
  65. //-----------------------------------------------------------------------------
  66. // File management
  67. //-----------------------------------------------------------------------------
  68. /// Returns whether the path is a file or directory on the disk
  69. bool exists(const char* path);
  70. /// Returns whether the path is a directory. (May not resolve symlinks.)
  71. bool is_directory(const char* path);
  72. /// Returns whether the path is a regular file. (May not resolve symlinks.)
  73. bool is_file(const char* path);
  74. /// Creates a regular file. Returns true if success, false if not
  75. bool create_file(const char* path);
  76. /// Deletes a regular file. Returns true if success, false if not
  77. bool delete_file(const char* path);
  78. /// Creates a directory. Returns true if success, false if not
  79. bool create_directory(const char* path);
  80. /// Deletes a directory. Returns true if success, false if not
  81. bool delete_directory(const char* path);
  82. /// Returns the list of @a files in the given @a dir directory. Optionally walks into
  83. /// subdirectories whether @a recursive is true.
  84. /// @note
  85. /// Does not follow symbolic links.
  86. void list_files(const char* path, Vector<DynamicString>& files);
  87. /// Returns os-dependent path from os-indipendent @a path
  88. const char* normalize_path(const char* path);
  89. //-----------------------------------------------------------------------------
  90. // OS ambient variables
  91. //-----------------------------------------------------------------------------
  92. /// Fills ret with the path of the current working directory. Returns true if success, false if not
  93. const char* get_cwd();
  94. /// Fills ret with the path of the user home directory
  95. const char* get_home();
  96. /// Returns the content of the 'env' environment variable or the empty string
  97. const char* get_env(const char* env);
  98. //-----------------------------------------------------------------------------
  99. // Render window and input management
  100. //-----------------------------------------------------------------------------
  101. CE_EXPORT void init_os();
  102. void get_cursor_xy(int32_t& x, int32_t& y);
  103. void set_cursor_xy(int32_t x, int32_t y);
  104. //-----------------------------------------------------------------------------
  105. // Timing
  106. //-----------------------------------------------------------------------------
  107. uint64_t milliseconds();
  108. uint64_t microseconds();
  109. //-----------------------------------------------------------------------------
  110. // Dynamic libraries
  111. //-----------------------------------------------------------------------------
  112. void* open_library(const char* path);
  113. void close_library(void* library);
  114. void* lookup_symbol(void* library, const char* name);
  115. //-----------------------------------------------------------------------------
  116. // Process execution
  117. //-----------------------------------------------------------------------------
  118. /// Executes a process.
  119. /// @a args is an array of arguments where:
  120. /// @a args[0] is the path to the program executable,
  121. /// @a args[1, 2, ..., n-1] is a list of arguments to pass to the executable,
  122. /// @a args[n] is NULL.
  123. void execute_process(const char* args[]);
  124. //-----------------------------------------------------------------------------
  125. } // namespace os
  126. EventBuffer* os_event_buffer();
  127. } // namespace crown