comparison src/file-io.cc @ 13194:3e1871badab9

allow sscanf to accept character arrays with more than one row * file-io.cc (get_sscanf_data): New function. Flatten character arrays before extracting character data. (Fsscanf): Use it. * test_io.m: New sscanf test.
author John W. Eaton <jwe@octave.org>
date Mon, 19 Sep 2011 16:45:31 -0400
parents c91bd3f10bec
children fba2cc36b762
comparison
equal deleted inserted replaced
13193:a00ff5cedb9b 13194:3e1871badab9
1172 } 1172 }
1173 1173
1174 return retval; 1174 return retval;
1175 } 1175 }
1176 1176
1177 static std::string
1178 get_sscanf_data (const octave_value& val)
1179 {
1180 std::string retval;
1181
1182 if (val.is_string ())
1183 {
1184 octave_value tmp = val.reshape (dim_vector (1, val.numel ()));
1185
1186 retval = tmp.string_value ();
1187 }
1188 else
1189 ::error ("sscanf: argument STRING must be a string");
1190
1191 return retval;
1192 }
1193
1177 DEFUN (sscanf, args, , 1194 DEFUN (sscanf, args, ,
1178 "-*- texinfo -*-\n\ 1195 "-*- texinfo -*-\n\
1179 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ 1196 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\
1180 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] =} sscanf (@var{string}, @var{template}, \"C\")\n\ 1197 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] =} sscanf (@var{string}, @var{template}, \"C\")\n\
1181 This is like @code{fscanf}, except that the characters are taken from the\n\ 1198 This is like @code{fscanf}, except that the characters are taken from the\n\
1192 1209
1193 int nargin = args.length (); 1210 int nargin = args.length ();
1194 1211
1195 if (nargin == 3 && args(2).is_string ()) 1212 if (nargin == 3 && args(2).is_string ())
1196 { 1213 {
1197 if (args(0).is_string ()) 1214 std::string data = get_sscanf_data (args(0));
1198 { 1215
1199 std::string data = args(0).string_value (); 1216 if (! error_state)
1200 1217 {
1201 octave_stream os = octave_istrstream::create (data); 1218 octave_stream os = octave_istrstream::create (data);
1202 1219
1203 if (os.is_valid ()) 1220 if (os.is_valid ())
1204 { 1221 {
1205 if (args(1).is_string ()) 1222 if (args(1).is_string ())
1221 retval(3) = -1.0; 1238 retval(3) = -1.0;
1222 retval(2) = "unknown error"; 1239 retval(2) = "unknown error";
1223 retval(1) = 0.0; 1240 retval(1) = 0.0;
1224 retval(0) = Matrix (); 1241 retval(0) = Matrix ();
1225 1242
1226 if (args(0).is_string ()) 1243 std::string data = get_sscanf_data (args(0));
1244
1245 if (! error_state)
1227 { 1246 {
1228 std::string data = args(0).string_value ();
1229
1230 octave_stream os = octave_istrstream::create (data); 1247 octave_stream os = octave_istrstream::create (data);
1231 1248
1232 if (os.is_valid ()) 1249 if (os.is_valid ())
1233 { 1250 {
1234 if (args(1).is_string ()) 1251 if (args(1).is_string ())
1261 } 1278 }
1262 else 1279 else
1263 ::error ("%s: unable to create temporary input buffer", 1280 ::error ("%s: unable to create temporary input buffer",
1264 who.c_str ()); 1281 who.c_str ());
1265 } 1282 }
1266 else
1267 ::error ("%s: argument STRING must be a string", who.c_str ());
1268 } 1283 }
1269 else 1284 else
1270 print_usage (); 1285 print_usage ();
1271 } 1286 }
1272 1287