996
|
1 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
|
|
2 *
|
|
3 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
|
|
4 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se>
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation, either version 3 of the License, or
|
|
9 * (at the proxy's option) any later version. Arvid Norlander is a
|
|
10 * proxy who can decide which future versions of the GNU General Public
|
|
11 * License can be used.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include "../global.h"
|
|
23 #include "io.h"
|
|
24 #include "../funge-space/funge-space.h"
|
|
25 #include "../vector.h"
|
|
26 #include "../rect.h"
|
|
27 #include "../stack.h"
|
|
28 #include "../ip.h"
|
|
29 #include "../settings.h"
|
|
30
|
|
31 #include <assert.h>
|
|
32 #include <stdbool.h>
|
|
33
|
|
34 FUNGE_ATTR_FAST void run_file_input(instructionPointer * restrict ip)
|
|
35 {
|
|
36 assert(ip != NULL);
|
|
37
|
|
38 if (setting_enable_sandbox) {
|
|
39 ip_reverse(ip);
|
|
40 return;
|
|
41 }
|
|
42
|
|
43 {
|
|
44 char * restrict filename;
|
|
45 bool binary;
|
|
46 funge_vector offset;
|
|
47 funge_vector size;
|
|
48
|
|
49 // Pop stuff.
|
|
50 filename = (char*)stack_pop_string(ip->stack, NULL);
|
|
51
|
|
52 // Sanity test!
|
|
53 if (*filename == '\0') {
|
|
54 stack_free_string(filename);
|
|
55 ip_reverse(ip);
|
|
56 return;
|
|
57 }
|
|
58
|
|
59 binary = (bool)(stack_pop(ip->stack) & 1);
|
|
60 offset = stack_pop_vector(ip->stack);
|
|
61
|
|
62 if (!fungespace_load_at_offset(filename,
|
|
63 vector_create_ref(offset.x + ip->storageOffset.x, offset.y + ip->storageOffset.y),
|
|
64 &size, binary)) {
|
|
65 ip_reverse(ip);
|
|
66 } else {
|
|
67 stack_push_vector(ip->stack, &size);
|
|
68 stack_push_vector(ip->stack, &offset);
|
|
69 }
|
|
70 stack_free_string(filename);
|
|
71 }
|
|
72 }
|
|
73
|
|
74 FUNGE_ATTR_FAST void run_file_output(instructionPointer * restrict ip)
|
|
75 {
|
|
76 assert(ip != NULL);
|
|
77
|
|
78 if (setting_enable_sandbox) {
|
|
79 ip_reverse(ip);
|
|
80 return;
|
|
81 }
|
|
82
|
|
83 {
|
|
84 char * restrict filename;
|
|
85 bool textfile;
|
|
86 funge_vector offset;
|
|
87 funge_vector size;
|
|
88
|
|
89 // Pop stuff.
|
|
90 filename = (char*)stack_pop_string(ip->stack, NULL);
|
|
91 textfile = (bool)(stack_pop(ip->stack) & 1);
|
|
92 offset = stack_pop_vector(ip->stack);
|
|
93 size = stack_pop_vector(ip->stack);
|
|
94
|
|
95 // Sanity test!
|
|
96 if (*filename == '\0' || size.x < 1 || size.y < 1) {
|
|
97 stack_free_string(filename);
|
|
98 ip_reverse(ip);
|
|
99 return;
|
|
100 }
|
|
101
|
|
102 if (!fungespace_save_to_file(filename,
|
|
103 vector_create_ref(offset.x + ip->storageOffset.x, offset.y + ip->storageOffset.y),
|
|
104 &size, textfile))
|
|
105 ip_reverse(ip);
|
|
106 stack_free_string(filename);
|
|
107 }
|
|
108
|
|
109 }
|