Mercurial > hg > octave-lyh
changeset 6383:65e9cf5c7718
[project @ 2007-03-05 22:03:31 by dbateman]
author | dbateman |
---|---|
date | Mon, 05 Mar 2007 22:03:31 +0000 |
parents | f74e71ef6612 |
children | c2eb95ca0e2b |
files | liboctave/ChangeLog liboctave/oct-md5.cc liboctave/oct-md5.h src/ChangeLog src/DLD-FUNCTIONS/md5sum.cc |
diffstat | 5 files changed, 87 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +2007-03-05 David Bateman <dbateman@free.fr> + + * oct-md5.c (oct_md5_file (const std::string&)): New function. + * oct-md5.h (oct_md5_file (const std::string&)): Declare it. + 2007-03-02 John W. Eaton <jwe@octave.org> * str-vec.h (string_vector::empty): Return bool, not int.
--- a/liboctave/oct-md5.cc +++ b/liboctave/oct-md5.cc @@ -28,6 +28,7 @@ #include "config.h" #endif +#include "lo-error.h" #include "oct-md5.h" #include "md5.h" @@ -49,6 +50,42 @@ return std::string (tmp); } +std::string +oct_md5_file (const std::string file) +{ + FILE *ifile = fopen (file.c_str (), "rb"); + + if (! ifile) + { + (*current_liboctave_error_handler) ("unable to open file `%s' for writing", + file.c_str()); + return std::string(); + } + else + { + md5_state_t state; + size_t nel; + + OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); + OCTAVE_LOCAL_BUFFER (md5_byte_t, buf, 1024); + + md5_init (&state); + + while ((nel = fread (buf, 1, 1024, ifile))) + md5_append (&state, buf, nel); + + fclose (ifile); + + md5_finish (&state, digest); + + OCTAVE_LOCAL_BUFFER (char, tmp, 33); + for (octave_idx_type i = 0; i < 16; i++) + sprintf (&tmp[2*i], "%02x", digest[i]); + tmp[32] = 0; + return std::string (tmp); + } +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/liboctave/oct-md5.h +++ b/liboctave/oct-md5.h @@ -22,6 +22,8 @@ */ extern std::string oct_md5 (const std::string str); + +extern std::string oct_md5_file (const std::string file); /* ;;; Local Variables: ***
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2007-03-05 David Bateman <dbateman@free.fr> + + * DLD-FUNCTIONS/md5sum.cc (Fmd5sum): Treat both files and strings. + 2007-03-05 John W. Eaton <jwe@octave.org> * DLD-FUNCTIONS/__glpk__.cc (F__glpk__): Check GLPK_PRE_4_14, not
--- a/src/DLD-FUNCTIONS/md5sum.cc +++ b/src/DLD-FUNCTIONS/md5sum.cc @@ -30,25 +30,59 @@ #endif #include "defun-dld.h" +#include "file-stat.h" +#include "file-ops.h" +#include "gripes.h" +#include "load-path.h" +#include "oct-env.h" #include "oct-md5.h" -DEFUN_DLD (md5sum, args, nargout, +DEFUN_DLD (md5sum, args, , "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {} md5sum (@var{str})\n\ -Calculates the MD5 sum of the string @var{str}.\n\ +@deftypefn {Loadable Function} {} md5sum (@var{file})\n\ +@deftypefnx {Loadable Function} {} md5sum (@var{str}, @var{opt})\n\ +Calculates the MD5 sum of the file @var{file}. If the second parameter\n\ +@var{opt} exists and is true, then calculate the MD5 sum of the\n\ +string @var{str}.\n\ @end deftypefn") { octave_value retval; int nargin = args.length (); - if (nargin != 1) + if (nargin != 1 && nargin != 2) print_usage(); else { + bool have_str = false; std::string str = args(0).string_value(); + if (nargin == 2) + have_str = args(1).bool_value(); + if (!error_state) - retval = oct_md5 (str); + { + if (have_str) + retval = oct_md5 (str); + else + { + file_stat fs (str); + + if (! fs.exists ()) + { + std::string tmp = octave_env::make_absolute + (load_path::find_file (str), octave_env::getcwd ()); + + if (! tmp.empty ()) + { + warning_with_id ("Octave:md5sum-file-in-path", + "md5sum: file found in load path"); + str = tmp; + } + } + + retval = oct_md5_file (str); + } + } } return retval;