807
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
807
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
807
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
807
|
25 #endif |
|
26 |
1355
|
27 #include <cctype> |
807
|
28 |
|
29 #include "dMatrix.h" |
|
30 |
1355
|
31 #include "defun.h" |
807
|
32 #include "error.h" |
|
33 #include "gripes.h" |
2366
|
34 #include "ov.h" |
1355
|
35 #include "oct-obj.h" |
4457
|
36 #include "unwind-prot.h" |
807
|
37 #include "utils.h" |
|
38 |
4358
|
39 DEFUN (char, args, , |
|
40 "-*- texinfo -*-\n\ |
|
41 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
|
42 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ |
|
43 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
|
44 Create a string array from a numeric matrix, cell array, or list of\n\ |
|
45 \n\ |
|
46 If the argument is a numeric matrix, each element of the matrix is\n\ |
|
47 converted to the corresponding ASCII character. For example,\n\ |
|
48 \n\ |
|
49 @example\n\ |
|
50 @group\n\ |
4734
|
51 char ([97, 98, 99])\n\ |
4358
|
52 @result{} \"abc\"\n\ |
|
53 @end group\n\ |
|
54 @end example\n\ |
|
55 \n\ |
|
56 If the argument is a cell array of strings, the result is a string array\n\ |
|
57 with each element corresponding to one element of the cell array.\n\ |
|
58 \n\ |
|
59 For multiple string arguments, the result is a string array with each\n\ |
|
60 element corresponding to the arguments.\n\ |
|
61 \n\ |
|
62 The returned values are padded with blanks as needed to make each row\n\ |
|
63 of the string array have the same length.\n\ |
|
64 @end deftypefn") |
|
65 { |
|
66 octave_value retval; |
|
67 |
|
68 int nargin = args.length (); |
|
69 |
|
70 if (nargin == 1) |
5281
|
71 retval = args(0).convert_to_str (true, true, |
|
72 args(0).is_dq_string () ? '"' : '\''); |
4358
|
73 else if (nargin > 1) |
|
74 { |
|
75 int n_elts = 0; |
|
76 |
|
77 int max_len = 0; |
|
78 |
|
79 for (int i = 0; i < nargin; i++) |
|
80 { |
4457
|
81 string_vector s = args(i).all_strings (false, true); |
4358
|
82 |
|
83 if (error_state) |
|
84 { |
4457
|
85 error ("char: unable to convert some args to strings"); |
4358
|
86 return retval; |
|
87 } |
|
88 |
|
89 n_elts += s.length (); |
|
90 |
|
91 int s_max_len = s.max_length (); |
|
92 |
|
93 if (s_max_len > max_len) |
|
94 max_len = s_max_len; |
|
95 } |
|
96 |
|
97 string_vector result (n_elts); |
|
98 |
|
99 int k = 0; |
|
100 |
|
101 for (int i = 0; i < nargin; i++) |
|
102 { |
4457
|
103 string_vector s = args(i).all_strings (false, true); |
4358
|
104 |
|
105 int n = s.length (); |
|
106 |
|
107 for (int j = 0; j < n; j++) |
|
108 { |
|
109 std::string t = s[j]; |
|
110 int t_len = t.length (); |
|
111 |
|
112 if (max_len > t_len) |
|
113 t += std::string (max_len - t_len, ' '); |
|
114 |
|
115 result[k++] = t; |
|
116 } |
|
117 } |
|
118 |
5280
|
119 retval = octave_value (result, '\''); |
4358
|
120 } |
|
121 else |
|
122 print_usage ("char"); |
|
123 |
|
124 return retval; |
|
125 } |
|
126 |
4535
|
127 DEFUN (ischar, args, , |
3361
|
128 "-*- texinfo -*-\n\ |
4535
|
129 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
3361
|
130 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
|
131 @end deftypefn") |
807
|
132 { |
4233
|
133 octave_value retval; |
807
|
134 |
|
135 int nargin = args.length (); |
|
136 |
|
137 if (nargin == 1 && args(0).is_defined ()) |
4233
|
138 retval = args(0).is_string (); |
807
|
139 else |
4535
|
140 print_usage ("ischar"); |
807
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 /* |
|
146 ;;; Local Variables: *** |
|
147 ;;; mode: C++ *** |
|
148 ;;; End: *** |
|
149 */ |