diff src/ov-struct.cc @ 4358:83d4452bc522

[project @ 2003-02-23 02:16:53 by jwe]
author jwe
date Sun, 23 Feb 2003 02:16:54 +0000
parents fc9a075d10fb
children 55695bf73797
line wrap: on
line diff
--- a/src/ov-struct.cc
+++ b/src/ov-struct.cc
@@ -31,7 +31,9 @@
 #include <iostream>
 
 #include "Cell.h"
+#include "defun.h"
 #include "error.h"
+#include "gripes.h"
 #include "oct-lvalue.h"
 #include "ov-list.h"
 #include "ov-struct.h"
@@ -467,6 +469,86 @@
   return retval;
 }
 
+DEFUN (isstruct, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} isstruct (@var{expr})\n\
+Return 1 if the value of the expression @var{expr} is a structure.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  if (args.length () == 1)
+    retval = args(0).is_map ();
+  else
+    print_usage ("isstruct");
+
+  return retval;
+}
+
+DEFUN (fieldnames, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\
+Return a cell array of strings naming the elements of the structure\n\
+@var{struct}.  It is an error to call @code{fieldnames} with an\n\
+argument that is not a structure.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 1)
+    {
+      if (args(0).is_map ())
+	{
+	  Octave_map m = args(0).map_value ();
+	  retval = Cell (m.keys ());
+	}
+      else
+	gripe_wrong_type_arg ("fieldnames", args(0));
+    }
+  else
+    print_usage ("fieldnames");
+
+  return retval;
+}
+
+DEFUN (isfield, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\
+Return true if the expression @var{expr} is a structure and it includes an\n\
+element named @var{name}.  The first argument must be a structure and\n\
+the second must be a string.\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 2)
+    {
+      retval = false;
+
+      // XXX FIXME XXX -- should this work for all types that can do
+      // structure reference operations?
+
+      if (args(0).is_map () && args(1).is_string ())
+	{
+	  std::string key = args(1).string_value ();
+
+	  Octave_map m = args(0).map_value ();
+
+	  retval = m.contains (key) != 0;
+	}
+      else
+	print_usage ("isfield");
+    }
+  else
+    print_usage ("isfield");
+
+  return retval;
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***