You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/03/01 03:14:20 UTC

[lucy-commits] svn commit: r1075619 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs lib/Clownfish/Util.pm src/CFCUtil.c src/CFCUtil.h

Author: marvin
Date: Tue Mar  1 02:14:19 2011
New Revision: 1075619

URL: http://svn.apache.org/viewvc?rev=1075619&view=rev
Log:
Replace Perl slurp_file() with copied C version.  Port write_if_changed() 
to C.

Modified:
    incubator/lucy/trunk/clownfish/lib/Clownfish.xs
    incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm
    incubator/lucy/trunk/clownfish/src/CFCUtil.c
    incubator/lucy/trunk/clownfish/src/CFCUtil.h

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1075619&r1=1075618&r2=1075619&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Tue Mar  1 02:14:19 2011
@@ -1538,6 +1538,25 @@ CODE:
     SvCUR_set(RETVAL, strlen(ptr));
 OUTPUT: RETVAL
 
+SV*
+slurp_file(path)
+    const char *path;
+CODE:
+    size_t len;
+    char *contents = CFCUtil_slurp_file(path, &len);
+    RETVAL = newSVpvn(contents, len);
+    FREEMEM(contents);
+OUTPUT: RETVAL
+
+void
+write_if_changed(path, content_sv)
+    const char *path;
+    SV *content_sv;
+PPCODE:
+    STRLEN len;
+    char *content = SvPV(content_sv, len);
+    CFCUtil_write_if_changed(path, content, len);
+
 
 MODULE = Clownfish   PACKAGE = Clownfish::Variable
 

Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm?rev=1075619&r1=1075618&r2=1075619&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm Tue Mar  1 02:14:19 2011
@@ -33,13 +33,6 @@ our @EXPORT_OK = qw(
     trim_whitespace
 );
 
-sub slurp_file {
-    my $path = shift;
-    open( my $fh, '<', $path ) or confess("Can't open '$path': $!");
-    local $/;
-    return <$fh>;
-}
-
 sub current {
     my ( $orig, $dest ) = @_;
     my $bubble_time = time;
@@ -102,20 +95,6 @@ sub a_isa_b {
     return $thing->isa($class);
 }
 
-sub write_if_changed {
-    my ( $path, $content ) = @_;
-    my $write = 1;
-    if ( -e $path ) {
-        open( my $fh, '<', $path ) or confess "Can't open '$path': $!";
-        my $orig = do { local $/; <$fh> };
-        return if $orig eq $content;
-    }
-    unlink $path;
-    sysopen( my $fh, $path, O_CREAT | O_EXCL | O_WRONLY )
-        or confess "Can't open '$path': $!";
-    print $fh $content;
-}
-
 1;
 
 __END__

Modified: incubator/lucy/trunk/clownfish/src/CFCUtil.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.c?rev=1075619&r1=1075618&r2=1075619&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.c Tue Mar  1 02:14:19 2011
@@ -21,6 +21,11 @@
 #include "perl.h"
 #include "XSUB.h"
 
+#ifndef true
+    #define true 1
+    #define false 0
+#endif
+
 #define CHAZ_USE_SHORT_NAMES
 
 #include "CFCUtil.h"
@@ -151,14 +156,13 @@ CFCUtil_wrapped_free(void *ptr)
 }
 
 void
-CFCUtil_write_file(const char *filename, const char *content)
+CFCUtil_write_file(const char *filename, const char *content, size_t len)
 {
     FILE *fh = fopen(filename, "w+");
-    size_t content_len = strlen(content);
     if (fh == NULL) {
         CFCUtil_die("Couldn't open '%s': %s", filename, strerror(errno));
     }
-    fwrite(content, sizeof(char), content_len, fh);
+    fwrite(content, sizeof(char), len, fh);
     if (fclose(fh)) {
         CFCUtil_die("Error when closing '%s': %s", filename, strerror(errno));
     }
@@ -209,6 +213,28 @@ CFCUtil_slurp_file(const char *file_path
     return contents;
 }
 
+void
+CFCUtil_write_if_changed(const char *path, const char *content, size_t len)
+{
+    FILE *f = fopen(path, "r");
+    if (f) { // Does file exist?
+        if (fclose(f)) { 
+            CFCUtil_die("Error closing file '%s': %s", path, strerror(errno));
+        }
+        size_t existing_len;
+        char *existing = CFCUtil_slurp_file(path, &existing_len);
+        int changed = true;
+        if (existing_len == len && strcmp(content, existing) == 0) {
+            changed = false;
+        }
+        FREEMEM(existing);
+        if (changed == false) {
+            return;
+        }
+    }
+    CFCUtil_write_file(path, content, len);
+}
+
 long 
 CFCUtil_flength(void *file) 
 {

Modified: incubator/lucy/trunk/clownfish/src/CFCUtil.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.h?rev=1075619&r1=1075618&r2=1075619&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.h Tue Mar  1 02:14:19 2011
@@ -83,7 +83,10 @@ CFCUtil_wrapped_free(void *ptr);
  * an error occurs.
  */
 void
-CFCUtil_write_file(const char *filename, const char *content);
+CFCUtil_write_file(const char *filename, const char *content, size_t len);
+
+void
+CFCUtil_write_if_changed(const char *path, const char *content, size_t len);
 
 /* Read an entire file into memory.
  */