MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
std.c
Go to the documentation of this file.
1
6// ...existing code...
7#include "mx_std.h"
8#include <stdlib.h>
9#include <string.h>
10
11static int g_argc = 0;
12static char **g_argv = NULL;
13static int g_args_registered = 0;
14
15static char *mx_strdup_c(const char *s) {
16 size_t len = strlen(s) + 1;
17 char *d = (char *)malloc(len);
18 if (d)
19 memcpy(d, s, len);
20 return d;
21}
22
24 if (g_argv) {
25 int i;
26 for (i = 0; i < g_argc; ++i) {
27 free(g_argv[i]);
28 }
29 free(g_argv);
30 g_argv = NULL;
31 g_argc = 0;
32 }
33}
34
35void set_program_args(int argc, const char **argv) {
36 int i;
37 if (g_argv) {
39 }
40
41 if (argc <= 0 || argv == NULL) {
42 return;
43 }
44
45 g_argv = (char **)malloc(sizeof(char *) * (size_t)argc);
46 if (!g_argv) {
47 g_argc = 0;
48 return;
49 }
50
51 for (i = 0; i < argc; ++i) {
52 const char *s = argv[i] ? argv[i] : "";
53 g_argv[i] = mx_strdup_c(s);
54 if (!g_argv[i]) {
55 int j;
56 for (j = 0; j < i; ++j)
57 free(g_argv[j]);
58 free(g_argv);
59 g_argv = NULL;
60 g_argc = 0;
61 return;
62 }
63 }
64 g_argc = argc;
65
66 if (!g_args_registered) {
67 // atexit(mxvm_free_program_args);
69 }
70}
71
72int argc(void) {
73 return g_argc;
74}
75
76const char *argv(int idx) {
77 if (idx < 0 || idx >= g_argc)
78 return NULL;
79 return g_argv[idx];
80}
81
82int64_t float_to_int(double d) {
83 return (int64_t)d;
84}
85
86double int_to_float(int64_t i) {
87 return (double)i;
88}
89
90void release(void *ptr) {
91 if (ptr != NULL)
92 free(ptr);
93}
Standard library module C API — function declarations exported to MXVM programs.
int argc(void)
Return the number of stored command-line arguments.
Definition std.c:72
const char * argv(int idx)
Return the command-line argument at the given index.
Definition std.c:76
static char ** g_argv
Definition std.c:12
void free_program_args(void)
Free the stored command-line argument memory.
Definition std.c:23
int64_t float_to_int(double d)
Definition std.c:82
static int g_argc
Definition std.c:11
void set_program_args(int argc, const char **argv)
Store command-line arguments for later access via argc()/argv().
Definition std.c:35
static char * mx_strdup_c(const char *s)
Definition std.c:15
static int g_args_registered
Definition std.c:13
int argc(void)
Return the number of stored command-line arguments.
Definition std.c:72
const char * argv(int idx)
Return the command-line argument at the given index.
Definition std.c:76
double int_to_float(int64_t i)
Definition std.c:86
void release(void *ptr)
Definition std.c:90