I'm trying to create some sort of "get resource pa...
# kotlin-native
k
I'm trying to create some sort of "get resource path" system going since I don't want to be passing absolute path to my resources to the C Interop code (and since K/N does not have a system like Java where you can do getResource) because then it wouldn't work on different machines... So I've hacked something together real quick using
getpid
and using
proc
on Linux, however. For some reason the first parts of the path I'm getting back looks garbled and I'm unsure why. I have a macOS version working perfectly fine. Code inside the thread:
Copy code
inline val resourcePath: String?
    get() {
        val pid = getpid()
        val buffer = ByteArray(PATH_MAX)

        // Build the path to the '/proc' directory for the process
        val procPath = "/proc/$pid/exe"

        // Use readlink to get the symbolic link pointing to the executable
        val pathLength = readlink(procPath, buffer.refTo(0), buffer.size.convert())

        if (pathLength <= 0) {
            return null
        }

        // Convert the buffer to a Kotlin string
        val path = buffer.toKString()

        return memScoped {
            val dirReturn = dirname(path.cstr)
            dirReturn?.getPointer(this)?.toKString() + "/resources"
        }
    }
As you can see here:
Copy code
p���W:%nta/Documents/Programming/untitled/build/bin/native/releaseExecutable/resources
the first part which is the username is messed up?
nvm I got it fixed:
Copy code
inline val resourcePath: String?
    get() {
        // Get the process ID (PID) of the current process
        val pid = getpid()

        // Create a buffer to store the path
        val buffer = ByteArray(PATH_MAX)

        // Build the path to the '/proc' directory for the process
        val procPath = "/proc/$pid/exe"

        // Use the realpath function to get the canonicalized absolute pathname of the executable
        if (realpath(procPath, buffer.refTo(0)) == null) {
            // If realpath fails, return null to indicate that the resource path could not be determined
            return null
        }

        // Convert the buffer, which contains the absolute path, to a Kotlin string
        val executablePath = buffer.toKString()

        // Find the last '/' character to extract the directory path
        val lastSlashIndex = executablePath.lastIndexOf('/')
        val executableDir = executablePath.substring(0, lastSlashIndex + 1)

        // Construct the resource path by appending "/resources" to the executable directory
        return executableDir + "resources"
    }