This source file includes following definitions.
- opal_uri_get_scheme
- opal_filename_to_uri
- opal_filename_from_uri
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include "opal_config.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #include "opal/util/output.h"
24 #include "opal/util/path.h"
25 #include "opal/util/show_help.h"
26 #include "opal/util/printf.h"
27
28 #include "opal/util/uri.h"
29
30 const static char *uri_reserved_path_chars="!$&'()*+,;=:@ ";
31
32 char *opal_uri_get_scheme(const char *uri)
33 {
34 char *turi = strdup(uri);
35 char *ptr;
36
37 if (NULL == (ptr = strchr(turi, ':'))) {
38 opal_show_help("help-opal-util.txt", "malformed-uri",
39 true, uri);
40 free(turi);
41 return NULL;
42 }
43 *ptr = '\0';
44 return turi;
45 }
46
47 char *opal_filename_to_uri(const char *filename,
48 const char *hostname)
49 {
50 char *uri, *fn;
51 size_t i, j, k, n;
52
53
54 if (!opal_path_is_absolute(filename)) {
55 opal_show_help("help-opal-util.txt", "relative-path",
56 true, filename);
57 return NULL;
58 }
59
60
61
62
63 if (NULL == hostname) {
64 opal_asprintf(&uri, "file://%s", filename);
65 return uri;
66 }
67
68
69
70
71 n=0;
72 for (j=0; j < strlen(uri_reserved_path_chars)-1; j++) {
73 if (NULL != strchr(filename, uri_reserved_path_chars[j])) {
74 n++;
75 }
76 }
77
78 if (0 < n) {
79 fn = (char*)malloc(strlen(filename) + n + 1);
80 i=0;
81 for (k=0; k < strlen(filename)-1; k++) {
82 for (j=0; j < strlen(uri_reserved_path_chars)-1; j++) {
83 if (filename[k] == uri_reserved_path_chars[j]) {
84 fn[i] = '\\';
85 i++;
86 break;
87 }
88 }
89 fn[i] = filename[k];
90 i++;
91 }
92 fn[i] = '\0';
93 } else {
94 fn = strdup(filename);
95 }
96
97
98
99
100
101 opal_asprintf(&uri, "file://%s%s", hostname, fn);
102 free(fn);
103 return uri;
104 }
105
106 char *opal_filename_from_uri(const char *uri,
107 char **hostname)
108 {
109 char *turi;
110 char *ptr, *fn, *sp;
111
112
113 turi = strdup(uri);
114
115
116 fn = NULL;
117 if (NULL != hostname) {
118 *hostname = NULL;
119 }
120
121
122 if (NULL == (ptr = strchr(turi, ':'))) {
123 opal_show_help("help-opal-util.txt", "malformed-uri",
124 true, uri);
125 free(turi);
126 return NULL;
127 }
128 *ptr = '\0';
129 ptr++;
130
131
132
133
134 if (0 == strncmp(ptr, "///", 3)) {
135
136
137
138
139 ptr += 2;
140 fn = strdup(ptr);
141 } else if (0 != strncmp(ptr, "//", 2)) {
142
143 opal_show_help("help-opal-util.txt", "malformed-uri",
144 true, uri);
145 } else {
146 ptr += 2;
147
148 if (NULL == (sp = strchr(ptr, '/'))) {
149 opal_show_help("help-opal-util.txt", "malformed-uri",
150 true, uri);
151 } else {
152 *sp = '\0';
153 if (NULL != hostname) {
154 *hostname = strdup(ptr);
155 }
156
157
158
159 *sp = '/';
160 fn = strdup(sp);
161 }
162 }
163 free(turi);
164 return fn;
165 }