octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pkg netlist


From: David Bateman
Subject: Re: pkg netlist
Date: Mon, 05 Mar 2007 23:04:43 +0100
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

John W. Eaton wrote:
> On  5-Mar-2007, David Bateman wrote:
> 
> | I'm not sure that
> | 
> | fid = fopen (file, "rb")
> | buf = fread(fid)
> | fclose(fid)
> | md5 = md5sum(char(buf)')
> | 
> | won't be as fast..
> 
> Won't this be slow for very large files if the system is forced to
> swap?
> 
> If we do handle files and character strings, then please use an option
> or separate function names (smd5sum/fmd5sum ?).  I would not want to
> see this function deciding which operation to perform based on the
> value of the string that is passed.
> 
> jwe
> 

Well MD5 only needs as little as 64bytes of a file in memory at any
time, so yes for very large files the above might swap whereas md5sum
system function won't. It's fairly easy to process a file rather than a
string, but as you say I'd not like md5sum choosing between a file or a
string based on context. I don't really like an option either, but
prefer it to two different function names. I'd therefore propose, a
trailing option like in the attached patch, where the default is to
treat the first argument as a filename (since thats what Bob prefers),
and as a string if the second argument evaluates to true. Consider the
attached patch. Given what you said above I applied it..

Regards
David

Index: liboctave/oct-md5.cc
===================================================================
RCS file: /cvs/octave/liboctave/oct-md5.cc,v
retrieving revision 1.1
diff -c -r1.1 oct-md5.cc
*** liboctave/oct-md5.cc        1 Mar 2007 17:23:39 -0000       1.1
--- liboctave/oct-md5.cc        5 Mar 2007 21:56:15 -0000
***************
*** 28,33 ****
--- 28,34 ----
  #include "config.h"
  #endif
  
+ #include "lo-error.h"
  #include "oct-md5.h"
  #include "md5.h"
   
***************
*** 49,54 ****
--- 50,91 ----
    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++ ***
Index: liboctave/oct-md5.h
===================================================================
RCS file: /cvs/octave/liboctave/oct-md5.h,v
retrieving revision 1.1
diff -c -r1.1 oct-md5.h
*** liboctave/oct-md5.h 1 Mar 2007 17:23:39 -0000       1.1
--- liboctave/oct-md5.h 5 Mar 2007 21:56:15 -0000
***************
*** 22,27 ****
--- 22,29 ----
  */
  
  extern std::string oct_md5 (const std::string str);
+ 
+ extern std::string oct_md5_file (const std::string file);
          
  /*
  ;;; Local Variables: ***
Index: src/DLD-FUNCTIONS/md5sum.cc
===================================================================
RCS file: /cvs/octave/src/DLD-FUNCTIONS/md5sum.cc,v
retrieving revision 1.1
diff -c -r1.1 md5sum.cc
*** src/DLD-FUNCTIONS/md5sum.cc 1 Mar 2007 17:23:40 -0000       1.1
--- src/DLD-FUNCTIONS/md5sum.cc 5 Mar 2007 21:56:17 -0000
***************
*** 30,54 ****
  #endif
  
  #include "defun-dld.h"
  #include "oct-md5.h"
  
! DEFUN_DLD (md5sum, args, nargout,
     "-*- texinfo -*-\n\
! @deftypefn {Loadable Function} {} md5sum (@var{str})\n\
! Calculates the MD5 sum of the string @var{str}.\n\
  @end deftypefn")
  {
    octave_value retval;
    int nargin = args.length ();
  
!   if (nargin != 1)
      print_usage();
    else
      {
        std::string str = args(0).string_value();
  
        if (!error_state)
!       retval = oct_md5 (str);
      }
  
    return retval;
--- 30,88 ----
  #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, ,
     "-*- texinfo -*-\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 && 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)
!       {
!         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;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]