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