| 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 | } |
no outgoing calls