ファイルパス文字列の操作を提供します
[詳解]
|
char | xfpath_drive (const char *path) |
|
bool | xfpath_is_absolute (const char *path) |
|
bool | xfpath_is_relative (const char *path) |
|
bool | xfpath_is_root (const char *path) |
|
char * | xfpath_join (char *p1, const char *p2, size_t size) |
| sizeバイトの領域を持つパスp1とパスp2を連結して返します [詳解]
|
|
char * | xfpath_name (const char *path, char **o_end) |
| パスのファイル名を返します [詳解]
|
|
char * | xfpath_parent (const char *path, const char *end, char **o_end) |
| パスの上位パスを返します [詳解]
|
|
XError | xfpath_resolve (char *dst, const char *cwd, const char *path, size_t size) |
| パスを正規化します [詳解]
|
|
char * | xfpath_resolve_dot (char *path) |
| パス中に含まれる".."や"."を展開した結果を返します [詳解]
|
|
char * | xfpath_stem (const char *path, char **o_end) |
| パスのファイル名から拡張子を除いた部分を返します [詳解]
|
|
char * | xfpath_suffix (const char *path, char **o_end) |
| パスのファイル名の拡張子を返します [詳解]
|
|
char * | xfpath_tail (const char *path, const char *end, char **o_end) |
| パスの末尾要素を返します [詳解]
|
|
char * | xfpath_top (const char *path, char **o_end) |
| パスの先頭要素を返します [詳解]
|
|
ファイルパス文字列の操作を提供します
パスから親ディレクトリを取り出す。パスを連結するといった、地味で面倒な作業を 補助します。
非対応項目
- マルチバイト文字
- C:等のドライブレター
- '/'以外のディレクトリセパレータ
char xfpath_drive |
( |
const char * |
path | ) |
|
パスのドライブ文字を返します
ドライブ文字として有効な文字は[0-9a-zA-Z]です。
- 事前条件
- path != NULL
- 解決済みのパスであること
bool xfpath_is_absolute |
( |
const char * |
path | ) |
|
bool xfpath_is_relative |
( |
const char * |
path | ) |
|
bool xfpath_is_root |
( |
const char * |
path | ) |
|
パスがルートディレクトリを指しているかどうかを返します
- 事前条件
- path != NULL
- 解決済みのパスであること
char* xfpath_join |
( |
char * |
p1, |
|
|
const char * |
p2, |
|
|
size_t |
size |
|
) |
| |
sizeバイトの領域を持つパスp1とパスp2を連結して返します
連結した結果がsizeバイトに収まらなかった場合はNULLを返します。
- 事前条件
- p1 != NULL p2 != NULL size > 0
p1 = "/foo/bar"
p2 = "baz/hello.txt"
ret = "/foo/bar/baz/hello.txt"
char* xfpath_name |
( |
const char * |
path, |
|
|
char ** |
o_end |
|
) |
| |
パスのファイル名を返します
xfpath_tail(path, NULL, o_end)
と同じ振る舞いをします。末尾要素がほしいだけ ならこちらを使用するのが安全です。
- 事前条件
- path != NULL
- o_end != NULL
- 引数
-
path | 検索対象のパス |
o_end | 末尾要素を指すポインタの格納先 |
- 戻り値
-
!=NULL | 末尾要素を指すポインタ |
NULL | 要素なし |
char* xfpath_parent |
( |
const char * |
path, |
|
|
const char * |
end, |
|
|
char ** |
o_end |
|
) |
| |
パスの上位パスを返します
endがNULLを指す場合はpath ~ (path + strlen(path))
の範囲を検索し、 endがNULLでなければpath ~ (path + (end - path))
の範囲を検索します。
- 引数
-
path | 検索対象のパス |
end | pathの検索範囲の終端 |
o_end | 上位パスの終端を指すポインタの格納先 |
v----ret
/foo/bar/baz.txt\0
^-----o_end
v----ret
/foo\0
^-----o_end
/\0 ret => NULL
const char* endptr = NULL;
const char* parent;
{
memcpy(parentpath, parent, endptr - parent);
parentpath[endptr - parent] = '\0';
printf("name => '%s'", parentpath);
}
XError xfpath_resolve |
( |
char * |
dst, |
|
|
const char * |
cwd, |
|
|
const char * |
path, |
|
|
size_t |
size |
|
) |
| |
パスを正規化します
パスの正規化とは以下の内容を指します。
- ".", ".."の解決
- 相対パスから絶対パスへの変換
- 余分なディレクトリセパレータの除去
- 引数
-
dst | 正規化したパスの格納先 |
size | dstが指す領域のバイト数 |
path | 正規化を行うパス |
cwd | 絶対パスの基準とするパス(正規化済みであること) |
- 戻り値
-
X_ERR_NAME_TOO_LONG | 展開後のサイズがsizeを超えた |
X_ERR_NO_ENTRY | "../"の展開により最上位ディレクトリを超えた |
X_ERR_NONE | 正常終了 |
char* xfpath_resolve_dot |
( |
char * |
path | ) |
|
パス中に含まれる".."や"."を展開した結果を返します
結果が最上位ディレクトリを超えた場合はNULLを返します。
- 事前条件
- path != NULL
path = "foo/bar/.././././baz"
ret = "foo/baz"
path = "foo/bar/../../baz"
ret = "foo"
path = "../"
ret = NULL
char* xfpath_stem |
( |
const char * |
path, |
|
|
char ** |
o_end |
|
) |
| |
パスのファイル名から拡張子を除いた部分を返します
- 事前条件
- path != NULL
- o_end != NULL
- 引数
-
path | 検索対象のパス |
o_end | 末尾要素を指すポインタの格納先 |
- 戻り値
-
v----ret
"/foo/bar/baz.txt\0"
^-----o_end
v----ret
"/foo/bar/baz.tar.gz\0"
^-----o_end
v----ret
"/foo/bar/baz.tar.gz////\0"
^-----o_end
v----ret
"/foo/bar/baz\0"
^------o_end
先頭'.'は拡張子としては扱わない
v----ret
"/foo/bar/.baz\0"
^------o_end
char* xfpath_suffix |
( |
const char * |
path, |
|
|
char ** |
o_end |
|
) |
| |
パスのファイル名の拡張子を返します
- 事前条件
- path != NULL
- o_end != NULL
- 引数
-
path | 検索対象のパス |
o_end | 末尾要素を指すポインタの格納先 |
- 戻り値
-
!=NULL | 拡張子を指すポインタ |
NULL | 拡張子なし |
v----ret
"/foo/bar/baz.txt\0"
^-----o_end
v----ret
"/foo/bar/baz.tar.gz\0"
^-----o_end
v----ret
"/foo/bar/baz.tar.gz////\0"
^-----o_end
先頭'.'は拡張子としては扱わない
"/foo/bar/.baz\0" ret => NULL
"/foo/bar/baz\0" ret => NULL
char* xfpath_tail |
( |
const char * |
path, |
|
|
const char * |
end, |
|
|
char ** |
o_end |
|
) |
| |
パスの末尾要素を返します
endがNULLを指す場合はpath ~ (path + strlen(path))
の範囲を検索し、 endがNULLでなければpath ~ (path + (end - path))
の範囲を検索します。
不正なアドレスをendに渡すと、プログラム暴走の原因となるので要注意です。どう してもミスしやすい部分なので、走査が目的ではなく単に末尾の要素がほしいだけな らxfpath_name()の使用を推奨します。
- 事前条件
- path != NULL
- o_end != NULL
- 引数
-
path | 検索対象のパス |
end | pathの検索範囲の終端 |
o_end | 末尾要素の終端を指すポインタの格納先 |
- 戻り値
-
!=NULL | 末尾要素を指すポインタ |
NULL | 要素なし |
v--- ret
"/foo/bar/baz\0"
^--- *o_end
v--- ret
"/foo/bar/baz//\0"
^--- *o_end
"/" => NULL
const char* endptr;
const char* next = NULL;
while ((next =
xfpath_tail(path, next, (
char**)&endptr)))
{
memcpy(name, next, endptr - next);
name[endptr - next] = '\0';
printf("name => '%s'", name);
}
char* xfpath_top |
( |
const char * |
path, |
|
|
char ** |
o_end |
|
) |
| |
パスの先頭要素を返します
- 事前条件
- path != NULL
- o_end != NULL
- 引数
-
path | 検索対象のパス |
o_end | 先頭要素の終端を指すポインタの格納先 |
- 戻り値
-
!=NULL | 先頭要素を指すポインタ |
NULL | 要素なし |
v--- ret
"/foo/bar/baz\0"
^--- *o_end
v--- ret
"foo///bar/baz\0"
^--- *o_end
v--- ret
"//////foo/bar/baz\0"
^--- *o_end
v--- ret
"/foo\0"
^--- *o_end
v--- ret
"/foo///\0"
^--- *o_end
"/" => NULL
const char* next = path;
const char* o_end = NULL;
memcpy(name, next, o_end - next);
name[o_end - next] = '\0';
printf("name => '%s'", name);
next = endptr;
}