GNU Octave  3.8.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-2013 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 "gripes.h"
35 #include "load-path.h"
36 #include "oct-env.h"
37 #include "oct-md5.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}. If the second parameter\n\
44 @var{opt} exists and is true, then calculate the MD5 sum of the\n\
45 string @var{str}.\n\
46 @end deftypefn")
47 {
48  octave_value retval;
49  int nargin = args.length ();
50 
51  if (nargin != 1 && nargin != 2)
52  print_usage ();
53  else
54  {
55  bool have_str = false;
56  std::string str = args(0).string_value ();
57 
58  if (nargin == 2)
59  have_str = args(1).bool_value ();
60 
61  if (!error_state)
62  {
63  if (have_str)
64  retval = oct_md5 (str);
65  else
66  {
67  file_stat fs (str);
68 
69  if (! fs.exists ())
70  {
71  std::string tmp
73 
74  if (! tmp.empty ())
75  {
76  warning_with_id ("Octave:md5sum-file-in-path",
77  "md5sum: file found in load path");
78  str = tmp;
79  }
80  }
81 
82  retval = oct_md5_file (str);
83  }
84  }
85  }
86 
87  return retval;
88 }
89 
90 /*
91 %!assert (md5sum ("abc\0", true), "147a664a2ca9410911e61986d3f0d52a");
92 
93 %!test
94 %! tfile = tmpnam ();
95 %! fid = fopen (tfile, "wb");
96 %! fwrite (fid, "abc\0");
97 %! fclose (fid);
98 %! assert (md5sum (tfile), "147a664a2ca9410911e61986d3f0d52a");
99 %! unlink (tfile);
100 */
101