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 02:45:05 UTC
[lucy-commits] svn commit: r1141768 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs
lib/Clownfish/Util.pm src/CFCHierarchy.c src/CFCUtil.c src/CFCUtil.h
t/001-util.t
Author: marvin
Date: Fri Jul 1 00:45:04 2011
New Revision: 1141768
URL: http://svn.apache.org/viewvc?rev=1141768&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/trunk/clownfish/lib/Clownfish.xs
incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm
incubator/lucy/trunk/clownfish/src/CFCHierarchy.c
incubator/lucy/trunk/clownfish/src/CFCUtil.c
incubator/lucy/trunk/clownfish/src/CFCUtil.h
incubator/lucy/trunk/clownfish/t/001-util.t
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1141768&r1=1141767&r2=1141768&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Fri Jul 1 00:45:04 2011
@@ -1568,11 +1568,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/trunk/clownfish/lib/Clownfish/Util.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm?rev=1141768&r1=1141767&r2=1141768&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm Fri Jul 1 00:45:04 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/trunk/clownfish/src/CFCHierarchy.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCHierarchy.c?rev=1141768&r1=1141767&r2=1141768&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCHierarchy.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCHierarchy.c Fri Jul 1 00:45:04 2011
@@ -267,7 +267,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/trunk/clownfish/src/CFCUtil.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.c?rev=1141768&r1=1141767&r2=1141768&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.c Fri Jul 1 00:45:04 2011
@@ -180,11 +180,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) {
@@ -192,25 +192,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)) {
@@ -228,7 +230,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/trunk/clownfish/src/CFCUtil.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.h?rev=1141768&r1=1141767&r2=1141768&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.h Fri Jul 1 00:45:04 2011
@@ -101,10 +101,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/trunk/clownfish/t/001-util.t
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/t/001-util.t?rev=1141768&r1=1141767&r2=1141768&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/t/001-util.t (original)
+++ incubator/lucy/trunk/clownfish/t/001-util.t Fri Jul 1 00:45:04 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" );