GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
md5sum.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2015 David Bateman
4 
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <string>
29 #include <vector>
30 
31 #include "defun.h"
32 #include "file-stat.h"
33 #include "file-ops.h"
34 #include "load-path.h"
35 #include "oct-env.h"
36 #include "oct-md5.h"
37 #include "utils.h"
38 
39 DEFUN (md5sum, args, ,
40  "-*- texinfo -*-\n\
41 @deftypefn {Built-in Function} {} md5sum (@var{file})\n\
42 @deftypefnx {Built-in Function} {} md5sum (@var{str}, @var{opt})\n\
43 Calculate the MD5 sum of the file @var{file}.\n\
44 \n\
45 If the second parameter @var{opt} exists and is true, then calculate the MD5\n\
46 sum of the string @var{str}.\n\
47 @end deftypefn")
48 {
49  octave_value retval;
50  int nargin = args.length ();
51 
52  if (nargin != 1 && nargin != 2)
53  print_usage ();
54  else
55  {
56  bool have_str = false;
57  std::string str = args(0).string_value ();
58 
59  if (nargin == 2)
60  have_str = args(1).bool_value ();
61 
62  if (!error_state)
63  {
64  if (have_str)
65  retval = oct_md5 (str);
66  else
67  {
68  std::string fname = file_ops::tilde_expand (str);
69 
70  fname = find_data_file_in_load_path ("md5sum", fname);
71 
72  retval = oct_md5_file (fname);
73  }
74  }
75  }
76 
77  return retval;
78 }
79 
80 /*
81 %!assert (md5sum ("abc\0", true), "147a664a2ca9410911e61986d3f0d52a");
82 
83 %!test
84 %! tfile = tempname ();
85 %! fid = fopen (tfile, "wb");
86 %! fwrite (fid, "abc\0");
87 %! fclose (fid);
88 %! assert (md5sum (tfile), "147a664a2ca9410911e61986d3f0d52a");
89 %! unlink (tfile);
90 
91 %!error md5sum ();
92 */
93 
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)
Definition: utils.cc:463
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
static std::string tilde_expand(const std::string &)
Definition: file-ops.cc:286
std::string oct_md5(const std::string str)
Definition: oct-md5.cc:51
int error_state
Definition: error.cc:101
octave_idx_type length(void) const
Definition: ov.cc:1525
std::string oct_md5_file(const std::string file)
Definition: oct-md5.cc:61