47#include <libdap/Error.h>
48#include <libdap/InternalErr.h>
49#include <libdap/mime_util.h>
50#include <libdap/util.h>
52#include "FreeFormCPP.h"
54#include <libdap/DDS.h>
64#include "FFSequence.h"
69#include "FFRequestHandler.h"
71void ff_read_descriptors(DDS &dds_table,
const string &filename)
73 if (!file_exist(filename.c_str()))
74 throw Error((
string)
"Could not open file " + path_to_filename(filename) +
string(
"."));
77 dds_table.set_dataset_name(name_path(filename));
79 FF_STD_ARGS_PTR SetUps = NULL;
80 SetUps = ff_create_std_args();
82 throw Error(
"Insufficient memory");
85 SetUps->user.is_stdin_redirected = 0;
87 SetUps->input_file =
const_cast<char*
>(filename.c_str());
98 if (FFRequestHandler::get_RSS_format_support()) {
99 iff = find_ancillary_rss_formats(filename);
100 SetUps->input_format_file =
const_cast<char*
>(iff.c_str());
103 if (FFRequestHandler::get_Regex_format_support()) {
104 iff = get_Regex_format_file(filename);
106 SetUps->input_format_file =
const_cast<char*
>(iff.c_str());
109 SetUps->output_file = NULL;
111 DATA_BIN_PTR dbin = NULL;
112 char Msgt[Msgt_size];
113 int error = SetDodsDB(SetUps, &dbin, Msgt);
114 if (error && error < ERR_WARNING_ONLY) {
117 ff_destroy_std_args(SetUps);
118 string msg = (string) Msgt +
" FreeForm error code: ";
119 append_long_to_string((
long) error, 10, msg);
123 ff_destroy_std_args(SetUps);
127 PROCESS_INFO_LIST pinfo_list = NULL;
128 char **var_names_vector = NULL;
129 char **dim_names_vector = NULL;
136 error = db_ask(dbin, DBASK_VAR_NAMES, FFF_INPUT | FFF_DATA, &num_names, &var_names_vector);
138 string msg =
"Could not get variable list from the input file. FreeForm error code: ";
139 append_long_to_string((
long) error, 10, msg);
143 error = db_ask(dbin, DBASK_PROCESS_INFO, FFF_INPUT | FFF_DATA, &pinfo_list);
145 string msg =
"Could not get process info for the input file. FreeForm error code: ";
146 append_long_to_string((
long) error, 10, msg);
151 bool is_array =
true;
152 for (
int i = 0; i < num_names; i++) {
153 int num_dim_names = 0;
157 error = db_ask(dbin, DBASK_ARRAY_DIM_NAMES, var_names_vector[i], &num_dim_names, &dim_names_vector);
159 string msg =
"Could not get array dimension names for variable: ";
160 msg += (string) var_names_vector[i] +
", FreeForm error code: ";
161 append_long_to_string((
long) error, 10, msg);
165 if (num_dim_names == 0)
166 cp = var_names_vector[i];
168 cp = strstr(var_names_vector[i],
"::");
174 pinfo_list = dll_first(pinfo_list);
175 PROCESS_INFO_PTR pinfo = ((PROCESS_INFO_PTR) (pinfo_list)->data.u.pi);
176 FORMAT_PTR iformat = PINFO_FORMAT(pinfo);
177 VARIABLE_PTR var = ff_find_variable(cp, iformat);
182 memFree(dim_names_vector,
"**dim_names_vector");
183 dim_names_vector = NULL;
188 pinfo_list = (pinfo_list)->next;
189 pinfo = ((PROCESS_INFO_PTR) (pinfo_list)->data.u.pi);
192 string msg =
"Variable " + (string)cp +
" was not found in the format file.";
196 iformat = PINFO_FORMAT(pinfo);
197 var = ff_find_variable(cp, iformat);
200 string input_format_file = PINFO_ORIGIN(pinfo);
203 switch (FFV_DATA_TYPE(var)) {
205 bt =
new FFStr(cp, filename);
206 static_cast<FFStr&
>(*bt).set_size(var->end_pos - var->start_pos + 1);
210 bt =
new FFByte(cp, filename);
214 bt =
new FFByte(cp, filename);
218 bt =
new FFInt16(cp, filename);
226 bt =
new FFInt32(cp, filename);
234 bt =
new FFInt32(cp, filename);
254 throw InternalErr(__FILE__, __LINE__,
"Unknown FreeForm type!");
257 if (num_dim_names == 0) {
258 if (!seq || newseq) {
261 seq =
new FFSequence(iformat->name, filename, input_format_file);
263 seq->add_var_nocopy(bt);
267 ar =
new FFArray(cp, filename, bt, input_format_file);
274 for (
int j = 0; j < num_dim_names; j++) {
275 FF_ARRAY_DIM_INFO_PTR array_dim_info = NULL;
277 error = db_ask(dbin, DBASK_ARRAY_DIM_INFO, var_names_vector[i], dim_names_vector[j],
280 string msg = string(
"Could not get array dimension info for variable ")
281 + string(var_names_vector[i]) + string(
", FreeForm error code: ");
282 append_long_to_string((
long) error, 10, msg);
286 int DimSiz = (array_dim_info->end_index - array_dim_info->start_index + 1)
287 / array_dim_info->granularity;
288 ar->append_dim(DimSiz, (
string) dim_names_vector[j]);
290 memFree(array_dim_info,
"");
291 array_dim_info = NULL;
295 memFree(dim_names_vector,
"**dim_names_vector");
296 dim_names_vector = NULL;
299 dds_table.add_var_nocopy(ar);
301 dds_table.add_var_nocopy(seq);
305 dds_table.add_var_nocopy(seq);
315 if (var_names_vector)
316 memFree(var_names_vector,
"**var_names_vector");
318 ff_destroy_process_info_list(pinfo_list);
319 if (dim_names_vector)
320 memFree(dim_names_vector,
"**dim_names_vector");
327 if (var_names_vector)
328 memFree(var_names_vector,
"**var_names_vector");
330 ff_destroy_process_info_list(pinfo_list);
331 if (dim_names_vector)
332 memFree(dim_names_vector,
"**dim_names_vector");