# HG changeset patch
# User Ben Abbott <bpabbott@mac.com>
# Date 1279231228 14400
# Node ID 9ce1f0606e282aa2e944405ac9c375492fd8b4c1
# Parent  6f640ed5bb931d345d93a811c981bca9c649cec6
Add new functions.

diff -r 6f640ed5bb93 -r 9ce1f0606e28 scripts/ChangeLog
--- a/scripts/ChangeLog	Wed Jul 14 19:29:02 2010 -0400
+++ b/scripts/ChangeLog	Thu Jul 15 18:00:28 2010 -0400
@@ -1,3 +1,9 @@
+2010-07-15  Ben Abbott <bpabbott@mac.com>
+
+	* miscellaneous/getappdata.m, miscellaneous/isappdata.m,
+	miscellaneous/rmappdata.m, miscellaneous/setappdata.m:
+	Add new files.
+
 2010-07-14  Ben Abbott <bpabbott@mac.com>
 
 	* plot/__gnuplot_print__.m: Fix broken options "-orientation",
diff -r 6f640ed5bb93 -r 9ce1f0606e28 scripts/miscellaneous/getappdata.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/getappdata.m	Thu Jul 15 18:00:28 2010 -0400
@@ -0,0 +1,43 @@
+## Copyright (C) 2010 Ben Abbott
+## 
+## This program 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 of the License, or
+## (at your option) any later version.
+## 
+## This program 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, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{value} =} getappdata (@var{h}, @var{name})
+## Returns the @var{value} for named application data for the object(s) with
+## handle(s) @var{h}.
+## @end deftypefn
+
+## Author: Ben Abbott <bpabbott@mac.com>
+## Created: 2010-07-15
+
+function val = getappdata (h, name)
+
+  if (! (all (ishandle (h)) && ischar (name)))
+    error ("getappdata: invalid input.")
+  endif
+
+  appdata(numel(h)) = struct();
+  for nh = 1:numel(h)
+    appdata(nh) = get (h(nh), "__appdata__");
+  end
+  if (nh > 1)
+    val = {appdata.(name)};
+  else
+    val = appdata.(name);
+  endif
+
+endfunction
+
diff -r 6f640ed5bb93 -r 9ce1f0606e28 scripts/miscellaneous/isappdata.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/isappdata.m	Thu Jul 15 18:00:28 2010 -0400
@@ -0,0 +1,47 @@
+## Copyright (C) 2010 Ben Abbott
+## 
+## This program 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 of the License, or
+## (at your option) any later version.
+## 
+## This program 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, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {@var{V} =} isappdata (@var{h}, @var{name})
+## Returns a logical vector indicating whether the named application data
+## for the object(s) with handle(s) @var{H} exists.
+## @end deftypefn
+
+## Author: Ben Abbott <bpabbott@mac.com>
+## Created: 2010-07-15
+
+function res = isappdata (h, name)
+
+  if (! (all (ishandle (h)) && ischar (name)))
+    error ("isappdata: invalid input.")
+  endif
+
+  for nh = 1:numel(h)
+    data = get (h(nh));
+    if (isfield (data, "__appdata__") && isfield (data.__appdata__, name))
+      res(nh) = true;
+    else
+      res(nh) = false;
+    endif
+  endfor
+
+endfunction
+
+%!test
+%! setappdata (0, "hello", "world")
+%! assert (isappdata (0, "hello"), true)
+%!assert (isappdata (0, "foobar"), false)
+
diff -r 6f640ed5bb93 -r 9ce1f0606e28 scripts/miscellaneous/module.mk
--- a/scripts/miscellaneous/module.mk	Wed Jul 14 19:29:02 2010 -0400
+++ b/scripts/miscellaneous/module.mk	Thu Jul 15 18:00:28 2010 -0400
@@ -24,11 +24,13 @@
   miscellaneous/fileparts.m \
   miscellaneous/flops.m \
   miscellaneous/fullfile.m \
