view src/ov-struct.cc @ 2376:2142216bf85a

[project @ 1996-10-12 01:39:07 by jwe]
author jwe
date Sat, 12 Oct 1996 01:39:21 +0000
parents
children 449f35baba49
line wrap: on
line source

/*

Copyright (C) 1996 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

Octave 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 General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#if defined (__GNUG__)
#pragma implementation
#endif

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream.h>

#include "error.h"
#include "ov-struct.h"
#include "unwind-prot.h"

int octave_struct::t_id = -1;

const string octave_struct::t_name ("struct");


octave_value
octave_struct::struct_elt_val (const string& nm) const
{
  octave_value retval;

  Pix idx = map.seek (nm);

  if (idx)
    retval = map.contents (idx);
  else
    error ("structure has no member `%s'", nm.c_str ());

  return retval;
}

octave_value&
octave_struct::struct_elt_ref (const string& nm)
{
  return map [nm];

#if 0
  static octave_value fooval;

  Pix idx = map.seek (nm);

  if (idx)
    return map.contents (idx);
  else if (insert)
    return map [nm];
  else
    error ("structure has no member `%s'", nm.c_str ());

  return fooval;
#endif
}

#if 0
octave_value&
octave_struct::lookup_map_element (const string& name, bool insert,
				   bool silent)
{
  static octave_value fooval;

  Pix idx = map.seek (name);

  if (idx)
    return map.contents (idx);
  else if (insert)
    return map [name];
  else if (! silent)
    error ("structure has no member `%s'", name.c_str ());

  return fooval;
}

octave_value
octave_struct::lookup_map_element (const string& ref, bool insert,
				   bool silent)
{
  octave_value retval;

  if (! ref.empty ())
    {
      SLList<string> list;

      size_t beg = 0;
      size_t end;

      do
	{
	  end = ref.find ('.', beg);

	  string tmp = (end == NPOS)
	    ? ref.substr (beg) : ref.substr (beg, end - beg);

	  list.append (tmp);
	}
      while (end != NPOS && (beg = end + 1));

      retval = lookup_map_element (list, insert, silent);
    }

  return retval;
}

octave_value
octave_struct::lookup_map_element (SLList<string>& list, bool insert,
				   bool silent)
{
  octave_value retval;

  Pix p = list.first ();

  while (p)
    {
      string elt = list (p);

      list.next (p);

      octave_value tmp = lookup_map_element (elt, insert, silent);

      if (error_state)
	break;

      tmp_rep = tmp.rep;

      if (! p)
	retval = tmp;
    }

  return retval;
}
#endif

void
octave_struct::print (ostream& os)
{
  // XXX FIXME XXX -- would be nice to print the output in some
  // standard order.  Maybe all substructures first, maybe
  // alphabetize entries, etc.

  begin_unwind_frame ("octave_struct_print");

  unwind_protect_int (struct_indent);
  unwind_protect_int (Vstruct_levels_to_print);

  if (Vstruct_levels_to_print-- > 0)
    {
      os.form ("\n%*s{\n", struct_indent, "");

      increment_struct_indent ();

      Pix p = map.first ();

      while (p)
	{
	  bool pad_after = false;

	  string key = map.key (p);
	  octave_value val = map.contents (p);

	  map.next (p);

	  os.form ("%*s%s =", struct_indent, "", key.c_str ());

	  if (val.print_as_scalar ())
	    os << " ";
	  else if (val.print_as_structure ())
	    {
	      if (p)
		pad_after = true;
	    }
	  else
	    {
	      if (p)
		pad_after = true;

	      os << "\n\n";
	    }

	  val.print (os);

	  if (pad_after)
	    os << "\n";
	}

      decrement_struct_indent ();

      os.form ("%*s%s", struct_indent, "", "}\n");
    }
  else
    os << " <structure>\n";

  run_unwind_frame ("octave_struct_print");
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/