common-helpers.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (c) 2017-2019, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/param.h>
  30. #include <unistd.h>
  31. /**
  32. * Value clamping helper, works like MIN/MAX but constraints a value within the range.
  33. */
  34. #define CLAMP(l, u, value) MAX(MIN(l, u), MIN(MAX(l, u), value))
  35. /**
  36. * Little helper to safely print into a buffer, returns a pointer into the buffer
  37. */
  38. #define buffered_snprintf(b, s, ...) \
  39. (snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? b : NULL)
  40. /**
  41. * Little helper to safely print into a buffer, returns a newly allocated string
  42. */
  43. #define safe_snprintf(b, s, ...) \
  44. (snprintf(b, sizeof(b), s, __VA_ARGS__) < (ssize_t)sizeof(b) ? strndup(b, sizeof(b)) : NULL)
  45. /**
  46. * Helper function: Test, if haystack ends with needle.
  47. */
  48. static inline const char *strtail(const char *haystack, const char *needle)
  49. {
  50. char *pos = strstr(haystack, needle);
  51. if (pos && (strlen(pos) == strlen(needle)))
  52. return pos;
  53. return NULL;
  54. }
  55. /**
  56. * Helper function for autoclosing file-descriptors. Does nothing if the argument
  57. * is NULL or the referenced integer < 0.
  58. */
  59. inline void cleanup_close(int *fd_ptr)
  60. {
  61. if (fd_ptr == NULL || *fd_ptr < 0)
  62. return;
  63. (void)close(*fd_ptr);
  64. }
  65. /**
  66. * Helper macro for autoclosing file-descriptors: use by prefixing the variable,
  67. * like "autoclose_fd int fd = -1;".
  68. */
  69. #define autoclose_fd __attribute__((cleanup(cleanup_close)))
  70. /**
  71. * Helper function for auto-freeing dynamically allocated memory. Does nothing
  72. * if *ptr is NULL (ptr must not be NULL).
  73. */
  74. inline void cleanup_free(void *ptr)
  75. {
  76. /* The function is defined to work with 'void *' because
  77. * that will make sure it compiles without warning also
  78. * for all types; what we are getting passed into is a
  79. * pointer to a pointer though, so we need to cast */
  80. void *target = *(void **)ptr;
  81. free(target); /* free can deal with NULL */
  82. }
  83. /**
  84. * Helper macro for auto-freeing dynamically allocated memory: use by
  85. * prefixing the variable, like "autofree char *data = NULL;".
  86. */
  87. #define autofree __attribute__((cleanup(cleanup_free)))