GNU Radio 3.5.3.2 C++ API
|
00001 /* -*- c++ -*- */ 00002 /* 00003 * Copyright 2010 Free Software Foundation, Inc. 00004 * 00005 * This file is part of GNU Radio 00006 * 00007 * GNU Radio is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 3, or (at your option) 00010 * any later version. 00011 * 00012 * GNU Radio is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with GNU Radio; see the file COPYING. If not, write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, 00020 * Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #ifndef _XYZZY_H_ 00024 #define _XYZZY_H_ 1 00025 00026 00027 #include <libguile.h> 00028 00029 #ifdef __cplusplus 00030 00031 #include <cstdio> 00032 #include <cstdlib> 00033 #include <string> 00034 #include <map> 00035 #include <vector> 00036 #include <iostream> 00037 #include <fstream> 00038 #include <boost/cstdint.hpp> 00039 #include <boost/shared_ptr.hpp> 00040 #include <boost/scoped_ptr.hpp> 00041 00042 using namespace std; 00043 00044 // - Special case filenames that start with /-xyzzy-/... and search for 00045 // and load them using the single file. We'd stick "/-zyzzy-" first 00046 // in the default load-path. 00047 00048 // - Create a C read-only "port" that "reads" from the string in the file. 00049 // (See guile docs on creating new kinds of ports) 00050 00051 // - Override the default implementation of "primitive-load" and "%search-load-path" 00052 // to make that happen. See load.c in the guile source code. Figure 00053 // out how to get the override done before guile is fully 00054 // initialized. (Guile loads ice-9/boot9.scm to start itself up. We'd 00055 // need to redirect before then.) 00056 00057 struct header { 00058 char magic[8]; 00059 00060 boost::uint32_t offset_to_directory; // byte offset from start of file 00061 boost::uint32_t size_of_directory; // bytes 00062 boost::uint32_t number_of_dir_entries; 00063 00064 boost::uint32_t offset_to_strings; // byte offset from start of file 00065 boost::uint32_t size_of_strings; // bytes 00066 }; 00067 00068 struct directory_entry { 00069 boost::uint32_t offset_to_name; // from start of strings 00070 boost::uint32_t offset_to_contents; // from start of strings 00071 }; 00072 00073 // Each string starts with a uint32_t length, followed by length bytes. 00074 // There is no trailing \0 in the string. 00075 struct string_entry { 00076 boost::uint32_t length; 00077 boost::uint8_t *base; 00078 }; 00079 00080 class XYZZY { 00081 public: 00082 XYZZY(); 00083 ~XYZZY(); 00084 00085 // Initialize with the data file produced by gen-xyzzy. 00086 bool init(); 00087 bool init(const std::string &filespec); 00088 00089 // Does a file with name 'filename' exist in magic filesystem? 00090 bool file_exists(const std::string &filespec); 00091 00092 /// Parse a string data structure 00093 static std::string read_string(boost::uint8_t *entry, size_t length); 00094 static std::string read_string(struct string_entry &entry); 00095 static std::string read_string(std::ifstream &stream); 00096 00097 // Read the header of the datafile 00098 boost::shared_ptr<struct header> read_header(boost::uint8_t *header); 00099 00100 boost::shared_ptr<struct directory_entry> read_dir_entry(boost::uint8_t *header); 00101 00102 std::string &get_contents(const std::string &filespec) { return _contents[filespec]; }; 00103 00104 private: 00105 std::string _filespec; 00106 std::map<std::string, std::string> _contents; 00107 }; 00108 00109 // C linkage bindings for Guile 00110 extern "C" { 00111 #endif 00112 00113 void scm_xyzzy_init (void); 00114 00115 // Initialize with the data file produced by gen-xyzzy. 00116 int xyzzy_init(const char *filename); 00117 00118 // Does a file with name 'filename' exist in magic filesystem? 00119 int xyzzy_file_exists(const char *filename); 00120 00121 // Return a readonly port that accesses filename. 00122 SCM xyzzy_open_file (SCM filename); 00123 00124 #ifdef __cplusplus 00125 } // end of extern C 00126 #endif 00127 00128 #endif // _XYZZY_H_ 1