-
Notifications
You must be signed in to change notification settings - Fork 5
/
frustrum.cpp
240 lines (215 loc) · 4.89 KB
/
frustrum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
extern "C" {
#include <frustrum_ifails.h>
#include <frustrum_tokens.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
#ifdef _WIN32
extern "C" {
#include <Windows.h>
}
#endif
#ifndef _WIN32
#define memcpy_s( dest, dest_size, src, len) memcpy(dest, src, len)
#define strcpy_s( dest, dest_size, src) strcpy(dest, src)
#define strcat_s( dest, dest_size, src) strcat(dest, src)
#endif
#include <map>
#include <fstream>
#include <string>
#include <memory>
static int frustrum_starts = 0;
static int num_open_files = 0;
std::string end_of_header_s = "**END_OF_HEADER";
static char end_of_header[32] = "**END_OF_HEADER";
static char newline_c = '\n';
class PSFile {
public:
PSFile(const char* name, const int* namelen) {
if (*namelen > 2 && name[0] == '*' && name[1] == '*') {
data.append(name);
loc = 0;
is_text = true;
}
else {
file.open(name);
is_text = false;
}
}
void skip_header() {
if (is_text) {
skip_header_text();
}
else {
skip_header_file();
}
}
void read(int max, char* buffer, int* n_read, int* ifail) {
if (is_text) {
read_text(max, buffer, n_read, ifail);
}
else {
read_file(max, buffer, n_read, ifail);
}
}
private:
void skip_header_text() {
size_t end_of_header_start = data.find(end_of_header);
loc = data.find("\n", end_of_header_start);
}
void skip_header_file() {
std::string line;
while (file.good() && line.compare(0, end_of_header_s.length(), end_of_header_s)) {
std::getline(file, line);
}
if (!file.good()) {
file.clear();
file.seekg(0);
}
}
void read_text(int max, char* buffer, int* n_read, int* ifail) {
*ifail = FR_no_errors;
if (loc >= data.size()) {
*ifail = FR_end_of_file;
}
if (loc + max > data.size()) {
*n_read = data.size() - loc;
}
else {
*n_read = max;
}
std::string to_return = data.substr(loc, *n_read);
loc += *n_read;
memcpy_s(buffer, max, to_return.c_str(), *n_read);
}
void read_file(int max, char* buffer, int* n_read, int* ifail) {
/*
// No idea why this failed -- it was producing garbage
// after a certain point in the file.
if (file.good()) {
*n_read = (int)file.readsome(buffer, max);
*ifail = FR_no_errors;
}
else {
*ifail = FR_end_of_file;
}
*/
*ifail = FR_no_errors;
*n_read = 0;
for (int i = 0; i < max; ++i) {
if (file.good()) {
buffer[i] = file.get();
++(*n_read);
if (buffer[i] == newline_c) break;
}
else {
if (*n_read < max) {
*ifail = FR_end_of_file;
}
break;
}
}
}
bool is_text;
std::ifstream file;
std::string data;
size_t loc;
};
std::map<int, std::shared_ptr<PSFile> > open_files;
int next_file_id = 0;
extern void FSTART(int* ifail)
{
*ifail = FR_unspecified;
if (frustrum_starts == 0) {
// Setup any data structures
}
++frustrum_starts;
*ifail = FR_no_errors;
}
extern void FSTOP(int* ifail)
{
*ifail = FR_unspecified;
if (frustrum_starts <= 0) return;
--frustrum_starts;
if (frustrum_starts == 0) {
next_file_id = 0;
open_files.clear();
}
*ifail = FR_no_errors;
}
extern void FMALLO(int* nbytes, char** memory, int* ifail)
{
*ifail = FR_unspecified;
if (frustrum_starts <= 0) {
*memory = 0;
return;
}
*memory = (char*)malloc(*nbytes);
if (*memory == NULL) {
*ifail = FR_memory_full;
return;
}
*ifail = FR_no_errors;
}
extern void FMFREE(int* nbytes, char** memory, int* ifail)
{
*ifail = FR_unspecified;
if (frustrum_starts <= 0) {
return;
}
free(*memory);
*ifail = FR_no_errors;
}
extern void FFOPRD(const int* guise, const int* format, const char* name,
const int* namlen, const int* skiphd, int* strid, int* ifail)
{
*ifail = FR_unspecified;
*strid = -1;
if (frustrum_starts <= 0) return;
auto file = std::make_shared<PSFile>(name, namlen);
open_files.emplace(next_file_id, file);
if (*skiphd) {
open_files[next_file_id]->skip_header();
}
*strid = next_file_id;
++next_file_id;
*ifail = FR_no_errors;
}
extern void FFOPWR(const int* guise, const int* format, const char* name,
const int* namlen, const char* pd2hdr, const int* pd2len,
int* strid, int* ifail)
{
// Dummy Function - we don't ever write
*strid = 1;
*ifail = FR_no_errors;
}
extern void FFWRIT(const int* guise, const int* strid, const int* nchars,
const char* bufer, int* ifail)
{
// Dummy Function - we don't ever write
*ifail = FR_no_errors;
}
extern void FFREAD(const int* guise, const int* strid, const int* nmax,
char* buffer, int* nactual, int* ifail)
{
*ifail = FR_unspecified;
*nactual = 0;
if (frustrum_starts <= 0) return;
auto file = open_files.find(*strid);
if (file != open_files.end()) {
*ifail = FR_no_errors;
file->second->read(*nmax, buffer, nactual, ifail);
}
}
extern void FFCLOS(const int* guise, const int* strid, const int* action,
int* ifail)
{
*ifail = FR_unspecified;
if (frustrum_starts <= 0) return;
auto it = open_files.find(*strid);
*ifail = FR_no_errors;
if (open_files.erase(*strid) == 0) {
*ifail = FR_close_fail;
}
}