DAS.cc
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
// Methods for the class DAS - a class used to parse the dataset attribute
// structure.
//
// jhrg 7/25/94
#include "config.h"
#include <cstdio>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#endif
#include <iostream>
#include <string>
#include "DAS.h"
#include "AttrTable.h"
#include "Error.h"
#include "InternalErr.h"
#include "parser.h"
#include "escaping.h"
#include "debug.h"
#include "DapIndent.h"
using std::cerr;
using std::endl;
// Glue routines declared in das.lex
extern void das_switch_to_buffer(void *new_buffer);
extern void das_delete_buffer(void * buffer);
extern void *das_buffer(FILE *fp);
//extern void dasrestart(FILE *yyin);
//extern int dasparse(void *arg); // defined in das.tab.c
extern int dasparse(libdap::parser_arg *arg); // defined in das.tab.c
namespace libdap {
void DAS::duplicate(const DAS &src)
{
// If the container field is set, perform a deep copy
if (src.d_container)
d_container = new AttrTable(*src.d_container);
else
d_container = 0;
d_container_name = src.d_container_name;
d_attrs = src.d_attrs;
}
DAS &DAS::operator=(const DAS &rhs)
{
if (this == &rhs)
return *this;
duplicate(rhs);
return *this;
}
void DAS::container_name(const string &cn)
{
// We want to find a top level attribute table with the given name. So
// set d_container to null first so that we aren't searching some
// previous container
if (cn != d_container_name) {
d_container = 0;
if (!cn.empty()) {
d_container = get_table(cn);
if (!d_container) {
d_container = add_table(cn, new AttrTable);
}
}
d_container_name = cn;
}
}
unsigned int DAS::get_size() const
{
if (d_container) {
return d_container->get_size();
}
return d_attrs.get_size();
}
void DAS::erase()
{
if (d_container) {
d_container->erase();
}
else {
d_attrs.erase();
}
}
AttrTable::Attr_iter DAS::var_begin()
{
if (d_container) {
return d_container->attr_begin();
}
return d_attrs.attr_begin();
}
AttrTable::Attr_iter DAS::var_end()
{
if (d_container) {
return d_container->attr_end();
}
return d_attrs.attr_end();
}
string DAS::get_name(AttrTable::Attr_iter &i)
{
if (d_container) {
return d_container->get_name(i);
}
return d_attrs.get_name(i);
}
AttrTable *
DAS::get_table(AttrTable::Attr_iter &i)
{
if (d_container) {
return d_container->get_attr_table(i);
}
return d_attrs.get_attr_table(i);
}
AttrTable *
DAS::get_table(const string &name)
{
if (d_container) {
return d_container->get_attr_table(name);
}
return d_attrs.get_attr_table(name);
}
AttrTable *
DAS::add_table( const string &name, AttrTable *at )
{
if (d_container) {
at->set_is_global_attribute(false);
return d_container->append_container(at, name);
}
return d_attrs.append_container( at, name ) ;
}
void
DAS::parse(string fname)
{
FILE *in = fopen(fname.c_str(), "r");
if (!in) {
throw Error(cannot_read_file, "Could not open: " + fname);
}
parse(in);
int res = fclose(in);
if (res) {
DBG(cerr << "DAS::parse - Failed to close file " << (void *)in << endl ;) ;
}
}
void
DAS::parse(int fd)
{
#ifdef WIN32
int new_fd = _dup(fd);
#else
int new_fd = dup(fd);
#endif
if (new_fd < 0)
throw InternalErr(__FILE__, __LINE__, "Could not access file.");
FILE *in = fdopen(new_fd, "r");
if (!in) {
throw InternalErr(__FILE__, __LINE__, "Could not access file.");
}
parse(in);
int res = fclose(in);
if (res) {
DBG(cerr << "DAS::parse(fd) - Failed to close " << (void *)in << endl ;) ;
}
}
void
DAS::parse(FILE *in)
{
if (!in) {
throw InternalErr(__FILE__, __LINE__, "Null input stream.");
}
void *buffer = das_buffer(in);
das_switch_to_buffer(buffer);
parser_arg arg(this);
//bool status = dasparse((void *) & arg) == 0;
bool status = dasparse(&arg) == 0;
das_delete_buffer(buffer);
// STATUS is the result of the parser function; if a recoverable error
// was found it will be true but arg.status() will be false.
if (!status || !arg.status()) {// Check parse result
if (arg.error())
throw *arg.error();
}
}
void
DAS::print(FILE *out, bool dereference)
{
fprintf(out, "Attributes {\n") ;
d_attrs.print(out, " ", dereference);
fprintf(out, "}\n") ;
}
void
DAS::print(ostream &out, bool dereference)
{
out << "Attributes {\n" ;
d_attrs.print(out, " ", dereference);
out << "}\n" ;
}
void DAS::dump(ostream &strm) const
{
strm << DapIndent::LMarg << "DAS::dump - (" << (void *) this << ")" << endl;
DapIndent::Indent();
if (d_container) {
strm << DapIndent::LMarg << "current container: " << d_container_name << endl;
}
else {
strm << DapIndent::LMarg << "current container: NONE" << endl;
}
d_attrs.dump(strm);
DapIndent::UnIndent();
}
} // namespace libdap
libdap::AttrTable
Contains the attributes for a dataset.
Definition AttrTable.h:154
libdap::AttrTable::append_container
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition AttrTable.cc:554
libdap::AttrTable::get_attr_table
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition AttrTable.cc:751
libdap::AttrTable::attr_end
virtual Attr_iter attr_end()
Definition AttrTable.cc:863
libdap::AttrTable::attr_begin
virtual Attr_iter attr_begin()
Definition AttrTable.cc:855
libdap::AttrTable::get_name
virtual string get_name() const
Get the name of this attribute table.
Definition AttrTable.cc:266
libdap::AttrTable::erase
virtual void erase()
Erase the attribute table.
Definition AttrTable.cc:1202
libdap::AttrTable::print
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition AttrTable.cc:1432
libdap::AttrTable::get_size
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition AttrTable.cc:259
libdap::AttrTable::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition AttrTable.cc:1715
libdap::DAS::var_begin
AttrTable::Attr_iter var_begin()
Returns a reference to the attribute table for the first variable.
Definition DAS.cc:145
libdap::DAS::get_size
virtual unsigned int get_size() const
Returns the number of attributes in the current attribute table.
Definition DAS.cc:123
libdap::DAS::add_table
virtual AttrTable * add_table(const string &name, AttrTable *at)
Adds a variable attribute table to the DAS or the current dataset container attribute table.
Definition DAS.cc:207
libdap::DAS::print
virtual void print(FILE *out, bool dereference=false)
Definition DAS.cc:329
libdap::DAS::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition DAS.cc:367
libdap::DAS::var_end
AttrTable::Attr_iter var_end()
Definition DAS.cc:156
libdap::DAS::parse
virtual void parse(string fname)
Reads a DAS from the named file.
Definition DAS.cc:230
libdap::DAS::get_table
AttrTable * get_table(AttrTable::Attr_iter &i)
Returns the referenced variable attribute table.
Definition DAS.cc:177
libdap::DAS::erase
virtual void erase()
erase all attributes in this DAS
Definition DAS.cc:133
libdap::DAS::get_name
string get_name(AttrTable::Attr_iter &i)
Returns the name of the referenced variable attribute table.
Definition DAS.cc:166
libdap::DAS::container_name
virtual string container_name() const
Returns the name of the current attribute container when multiple files used to build this DAS.
Definition DAS.h:149
libdap::Error
A class for error processing.
Definition Error.h:94
libdap::InternalErr
A class for software fault reporting.
Definition InternalErr.h:65
libdap
top level DAP object to house generic methods
Definition AlarmHandler.h:36
libdap::parser_arg
Pass parameters by reference to a parser.
Definition parser.h:69