OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
FFD4Sequence.cc
Go to the documentation of this file.
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 
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 
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 
void ff_destroy_process_info_list(PROCESS_INFO_LIST)
long Records(const string &filename)
Figure out how many records there are in the dataset.
Definition: util_ff.cc:487
#define FFF_INPUT
Definition: freeform.h:660
int SetDodsDB(FF_STD_ARGS_PTR std_args, DATA_BIN_HANDLE dbin_h, char *Msgt)
Given a set of standard arguments (input filenames), allocate a DATA-BIN_HANDLE and return an error c...
Definition: util_ff.cc:402
char * BufVal
DLL_NODE_PTR dll_first(DLL_NODE_PTR node)
struct struct_ff_std_args::struct_std_args_user user
#define DBASK_PROCESS_INFO
Definition: freeform.h:1343
long BufSiz
#define PINFO_SUPER_ARRAY_ELS(pi)
Definition: freeform.h:1399
#define NULL
Definition: wcsUtil.h:65
#define FFF_DATA
Definition: freeform.h:656
char * output_file
Definition: freeform.h:877
void ff_destroy_std_args(FF_STD_ARGS_PTR)
virtual bool read()
Read a row from the Sequence.
#define ERR_WARNING_ONLY
Definition: err.h:130
void db_destroy(DATA_BIN_PTR)
FF_STD_ARGS_PTR ff_create_std_args(void)
const string ff_types(Type dods_type)
Definition: util_ff.cc:247
int db_ask(DATA_BIN_PTR, int,...)
int ff_prec(Type dods_type)
Definition: util_ff.cc:279
long BufPtr
struct struct_ff_process_info * PROCESS_INFO_PTR
Definition: freeform.h:1192
virtual void transfer_attributes(AttrTable *at)
long read_ff(const char *dataset, const char *if_file, const char *o_format, char *o_buffer, unsigned long bsize)
Read from a file/database using the FreeForm API.
Definition: util_ff.cc:156