FireBirdLib - Topfield TMS PVR TAP Programming Library
ConvertPathType.c
Go to the documentation of this file.
1#include <string.h>
2#include "libFireBird.h"
3
4void ConvertPathType(const char *Source, char *Dest, tPathFormat DestFormat)
5{
7
8 tPathFormat SourceFormat;
9
10 if(!Source || !Dest)
11 {
12 TRACEEXIT();
13 return;
14 }
15
16 SourceFormat = GetPathType(Source);
17
18 //A shortcut if source and destination formats are the same
19 if(SourceFormat == DestFormat)
20 {
21 strcpy(Dest, Source);
22
23 if((DestFormat == PF_TAPPathOnly) || (DestFormat == PF_LinuxPathOnly))
24 {
25 if(Dest[strlen(Dest) - 1] != '/') strcat(Dest, "/");
26 }
27
28 TRACEEXIT();
29 return;
30 }
31
32 switch(SourceFormat)
33 {
34 case PF_FileNameOnly:
35 {
36 switch(DestFormat)
37 {
38 case PF_FileNameOnly: break;
39
40 case PF_TAPPathOnly:
41 {
42 //Simply return the current TAP directory
44 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
45 break;
46 }
47
49 {
50 //Return the Linux path to the TAP's current directory
51 strcpy(Dest, "/mnt/hd");
52 HDD_TAP_GetCurrentDir(&Dest[7]);
53 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
54 break;
55 }
56
57 case PF_FullTAPPath:
58 {
59 //Return the current TAP directory including the file name from Source
61 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
62 strcat(Dest, Source);
63 break;
64 }
65
67 {
68 //Return the Linux path to the TAP's current directory plus the file name passed in Source
69 strcpy(Dest, "/mnt/hd");
70 HDD_TAP_GetCurrentDir(&Dest[7]);
71 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
72 strcat(Dest, Source);
73 break;
74 }
75 }
76 break;
77 }
78
79 case PF_TAPPathOnly:
80 {
81 switch(DestFormat)
82 {
83 case PF_FileNameOnly:
84 {
85 //If Source contains only a path, there is no file name to extract
86 Dest[0] = '\0';
87 break;
88 }
89
90 case PF_TAPPathOnly: break;
91
93 {
94 //Check if Source contains an absolute or relative path
95 if(Source[0] == '/')
96 {
97 //Insert the mount point at the beginning
98 TAP_SPrint(Dest, "/mnt/hd%s", Source);
99 }
100 else
101 {
102 //As the path is relative, we need to get the current path to make it absolute
103 char CurrentPath[FBLIB_DIR_SIZE];
104
105 HDD_TAP_GetCurrentDir(CurrentPath);
106 if(!StringEndsWith(CurrentPath, "/")) strcat(CurrentPath, "/");
107 strcat(CurrentPath, Source);
108 if(!StringEndsWith(CurrentPath, "/")) strcat(CurrentPath, "/");
109 TAP_SPrint(Dest, "/mnt/hd%s", CurrentPath);
110 }
111 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
112 break;
113 }
114
115 case PF_FullTAPPath:
116 {
117 //Not possible, there is no file name in source
118 Dest[0] = '\0';
119 break;
120 }
121
122 case PF_FullLinuxPath:
123 {
124 //Not possible, there is no file name in source
125 Dest[0] = '\0';
126 break;
127 }
128 }
129 break;
130 }
131
132 case PF_LinuxPathOnly:
133 {
134 switch(DestFormat)
135 {
136 case PF_FileNameOnly:
137 {
138 //Not possible, there is no file name in source
139 Dest[0] = '\0';
140 break;
141 }
142
143 case PF_TAPPathOnly:
144 {
145 //Cut away the mount point
146 if(strncmp(Source, TAPFSROOT, strlen(TAPFSROOT)) == 0)
147 {
148 strcpy(Dest, &Source[7]);
149 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
150 }
151 else
152 Dest[0] = '\0';
153
154 break;
155 }
156
157 case PF_LinuxPathOnly: break;
158
159 case PF_FullTAPPath:
160 {
161 //Not possible, there is no file name in source
162 Dest[0] = '\0';
163 break;
164 }
165
166 case PF_FullLinuxPath:
167 {
168 //Not possible, there is no file name in source
169 //But it is possible that only a path is needed
170 strcpy(Dest, Source);
171 break;
172 }
173 }
174 break;
175 }
176
177 case PF_FullTAPPath:
178 {
179 switch(DestFormat)
180 {
181 case PF_FileNameOnly:
182 {
183 char *LastSlash;
184
185 //Just copy the file name, which starts after the last /
186 LastSlash = strrchr(Source, '/');
187 strcpy(Dest, LastSlash + 1);
188 break;
189 }
190
191 case PF_TAPPathOnly:
192 {
193 char *LastSlash;
194 dword i;
195
196 //Copy from the beginning up to the last slash
197 LastSlash = strrchr(Source, '/');
198 i = (dword)LastSlash - (dword)Source;
199 strncpy(Dest, Source, i);
200 Dest[i] = '\0';
201 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
202 break;
203 }
204
205 case PF_LinuxPathOnly:
206 {
207 char *LastSlash;
208 dword i;
209
210 //To calculate the Linux path, we need to determine if this is a relative or absolute path
211 if(Source[0] == '/')
212 {
213 //Just insert the mount point, then copy everything up to the last slash
214 strcpy(Dest, "/mnt/hd");
215 LastSlash = strrchr(Source, '/');
216 i = (dword)LastSlash - (dword)Source;
217 strncpy(&Dest[7], Source, i);
218 Dest[i+7] = '\0';
219 }
220 else
221 {
222 //As the path is relative, we need to get the current path to make it absolute
223 char CurrentPath[FBLIB_DIR_SIZE];
224
225 HDD_TAP_GetCurrentDir(CurrentPath);
226 if(!StringEndsWith(CurrentPath, "/")) strcat(CurrentPath, "/");
227 strcat(CurrentPath, Source);
228
229 strcpy(Dest, "/mnt/hd");
230 LastSlash = strrchr(CurrentPath, '/');
231 i = (dword)LastSlash - (dword)CurrentPath;
232 strncpy(&Dest[7], CurrentPath, i);
233 Dest[i+7] = '\0';
234 }
235 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
236
237 break;
238 }
239
240 case PF_FullTAPPath: break;
241
242 case PF_FullLinuxPath:
243 {
244 //To calculate the Linux path, we need to determine if this is a relative or absolute path
245 if(Source[0] == '/')
246 {
247 //Simply insert the mount point
248 TAP_SPrint(Dest, "/mnt/hd%s", Source);
249 }
250 else
251 {
252 //As the path is relative, we need to get the current path to make it absolute
253 char CurrentPath[FBLIB_DIR_SIZE];
254
255 HDD_TAP_GetCurrentDir(CurrentPath);
256 if(!StringEndsWith(CurrentPath, "/")) strcat(CurrentPath, "/");
257 strcat(CurrentPath, Source);
258
259 TAP_SPrint(Dest, "/mnt/hd%s", CurrentPath);
260 }
261 break;
262 }
263 }
264 break;
265 }
266
267 case PF_FullLinuxPath:
268 {
269 switch(DestFormat)
270 {
271 case PF_FileNameOnly:
272 {
273 char *LastSlash;
274
275 //Just copy the file name, which starts after the last /
276 LastSlash = strrchr(Source, '/');
277 strcpy(Dest, LastSlash + 1);
278 break;
279 }
280
281 case PF_TAPPathOnly:
282 {
283 char *LastSlash;
284 dword i;
285
286 //Copy from the beginning up to the last slash, but skip the mount point
287 if(strncmp(Source, TAPFSROOT, strlen(TAPFSROOT)) == 0)
288 {
289 LastSlash = strrchr(Source, '/');
290 i = (dword)LastSlash - (dword)&Source[7];
291 strncpy(Dest, &Source[7], i);
292 Dest[i] = '\0';
293 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
294 }
295 else
296 Dest[0] = '\0';
297
298 break;
299 }
300
301 case PF_LinuxPathOnly:
302 {
303 char *LastSlash;
304 dword i;
305
306 //Copy from the beginning up to the last slash
307 LastSlash = strrchr(Source, '/');
308 i = (dword)LastSlash - (dword)Source;
309 strncpy(Dest, Source, i);
310 Dest[i] = '\0';
311 if(!StringEndsWith(Dest, "/")) strcat(Dest, "/");
312
313 break;
314 }
315
316 case PF_FullTAPPath:
317 {
318 //Copy everything except the mount point
319 if(strncmp(Source, TAPFSROOT, strlen(TAPFSROOT)) == 0)
320 strcpy(Dest, &Source[7]);
321 else
322 Dest[0] = '\0';
323 break;
324 }
325
326 case PF_FullLinuxPath: break;
327 }
328 break;
329 }
330 }
331
332 TRACEEXIT();
333}
void ConvertPathType(const char *Source, char *Dest, tPathFormat DestFormat)
tPathFormat GetPathType(const char *Source)
Definition: GetPathType.c:5
int HDD_TAP_GetCurrentDir(char *CurrentDir)
#define FBLIB_DIR_SIZE
Definition: libFireBird.h:1871
#define TRACEEXIT()
Definition: libFireBird.h:1244
#define TRACEENTER()
Definition: libFireBird.h:1243
bool StringEndsWith(const char *text, const char *postfix)
Definition: StringEndsWith.c:4
tPathFormat
Definition: libFireBird.h:1921
@ PF_FullTAPPath
Definition: libFireBird.h:1925
@ PF_LinuxPathOnly
Definition: libFireBird.h:1924
@ PF_FullLinuxPath
Definition: libFireBird.h:1926
@ PF_TAPPathOnly
Definition: libFireBird.h:1923
@ PF_FileNameOnly
Definition: libFireBird.h:1922
#define TAPFSROOT
Definition: libFireBird.h:1870