md5sum.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 David Bateman
00004 
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include "defun-dld.h"
00032 #include "file-stat.h"
00033 #include "file-ops.h"
00034 #include "gripes.h"
00035 #include "load-path.h"
00036 #include "oct-env.h"
00037 #include "oct-md5.h"
00038 
00039 DEFUN_DLD (md5sum, args, ,
00040    "-*- texinfo -*-\n\
00041 @deftypefn  {Loadable Function} {} md5sum (@var{file})\n\
00042 @deftypefnx {Loadable Function} {} md5sum (@var{str}, @var{opt})\n\
00043 Calculate the MD5 sum of the file @var{file}.  If the second parameter\n\
00044 @var{opt} exists and is true, then calculate the MD5 sum of the\n\
00045 string @var{str}.\n\
00046 @end deftypefn")
00047 {
00048   octave_value retval;
00049   int nargin = args.length ();
00050 
00051   if (nargin != 1 && nargin != 2)
00052     print_usage();
00053   else
00054     {
00055       bool have_str = false;
00056       std::string str = args(0).string_value();
00057 
00058       if (nargin == 2)
00059         have_str = args(1).bool_value();
00060 
00061       if (!error_state)
00062         {
00063           if (have_str)
00064             retval = oct_md5 (str);
00065           else
00066             {
00067               file_stat fs (str);
00068 
00069               if (! fs.exists ())
00070                 {
00071                   std::string tmp
00072                     = octave_env::make_absolute (load_path::find_file (str));
00073 
00074                   if (! tmp.empty ())
00075                     {
00076                       warning_with_id ("Octave:md5sum-file-in-path",
00077                                        "md5sum: file found in load path");
00078                       str = tmp;
00079                     }
00080                 }
00081 
00082               retval = oct_md5_file (str);
00083             }
00084         }
00085     }
00086 
00087   return retval;
00088 }
00089 
00090 /*
00091 %!assert (md5sum ("abc\0", true), "147a664a2ca9410911e61986d3f0d52a");
00092 
00093 %!test
00094 %! tfile = tmpnam ();
00095 %! fid = fopen (tfile, "wb");
00096 %! fwrite (fid, "abc\0");
00097 %! fclose (fid);
00098 %! assert (md5sum (tfile), "147a664a2ca9410911e61986d3f0d52a");
00099 %! unlink (tfile);
00100 */
00101 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines