helpers.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef LIGHT_HELPERS_H
  2. #define LIGHT_HELPERS_H
  3. /* Clamps x(value) between y(min) and z(max) in a nested ternary operation.
  4. * if(x < y)
  5. * {
  6. * y;
  7. * }else{
  8. * if(x>z)
  9. * {
  10. * z;
  11. * }else{
  12. * x;
  13. * }
  14. * }*/
  15. #define LIGHT_CLAMP(x, y, z) ((x<y) ? (light_logInfClamp(y)) : ((x>z) ? (light_logSupClamp(z)) : x ))
  16. #define LIGHT_LOG_FMT_BUF_SIZE 1024
  17. /* Verbosity levels:
  18. * 0 - No output
  19. * 1 - Errors
  20. * 2 - Errors, warnings
  21. * 3 - Errors, warnings, notices */
  22. typedef enum LIGHT_LOG_LEVEL {
  23. LIGHT_ERROR_LEVEL = 1,
  24. LIGHT_WARN_LEVEL,
  25. LIGHT_NOTE_LEVEL
  26. } LIGHT_LOG_LEVEL;
  27. LIGHT_LOG_LEVEL light_verbosity;
  28. char light_log_buffer[LIGHT_LOG_FMT_BUF_SIZE];
  29. #define LIGHT_LOG(lvl,f,t,x)if(light_verbosity >= lvl){fprintf(f,t": \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
  30. #define LIGHT_NOTE(x)LIGHT_LOG(LIGHT_NOTE_LEVEL,stdout,"notice",x)
  31. #define LIGHT_WARN(x)LIGHT_LOG(LIGHT_WARN_LEVEL,stderr,"warning",x)
  32. #define LIGHT_ERR(x)LIGHT_LOG(LIGHT_ERROR_LEVEL,stderr,"error",x)
  33. #define LIGHT_LOG_FMT(x,s,f)if(snprintf(light_log_buffer, LIGHT_LOG_FMT_BUF_SIZE,x,s) > 0){f(light_log_buffer);}
  34. #define LIGHT_NOTE_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_NOTE);
  35. #define LIGHT_WARN_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_WARN);
  36. #define LIGHT_ERR_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_ERR);
  37. #define LIGHT_MEMERR() LIGHT_ERR("memory error");
  38. #define LIGHT_PERMLOG(x,f)f##_FMT("could not open '%s' for "x,filename); f("check if this file exists or if you have the right permissions");
  39. #define LIGHT_PERMERR(x) LIGHT_PERMLOG(x,LIGHT_ERR)
  40. #define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x,LIGHT_WARN)
  41. /* Typedef for boolean values */
  42. typedef enum LIGHT_BOOL {
  43. FALSE = 0,
  44. TRUE
  45. } LIGHT_BOOL;
  46. /* Reads an unsigned integer from a file into `i` if able, otherwise returns FALSE and leaves `i` untouched */
  47. LIGHT_BOOL light_readUInt(char const *filename, unsigned int *v);
  48. /* Writes an unsigned integer `i` into file `filename` if able, otherwise returns FALSE */
  49. LIGHT_BOOL light_writeUInt(char const *filename, unsigned int v);
  50. LIGHT_BOOL light_writeULong(char const *filename, unsigned long v);
  51. LIGHT_BOOL light_readULong(char const *filename, unsigned long *v);
  52. /* Reads a file into null-terminated `buffer` if able, otherwise returns FALSE
  53. * If `size` isn't NULL, it will be set to the read size.
  54. *
  55. * WARNING: `buffer` HAS to be freed by the user, also make sure it is NULL before passed */
  56. LIGHT_BOOL light_readString(char const * filename, char * buffer, long * size);
  57. /* Returns TRUE if `path` is a valid directory, FALSE otherwise */
  58. LIGHT_BOOL light_isDir(char const * path);
  59. /* Returns TRUE if file is writable, FALSE otherwise */
  60. LIGHT_BOOL light_isWritable(char const * filename);
  61. /* Returns TRUE if file is readable, FALSE otherwise */
  62. LIGHT_BOOL light_isReadable(char const * filename);
  63. /* Clamps the `percent` value between 0% and 100% */
  64. double light_clampPercent(double percent);
  65. /* Prints a notice about a value which was below `x` and was adjusted to it */
  66. unsigned long light_logInfClamp(unsigned long x);
  67. /* Prints a notice about a value which was above `x` and was adjusted to it */
  68. unsigned long light_logSupClamp(unsigned long x);
  69. #endif /* LIGHT_HELPERS_H */