Directories in AngelScript

Here’s the translation of the Go code to AngelScript, formatted in Markdown suitable for Hugo:

Our first example demonstrates working with directories in the file system. Here’s the full source code:

#include <angelscript.h>
#include <scriptbuilder.h>
#include <iostream>
#include <filesystem>
#include <fstream>

namespace fs = std::filesystem;

void check(bool condition, const std::string& message) {
    if (!condition) {
        std::cerr << "Error: " << message << std::endl;
        exit(1);
    }
}

void createEmptyFile(const std::string& name) {
    std::ofstream file(name);
    check(file.is_open(), "Failed to create file: " + name);
}

void main() {
    // Create a new sub-directory in the current working directory
    fs::create_directory("subdir");
    check(fs::exists("subdir"), "Failed to create subdir");

    // When creating temporary directories, it's good practice to remove them afterwards
    // We'll use a try-finally block to ensure cleanup
    try {
        createEmptyFile("subdir/file1");

        // Create a hierarchy of directories, including parents
        fs::create_directories("subdir/parent/child");
        check(fs::exists("subdir/parent/child"), "Failed to create directory hierarchy");

        createEmptyFile("subdir/parent/file2");
        createEmptyFile("subdir/parent/file3");
        createEmptyFile("subdir/parent/child/file4");

        // List directory contents
        std::cout << "Listing subdir/parent" << std::endl;
        for (const auto& entry : fs::directory_iterator("subdir/parent")) {
            std::cout << " " << entry.path().filename().string() << " " << fs::is_directory(entry) << std::endl;
        }

        // Change the current working directory
        fs::current_path("subdir/parent/child");

        // Now we'll see the contents of subdir/parent/child when listing the current directory
        std::cout << "Listing subdir/parent/child" << std::endl;
        for (const auto& entry : fs::directory_iterator(".")) {
            std::cout << " " << entry.path().filename().string() << " " << fs::is_directory(entry) << std::endl;
        }

        // Change back to the original directory
        fs::current_path("../../..");

        // Recursively visit a directory
        std::cout << "Visiting subdir" << std::endl;
        for (const auto& entry : fs::recursive_directory_iterator("subdir")) {
            std::cout << " " << entry.path().string() << " " << fs::is_directory(entry) << std::endl;
        }
    } finally {
        // Clean up: remove the directory and all its contents
        fs::remove_all("subdir");
    }
}

This script demonstrates various operations with directories:

  1. We start by creating a new subdirectory using fs::create_directory.

  2. We use a helper function createEmptyFile to create empty files in our directory structure.

  3. We create a hierarchy of directories using fs::create_directories.

  4. We list the contents of a directory using fs::directory_iterator.

  5. We change the current working directory using fs::current_path.

  6. We recursively visit all files and directories using fs::recursive_directory_iterator.

  7. Finally, we clean up by removing the entire directory structure with fs::remove_all.

To run this script, you would need to compile it with an AngelScript compiler and link it with the necessary libraries. The exact process may vary depending on your development environment.

Note that AngelScript doesn’t have built-in file system operations, so we’re using C++ standard library’s <filesystem> here. In a real AngelScript environment, you would need to implement or bind these file system operations yourself.