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
|
/* -*- c++ -*- */
/*
* Copyright 2004 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <libguile.h>
#include <boost/cstdint.hpp>
#include <boost/shared_ptr.hpp>
// Include our definitions
#include "xyzzy.h"
using namespace std;
typedef void* handle_t;
XYZZY::XYZZY()
{
// nothing to initialize
}
XYZZY::~XYZZY()
{
// nothing to destruct
}
// Initialize with the data file produced by gen-xyzzy.
bool
XYZZY::init()
{
_filespec = DATAROOTDIR;
_filespec += "/gnuradio/gr-run-waveform/";
_filespec += "filesystem.dat";
return init(_filespec);
};
bool
XYZZY::init(const std::string &file)
{
ifstream in(file.c_str(), ios_base::binary|ios_base::in);
if (!in) {
cerr << ("run_waveform: couldn't open data file: ") << file << endl;
return SCM_BOOL_F;
}
size_t length = sizeof(struct header);
char head[length];
in.read(head, length);
boost::shared_ptr<struct header> header = read_header(reinterpret_cast<boost::uint8_t *>(&head));
// Check the magic number to make sure it's the right type of file.
if (strncmp(header->magic, "-XyZzY-", 8) != 0) {
cerr << "ERROR: bad magic number" << endl;
return false;
}
cout << "There are " << header->number_of_dir_entries << " of directory entries" << endl;
// Read in the Directory table
length = sizeof(struct directory_entry);
char dir[length];
for (size_t i=0; i<header->number_of_dir_entries; ++i) {
in.read(dir, length);
boost::shared_ptr<struct directory_entry> entry = read_dir_entry(
reinterpret_cast<boost::uint8_t *>(dir));
_directories.push_back(entry);
// cout << entry->offset_to_name << " : ";
// cout << entry->offset_to_contents << endl;
}
cout << "Loaded " << _directories.size() << " Directory entries" << endl;
// Read in the String Table
size_t total = header->number_of_dir_entries;
boost::uint32_t ssize;
// in.seekg(1540);
while (total) {
// Read just the length part
in.read(reinterpret_cast<char *>(&ssize), sizeof(boost::uint32_t));
boost::uint32_t len = __builtin_bswap32(ssize);
if ((len < 0) || (len > 256)) {
cerr << "ERROR: length out of range! " << len << ":" << ssize << endl;
return false;
}
// All the strings are 32 bit word aligned, so we have to adjust
// how many bytes to read.
size_t padding = sizeof(boost::uint32_t) - (len % sizeof(boost::uint32_t));
size_t newsize = (padding == 4) ? len : len + padding;
// cerr << hex << len << " : " << padding << " : "
// << " : " << 4-(len % sizeof(boost::uint32_t)) << dec << endl;
char sstr[newsize];
in.read(sstr, newsize);
// Use the actual string length, not the padded version
string filespec(reinterpret_cast<const char *>(sstr), len);
_strings.push_back(filespec);
// cout << filespec << endl;
total--;
}
cout << "Loaded " << _strings.size() << " String entries" << endl;
in.close();
return true;
};
// Does a file with name 'filename' exist in magic filesystem?
// bool file_exists(handle, const std::string &filespec);
bool
XYZZY::file_exists(const std::string &filespec)
{
std::vector<std::string>::iterator it;
for (it=_strings.begin(); it<_strings.end(); ++it) {
if ((*it) == filespec) {
return true;
}
}
return false;
}
// Return a C port that will read the file contents
// SCM make_read_only_port(handle, const std::string &filespec)
SCM
XYZZY::make_read_only_port(const std::string &filespec)
{
}
string
XYZZY::read_string(boost::uint8_t *entry, size_t length)
{
string str(reinterpret_cast<const char *>(entry), length);
return str;
}
string
XYZZY::read_string(struct string_entry & entry)
{
return read_string(entry.base, entry.length);
}
boost::shared_ptr<struct header>
XYZZY::read_header(boost::uint8_t *header)
{
boost::shared_ptr<struct header> newhead(new struct header);
struct header *ptr = reinterpret_cast<struct header *>(header);
std::copy(header, header + 8, newhead->magic);
newhead->offset_to_directory = __builtin_bswap32(ptr->offset_to_directory);
newhead->size_of_directory = __builtin_bswap32(ptr->size_of_directory);
newhead->number_of_dir_entries = __builtin_bswap32(ptr->number_of_dir_entries);
newhead->offset_to_strings = __builtin_bswap32(ptr->offset_to_strings);
newhead->size_of_strings = __builtin_bswap32(ptr->size_of_strings);
return newhead;
}
boost::shared_ptr<struct directory_entry>
XYZZY::read_dir_entry(boost::uint8_t *entry)
{
boost::shared_ptr<struct directory_entry> newdir(new struct directory_entry);
struct directory_entry *ptr = reinterpret_cast<struct directory_entry *>(entry);
newdir->offset_to_name = __builtin_bswap32(ptr->offset_to_name);
newdir->offset_to_name = __builtin_bswap32(ptr->offset_to_name);
return newdir;
}
// C linkage for guile
extern "C" {
static XYZZY datafile;
// Initialize with the data file produced by gen-xyzzy.
bool
xyzzy_init(const std::string &filespec)
{
return datafile.init(filespec);
}
// Does a file with name 'filename' exist in magic filesystem?
bool
xyzzy_file_exists(const std::string &filespec)
{
return datafile.file_exists(filespec);
}
// Return a C port that will read the file contents
SCM
xyzzy_make_read_only_port(const std::string &filespec)
{
return datafile.make_read_only_port(filespec);
}
} // end of extern C
|