-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_utils.c
121 lines (94 loc) · 2.75 KB
/
path_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
FTP file system
Copyright (C) 2007 Robson Braga Araujo <[email protected]>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include "path_utils.h"
#include "charset_utils.h"
#include "ftpfs.h"
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
/* Converts an integer value to its hex character*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(char *str) {
char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~'
|| *pstr == ':' || *pstr == '/')
*pbuf++ = *pstr;
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
char* get_file_name(const char* path) {
const char* filename = strrchr(path, '/');
if (filename == NULL) filename = path;
else ++filename;
char* ret = strdup(filename);
if (ftpfs.codepage) {
convert_charsets(ftpfs.iocharset, ftpfs.codepage, &ret);
}
ret=url_encode(ret);
return ret;
}
char* get_full_path(const char* path) {
char* ret;
char* converted_path = NULL;
++path;
if (ftpfs.codepage && strlen(path)) {
converted_path = strdup(path);
convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path);
path = converted_path;
}
ret = g_strdup_printf("%s%s", ftpfs.host, path);
free(converted_path);
ret=url_encode(ret);
return ret;
}
char* get_fulldir_path(const char* path) {
char* ret;
char* converted_path = NULL;
++path;
if (ftpfs.codepage && strlen(path)) {
converted_path = strdup(path);
convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path);
path = converted_path;
}
ret = g_strdup_printf("%s%s%s", ftpfs.host, path, strlen(path) ? "/" : "");
free(converted_path);
ret=url_encode(ret);
return ret;
}
char* get_dir_path(const char* path) {
char* ret;
char* converted_path = NULL;
const char *lastdir;
++path;
lastdir = strrchr(path, '/');
if (lastdir == NULL) lastdir = path;
if (ftpfs.codepage && (lastdir - path > 0)) {
converted_path = g_strndup(path, lastdir - path);
convert_charsets(ftpfs.iocharset, ftpfs.codepage, &converted_path);
path = converted_path;
lastdir = path + strlen(path);
}
ret = g_strdup_printf("%s%.*s%s",
ftpfs.host,
lastdir - path,
path,
lastdir - path ? "/" : "");
free(converted_path);
ret=url_encode(ret);
return ret;
}