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/07/01 08:33:12 UTC

[lucy-commits] svn commit: r1141836 - in /incubator/lucy/branches/0.1: ./ clownfish/lib/Clownfish.xs clownfish/lib/Clownfish/Util.pm clownfish/src/CFCHierarchy.c clownfish/src/CFCUtil.c clownfish/src/CFCUtil.h clownfish/t/001-util.t

Author: marvin
Date: Fri Jul  1 06:33:11 2011
New Revision: 1141836

URL: http://svn.apache.org/viewvc?rev=1141836&view=rev
Log:
LUCY-172 Slurp files in text mode.

Replace slurp_file() with slurp_text(), which operates in text mode.  This
fixes line-ending problems with autogenerated files on Windows.

Modified:
    incubator/lucy/branches/0.1/   (props changed)
    incubator/lucy/branches/0.1/clownfish/lib/Clownfish.xs
    incubator/lucy/branches/0.1/clownfish/lib/Clownfish/Util.pm
    incubator/lucy/branches/0.1/clownfish/src/CFCHierarchy.c
    incubator/lucy/branches/0.1/clownfish/src/CFCUtil.c
    incubator/lucy/branches/0.1/clownfish/src/CFCUtil.h
    incubator/lucy/branches/0.1/clownfish/t/001-util.t

Propchange: incubator/lucy/branches/0.1/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Jul  1 06:33:11 2011
@@ -1 +1 @@
-/incubator/lucy/trunk:1126586,1134011,1134355,1134472,1134843-1134848,1134953,1134956,1139709,1139711,1139714,1140001,1140287,1140296,1140387,1140393,1140400,1140418,1140420,1140422,1140437,1140440,1140894,1140924,1140930-1140931,1140940,1140955,1140958-1140959,1140964,1141362,1141721
+/incubator/lucy/trunk:1126586,1134011,1134355,1134472,1134843-1134848,1134953,1134956,1139709,1139711,1139714,1140001,1140287,1140296,1140387,1140393,1140400,1140418,1140420,1140422,1140437,1140440,1140894,1140924,1140930-1140931,1140940,1140955,1140958-1140959,1140964,1141362,1141721,1141768

Modified: incubator/lucy/branches/0.1/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/branches/0.1/clownfish/lib/Clownfish.xs?rev=1141836&r1=1141835&r2=1141836&view=diff
==============================================================================
--- incubator/lucy/branches/0.1/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/branches/0.1/clownfish/lib/Clownfish.xs Fri Jul  1 06:33:11 2011
@@ -1556,11 +1556,11 @@ CODE:
 OUTPUT: RETVAL
 
 SV*
-slurp_file(path)
+slurp_text(path)
     const char *path;
 CODE:
     size_t len;
-    char *contents = CFCUtil_slurp_file(path, &len);
+    char *contents = CFCUtil_slurp_text(path, &len);
     RETVAL = newSVpvn(contents, len);
     FREEMEM(contents);
 OUTPUT: RETVAL

