summaryrefslogtreecommitdiff
path: root/gruel/src/lib/pmt/pmt_io.cc
blob: b909c1b64b8210f5f182ebd73baef4c7050d4fc3 (plain)
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
/* -*- c++ -*- */
/*
 * Copyright 2006,2009 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <vector>
#include <gruel/pmt.h>
#include "pmt_int.h"
#include <sstream>

namespace pmt {

static void
pmt_write_list_tail(pmt_t obj, std::ostream &port)
{
  pmt_write(pmt_car(obj), port); // write the car
  obj = pmt_cdr(obj);		 // step to cdr

  if (pmt_is_null(obj))		 // ()
    port << ")";

  else if (pmt_is_pair(obj)){	 // normal list
    port << " ";
    pmt_write_list_tail(obj, port);
  }
  else {			 // dotted pair
    port << " . ";
    pmt_write(obj, port);
    port << ")";
  }
}

void
pmt_write(pmt_t obj, std::ostream &port)
{
  if (pmt_is_bool(obj)){
    if (pmt_is_true(obj))
      port << "#t";
    else
      port << "#f";
  }
  else if (pmt_is_symbol(obj)){
    port << pmt_symbol_to_string(obj);
  }
  else if (pmt_is_number(obj)){
    if (pmt_is_integer(obj))
      port << pmt_to_long(obj);
    else if (pmt_is_uint64(obj))
      port << pmt_to_uint64(obj);
    else if (pmt_is_real(obj))
      port << pmt_to_double(obj);
    else if (pmt_is_complex(obj)){
      std::complex<double> c = pmt_to_complex(obj);
      port << c.real() << '+' << c.imag() << 'i';
    }
    else
      goto error;
  }
  else if (pmt_is_null(obj)){
    port << "()";
  }
  else if (pmt_is_pair(obj)){
    port << "(";
    pmt_write_list_tail(obj, port);
  }
  else if (pmt_is_tuple(obj)){
    port << "{";
    size_t len = pmt_length(obj);
    if (len > 0){
      port << pmt_tuple_ref(obj, 0);
      for (size_t i = 1; i < len; i++)
	port << " " << pmt_tuple_ref(obj, i);
    }
    port << "}";
  }
  else if (pmt_is_vector(obj)){
    port << "#(";
    size_t len = pmt_length(obj);
    if (len > 0){
      port << pmt_vector_ref(obj, 0);
      for (size_t i = 1; i < len; i++)
	port << " " << pmt_vector_ref(obj, i);
    }
    port << ")";
  }
  else if (pmt_is_dict(obj)){
    // FIXME
    // port << "#<dict " << obj << ">";
    port << "#<dict>";
  }
  else if (pmt_is_uniform_vector(obj)){
    // FIXME
    // port << "#<uniform-vector " << obj << ">";
    port << "#<uniform-vector>";
  }
  else {
  error:
    // FIXME
    // port << "#<" << obj << ">";
    port << "#<unknown>";
  }
}

std::ostream& operator<<(std::ostream &os, pmt_t obj)
{
  pmt_write(obj, os);
  return os;
}

std::string 
pmt_write_string(pmt_t obj)
{
  std::ostringstream s;
  s << obj;
  return s.str();
}

pmt_t
pmt_read(std::istream &port)
{
  throw pmt_notimplemented("notimplemented: pmt_read", PMT_NIL);
}

void
pmt_serialize(pmt_t obj, std::ostream &sink)
{
  throw pmt_notimplemented("notimplemented: pmt_serialize", obj);
}

/*!
 * \brief Create obj from portable byte-serial representation
 */
pmt_t 
pmt_deserialize(std::istream &source)
{
  throw pmt_notimplemented("notimplemented: pmt_deserialize", PMT_NIL);
}

} /* namespace pmt */