1 | /* |
---|
2 | FTP file system |
---|
3 | Copyright (C) 2007 Robson Braga Araujo <robsonbraga@gmail.com> |
---|
4 | |
---|
5 | This program can be distributed under the terms of the GNU GPL. |
---|
6 | See the file COPYING. |
---|
7 | */ |
---|
8 | |
---|
9 | #include "path_utils.h" |
---|
10 | #include "charset_utils.h" |
---|
11 | #include "ftpfs.h" |
---|
12 | |
---|
13 | #include <string.h> |
---|
14 | #include <stdlib.h> |
---|
15 | |
---|
16 | char* get_file_name(const char* path) { |
---|
17 | const char* filename = strrchr(path, '/'); |
---|
18 | if (filename == NULL) filename = path; |
---|
19 | else ++filename; |
---|
20 | |
---|
21 | char* ret = strdup(filename); |
---|
22 | if (ftpfs.codepage) { |
---|
23 | convert_charsets(ftpfs.iocharset, ftpfs.codepage, &ret); |
---|
24 | } |
---|
25 | |
---|
26 | return ret; |
---|
27 | } |
---|
28 | |
---|
29 | char* get_full_path(const char* path) { |
---|
30 | char* ret; |
---|
31 | char* converted_path = NULL; |
---|
32 | |
---|
33 | ++path; |
---|
34 | |
---|
35 | if (ftpfs.codepage && strlen(path)) { |
---|
36 | converted_path = strdup(path); |
---|
37 | convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path); |
---|
38 | path = converted_path; |
---|
39 | } |
---|
40 | |
---|
41 | ret = g_strdup_printf("%s%s", ftpfs.host, path); |
---|
42 | |
---|
43 | free(converted_path); |
---|
44 | |
---|
45 | return ret; |
---|
46 | } |
---|
47 | |
---|
48 | char* get_fulldir_path(const char* path) { |
---|
49 | char* ret; |
---|
50 | char* converted_path = NULL; |
---|
51 | |
---|
52 | ++path; |
---|
53 | |
---|
54 | if (ftpfs.codepage && strlen(path)) { |
---|
55 | converted_path = strdup(path); |
---|
56 | convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path); |
---|
57 | path = converted_path; |
---|
58 | } |
---|
59 | |
---|
60 | ret = g_strdup_printf("%s%s%s", ftpfs.host, path, strlen(path) ? "/" : ""); |
---|
61 | |
---|
62 | free(converted_path); |
---|
63 | |
---|
64 | return ret; |
---|
65 | } |
---|
66 | |
---|
67 | char* get_dir_path(const char* path) { |
---|
68 | char* ret; |
---|
69 | char* converted_path = NULL; |
---|
70 | const char *lastdir; |
---|
71 | |
---|
72 | ++path; |
---|
73 | |
---|
74 | lastdir = strrchr(path, '/'); |
---|
75 | if (lastdir == NULL) lastdir = path; |
---|
76 | |
---|
77 | if (ftpfs.codepage && (lastdir - path > 0)) { |
---|
78 | converted_path = g_strndup(path, lastdir - path); |
---|
79 | convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path); |
---|
80 | path = converted_path; |
---|
81 | lastdir = path + strlen(path); |
---|
82 | } |
---|
83 | |
---|
84 | ret = g_strdup_printf("%s%.*s%s", |
---|
85 | ftpfs.host, |
---|
86 | lastdir - path, |
---|
87 | path, |
---|
88 | lastdir - path ? "/" : ""); |
---|
89 | |
---|
90 | free(converted_path); |
---|
91 | |
---|
92 | return ret; |
---|
93 | } |
---|