helpers.h 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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) ? y : ((x>z) ? z : x ));
  16. #define LIGHT_NOTE(x) if(light_verbosity > 2){printf("%s.\n", x);}
  17. #define LIGHT_WARN(x) if(light_verbosity > 1){printf("warning: \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
  18. #define LIGHT_ERR(x) if(light_verbosity > 0){printf("error: \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
  19. #define LIGHT_MEMERR() LIGHT_ERR("memory error");
  20. /* Verbosity levels:
  21. * 0 - No output
  22. * 1 - Errors
  23. * 2 - Errors, warnings
  24. * 3 - Errors, warnings, notices */
  25. int light_verbosity;
  26. /* Typedef for boolean values */
  27. typedef enum LIGHT_BOOL {
  28. FALSE = 0,
  29. TRUE
  30. } LIGHT_BOOL;
  31. /* Reads an unsigned integer from a file into `i` if able, otherwise returns FALSE and leaves `i` untouched */
  32. LIGHT_BOOL light_readUInt(char const *filename, unsigned int *v);
  33. /* Writes an unsigned integer `i` into file `filename` if able, otherwise returns FALSE */
  34. LIGHT_BOOL light_writeUInt(char const *filename, unsigned int v);
  35. LIGHT_BOOL light_writeULong(char const *filename, unsigned long v);
  36. LIGHT_BOOL light_readULong(char const *filename, unsigned long *v);
  37. /* Reads a file into null-terminated `buffer` if able, otherwise returns FALSE
  38. * If `size` isn't NULL, it will be set to the read size.
  39. *
  40. * WARNING: `buffer` HAS to be freed by the user, also make sure it is NULL before passed */
  41. LIGHT_BOOL light_readString(char const * filename, char * buffer, long * size);
  42. /* Returns TRUE if `path` is a valid directory, FALSE otherwise */
  43. LIGHT_BOOL light_isDir(char const * path);
  44. /* Returns TRUE if file is writable, FALSE otherwise */
  45. LIGHT_BOOL light_isWritable(char const * filename);
  46. /* Returns TRUE if file is readable, FALSE otherwise */
  47. LIGHT_BOOL light_isReadable(char const * filename);
  48. #endif /* LIGHT_HELPERS_H */