aboutsummaryrefslogtreecommitdiff
path: root/src/Utilities/Path.kt
blob: 292b0eed31e8d1f473f8ea71536133fce7f4cc67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package org.jetbrains.dokka

import java.io.File
import java.io.IOException

fun File.getRelativePath(name: File): File {
    val parent = parentFile

    if (parent == null)
        throw IOException("No common directory");

    val basePath = canonicalPath + File.separator;
    val targetPath = name.canonicalPath;

    if (targetPath.startsWith(basePath)) {
        return File(targetPath.substring(basePath.length))
    } else {
        return File(".." + File.separator + parent.getRelativePath(name))
    }
}

fun File.appendExtension(extension: String) = if (extension.isEmpty()) this else File(path + "." + extension)