platform.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include <stdio.h>
  18. #include <wchar.h>
  19. #include <sys/types.h>
  20. #include "c99defs.h"
  21. /*
  22. * Platform-independent functions for Accessing files, encoding, DLLs,
  23. * sleep, timer, and timing.
  24. */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. EXPORT FILE *os_wfopen(const wchar_t *path, const char *mode);
  29. EXPORT FILE *os_fopen(const char *path, const char *mode);
  30. EXPORT int64_t os_fgetsize(FILE *file);
  31. EXPORT int os_fseeki64(FILE *file, int64_t offset, int origin);
  32. EXPORT int64_t os_ftelli64(FILE *file);
  33. EXPORT size_t os_fread_mbs(FILE *file, char **pstr);
  34. EXPORT size_t os_fread_utf8(FILE *file, char **pstr);
  35. /* functions purely for convenience */
  36. EXPORT char *os_quick_read_utf8_file(const char *path);
  37. EXPORT bool os_quick_write_utf8_file(const char *path, const char *str,
  38. size_t len, bool marker);
  39. EXPORT char *os_quick_read_mbs_file(const char *path);
  40. EXPORT bool os_quick_write_mbs_file(const char *path, const char *str,
  41. size_t len);
  42. EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst,
  43. size_t dst_size);
  44. EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
  45. size_t dst_size);
  46. EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst,
  47. size_t dst_size);
  48. EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
  49. size_t dst_size);
  50. EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
  51. EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
  52. EXPORT size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr);
  53. EXPORT size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr);
  54. EXPORT size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr);
  55. EXPORT size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr);
  56. EXPORT void *os_dlopen(const char *path);
  57. EXPORT void *os_dlsym(void *module, const char *func);
  58. EXPORT void os_dlclose(void *module);
  59. struct os_cpu_usage_info;
  60. typedef struct os_cpu_usage_info *os_cpu_usage_info_t;
  61. EXPORT os_cpu_usage_info_t os_cpu_usage_info_start(void);
  62. EXPORT double os_cpu_usage_info_query(os_cpu_usage_info_t info);
  63. EXPORT void os_cpu_usage_info_destroy(os_cpu_usage_info_t info);
  64. /**
  65. * Sleeps to a specific time (in nanoseconds). Doesn't have to be super
  66. * accurate in terms of actual slept time because the target time is ensured.
  67. * Returns false if already at or past target time.
  68. */
  69. EXPORT bool os_sleepto_ns(uint64_t time_target);
  70. EXPORT void os_sleep_ms(uint32_t duration);
  71. EXPORT uint64_t os_gettime_ns(void);
  72. EXPORT char *os_get_config_path(const char *name);
  73. EXPORT bool os_file_exists(const char *path);
  74. struct os_dir;
  75. typedef struct os_dir *os_dir_t;
  76. struct os_dirent {
  77. char d_name[256];
  78. bool directory;
  79. };
  80. EXPORT os_dir_t os_opendir(const char *path);
  81. EXPORT struct os_dirent *os_readdir(os_dir_t dir);
  82. EXPORT void os_closedir(os_dir_t dir);
  83. EXPORT int os_unlink(const char *path);
  84. #define MKDIR_EXISTS 1
  85. #define MKDIR_SUCCESS 0
  86. #define MKDIR_ERROR -1
  87. EXPORT int os_mkdir(const char *path);
  88. #ifdef _MSC_VER
  89. #define strtoll _strtoi64
  90. #endif
  91. #ifdef __cplusplus
  92. }
  93. #endif