+  miscellaneous/getappdata.m \
   miscellaneous/getfield.m \
   miscellaneous/gunzip.m \
   miscellaneous/gzip.m \
   miscellaneous/info.m \
   miscellaneous/inputname.m \
+  miscellaneous/isappdata.m \
   miscellaneous/ismac.m \
   miscellaneous/ispc.m \
   miscellaneous/isunix.m \
@@ -48,8 +50,10 @@
   miscellaneous/paren.m \
   miscellaneous/parseparams.m \
   miscellaneous/perl.m \
+  miscellaneous/rmappdata.m \
   miscellaneous/run.m \
   miscellaneous/semicolon.m \
+  miscellaneous/setappdata.m \
   miscellaneous/setfield.m \
   miscellaneous/substruct.m \
   miscellaneous/swapbytes.m \
diff -r 6f640ed5bb93 -r 9ce1f0606e28 scripts/miscellaneous/rmappdata.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/rmappdata.m	Thu Jul 15 18:00:28 2010 -0400
@@ -0,0 +1,44 @@
+## Copyright (C) 2010 Ben Abbott
+## 
+## This program 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 of the License, or
+## (at your option) any later version.
+## 
+## This program 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, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} rmappdata (@var{h}, @var{name})
+## Deletes the named application data for the object(s) with
+## handle(s) @var{h}.
+## @end deftypefn
+
+## Author: Ben Abbott <bpabbott@mac.com>
+## Created: 2010-07-15
+
+function rmappdata (h, varargin)
+
+  if (! (all (ishandle (h)) && iscellstr (varargin)))
+    error ("rmappdata: invalid input.")
+  endif
+
+  for nh = 1:numel(h)
+    appdata = get (h(nh), "__appdata__");
+    appdata = rmfield (appdata, varargin);
+    set (h(nh), "__appdata__", appdata);
+  endfor
+
+endfunction
+
+%!test
+%! setappdata (0, "hello", "world")
+%! rmappdata (0, "hello")
+%! assert (isappdata (0, "hello"), false)
+
diff -r 6f640ed5bb93 -r 9ce1f0606e28 scripts/miscellaneous/setappdata.m
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/setappdata.m	Thu Jul 15 18:00:28 2010 -0400
@@ -0,0 +1,59 @@
+## Copyright (C) 2010 Ben Abbott
+## 
+## This program 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 of the License, or
+## (at your option) any later version.
+## 
+## This program 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, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} setappdata (@var{h}, @var{name}, @var{value})
+## Sets the named application data to @var{value} for the object(s) with
+## handle(s) @var{h}. If the application data with the specified name does
+## not exist, it is created.
+## @end deftypefn
+
+## Author: Ben Abbott <bpabbott@mac.com>
+## Created: 2010-07-15
+
+function setappdata (h, varargin)
+
+  if (! (all (ishandle (h)) && mod (numel (varargin), 2) == 0))
+    error ("setappdata: invalid input.")
+  endif
+
+  for nh = 1:numel(h)
+    if (! isfield (get (h(nh)), "__appdata__"))
+      addproperty ("__appdata__", h(nh), "any", struct ());
+    end
+    appdata = get (h(nh), "__appdata__");
+    for narg = 1:2:numel(varargin)
+      if (iscellstr (varargin{narg}))
+        ## Handle cell arrays like set() does.
+        set (h(nh), "__appdata__", appdata);
+        setappdata (h(nh), vertcat (varargin{narg}', varargin{narg+1}'){:});
+        appdata = get (h(nh), "__appdata__");
+      elseif (ischar (varargin{narg}))
+        appdata.(varargin{narg}) = varargin{narg+1};
+      else
+        error ("setappdata: invalid input.")
+      endif
+    endfor
+    set (h(nh), "__appdata__", appdata);
+  endfor
+
+endfunction
+
+%!test
+%! setappdata (0, "hello", "world")
+%! assert (isappdata (0, "hello"), true)
+%!assert (getappdata (0, "hello"), "world")
+
