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) |
5280
|
71 retval = args(0).convert_to_str (true, true, '\''); |
4358
|
72 else if (nargin > 1) |
|
73 { |
|
74 int n_elts = 0; |
|
75 |
|
76 int max_len = 0; |
|
77 |
|
78 for (int i = 0; i < nargin; i++) |
|
79 { |
4457
|
80 string_vector s = args(i).all_strings (false, true); |
4358
|
81 |
|
82 if (error_state) |
|
83 { |
4457
|
84 error ("char: unable to convert some args to strings"); |
4358
|
85 return retval; |
|
86 } |
|
87 |
|
88 n_elts += s.length (); |
|
89 |
|
90 int s_max_len = s.max_length (); |
|
91 |
|
92 if (s_max_len > max_len) |
|
93 max_len = s_max_len; |
|
94 } |
|
95 |
|
96 string_vector result (n_elts); |
|
97 |
|
98 int k = 0; |
|
99 |
|
100 for (int i = 0; i < nargin; i++) |
|
101 { |
4457
|
102 string_vector s = args(i).all_strings (false, true); |
4358
|
103 |
|
104 int n = s.length (); |
|
105 |
|
106 for (int j = 0; j < n; j++) |
|
107 { |
|
108 std::string t = s[j]; |
|
109 int t_len = t.length (); |
|
110 |
|
111 if (max_len > t_len) |
|
112 t += std::string (max_len - t_len, ' '); |
|
113 |
|
114 result[k++] = t; |
|
115 } |
|
116 } |
|
117 |
5280
|
118 retval = octave_value (result, '\''); |
4358
|
119 } |
|
120 else |
|
121 print_usage ("char"); |
|
122 |
|
123 return retval; |
|
124 } |
|
125 |
4535
|
126 DEFUN (ischar, args, , |
3361
|
127 "-*- texinfo -*-\n\ |
4535
|
128 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
3361
|
129 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
|
130 @end deftypefn") |
807
|
131 { |
4233
|
132 octave_value retval; |
807
|
133 |
|
134 int nargin = args.length (); |
|
135 |
|
136 if (nargin == 1 && args(0).is_defined ()) |
4233
|
137 retval = args(0).is_string (); |
807
|
138 else |
4535
|
139 print_usage ("ischar"); |
807
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
|
144 /* |
|
145 ;;; Local Variables: *** |
|
146 ;;; mode: C++ *** |
|
147 ;;; End: *** |
|
148 */ |