fs.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef __sti__fs_h__
  2. #define __sti__fs_h__
  3. // Public Domain.
  4. #include <stddef.h> // size_t
  5. #include "macros.h"
  6. typedef struct rglob_entry {
  7. char type;
  8. char* full_path;
  9. char* file_name;
  10. // char* dir_name;
  11. } rglob_entry;
  12. typedef struct rglob {
  13. char* pattern;
  14. int len;
  15. int alloc;
  16. rglob_entry* entries;
  17. } rglob;
  18. void recursive_glob(char* base_path, char* pattern, int flags, rglob* results);
  19. // given a longer name so as to not conflict with other things
  20. // handles ~ properly
  21. // returns 0 for false, 1 for true, and 0 on any error
  22. int is_path_a_dir(char* path);
  23. // join all path segments in a new buffer
  24. #define path_join(...) path_join_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  25. char* path_join_(size_t nargs, ...);
  26. // gets a pointer to the first character of the file extension, or to the null terminator if none
  27. char* path_ext(char* path);
  28. // gets a pointer to the first character of the file extension, or to the null terminator if none
  29. // also provides the length of the path without the period and extension
  30. char* path_ext2(char* path, int* end);
  31. // returns a null terminated string. srcLen does NOT include the null terminator
  32. // nulls inside the string are not escaped or removed; the first null is not
  33. // necessarily the terminating null
  34. char* read_whole_file(char* path, size_t* srcLen);
  35. // reserves extra space in memory just in case you want to append a \n or something
  36. // srcLen reflects the length of the content, not the allocation
  37. char* read_whole_file_extra(char* path, size_t extraAlloc, size_t* srcLen);
  38. // returns 0 on success
  39. int write_whole_file(char* path, void* data, size_t len);
  40. // return 0 to continue, nonzero to stop all directory scanning
  41. typedef int (*readDirCallbackFn)(char* /*fullPath*/, char* /*fileName*/, void* /*data*/);
  42. #define FSU_EXCLUDE_HIDDEN (1<<0)
  43. #define FSU_NO_FOLLOW_SYMLINKS (1<<1)
  44. #define FSU_INCLUDE_DIRS (1<<2)
  45. #define FSU_EXCLUDE_FILES (1<<3)
  46. // returns negative on error, nonzero if scanning was halted by the callback
  47. int recurse_dirs(
  48. char* path,
  49. readDirCallbackFn fn,
  50. void* data,
  51. int depth,
  52. unsigned int flags
  53. );
  54. // works like realpath(), except also handles ~/
  55. char* resolve_path(char* in);
  56. // works like wordexp, except accepts a list of ;-separated paths
  57. // and returns an array of char*'s, all allocated with normal malloc
  58. char** multi_wordexp_dup(char* input, size_t* out_len);
  59. #ifndef NO_STI_V0_COMPAT
  60. #define recurseDirs(...) recurse_dirs(__VA_ARGS__)
  61. #define readWholeFileExtra(...) read_whole_file_extra(__VA_ARGS__)
  62. #define readWholeFile(...) read_whole_file(__VA_ARGS__)
  63. #define pathExt(...) path_ext(__VA_ARGS__)
  64. #define pathExt2(...) path_ext2(__VA_ARGS__)
  65. #endif
  66. #endif // __sti__fs_h__