nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
File I/O and Workspace Persistence (iofun)

Formatted file streams, textscan/csvread parsing, and workspace save/load persistence. More...

Files

file  iofun.hpp
 Formatted text I/O and display builtins.
file  env.hpp
 Process-environment builtins.
file  io.hpp
file  saveload.hpp
 Workspace persistence — save / load (ASCII + matio v5 .mat).

Functions

Value nk::builtin::sprintf (const Value &fmt, Span< const Value > args, std::pmr::memory_resource *mr=nullptr)
 Formats data into a character array or string according to format specifier fmt (sprintf(fmt, ...)).
std::string nk::builtin::sprintf (const std::string &fmt, Span< const Value > args, std::pmr::memory_resource *mr=nullptr)
 Formats data into a C++ string according to format string fmt.
std::string nk::builtin::dispFormat (const Value &a)
 Formats Value for terminal display via disp.
void nk::builtin::disp (const Value &v, std::ostream &os=std::cout)
 Displays value to the standard output or specified stream (disp(v)).
int nk::builtin::fprintf (std::ostream &os, const std::string &fmt, Span< const Value > args, std::pmr::memory_resource *mr=nullptr)
 Formats and writes data to an output stream (fprintf(os, fmt, ...)).
Value nk::builtin::sscanf (const std::string &str, const std::string &fmt, std::pmr::memory_resource *mr=nullptr)
 Reads formatted data from a string according to format specifier fmt (sscanf(str, fmt)).
void nk::builtin::sscanf (Span< const Value > args, size_t nargout, Span< Value > outs, std::pmr::memory_resource *mr=nullptr)
 Reads formatted data from a string with multi-output support ([A, count, errmsg, nextindex] = sscanf(...)).
void nk::runtime::registerEnvRuntime (Engine &engine)
void nk::runtime::setenv (Span< const Value > args)
 Set / unset an environment variable (setenv(name, value)).
Value nk::runtime::getenv (Span< const Value > args, std::pmr::memory_resource *mr=nullptr)
 Read an environment variable (val = getenv(name)).
std::size_t nk::runtime::fprintf (::nk::Engine &engine, Span< const Value > args)
 Formats and writes text to engine stdout or open file descriptor.
void nk::runtime::disp (::nk::Engine &engine, Span< const Value > args)
 Displays value to engine standard output (disp(v)).
void nk::runtime::fscanf (::nk::Engine &engine, Span< const Value > args, size_t nargout, Span< Value > outs)
 Reads formatted data from file descriptor (fscanf(fid, fmt, size)).
void nk::runtime::textscan (::nk::Engine &engine, Span< const Value > args, size_t nargout, Span< Value > outs)
 Reads tabular data from open file descriptor or string (textscan(fid, fmt)).
void nk::runtime::save (Engine &engine, Environment &env, Span< const Value > args)
 Save workspace variables to an ASCII file (save filename var1 var2 ...).
void nk::runtime::load (Engine &engine, Environment &env, Span< const Value > args, size_t nargout, Span< Value > outs)
 Load workspace variables from an ASCII file (load filename / S = load(filename)).

Detailed Description

Formatted file streams, textscan/csvread parsing, and workspace save/load persistence.

Function Documentation

◆ disp() [1/2]

void nk::builtin::disp ( const Value & v,
std::ostream & os = std::cout )

Displays value to the standard output or specified stream (disp(v)).

Parameters
vValue to display.
osTarget output stream (defaults to std::cout).

◆ disp() [2/2]

void nk::runtime::disp ( ::nk::Engine & engine,
Span< const Value > args )

Displays value to engine standard output (disp(v)).

Parameters
engineReference to executing Engine instance.
argsArguments span containing value to display.

◆ dispFormat()

std::string nk::builtin::dispFormat ( const Value & a)

Formats Value for terminal display via disp.

Parameters
aInput value to format.
Returns
Formatted string.
Parameters
aInput value.
Returns
Rendered string for display.

◆ fprintf() [1/2]

int nk::builtin::fprintf ( std::ostream & os,
const std::string & fmt,
Span< const Value > args,
std::pmr::memory_resource * mr = nullptr )

Formats and writes data to an output stream (fprintf(os, fmt, ...)).

Parameters
osTarget output stream.
fmtFormat string.
argsSpan of arguments to format.
mrMemory resource.
Returns
Number of characters written.
See also
sprintf, disp

◆ fprintf() [2/2]

std::size_t nk::runtime::fprintf ( ::nk::Engine & engine,
Span< const Value > args )

Formats and writes text to engine stdout or open file descriptor.

