1 /* -*- c++ -*- */
2 /*
3 * Copyright 2006,2009 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "pmt_int.h"
27 #include <pmt/pmt.h>
28 #include <iostream>
29 #include <sstream>
30 #include <vector>
31
32 namespace pmt {
33
write_list_tail(pmt_t obj,std::ostream & port)34 static void write_list_tail(pmt_t obj, std::ostream& port)
35 {
36 write(car(obj), port); // write the car
37 obj = cdr(obj); // step to cdr
38
39 if (is_null(obj)) // ()
40 port << ")";
41
42 else if (is_pair(obj)) { // normal list
43 port << " ";
44 write_list_tail(obj, port);
45 } else { // dotted pair
46 port << " . ";
47 write(obj, port);
48 port << ")";
49 }
50 }
51
write(pmt_t obj,std::ostream & port)52 void write(pmt_t obj, std::ostream& port)
53 {
54 if (is_bool(obj)) {
55 if (is_true(obj))
56 port << "#t";
57 else
58 port << "#f";
59 } else if (is_symbol(obj)) {
60 port << symbol_to_string(obj);
61 } else if (is_number(obj)) {
62 if (is_integer(obj))
63 port << to_long(obj);
64 else if (is_uint64(obj))
65 port << to_uint64(obj);
66 else if (is_real(obj))
67 port << to_double(obj);
68 else if (is_complex(obj)) {
69 std::complex<double> c = to_complex(obj);
70 port << c.real() << '+' << c.imag() << 'i';
71 } else
72 goto error;
73 } else if (is_null(obj)) {
74 port << "()";
75 } else if (is_pair(obj)) {
76 port << "(";
77 write_list_tail(obj, port);
78 } else if (is_tuple(obj)) {
79 port << "{";
80 size_t len = length(obj);
81 if (len > 0) {
82 port << tuple_ref(obj, 0);
83 for (size_t i = 1; i < len; i++)
84 port << " " << tuple_ref(obj, i);
85 }
86 port << "}";
87 } else if (is_vector(obj)) {
88 port << "#(";
89 size_t len = length(obj);
90 if (len > 0) {
91 port << vector_ref(obj, 0);
92 for (size_t i = 1; i < len; i++)
93 port << " " << vector_ref(obj, i);
94 }
95 port << ")";
96 } else if (is_dict(obj)) {
97 // FIXME
98 // port << "#<dict " << obj << ">";
99 port << "#<dict>";
100 } else if (is_uniform_vector(obj)) {
101 port << "#[";
102 size_t len = length(obj);
103 if (len) {
104 pmt_uniform_vector* uv = static_cast<pmt_uniform_vector*>(obj.get());
105 port << uv->string_ref(0);
106 for (size_t i = 1; i < len; i++)
107 port << " " << uv->string_ref(i);
108 }
109 port << "]";
110 } else {
111 error:
112 // FIXME
113 // port << "#<" << obj << ">";
114 port << "#<unknown>";
115 }
116 }
117
operator <<(std::ostream & os,pmt_t obj)118 std::ostream& operator<<(std::ostream& os, pmt_t obj)
119 {
120 write(obj, os);
121 return os;
122 }
123
write_string(pmt_t obj)124 std::string write_string(pmt_t obj)
125 {
126 std::ostringstream s;
127 s << obj;
128 return s.str();
129 }
130
read(std::istream & port)131 pmt_t read(std::istream& port)
132 {
133 throw notimplemented("notimplemented: pmt::read", PMT_NIL);
134 }
135
serialize(pmt_t obj,std::ostream & sink)136 void serialize(pmt_t obj, std::ostream& sink)
137 {
138 throw notimplemented("notimplemented: pmt::serialize", obj);
139 }
140
141 /*!
142 * \brief Create obj from portable byte-serial representation
143 */
deserialize(std::istream & source)144 pmt_t deserialize(std::istream& source)
145 {
146 throw notimplemented("notimplemented: pmt::deserialize", PMT_NIL);
147 }
148
149 } /* namespace pmt */
150
151
print(pmt_t v)152 void pmt::print(pmt_t v) { std::cout << write_string(v) << std::endl; }
153