OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
AsciiSequence.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of asciival, software which can return an ASCII
5 // representation of the data read from a DAP server.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1998,2000
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // Implementation for the class AsciiStructure. See AsciiByte.cc
33 //
34 // 3/12/98 jhrg
35 
36 #include "config.h"
37 
38 #include <iostream>
39 #include <string>
40 
41 #include <BESDebug.h>
42 
43 #include "InternalErr.h"
44 #include "AsciiSequence.h"
45 #include "AsciiStructure.h"
46 #include "get_ascii.h"
47 #include "debug.h"
48 
49 using std::endl ;
50 using namespace dap_asciival;
51 
52 BaseType *
54 {
55  return new AsciiSequence(*this);
56 }
57 
58 AsciiSequence::AsciiSequence(const string &n) : Sequence(n)
59 {
60 }
61 
63  : Sequence( bt->name() ), AsciiOutput( bt )
64 {
65  // Let's make the alternative structure of Ascii types now so that we
66  // don't have to do it on the fly.
67  Vars_iter p = bt->var_begin();
68  while (p != bt->var_end()) {
69  BaseType *new_bt = basetype_to_asciitype(*p);
70  add_var(new_bt);
71  delete new_bt;
72  p++;
73  }
74 
75  BaseType::set_send_p(bt->send_p());
76 }
77 
79 {
80 }
81 
82 int
84 {
85  return -1;
86 }
87 
88 // This specialization is different from the Sequence version only in that
89 // it tests '(*iter)->send_p()' before incrementing 'i' by
90 // '(*iter)->element_count(true)'.
91 int
93 {
94  if (!leaves)
95  return d_vars.size();
96  else {
97  int i = 0;
98  for (Vars_iter iter = d_vars.begin(); iter != d_vars.end(); iter++) {
99  if ((*iter)->send_p())
100  i += (*iter)->element_count(true);
101  }
102  return i;
103  }
104 }
105 
106 void
107 AsciiSequence::print_ascii_row(ostream &strm, int row, BaseTypeRow outer_vars)
108 {
109  BESDEBUG("ascii", " In AsciiSequence::print_ascii_row" << endl);
110 
111  Sequence *seq = dynamic_cast < Sequence * >(_redirect);
112  if (!seq)
113  seq = this;
114 
115  // Print the values from this sequence.
116  // AsciiSequence::element_count() returns only vars with send_p() set.
117  const int elements = element_count();
118  bool first_var = true; // used to control printing the comma separator
119  int j = 0;
120  do {
121  BaseType *bt_ptr = seq->var_value(row, j);
122  if (bt_ptr) { // Check for data.
123  BaseType *abt_ptr = basetype_to_asciitype(bt_ptr);
124  if (abt_ptr->type() == dods_sequence_c) {
125  if (abt_ptr->send_p()) {
126  if (!first_var)
127  strm << ", ";
128  else
129  first_var = false;
130 
131  dynamic_cast <AsciiSequence * >(abt_ptr)
132  ->print_ascii_rows(strm, outer_vars);
133  }
134  }
135  else {
136  // push the real base type pointer instead of the ascii one.
137  // We can cast it again later from the outer_vars vector.
138  outer_vars.push_back(bt_ptr);
139  if (abt_ptr->send_p()) {
140  if (!first_var)
141  strm << ", ";
142  else
143  first_var = false;
144 
145  dynamic_cast < AsciiOutput * >(abt_ptr)->print_ascii(strm,
146  false);
147  }
148  }
149 
150  // we only need the ascii type here, so delete it
151  delete abt_ptr;
152  }
153 
154  ++j;
155  } while (j < elements);
156 }
157 
158 void
159 AsciiSequence::print_leading_vars(ostream &strm, BaseTypeRow & outer_vars)
160 {
161  BESDEBUG("ascii", " In AsciiSequence::print_leading_vars" << endl);
162 
163  bool first_var = true;
164  BaseTypeRow::iterator BTR_iter = outer_vars.begin();
165  while (BTR_iter != outer_vars.end()) {
166  BaseType *abt_ptr = basetype_to_asciitype(*BTR_iter);
167  if (!first_var)
168  strm << ", " ;
169  else
170  first_var = false;
171  dynamic_cast < AsciiOutput * >(abt_ptr)->print_ascii(strm, false);
172  delete abt_ptr;
173 
174  ++BTR_iter;
175  }
176 
177  BESDEBUG("ascii", " Out AsciiSequence::print_leading_vars" << endl);
178 }
179 
180 void
181 AsciiSequence::print_ascii_rows(ostream &strm, BaseTypeRow outer_vars)
182 {
183  Sequence *seq = dynamic_cast < Sequence * >(_redirect);
184  if (!seq)
185  seq = this;
186 
187  const int rows = seq->number_of_rows() - 1;
188  int i = 0;
189  bool done = false;
190  do {
191  if (i > 0 && !outer_vars.empty())
192  print_leading_vars(strm, outer_vars);
193 
194  print_ascii_row(strm, i++, outer_vars);
195 
196  if (i > rows)
197  done = true;
198  else
199  strm << "\n" ;
200  } while (!done);
201 
202  BESDEBUG("ascii", " Out AsciiSequence::print_ascii_rows" << endl);
203 }
204 
205 void
207 {
208  bool first_var = true; // Print commas as separators
209  Vars_iter p = var_begin();
210  while (p != var_end())
211  {
212  if ((*p)->send_p()) {
213  if (!first_var)
214  strm << ", ";
215  else
216  first_var = false;
217 
218  if ((*p)->is_simple_type())
219  strm << dynamic_cast<AsciiOutput *> (*p)->get_full_name();
220  else if ((*p)->type() == dods_sequence_c)
221  dynamic_cast<AsciiSequence *> ((*p))->print_header(strm);
222  else if ((*p)->type() == dods_structure_c)
223  dynamic_cast<AsciiStructure *> ((*p))->print_header(strm);
224  else
225  throw InternalErr(
226  __FILE__,
227  __LINE__,
228  "This method should only be called by instances for which `is_simple_sequence' returns true.");
229 
230  }
231 
232  ++p;
233  }
234 }
235 
236 void
237 AsciiSequence::print_ascii(ostream &strm, bool print_name) throw(InternalErr)
238 {
239  BESDEBUG("ascii", "In AsciiSequence::print_ascii" << endl);
240  Sequence *seq = dynamic_cast < Sequence * >(_redirect);
241  if (!seq)
242  seq = this;
243 
244  if (seq->is_linear()) {
245  if (print_name) {
246  print_header(strm);
247  strm << "\n" ;
248  }
249 
250  BaseTypeRow outer_vars(0);
251  print_ascii_rows(strm, outer_vars);
252  }
253  else {
254  const int rows = seq->number_of_rows() - 1;
255  const int elements = seq->element_count() - 1;
256 
257  // For each row of the Sequence...
258  bool rows_done = false;
259  int i = 0;
260  do {
261  // For each variable of the row...
262  bool vars_done = false;
263  int j = 0;
264  do {
265  BaseType *bt_ptr = seq->var_value(i, j++);
266  BaseType *abt_ptr = basetype_to_asciitype(bt_ptr);
267  dynamic_cast < AsciiOutput * >(abt_ptr)->print_ascii(strm,
268  true);
269  // abt_ptr is not stored for future use, so delete it
270  delete abt_ptr;
271 
272  if (j > elements)
273  vars_done = true;
274  else
275  strm << "\n" ;
276  } while (!vars_done);
277 
278  i++;
279  if (i > rows)
280  rows_done = true;
281  else
282  strm << "\n" ;
283  } while (!rows_done);
284  }
285 }
virtual int element_count(bool leaves=false)
virtual BaseType * ptr_duplicate()
BaseType * _redirect
Definition: AsciiOutput.h:52
AsciiSequence(const string &n)
virtual int length() const
virtual void print_leading_vars(ostream &strm, BaseTypeRow &outer_vars)
virtual void print_header(ostream &strm)
virtual void print_ascii(ostream &strm, bool print_name)
Print an ASCII representation for an instance of BaseType's children.
virtual void print_ascii_rows(ostream &strm, BaseTypeRow out_vars)
BaseType * basetype_to_asciitype(BaseType *bt)
Definition: get_ascii.cc:116
virtual ~AsciiSequence()
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
virtual void print_ascii_row(ostream &strm, int row, BaseTypeRow out_vars)