Parameters
engineReference to executing Engine instance.
argsArguments [fid, fmt, ...] or [fmt, ...].
Returns
Number of characters written.

◆ fscanf()

void nk::runtime::fscanf ( ::nk::Engine & engine,
Span< const Value > args,
size_t nargout,
Span< Value > outs )

Reads formatted data from file descriptor (fscanf(fid, fmt, size)).

Parameters
engineReference to executing Engine instance.
argsInput arguments span [fid, fmt, size].
nargoutNumber of requested outputs.
outsOutput spans to fill with results.

◆ getenv()

Value nk::runtime::getenv ( Span< const Value > args,
std::pmr::memory_resource * mr = nullptr )

Read an environment variable (val = getenv(name)).

Parameters
args(name) — CHAR / STRING.
mrMemory resource (nullptr -> process default).
Returns
Value of the variable as a CHAR row, or empty '' if the variable is unset.
Exceptions
ErrorBad argument type or count (m:getenv:nargin / m:getenv:badArg).

◆ load()

void nk::runtime::load ( Engine & engine,
Environment & env,
Span< const Value > args,
size_t nargout,
Span< Value > outs )

Load workspace variables from an ASCII file (load filename / S = load(filename)).

Reads the file through engine's VFS, parses ASCII matrices, and either:

  • (no-LHS form) writes each parsed variable into env using a stem-derived var name, or
  • (LHS form) returns a struct with one field per variable.
Parameters
engineEngine context (VFS).
envVariable environment to assign into.
args(filename [, var1, var2, …]).
nargoutNumber of requested outputs (0 = assign into env, 1 = return struct).
outsOutput slot for the struct form.
Exceptions
ErrorOn VFS failure, parse failure, or unknown variable.
See also
save

◆ save()

void nk::runtime::save ( Engine & engine,
Environment & env,
Span< const Value > args )

Save workspace variables to an ASCII file (save filename var1 var2 ...).

Looks each requested variable up in env, formats it as ASCII, and writes the file through engine's VFS. With no variables specified, saves the entire workspace.

Parameters
engineEngine context (VFS).
envVariable environment to read from.
args(filename [, var1, var2, …]) — strings or symbol names.
Exceptions
ErrorOn VFS failure or unknown variable name.
See also
load

◆ setenv()

void nk::runtime::setenv ( Span< const Value > args)

Set / unset an environment variable (setenv(name, value)).

Two-argument form sets name = value. Empty value removes the variable from the process environment.

Parameters
args(name, value) — both CHAR / STRING.
Exceptions
ErrorBad argument types or count (m:setenv:nargin / m:setenv:badArg).

◆ sprintf() [1/2]

std::string nk::builtin::sprintf ( const std::string & fmt,
Span< const Value > args,
std::pmr::memory_resource * mr = nullptr )

Formats data into a C++ string according to format string fmt.

Parameters
fmtFormat string.
argsSpan of arguments to format.
mrMemory resource.
Returns
Formatted std::string.
See also
fprintf, disp

◆ sprintf() [2/2]

Value nk::builtin::sprintf ( const Value & fmt,
Span< const Value > args,
std::pmr::memory_resource * mr = nullptr )

Formats data into a character array or string according to format specifier fmt (sprintf(fmt, ...)).

Parameters
fmtFormat specifier string Value.
argsSpan of arguments to format.
mrMemory resource for allocations (nullptr for default).
Returns
Formatted character array or string Value.
See also
fprintf, sscanf

◆ sscanf() [1/2]

Value nk::builtin::sscanf ( const std::string & str,
const std::string & fmt,
std::pmr::memory_resource * mr = nullptr )

Reads formatted data from a string according to format specifier fmt (sscanf(str, fmt)).

Parameters
strSource string to scan.
fmtFormat specifier string.
mrMemory resource.
Returns
Scanned values array.
See also
textscan, sprintf

◆ sscanf() [2/2]

void nk::builtin::sscanf ( Span< const Value > args,
size_t nargout,
Span< Value > outs,
std::pmr::memory_resource * mr = nullptr )

Reads formatted data from a string with multi-output support ([A, count, errmsg, nextindex] = sscanf(...)).

Parameters
argsInput arguments span [str, fmt, size].
nargoutNumber of requested outputs.
outsOutput spans to fill with results.
mrMemory resource.

◆ textscan()

void nk::runtime::textscan ( ::nk::Engine & engine,
Span< const Value > args,
size_t nargout,
Span< Value > outs )

Reads tabular data from open file descriptor or string (textscan(fid, fmt)).

Parameters
engineReference to executing Engine instance.
argsInput arguments span [fid/str, fmt, ...].
nargoutNumber of requested outputs.
outsOutput spans to fill with results.