plptools
Loading...
Searching...
No Matches
path.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2002 Fritz Elfert <felfert@to.com>
6 * Copyright (C) 2006-2025 Reuben Thomas <rrt@sc3d.org>
7 * Copyright (C) 2026 Jason Morley <hello@jbmorley.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "config.h"
25
26#include "path.h"
27
28#include "xalloc.h"
29#include "xvasprintf.h"
30
31std::string Path::getEPOCBasename(std::string path) {
32 size_t end = path.find_last_of("\\");
33 if (end == std::string::npos) {
34 return std::string(path);
35 }
36 return path.substr(end+1);
37}
38
39char *Path::getEPOCDirname(const char *path) {
40 char *f1 = xstrdup(path);
41 char *p = f1 + strlen(f1);
42
43 // Skip trailing slash.
44 if (p > f1) {
45 *--p = '\0';
46 }
47
48 // Skip backwards to next slash.
49 while ((p > f1) && (*p != '/') && (*p != '\\')) {
50 p--;
51 }
52
53 // If we have just a drive letter, colon and slash, keep exactly that.
54 if (p - f1 < 3) {
55 p = f1 + 3;
56 }
57
58 // Truncate the string at the current point, and return it.
59 *p = '\0';
60
61 return f1;
62}
63
64char *Path::resolveEPOCPath(const char *path, const char *relativeToPath) {
65 char *f1;
66
67 // If we have asked for parent dir, get dirname of cwd.
68 if (!strcmp(path, "..")) {
69 f1 = getEPOCDirname(relativeToPath);
70 } else {
71 if ((path[0] != '/') && (path[0] != '\\') && (path[1] != ':')) {
72 // If path is relative, append it to cwd.
73 f1 = xasprintf("%s%s", relativeToPath, path);
74 } else {
75 // Otherwise, path is absolute, so duplicate it.
76 f1 = xstrdup(path);
77 }
78 }
79
80 /* Convert forward slashes in new path to backslashes. */
81 for (char *p = f1; *p; p++) {
82 if (*p == '/') {
83 *p = '\\';
84 }
85 }
86
87 return f1;
88}
static std::string getEPOCBasename(std::string path)
Returns the last path component of an EPOC path.
Definition: path.cc:31
static char * resolveEPOCPath(const char *path, const char *initialPath)
Returns a new absolute EPOC path, determined by resolving path relative to initialPath.
Definition: path.cc:64
static char * getEPOCDirname(const char *path)
Compute parent directory of an EPOC directory.
Definition: path.cc:39