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" |
807
|
36 #include "utils.h" |
|
37 |
4358
|
38 DEFUN (char, args, , |
|
39 "-*- texinfo -*-\n\ |
|
40 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
|
41 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ |
|
42 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
|
43 Create a string array from a numeric matrix, cell array, or list of\n\ |
|
44 \n\ |
|
45 If the argument is a numeric matrix, each element of the matrix is\n\ |
|
46 converted to the corresponding ASCII character. For example,\n\ |
|
47 \n\ |
|
48 @example\n\ |
|
49 @group\n\ |
|
50 setstr ([97, 98, 99])\n\ |
|
51 @result{} \"abc\"\n\ |
|
52 @end group\n\ |
|
53 @end example\n\ |
|
54 \n\ |
|
55 If the argument is a cell array of strings, the result is a string array\n\ |
|
56 with each element corresponding to one element of the cell array.\n\ |
|
57 \n\ |
|
58 For multiple string arguments, the result is a string array with each\n\ |
|
59 element corresponding to the arguments.\n\ |
|
60 \n\ |
|
61 The returned values are padded with blanks as needed to make each row\n\ |
|
62 of the string array have the same length.\n\ |
|
63 @end deftypefn") |
|
64 { |
|
65 octave_value retval; |
|
66 |
|
67 int nargin = args.length (); |
|
68 |
|
69 if (nargin == 1) |
|
70 retval = args(0).convert_to_str (true); |
|
71 else if (nargin > 1) |
|
72 { |
|
73 int n_elts = 0; |
|
74 |
|
75 int max_len = 0; |
|
76 |
|
77 for (int i = 0; i < nargin; i++) |
|
78 { |
|
79 string_vector s = args(i).all_strings (); |
|
80 |
|
81 if (error_state) |
|
82 { |
|
83 error ("char: expecting arguments to be strings"); |
|
84 return retval; |
|
85 } |
|
86 |
|
87 n_elts += s.length (); |
|
88 |
|
89 int s_max_len = s.max_length (); |
|
90 |
|
91 if (s_max_len > max_len) |
|
92 max_len = s_max_len; |
|
93 } |
|
94 |
|
95 string_vector result (n_elts); |
|
96 |
|
97 int k = 0; |
|
98 |
|
99 for (int i = 0; i < nargin; i++) |
|
100 { |
|
101 string_vector s = args(i).all_strings (); |
|
102 |
|
103 int n = s.length (); |
|
104 |
|
105 for (int j = 0; j < n; j++) |
|
106 { |
|
107 std::string t = s[j]; |
|
108 int t_len = t.length (); |
|
109 |
|
110 if (max_len > t_len) |
|
111 t += std::string (max_len - t_len, ' '); |
|
112 |
|
113 result[k++] = t; |
|
114 } |
|
115 } |
|
116 |
|
117 retval = result; |
|
118 } |
|
119 else |
|
120 print_usage ("char"); |
|
121 |
|
122 return retval; |
|
123 } |
|
124 |
1957
|
125 DEFUN (isstr, args, , |
3361
|
126 "-*- texinfo -*-\n\ |
|
127 @deftypefn {Built-in Function} {} isstr (@var{a})\n\ |
|
128 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ |
|
129 @end deftypefn") |
807
|
130 { |
4233
|
131 octave_value retval; |
807
|
132 |
|
133 int nargin = args.length (); |
|
134 |
|
135 if (nargin == 1 && args(0).is_defined ()) |
4233
|
136 retval = args(0).is_string (); |
807
|
137 else |
|
138 print_usage ("isstr"); |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
|
143 /* |
|
144 ;;; Local Variables: *** |
|
145 ;;; mode: C++ *** |
|
146 ;;; End: *** |
|
147 */ |