| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <cstring> |
| 20 | |
| 21 | void _splitpath(const char *srcPath, char *drive, char *path, char *filename, char *ext) { |
| 22 | int pathStart = -1; |
| 23 | int pathEnd = -1; |
| 24 | int fileStart = -1; |
| 25 | int fileEnd = -1; |
| 26 | int extStart = -1; |
| 27 | int extEnd = -1; |
| 28 | |
| 29 | int totalLen = strlen(srcPath); |
| 30 | |
| 31 | if (drive) |
| 32 | *drive = '\0'; |
| 33 | |
| 34 | // Check for an extension |
| 35 | /////////////////////////////////////// |
| 36 | int t = totalLen - 1; |
| 37 | while ((srcPath[t] != '.') && (srcPath[t] != '/') && (t >= 0)) |
| 38 | t--; |
| 39 | // see if we are at an extension |
| 40 | if ((t >= 0) && (srcPath[t] == '.')) { |
| 41 | // we have an extension |
| 42 | extStart = t; |
| 43 | extEnd = totalLen - 1; |
| 44 | if (ext) { |
| 45 | strncpy(ext, &(srcPath[extStart]), extEnd - extStart + 1); |
| 46 | ext[extEnd - extStart + 1] = '\0'; |
| 47 | } |
| 48 | } else { |
| 49 | // no extension |
| 50 | if (ext) |
| 51 | ext[0] = '\0'; |
| 52 | } |
| 53 | |
| 54 | // Check for file name |
| 55 | //////////////////////////////////// |
| 56 | int temp = (extStart != -1) ? (extStart) : (totalLen - 1); |
| 57 | while ((srcPath[temp] != '/') && (temp >= 0)) |
| 58 | temp--; |
| 59 | if (temp < 0) |
| 60 | temp = 0; |
| 61 | if (srcPath[temp] == '/') { |
| 62 | // we have a file |
| 63 | fileStart = temp + 1; |
| 64 | if (extStart != -1) |
| 65 | fileEnd = extStart - 1; |
| 66 | else |
| 67 | fileEnd = totalLen - 1; |
| 68 | if (filename) { |
| 69 | strncpy(filename, &(srcPath[fileStart]), fileEnd - fileStart + 1); |
| 70 | filename[fileEnd - fileStart + 1] = '\0'; |
| 71 | } |
| 72 | pathStart = 0; |
| 73 | pathEnd = fileStart - 2; |
| 74 | // Copy the rest into the path name |
| 75 | if (path) { |
| 76 | strncpy(path, &(srcPath[pathStart]), pathEnd - pathStart + 1); |
| 77 | path[pathEnd - pathStart + 1] = 0; |
| 78 | } |
| 79 | } else { |
| 80 | // only file, no path |
| 81 | fileStart = 0; |
| 82 | if (extStart != -1) |
| 83 | fileEnd = extStart - 1; |
| 84 | else |
| 85 | fileEnd = totalLen - 1; |
| 86 | |
| 87 | if (filename) { |
| 88 | strncpy(filename, &(srcPath[fileStart]), fileEnd - fileStart + 1); |
| 89 | filename[fileEnd - fileStart + 1] = 0; |
| 90 | } |
| 91 | |
| 92 | // Only file no path |
| 93 | if (path) { |
| 94 | path[0] = 0; |
| 95 | } |
| 96 | } |
| 97 | } |