fs.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // handles ~ properly
  24. // returns 0 for false, 1 for true, and 0 on any error
  25. int is_regular_file(char* path);
  26. // join all path segments in a new buffer
  27. #define path_join(...) path_join_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  28. char* path_join_(size_t nargs, ...);
  29. // gets a pointer to the first character of the file extension, or to the null terminator if none
  30. char* path_ext(char* path);
  31. // gets a pointer to the first character of the file extension, or to the null terminator if none
  32. // also provides the length of the path without the period and extension
  33. char* path_ext2(char* path, int* end);
  34. // returns a null terminated string. srcLen does NOT include the null terminator
  35. // nulls inside the string are not escaped or removed; the first null is not
  36. // necessarily the terminating null
  37. char* read_whole_file(char* path, size_t* srcLen);
  38. // reserves extra space in memory just in case you want to append a \n or something
  39. // srcLen reflects the length of the content, not the allocation
  40. char* read_whole_file_extra(char* path, size_t extraAlloc, size_t* srcLen);
  41. // returns 0 on success
  42. int write_whole_file(char* path, void* data, size_t len);
  43. // returns a list of the relative file names
  44. char** read_whole_dir(char* path, unsigned int flags, size_t* outLen);
  45. // returns a list of the absolute file names
  46. char** read_whole_dir_abs(char* path, unsigned int flags, size_t* outLen);
  47. // return 0 to continue, nonzero to stop all directory scanning
  48. typedef int (*readDirCallbackFn)(char* /*fullPath*/, char* /*fileName*/, void* /*data*/);
  49. #define FSU_EXCLUDE_HIDDEN (1<<0)
  50. #define FSU_NO_FOLLOW_SYMLINKS (1<<1)
  51. #define FSU_INCLUDE_DIRS (1<<2)
  52. #define FSU_EXCLUDE_FILES (1<<3)
  53. #define FSU_DIRS_ONLY (FSU_EXCLUDE_FILES | FSU_INCLUDE_DIRS)
  54. // returns negative on error, nonzero if scanning was halted by the callback
  55. int recurse_dirs(
  56. char* path,
  57. readDirCallbackFn fn,
  58. void* data,
  59. int depth,
  60. unsigned int flags
  61. );
  62. // works like realpath(), except also handles ~/
  63. char* resolve_path(char* in);
  64. // works like wordexp, except accepts a list of ;-separated paths
  65. // and returns an array of char*'s, all allocated with normal malloc
  66. char** multi_wordexp_dup(char* input, size_t* out_len);
  67. #ifndef NO_STI_V0_COMPAT
  68. #define recurseDirs(...) recurse_dirs(__VA_ARGS__)
  69. #define readWholeFileExtra(...) read_whole_file_extra(__VA_ARGS__)
  70. #define readWholeFile(...) read_whole_file(__VA_ARGS__)
  71. #define pathExt(...) path_ext(__VA_ARGS__)
  72. #define pathExt2(...) path_ext2(__VA_ARGS__)
  73. #endif
  74. #endif // __sti__fs_h__