bes  Updated for version 3.20.6
FFD4Sequence.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of freeform_handler; a FreeForm API handler for the OPeNDAP
4 // data server.
5 
6 // Copyright (c) 2014 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "config_ff.h"
26 
27 #include <sstream>
28 
29 using std::endl;
30 using std::ostringstream;
31 
32 // #define DODS_DEBUG
33 
34 #include "Error.h"
35 #include "debug.h"
36 
37 #include "FFStr.h"
38 #include "FFD4Sequence.h"
39 #include "util_ff.h"
40 
41 extern long BufPtr;
42 extern char *BufVal;
43 extern long BufSiz;
44 
45 #if 0
46 static long Records(const string &filename)
47 {
48  int error = 0;
49  DATA_BIN_PTR dbin = NULL;
50  FF_STD_ARGS_PTR SetUps = NULL;
51  PROCESS_INFO_LIST pinfo_list = NULL;
52  PROCESS_INFO_PTR pinfo = NULL;
53  static char Msgt[255];
54 
55  SetUps = ff_create_std_args();
56  if (!SetUps) {
57  return -1;
58  }
59 
61  SetUps->user.is_stdin_redirected = 0;
62  SetUps->input_file = const_cast<char*>(filename.c_str());
63 
64  SetUps->output_file = NULL;
65 
66  error = SetDodsDB(SetUps, &dbin, Msgt);
67  if (error && error < ERR_WARNING_ONLY) {
68  db_destroy(dbin);
69  return -1;
70  }
71 
72  ff_destroy_std_args(SetUps);
73 
74  error = db_ask(dbin, DBASK_PROCESS_INFO, FFF_INPUT | FFF_DATA,
75  &pinfo_list);
76  if (error)
77  return (-1);
78 
79  pinfo_list = dll_first(pinfo_list);
80 
81  pinfo = ((PROCESS_INFO_PTR) (pinfo_list)->data.u.pi);
82 
83  long num_records = PINFO_SUPER_ARRAY_ELS(pinfo);
84 
85  ff_destroy_process_info_list(pinfo_list);
86  db_destroy(dbin);
87 
88  return num_records;
89 }
90 #endif
91 
101 {
102  DBG(cerr << "Entering FFD4Sequence::read..." << endl);
103 
104  if (read_p()) // Nothing to do
105  return true;
106 
107  if ((BufPtr >= BufSiz) && (BufSiz != 0))
108  return true; // End of sequence
109 
110  if (!BufVal) { // Make the cache (BufVal is global)
111  // Create the output Sequence format
112  ostringstream o_fmt;
113  int endbyte = 0;
114  int stbyte = 1;
115 
116  o_fmt << "binary_output_data \"DODS binary output data\"" << endl;
117  for (Vars_iter p = var_begin(); p != var_end(); ++p) {
118  if ((*p)->synthesized_p())
119  continue;
120  if ((*p)->type() == dods_str_c)
121  endbyte += static_cast<FFStr&>(**p).length();
122  else
123  endbyte += (*p)->width();
124 
125  o_fmt << (*p)->name() << " " << stbyte << " " << endbyte << " " << ff_types((*p)->type()) << " "
126  << ff_prec((*p)->type()) << endl;
127  stbyte = endbyte + 1;
128  }
129 
130  DBG(cerr << o_fmt.str());
131 
132  // num_rec could come from DDS if sequence length was known...
133  long num_rec = Records(dataset());
134  if (num_rec == -1) {
135  return true;
136  }
137 
138  BufSiz = num_rec * (stbyte - 1);
139  BufVal = new char[BufSiz];
140 
141  long bytes = read_ff(dataset().c_str(), d_input_format_file.c_str(), o_fmt.str().c_str(), BufVal, BufSiz);
142 
143  if (bytes == -1)
144  throw Error("Could not read requested data from the dataset.");
145  }
146 
147  for (Vars_iter p = var_begin(); p != var_end(); ++p)
148  (*p)->read();
149 
150  set_read_p(false);
151 
152  return false;
153 }
154 
155 void FFD4Sequence::transfer_attributes(AttrTable *at)
156 {
157  if (at) {
158  Vars_iter var = var_begin();
159  while (var != var_end()) {
160  (*var)->transfer_attributes(at);
161  var++;
162  }
163  }
164 }
165 
struct_ff_std_args
Definition: freeform.h:867
FFD4Sequence::read
virtual bool read()
Definition: FFD4Sequence.cc:100
struct_ff_dll_node
Definition: freeform.h:1278
struct_ff_process_info
Definition: freeform.h:1193
struct_databin
Definition: freeform.h:1294
Error
FFStr
Definition: FFStr.h:47