pmt_write now displays vector and tuple elements.
[debian/gnuradio] / gruel / src / lib / pmt / pmt_io.cc
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 #include <vector>
26 #include <gruel/pmt.h>
27 #include "pmt_int.h"
28 #include <sstream>
29
30 namespace pmt {
31
32 static void
33 pmt_write_list_tail(pmt_t obj, std::ostream &port)
34 {
35   pmt_write(pmt_car(obj), port); // write the car
36   obj = pmt_cdr(obj);            // step to cdr
37
38   if (pmt_is_null(obj))          // ()
39     port << ")";
40
41   else if (pmt_is_pair(obj)){    // normal list
42     port << " ";
43     pmt_write_list_tail(obj, port);
44   }
45   else {                         // dotted pair
46     port << " . ";
47     pmt_write(obj, port);
48     port << ")";
49   }
50 }
51
52 void
53 pmt_write(pmt_t obj, std::ostream &port)
54 {
55   if (pmt_is_bool(obj)){
56     if (pmt_is_true(obj))
57       port << "#t";
58     else
59       port << "#f";
60   }
61   else if (pmt_is_symbol(obj)){
62     port << pmt_symbol_to_string(obj);
63   }
64   else if (pmt_is_number(obj)){
65     if (pmt_is_integer(obj))
66       port << pmt_to_long(obj);
67     else if (pmt_is_real(obj))
68       port << pmt_to_double(obj);
69     else if (pmt_is_complex(obj)){
70       std::complex<double> c = pmt_to_complex(obj);
71       port << c.real() << '+' << c.imag() << 'i';
72     }
73     else
74       goto error;
75   }
76   else if (pmt_is_null(obj)){
77     port << "()";
78   }
79   else if (pmt_is_pair(obj)){
80     port << "(";
81     pmt_write_list_tail(obj, port);
82   }
83   else if (pmt_is_tuple(obj)){
84     port << "{";
85     size_t len = pmt_length(obj);
86     if (len > 0){
87       port << pmt_tuple_ref(obj, 0);
88       for (size_t i = 1; i < len; i++)
89         port << " " << pmt_tuple_ref(obj, i);
90     }
91     port << "}";
92   }
93   else if (pmt_is_vector(obj)){
94     port << "#(";
95     size_t len = pmt_length(obj);
96     if (len > 0){
97       port << pmt_vector_ref(obj, 0);
98       for (size_t i = 1; i < len; i++)
99         port << " " << pmt_vector_ref(obj, i);
100     }
101     port << ")";
102   }
103   else if (pmt_is_dict(obj)){
104     // FIXME
105     // port << "#<dict " << obj << ">";
106     port << "#<dict>";
107   }
108   else if (pmt_is_uniform_vector(obj)){
109     // FIXME
110     // port << "#<uniform-vector " << obj << ">";
111     port << "#<uniform-vector>";
112   }
113   else {
114   error:
115     // FIXME
116     // port << "#<" << obj << ">";
117     port << "#<unknown>";
118   }
119 }
120
121 std::ostream& operator<<(std::ostream &os, pmt_t obj)
122 {
123   pmt_write(obj, os);
124   return os;
125 }
126
127 std::string 
128 pmt_write_string(pmt_t obj)
129 {
130   std::ostringstream s;
131   s << obj;
132   return s.str();
133 }
134
135 pmt_t
136 pmt_read(std::istream &port)
137 {
138   throw pmt_notimplemented("notimplemented: pmt_read", PMT_NIL);
139 }
140
141 void
142 pmt_serialize(pmt_t obj, std::ostream &sink)
143 {
144   throw pmt_notimplemented("notimplemented: pmt_serialize", obj);
145 }
146
147 /*!
148  * \brief Create obj from portable byte-serial representation
149  */
150 pmt_t 
151 pmt_deserialize(std::istream &source)
152 {
153   throw pmt_notimplemented("notimplemented: pmt_deserialize", PMT_NIL);
154 }
155
156 } /* namespace pmt */