__zigar‐>redirect(name, stream) ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

JavaScript | PHP


Redirect a standard stream to another stream.

Possible values for name:

  • stdin
  • stdout
  • stderr
  • root

Usage:

const c = @import("c");

pub fn scan() ?struct {
    first: i32,
    second: i32,
    third: f64,
} {
    var num1: i32 = undefined;
    var num2: i32 = undefined;
    var num3: f64 = undefined;
    if (c.scanf("%d, %d, %lf", &num1, &num2, &num3) != 3) return null;
    return .{ .first = num1, .second = num2, .third = num3 };
}
<?php

$m = zigar_use(__DIR__ . '/redirect-example-1.zig');

$strm = fopen('php://memory', "r+");
fwrite($strm, "1234, 4567, 3.1416\n");
rewind($strm);

$m->__zigar->redirect('stdin', $strm);
$result = $m->scan();
print_r($result);
S0 Object
(
    [first] => 1234
    [second] => 4567
    [third] => 3.1416
)

When name is root, stream can be a callback function. The function receives the path whenever the Zig module tries to open a file (or directory). If it returns a resource handle from opendir(), that would be used a the file system root. If it returns a string, that string would become the actual path. If it returns null or false, then the call would pass through to the real file system.

const std = @import("std");

pub fn print(path: []const u8) !void {
    var file: std.fs.File = try std.fs.openFileAbsolute(path, .{});
    defer file.close();
    var write_buffer: [1024]u8 = undefined;
    var stdout_writer = std.fs.File.stdout().writer(&write_buffer);
    const stdout = &stdout_writer.interface;
    var read_buffer: [1024]u8 = undefined;
    while (true) {
        const len = try file.read(&read_buffer);
        if (len == 0) break;
        _ = try stdout.write(read_buffer[0..len]);
    }
    try stdout.flush();
}
<?php

require_once __DIR__ . '/VirtualFSStream.php';

$m = zigar_use(__DIR__ . '/redirect-example-3.zig');

$root = new VirtualDir([
    'sub' => new VirtualDir([
        'aardvark.txt' => new VirtualFile(<<<AARDVARK
            _.---._    /\\
            ./'       "--`\//
        ./              o \
        /./\  )______   \__ \
        ./  / /\ \   | \ \  \ \
        / /  \ \  | |\ \  \7
            "     "    "  "        VK

        AARDVARK),
    ]),
]);
VirtualFSStream::add_root_node('hello', $root);
$dir = opendir("vfs://hello");
$m->__zigar->redirect('root', function($path) use($dir) {
    if (str_starts_with($path, '/remote/')) {
        $query = rawurlencode(substr($path, 8));
        return "https://echo.free.beeceptor.com?q=$query"; 
    }
    return $dir;
});
$m->print("/sub/aardvark.txt");
$m->print("/remote/something");
    _.---._    /\
    ./'       "--`\//
./              o \
/./\  )______   \__ \
./  / /\ \   | \ \  \ \
/ /  \ \  | |\ \  
    "     "    "  "        VK
{
  "method": "GET",
  "protocol": "https",
  "host": "echo.free.beeceptor.com",
  "path": "/?q=something",
  "ip": "5.173.194.47",
  "headers": {
    "Host": "echo.free.beeceptor.com",
    "Accept-Encoding": "gzip"
  },
  "parsedQueryParams": {
    "q": "something"
  }
}

Virtual file system | VirtualFSStream