Modified: incubator/lucy/branches/0.1/clownfish/lib/Clownfish/Util.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/branches/0.1/clownfish/lib/Clownfish/Util.pm?rev=1141836&r1=1141835&r2=1141836&view=diff
==============================================================================
--- incubator/lucy/branches/0.1/clownfish/lib/Clownfish/Util.pm (original)
+++ incubator/lucy/branches/0.1/clownfish/lib/Clownfish/Util.pm Fri Jul  1 06:33:11 2011
@@ -24,7 +24,7 @@ use Carp;
 use Fcntl;
 
 our @EXPORT_OK = qw(
-    slurp_file
+    slurp_text
     current
     strip_c_comments
     verify_args
@@ -78,12 +78,11 @@ other Clownfish modules.
 
 =head1 FUNCTIONS
 
-=head2 slurp_file
+=head2 slurp_text
 
-    my $foo_contents = slurp_file('foo.txt');
+    my $foo_contents = slurp_text('foo.txt');
 
-Open a file, read it in, return its contents.  Assumes either binary data or
-text with an encoding of Latin-1.
+Open a file, read it in (as text), return its contents.
 
 =head2 current
 

Modified: incubator/lucy/branches/0.1/clownfish/src/CFCHierarchy.c
URL: http://svn.apache.org/viewvc/incubator/lucy/branches/0.1/clownfish/src/CFCHierarchy.c?rev=1141836&r1=1141835&r2=1141836&view=diff
==============================================================================
--- incubator/lucy/branches/0.1/clownfish/src/CFCHierarchy.c (original)
+++ incubator/lucy/branches/0.1/clownfish/src/CFCHierarchy.c Fri Jul  1 06:33:11 2011
@@ -268,7 +268,7 @@ S_parse_cf_files(CFCHierarchy *self) {
 
         // Slurp, parse, add parsed file to pool.
         size_t unused;
-        char *content = CFCUtil_slurp_file(source_path, &unused);
+        char *content = CFCUtil_slurp_text(source_path, &unused);
         CFCFile *file = S_parse_file(self->parser, content, source_class);
         if (!file) {
             croak("parser error for %s", source_path);

Modified: incubator/lucy/branches/0.1/clownfish/src/CFCUtil.c
URL: http://svn.apache.org/viewvc/incubator/lucy/branches/0.1/clownfish/src/CFCUtil.c?rev=1141836&r1=1141835&r2=1141836&view=diff
==============================================================================
--- incubator/lucy/branches/0.1/clownfish/src/CFCUtil.c (original)
+++ incubator/lucy/branches/0.1/clownfish/src/CFCUtil.c Fri Jul  1 06:33:11 2011
@@ -164,11 +164,11 @@ CFCUtil_write_file(const char *filename,
 }
 
 char*
-CFCUtil_slurp_file(const char *file_path, size_t *len_ptr) {
-    FILE   *const file = fopen(file_path, "rb");
+CFCUtil_slurp_text(const char *file_path, size_t *len_ptr) {
+    FILE   *const file = fopen(file_path, "r");
     char   *contents;
-    size_t  len;
-    long    check_val;
+    size_t  binary_len;
+    long    text_len;
 
     /* Sanity check. */
     if (file == NULL) {
@@ -176,25 +176,27 @@ CFCUtil_slurp_file(const char *file_path
     }
 
     /* Find length; return NULL if the file has a zero-length. */
-    len = CFCUtil_flength(file);
-    if (len == 0) {
+    binary_len = CFCUtil_flength(file);
+    if (binary_len == 0) {
         *len_ptr = 0;
         return NULL;
     }
 
     /* Allocate memory and read the file. */
-    contents = (char*)MALLOCATE(len * sizeof(char) + 1);
-    contents[len] = '\0';
-    check_val = fread(contents, sizeof(char), len, file);
+    contents = (char*)MALLOCATE(binary_len * sizeof(char) + 1);
+    text_len = fread(contents, sizeof(char), binary_len, file);
 
     /* Weak error check, because CRLF might result in fewer chars read. */
-    if (check_val <= 0) {
-        CFCUtil_die("Tried to read %d characters of '%s', got %d", (int)len,
-                    file_path, check_val);
+    if (text_len <= 0) {
+        CFCUtil_die("Tried to read %ld bytes of '%s', got return code %ld",
+                    (long)binary_len, file_path, (long)text_len);
     }
 
+    /* NULL-terminate. */
+    contents[text_len] = '\0';
+
     /* Set length pointer for benefit of caller. */
-    *len_ptr = check_val;
+    *len_ptr = text_len;
 
     /* Clean up. */
     if (fclose(file)) {
@@ -212,7 +214,7 @@ CFCUtil_write_if_changed(const char *pat
             CFCUtil_die("Error closing file '%s': %s", path, strerror(errno));
         }
         size_t existing_len;
-        char *existing = CFCUtil_slurp_file(path, &existing_len);
+        char *existing = CFCUtil_slurp_text(path, &existing_len);
         int changed = true;
         if (existing_len == len && strcmp(content, existing) == 0) {
             changed = false;

Modified: incubator/lucy/branches/0.1/clownfish/src/CFCUtil.h
URL: http://svn.apache.org/viewvc/incubator/lucy/branches/0.1/clownfish/src/CFCUtil.h?rev=1141836&r1=1141835&r2=1141836&view=diff
==============================================================================
--- incubator/lucy/branches/0.1/clownfish/src/CFCUtil.h (original)
+++ incubator/lucy/branches/0.1/clownfish/src/CFCUtil.h Fri Jul  1 06:33:11 2011
@@ -95,10 +95,10 @@ CFCUtil_write_file(const char *filename,
 void
 CFCUtil_write_if_changed(const char *path, const char *content, size_t len);
 
-/* Read an entire file into memory.
+/* Read an entire file (as text) into memory.
  */
 char*
-CFCUtil_slurp_file(const char *file_path, size_t *len_ptr);
+CFCUtil_slurp_text(const char *file_path, size_t *len_ptr);
 
 /* Get the length of a file (may overshoot on text files under DOS).
  */

Modified: incubator/lucy/branches/0.1/clownfish/t/001-util.t
URL: http://svn.apache.org/viewvc/incubator/lucy/branches/0.1/clownfish/t/001-util.t?rev=1141836&r1=1141835&r2=1141836&view=diff
==============================================================================
--- incubator/lucy/branches/0.1/clownfish/t/001-util.t (original)
+++ incubator/lucy/branches/0.1/clownfish/t/001-util.t Fri Jul  1 06:33:11 2011
@@ -19,7 +19,7 @@ use warnings;
 use Test::More tests => 15;
 use File::stat qw( stat );
 use Clownfish::Util qw(
-    slurp_file
+    slurp_text
     current
     verify_args
     a_isa_b
@@ -31,7 +31,7 @@ unlink $foo_txt;
 open( my $fh, '>', $foo_txt ) or die "Can't open '$foo_txt': $!";
 print $fh "foo";
 close $fh or die "Can't close '$foo_txt': $!";
-is( slurp_file($foo_txt), "foo", "slurp_file" );
+is( slurp_text($foo_txt), "foo", "slurp_text" );
 
 ok( current( $foo_txt, $foo_txt ), "current" );
 ok( !current( $foo_txt, 't' ), "not current" );