| 1 |
#ifndef RUBY_INTERNAL_ENCODING_PATHNAME_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RUBY_INTERNAL_ENCODING_PATHNAME_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Routines to manipulate encodings of pathnames. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "ruby/internal/attr/nonnull.h" |
| 25 |
#include "ruby/internal/dllexport.h" |
| 26 |
#include "ruby/internal/encoding/encoding.h" |
| 27 |
#include "ruby/internal/value.h" |
| 28 |
|
| 29 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 30 |
RBIMPL_ATTR_NONNULL(()) |
| 31 |
/** |
| 32 |
* Returns a path component directly adjacent to the passed pointer. |
| 33 |
* |
| 34 |
* ``` |
| 35 |
* "/multi/byte/encoded/pathname.txt" |
| 36 |
* ^ ^ ^ |
| 37 |
* | | +--- end |
| 38 |
* | +--- @return |
| 39 |
* +--- path |
| 40 |
* ``` |
| 41 |
* |
| 42 |
* @param[in] path Where to start scanning. |
| 43 |
* @param[in] end End of the path string. |
| 44 |
* @param[in] enc Encoding of the string. |
| 45 |
* @return A pointer in the passed string where the next path component |
| 46 |
* resides, or `end` if there is no next path component. |
| 47 |
*/ |
| 48 |
char *rb_enc_path_next(const char *path, const char *end, rb_encoding *enc); |
| 49 |
|
| 50 |
RBIMPL_ATTR_NONNULL(()) |
| 51 |
/** |
| 52 |
* Seeks for non-prefix part of a pathname. This can be a no-op when the OS |
| 53 |
* has no such concept like a path prefix. But there are OSes where path |
| 54 |
* prefixes do exist. |
| 55 |
* |
| 56 |
* ``` |
| 57 |
* "C:\multi\byte\encoded\pathname.txt" |
| 58 |
* ^ ^ ^ |
| 59 |
* | | +--- end |
| 60 |
* | +--- @return |
| 61 |
* +--- path |
| 62 |
* ``` |
| 63 |
* |
| 64 |
* @param[in] path Where to start scanning. |
| 65 |
* @param[in] end End of the path string. |
| 66 |
* @param[in] enc Encoding of the string. |
| 67 |
* @return A pointer in the passed string where non-prefix part starts, or |
| 68 |
* `path` if the OS does not have path prefix. |
| 69 |
*/ |
| 70 |
char *rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc); |
| 71 |
|
| 72 |
RBIMPL_ATTR_NONNULL(()) |
| 73 |
/** |
| 74 |
* Returns the last path component. |
| 75 |
* |
| 76 |
* ``` |
| 77 |
* "/multi/byte/encoded/pathname.txt" |
| 78 |
* ^ ^ ^ |
| 79 |
* | | +--- end |
| 80 |
* | +--- @return |
| 81 |
* +--- path |
| 82 |
* ``` |
| 83 |
* |
| 84 |
* @param[in] path Where to start scanning. |
| 85 |
* @param[in] end End of the path string. |
| 86 |
* @param[in] enc Encoding of the string. |
| 87 |
* @return A pointer in the passed string where the last path component |
| 88 |
* resides, or `end` if there is no more path component. |
| 89 |
*/ |
| 90 |
char *rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc); |
| 91 |
|
| 92 |
RBIMPL_ATTR_NONNULL(()) |
| 93 |
/** |
| 94 |
* This just returns the passed end basically. It makes difference in case the |
| 95 |
* passed string ends with tons of path separators like the following: |
| 96 |
* |
| 97 |
* ``` |
| 98 |
* "/path/that/ends/with/lots/of/slashes//////////////" |
| 99 |
* ^ ^ ^ |
| 100 |
* | | +--- end |
| 101 |
* | +--- @return |
| 102 |
* +--- path |
| 103 |
* ``` |
| 104 |
* |
| 105 |
* @param[in] path Where to start scanning. |
| 106 |
* @param[in] end End of the path string. |
| 107 |
* @param[in] enc Encoding of the string. |
| 108 |
* @return A pointer in the passed string where the trailing path |
| 109 |
* separators start, or `end` if there is no trailing path |
| 110 |
* separators. |
| 111 |
* |
| 112 |
* @internal |
| 113 |
* |
| 114 |
* It seems this function was introduced to mimic what POSIX says about |
| 115 |
* `basename(3)`. |
| 116 |
*/ |
| 117 |
char *rb_enc_path_end(const char *path, const char *end, rb_encoding *enc); |
| 118 |
|
| 119 |
RBIMPL_ATTR_NONNULL((1, 4)) |
| 120 |
/** |
| 121 |
* Our own encoding-aware version of `basename(3)`. Normally, this function |
| 122 |
* returns the last path component of the given name. However in case the |
| 123 |
* passed name ends with a path separator, it returns the name of the |
| 124 |
* directory, not the last (empty) component. Also if the passed name is a |
| 125 |
* root directory, it returns that root directory. Note however that Windows |
| 126 |
* filesystem have drive letters, which this function does not return. |
| 127 |
* |
| 128 |
* @param[in] name Target path. |
| 129 |
* @param[out] baselen Return buffer. |
| 130 |
* @param[in,out] alllen Number of bytes of `name`. |
| 131 |
* @param[enc] enc Encoding of `name`. |
| 132 |
* @return The rightmost component of `name`. |
| 133 |
* @post `baselen`, if passed, is updated to be the number of bytes |
| 134 |
* of the returned basename. |
| 135 |
* @post `alllen`, if passed, is updated to be the number of bytes of |
| 136 |
* strings not considered as the basename. |
| 137 |
*/ |
| 138 |
const char *ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc); |
| 139 |
|
| 140 |
RBIMPL_ATTR_NONNULL((1, 3)) |
| 141 |
/** |
| 142 |
* Our own encoding-aware version of `extname`. This function first applies |
| 143 |
* rb_enc_path_last_separator() to the passed name and only concerns its return |
| 144 |
* value (ignores any parent directories). This function returns complicated |
| 145 |
* results: |
| 146 |
* |
| 147 |
* ```CXX |
| 148 |
* auto path = "..."; |
| 149 |
* auto len = strlen(path); |
| 150 |
* auto ret = ruby_enc_find_extname(path, &len, rb_ascii8bit_encoding()); |
| 151 |
* |
| 152 |
* switch(len) { |
| 153 |
* case 0: |
| 154 |
* if (ret == 0) { |
| 155 |
* // `path` is a file without extensions. |
| 156 |
* } |
| 157 |
* else { |
| 158 |
* // `path` is a dotfile. |
| 159 |
* // `ret` is the file's name. |
| 160 |
* } |
| 161 |
* break; |
| 162 |
* |
| 163 |
* case 1: |
| 164 |
* // `path` _ends_ with a dot. |
| 165 |
* // `ret` is that dot. |
| 166 |
* break; |
| 167 |
* |
| 168 |
* default: |
| 169 |
* // `path` has an extension. |
| 170 |
* // `ret` is that extension. |
| 171 |
* } |
| 172 |
* ``` |
| 173 |
* |
| 174 |
* @param[in] name Target path. |
| 175 |
* @param[in,out] len Number of bytes of `name`. |
| 176 |
* @param[in] enc Encoding of `name`. |
| 177 |
* @return See above. |
| 178 |
* @post `len`, if passed, is updated (see above). |
| 179 |
*/ |
| 180 |
const char *ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc); |
| 181 |
|
| 182 |
RBIMPL_SYMBOL_EXPORT_END() |
| 183 |
|
| 184 |
#endif /* RUBY_INTERNAL_ENCODING_PATHNAME_H */ |