Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File creation. #348

Open
Spoiledpay opened this issue Dec 14, 2024 · 1 comment
Open

File creation. #348

Spoiledpay opened this issue Dec 14, 2024 · 1 comment

Comments

@Spoiledpay
Copy link

Hello! I hope you are well.
Could you provide examples or guidance on how to use the Adept Language for directory and file management? Specifically, I'm interested in creating directories, working with files (reading and saving), and understanding if there are libraries or built-in features in Adept to facilitate these tasks. Examples or links to documentation would be highly appreciated!

@IsaacShelton
Copy link
Collaborator

IsaacShelton commented Dec 15, 2024

Hello! There is no standard file system library yet, however you can use the same functions as you would use in C to achieve the same effect.

For example:

import basics
import cstdio
import String

func main int {
    unless createFolder('my_folder') {
        print("Failed to create folder :(")
        return 1
    }

    message String = "Hello, world!"

    unless writeFile('my_folder/hello.txt', message.array, message.length, ::TEXT) {
        print("Failed to write file :(")
        return 1
    }

    content String
    unless readFile('my_folder/hello.txt', &content, ::TEXT) {
        print("Failed to read file :(")
        return 1
    }

    print("Got content from file: " + content)
    return 0
}

#if __windows__
    foreign CreateDirectoryA(*ubyte, ptr) int

    func createFolder(path *ubyte) successful {
        return CreateDirectoryA(path, null) != 0
    }
#elif __unix__
    alias mode_t = ushort
    define S_IRUSR = 1 << 8
    define S_IWUSR = 1 << 7
    define S_IXUSR = 1 << 6
    define S_IRGRP = 1 << 5
    define S_IWGRP = 1 << 4
    define S_IXGRP = 1 << 3
    define S_IROTH = 1 << 2
    define S_IWOTH = 1 << 1
    define S_IXOTH = 1 << 0

    define S_IRWXU = S_IRUSR | S_IWUSR | S_IXUSR
    define S_IRWXG = S_IRGRP | S_IWGRP | S_IXGRP
    define S_IRWXO = S_IROTH | S_IWOTH | S_IXOTH

    foreign mkdir(*ubyte, mode_t) int

    func createFolder(path *ubyte) successful {
        return mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) != -1
    }
#end

enum FileMode (TEXT, BINARY)

func writeFile(filename *ubyte, bytes ptr, length usize, mode FileMode) successful {
    file *FILE = fopen(filename, mode == ::TEXT ? 'w' : 'wb')

    if file == null {
        return false
    }

    defer fclose(file)

    if fwrite(bytes, sizeof ubyte, length, file) != length {
        return false
    }

    return true
}

func readFile(filename *ubyte, result *String, mode FileMode) successful {
    file *FILE = fopen(filename, mode == ::TEXT ? 'r' : 'rb')

    if file == null {
        return false
    }

    defer fclose(file)
    
    *result = ""

    until feof(file) {
        buffer 256 ubyte = undef
        num_read int = fread(buffer at 0, sizeof ubyte, sizeof(buffer), file)

        if num_read != sizeof(buffer) && !feof(file) {
            return false
        }

        result.append(StringView(buffer at 0, num_read))
    }

    return true
}

Once better filesystem support is added to the standard library, it will most likely use something similar to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants