1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {[@var{d}, @var{err}] = } tbl_delim (@var{d})
18 ## Return the delimiter for tblread or tblwrite.
20 ## The delimeter, @var{d} may be any single character or
22 ## @item "space" " " (default)
29 ## @var{err} will be empty if there is no error, and @var{d} will be NaN
30 ## if there is an error. You MUST check the value of @var{err}.
31 ## @seealso{tblread, tblwrite}
34 function [d, err] = tbl_delim (d)
42 ## Format the delimiter
44 ## allow for escape characters
47 ## allow the word forms
53 if ! ismember (d, fieldnames (s))
54 err = ["tblread: delimiter must be either a single " ...
55 "character or one of\n" ...
56 sprintf("%s, ", fieldnames (s){:})(1:end-2)];
63 err = "delimiter must be a character";
67 err = "the delimiter may not be empty";
76 %! [d err] = tbl_delim (" ");
81 %! [d err] = tbl_delim ("space");
85 %! [d err] = tbl_delim ("tab");
86 %! assert (d, sprintf ("\t"));
89 %! [d err] = tbl_delim ("comma");
93 %! [d err] = tbl_delim ("semi");
97 %! [d err] = tbl_delim ("bar");
100 ## An arbitrary character
102 %! [d err] = tbl_delim ("x");
105 ## An arbitrary escape string
107 %! [d err] = tbl_delim ('\r');
108 %! assert (d, sprintf ('\r'))
112 %! [d err] = tbl_delim ("bars");
113 %! assert (isnan (d));
114 %! assert (! isempty (err));
116 %! [d err] = tbl_delim ("");
117 %! assert (isnan (d));
118 %! assert (! isempty (err));
120 %! [d err] = tbl_delim (5);
121 %! assert (isnan (d));
122 %! assert (! isempty (err));
124 %! [d err] = tbl_delim ({"."});
125 %! assert (isnan (d));
126 %! assert (! isempty (err));