# HG changeset patch # User Rik # Date 1343616551 25200 # Node ID 70a1b50bcb458d77cd391722ce9c1e9504f10d24 # Parent d43fec5684dd59f95bc142dae210017da56c9443 maint: Use some Octave coding conventions for base64_encode, base64_decode. * data.cc (base64_encode): Remove unused, but declared, nargout variable. Reduce all lines to less than 80 characters. Trim whitespace at end of line. Rephrase docstring to use @strong macro when warning about accepted inputs. Add a few more input validation tests. * data.cc (base64_decode): Remove unused, but declared, nargout variable. Reduce all lines to less than 80 characters. Trim whitespace at end of line. Rephrase docstring to use @deftypefnx macro for second form of calling function. Move variable declarations to be as close to position of use as possible. Add a few more input validation tests. diff --git a/src/data.cc b/src/data.cc --- a/src/data.cc +++ b/src/data.cc @@ -7233,84 +7233,93 @@ return retval; } -DEFUN (base64_encode, args, nargout, "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{s} =} base64_encode (@var{x})\n\ -Encode a double matrix or array @var{x} into the base64 format string @var{s}.\n\ -Encoding different numeric types is currently not supported, variables of such types \ -will be converted to double before encoding.\n\ +DEFUN (base64_encode, args, , "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {@var{s} =} base64_encode (@var{x})\n\ +Encode a double matrix or array @var{x} into the base64 format string\n\ +@var{s}.\n\ +\n\ +@strong{Warning:} Encoding different numeric types, such as single or\n\ +integer, is not currently supported. Any non-double input will be converted\n\ +to type double before encoding.\n\ @seealso{base64_decode}\n\ @end deftypefn") { octave_value_list retval; - + int nargin = args.length (); + if (nargin != 1) print_usage (); else { const Array in = args(0).array_value (); if (! error_state) - { + { const char* inc = reinterpret_cast (in.data ()); - size_t inlen = in.numel () * sizeof (double) / sizeof (char); + size_t inlen = in.numel () * sizeof (double) / sizeof (char); char* out; - size_t outlen = base64_encode_alloc (inc, inlen, &out); - + if (! out && outlen == 0 && inlen != 0) error ("base64_encode: input array too large"); else if (! out) error ("base64_encode: memory allocation error"); else - retval(0) = octave_value (out); + retval(0) = octave_value (out); } } - return retval; + + return retval; } /* - %!assert (base64_encode (single (pi)), base64_encode (double (single (pi)))); - %!assert (base64_encode (uint8 (pi)), base64_encode (double (uint8 (pi)))); - - %!error (base64_encode ("A string")); +%!assert (base64_encode (single (pi)), "AAAAYPshCUA=") +%!assert (base64_encode (uint8 (pi)), base64_encode (double (uint8 (pi)))) + +%!error base64_encode () +%!error base64_encode (1,2) +%!error base64_encode ("A string") */ -DEFUN (base64_decode, args, nargout, "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {@var{x} =} base64_decode (@var{s}, @var{dims})\n\ -Decode the double matrix or array @var{x} from the base64 format string @var{s}.\n\ -The optional input parameter @var{dims} should be a vector containing the dimensions \ -of the decoded array.\n\ +DEFUN (base64_decode, args, , "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {@var{x} =} base64_decode (@var{s})\n\ +@deftypefnx {Built-in Function} {@var{x} =} base64_decode (@var{s}, @var{dims})\n\ +Decode the double matrix or array @var{x} from the base64 format string\n\ +@var{s}. The optional input parameter @var{dims} should be a vector\n\ +containing the dimensions of the decoded array.\n\ @seealso{base64_encode}\n\ @end deftypefn") { octave_value_list retval; - dim_vector new_dims; - Array res; int nargin = args.length (); + if (nargin < 1 || nargin > 2) print_usage (); - else + else { + dim_vector new_dims; + Array res; + if (nargin > 1) { - const Array new_size = args(1).octave_idx_type_vector_value (); + const Array new_size = + args(1).octave_idx_type_vector_value (); if (! error_state) { new_dims = dim_vector::alloc (new_size.length ()); for (octave_idx_type i = 0; i < new_size.length (); i++) - new_dims(i) = new_size(i); + new_dims(i) = new_size(i); } - } + } const std::string in = args(0).string_value (); if (! error_state) - { + { const char *inc = &(in[0]); char *out; - size_t inlen = in.length (), - outlen; + size_t inlen = in.length (), outlen; bool ok = base64_decode_alloc (inc, inlen, &out, &outlen); @@ -7324,7 +7333,8 @@ error ("base64_decode: incorrect input size"); else { - octave_idx_type l = (outlen * sizeof (char)) / sizeof (double); + octave_idx_type l; + l = (outlen * sizeof (char)) / sizeof (double); res.resize1 (l); double *dout = reinterpret_cast (out); std::copy (dout, dout + l, res.fortran_vec ()); @@ -7337,20 +7347,24 @@ } } } + return retval; } /* - %!assert (base64_decode (base64_encode (pi))); - %!test - %! in = randn (10); - %! outv = base64_decode (base64_encode (in)); - %! outm = base64_decode (base64_encode (in), size (in)); - %! assert (outv, in(:).'); - %! assert (outm, in); - - %!error (base64_decode (1, "this is not a valid set of dimensions")) - %!error (base64_decode (1)) - %!error (base64_decode ("AQ=")) - %!error (base64_decode ("AQ==")) +%!assert (base64_decode (base64_encode (pi)), pi) +%! +%!test +%! in = randn (10); +%! outv = base64_decode (base64_encode (in)); +%! outm = base64_decode (base64_encode (in), size (in)); +%! assert (outv, in(:).'); +%! assert (outm, in); + +%!error base64_decode () +%!error base64_decode (1,2,3) +%!error base64_decode (1, "this is not a valid set of dimensions") +%!error base64_decode (1) +%!error base64_decode ("AQ=") +%!error base64_decode ("AQ==") */