path.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "path.h"
  2. #include "m-string.h"
  3. #include <stddef.h>
  4. void path_extract_filename_no_ext(const char* path, string_t filename) {
  5. string_set(filename, path);
  6. size_t start_position = string_search_rchar(filename, '/');
  7. size_t end_position = string_search_rchar(filename, '.');
  8. if(start_position == STRING_FAILURE) {
  9. start_position = 0;
  10. } else {
  11. start_position += 1;
  12. }
  13. if(end_position == STRING_FAILURE) {
  14. end_position = string_size(filename);
  15. }
  16. string_mid(filename, start_position, end_position - start_position);
  17. }
  18. void path_extract_filename(string_t path, string_t name, bool trim_ext) {
  19. size_t filename_start = string_search_rchar(path, '/');
  20. if(filename_start > 0) {
  21. filename_start++;
  22. string_set_n(name, path, filename_start, string_size(path) - filename_start);
  23. }
  24. if(trim_ext) {
  25. size_t dot = string_search_rchar(name, '.');
  26. if(dot > 0) {
  27. string_left(name, dot);
  28. }
  29. }
  30. }
  31. void path_extract_extension(string_t path, char* ext, size_t ext_len_max) {
  32. size_t dot = string_search_rchar(path, '.');
  33. size_t filename_start = string_search_rchar(path, '/');
  34. if((dot > 0) && (filename_start < dot)) {
  35. strlcpy(ext, &(string_get_cstr(path))[dot], ext_len_max);
  36. }
  37. }
  38. static inline void path_cleanup(string_t path) {
  39. string_strim(path);
  40. while(string_end_with_str_p(path, "/")) {
  41. string_left(path, string_size(path) - 1);
  42. }
  43. }
  44. void path_extract_basename(const char* path, string_t basename) {
  45. string_set(basename, path);
  46. path_cleanup(basename);
  47. size_t pos = string_search_rchar(basename, '/');
  48. if(pos != STRING_FAILURE) {
  49. string_right(basename, pos + 1);
  50. }
  51. }
  52. void path_extract_dirname(const char* path, string_t dirname) {
  53. string_set(dirname, path);
  54. path_cleanup(dirname);
  55. size_t pos = string_search_rchar(dirname, '/');
  56. if(pos != STRING_FAILURE) {
  57. string_left(dirname, pos);
  58. }
  59. }
  60. void path_append(string_t path, const char* suffix) {
  61. path_cleanup(path);
  62. string_t suffix_str;
  63. string_init_set(suffix_str, suffix);
  64. string_strim(suffix_str);
  65. string_strim(suffix_str, "/");
  66. string_cat_printf(path, "/%s", string_get_cstr(suffix_str));
  67. string_clear(suffix_str);
  68. }
  69. void path_concat(const char* path, const char* suffix, string_t out_path) {
  70. string_set(out_path, path);
  71. path_append(out_path, suffix);
  72. }
  73. bool path_contains_only_ascii(const char* path) {
  74. const char* name_pos = strrchr(path, '/');
  75. if(name_pos == NULL) {
  76. name_pos = path;
  77. } else {
  78. name_pos++;
  79. }
  80. while(*name_pos != '\0') {
  81. if((*name_pos >= '0') && (*name_pos <= '9')) {
  82. name_pos++;
  83. continue;
  84. } else if((*name_pos >= 'A') && (*name_pos <= 'Z')) {
  85. name_pos++;
  86. continue;
  87. } else if((*name_pos >= 'a') && (*name_pos <= 'z')) {
  88. name_pos++;
  89. continue;
  90. } else if(strchr(" .!#\\$%&'()-@^_`{}~", *name_pos) != NULL) {
  91. name_pos++;
  92. continue;
  93. }
  94. return false;
  95. }
  96. return true;
  97. }