avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dcrea...@apache.org
Subject svn commit: r1221072 - in /avro/trunk: CHANGES.txt lang/c/src/avro/io.h lang/c/src/avrocat.c lang/c/src/avropipe.c lang/c/src/datafile.c lang/c/src/io.c
Date Tue, 20 Dec 2011 02:09:24 GMT
Author: dcreager
Date: Tue Dec 20 02:09:23 2011
New Revision: 1221072

URL: http://svn.apache.org/viewvc?rev=1221072&view=rev
Log:
AVRO-961. C: avrocat/pipe use stdin when no file is specified.

The avrocat and avropipe commands will now read an Avro data file from
stdin if you don't provide a filename on the command line.  Contributed
by Michael Cooper.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c/src/avro/io.h
    avro/trunk/lang/c/src/avrocat.c
    avro/trunk/lang/c/src/avropipe.c
    avro/trunk/lang/c/src/datafile.c
    avro/trunk/lang/c/src/io.c

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1221072&r1=1221071&r2=1221072&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Dec 20 02:09:23 2011
@@ -30,6 +30,9 @@ Avro 1.6.2 (unreleased)
     input parameters are now "const char *" instead of "char *".
     (Lucas Martin-King via dcreager)
 
+    AVRO-961. C: avrocat/avropipe can now read from stdin.
+    (Michael Cooper via dcreager)
+
   BUG FIXES
 
     AVRO-962. Java: Fix Maven plugin to support string type override.

Modified: avro/trunk/lang/c/src/avro/io.h
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avro/io.h?rev=1221072&r1=1221071&r2=1221072&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avro/io.h (original)
+++ avro/trunk/lang/c/src/avro/io.h Tue Dec 20 02:09:23 2011
@@ -40,7 +40,9 @@ typedef struct avro_writer_t_ *avro_writ
  */
 
 avro_reader_t avro_reader_file(FILE * fp);
+avro_reader_t avro_reader_file_fp(FILE * fp, int should_close);
 avro_writer_t avro_writer_file(FILE * fp);
+avro_writer_t avro_writer_file_fp(FILE * fp, int should_close);
 avro_reader_t avro_reader_memory(const char *buf, int64_t len);
 avro_writer_t avro_writer_memory(const char *buf, int64_t len);
 
@@ -99,6 +101,8 @@ int avro_file_writer_create(const char *
 			    avro_file_writer_t * writer);
 int avro_file_writer_open(const char *path, avro_file_writer_t * writer);
 int avro_file_reader(const char *path, avro_file_reader_t * reader);
+int avro_file_reader_fp(FILE *fp, const char *path, int should_close,
+			avro_file_reader_t * reader);
 
 avro_schema_t
 avro_file_reader_get_writer_schema(avro_file_reader_t reader);

Modified: avro/trunk/lang/c/src/avrocat.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avrocat.c?rev=1221072&r1=1221071&r2=1221072&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avrocat.c (original)
+++ avro/trunk/lang/c/src/avrocat.c Tue Dec 20 02:09:23 2011
@@ -31,10 +31,18 @@ process_file(const char *filename)
 {
 	avro_file_reader_t  reader;
 
-	if (avro_file_reader(filename, &reader)) {
-		fprintf(stderr, "Error opening %s:\n  %s\n",
-			filename, strerror(errno));
-		exit(1);
+	if (filename == NULL) {
+		if (avro_file_reader_fp(stdin, "<stdin>", 0, &reader)) {
+			fprintf(stderr, "Error opening <stdin>:\n  %s\n",
+				strerror(errno));
+			exit(1);
+		}
+	} else {
+		if (avro_file_reader(filename, &reader)) {
+			fprintf(stderr, "Error opening %s:\n  %s\n",
+				filename, strerror(errno));
+			exit(1);
+		}
 	}
 
 	avro_schema_t  wschema;
@@ -75,9 +83,7 @@ int main(int argc, char **argv)
 	if (argc == 2) {
 		data_filename = argv[1];
 	} else if (argc == 1) {
-		fprintf(stderr, "Must provide an input file.\n");
-		usage();
-		exit(1);
+		data_filename = NULL;
 	} else {
 		fprintf(stderr, "Can't read from multiple input files.\n");
 		usage();

Modified: avro/trunk/lang/c/src/avropipe.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avropipe.c?rev=1221072&r1=1221071&r2=1221072&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avropipe.c (original)
+++ avro/trunk/lang/c/src/avropipe.c Tue Dec 20 02:09:23 2011
@@ -335,10 +335,18 @@ process_file(const char *filename)
 {
 	avro_file_reader_t  reader;
 
-	if (avro_file_reader(filename, &reader)) {
-		fprintf(stderr, "Error opening %s:\n  %s\n",
-			filename, strerror(errno));
-		exit(1);
+	if (filename == NULL) {
+		if (avro_file_reader_fp(stdin, "<stdin>", 0, &reader)) {
+			fprintf(stderr, "Error opening <stdin>:\n  %s\n",
+				strerror(errno));
+			exit(1);
+		}
+	} else {
+		if (avro_file_reader(filename, &reader)) {
+			fprintf(stderr, "Error opening %s:\n  %s\n",
+				filename, strerror(errno));
+			exit(1);
+		}
 	}
 
 	/* The JSON root is an array */
@@ -405,9 +413,7 @@ int main(int argc, char **argv)
 	if (argc == 1) {
 		data_filename = argv[0];
 	} else if (argc == 0) {
-		fprintf(stderr, "Must provide an input file.\n");
-		usage();
-		exit(1);
+		data_filename = NULL;
 	} else {
 		fprintf(stderr, "Can't read from multiple input files.\n");
 		usage();

Modified: avro/trunk/lang/c/src/datafile.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/datafile.c?rev=1221072&r1=1221071&r2=1221072&view=diff
==============================================================================
--- avro/trunk/lang/c/src/datafile.c (original)
+++ avro/trunk/lang/c/src/datafile.c Tue Dec 20 02:09:23 2011
@@ -280,23 +280,24 @@ static int file_read_block_count(avro_fi
 	return 0;
 }
 
-int avro_file_reader(const char *path, avro_file_reader_t * reader)
+int avro_file_reader_fp(FILE *fp, const char *path, int should_close,
+			avro_file_reader_t * reader)
 {
 	int rval;
-	FILE *fp;
 	avro_file_reader_t r = avro_new(struct avro_file_reader_t_);
 	if (!r) {
+		if (should_close) {
+			fclose(fp);
+		}
 		avro_set_error("Cannot allocate file reader for %s", path);
 		return ENOMEM;
 	}
 
-	fp = fopen(path, "r");
-	if (!fp) {
-		avro_freet(struct avro_file_reader_t_, r);
-		return errno;
-	}
-	r->reader = avro_reader_file(fp);
+	r->reader = avro_reader_file_fp(fp, should_close);
 	if (!r->reader) {
+		if (should_close) {
+			fclose(fp);
+		}
 		avro_set_error("Cannot allocate reader for file %s", path);
 		avro_freet(struct avro_file_reader_t_, r);
 		return ENOMEM;
@@ -305,12 +306,14 @@ int avro_file_reader(const char *path, a
 	rval = file_read_header(r->reader, &r->writers_schema, r->sync,
 				sizeof(r->sync));
 	if (rval) {
+		avro_reader_free(r->reader);
 		avro_freet(struct avro_file_reader_t_, r);
 		return rval;
 	}
 
 	rval = file_read_block_count(r);
 	if (rval) {
+		avro_reader_free(r->reader);
 		avro_freet(struct avro_file_reader_t_, r);
 		return rval;
 	}
@@ -319,6 +322,18 @@ int avro_file_reader(const char *path, a
 	return rval;
 }
 
+int avro_file_reader(const char *path, avro_file_reader_t * reader)
+{
+	FILE *fp;
+
+	fp = fopen(path, "r");
+	if (!fp) {
+		return errno;
+	}
+
+	return avro_file_reader_fp(fp, path, 1, reader);
+}
+
 avro_schema_t
 avro_file_reader_get_writer_schema(avro_file_reader_t r)
 {

Modified: avro/trunk/lang/c/src/io.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/io.c?rev=1221072&r1=1221071&r2=1221072&view=diff
==============================================================================
--- avro/trunk/lang/c/src/io.c (original)
+++ avro/trunk/lang/c/src/io.c Tue Dec 20 02:09:23 2011
@@ -45,6 +45,7 @@ struct avro_writer_t_ {
 struct _avro_reader_file_t {
 	struct avro_reader_t_ reader;
 	FILE *fp;
+	int should_close;
 	char *cur;
 	char *end;
 	char buffer[4096];
@@ -53,6 +54,7 @@ struct _avro_reader_file_t {
 struct _avro_writer_file_t {
 	struct avro_writer_t_ writer;
 	FILE *fp;
+	int should_close;
 };
 
 struct _avro_reader_memory_t {
@@ -90,7 +92,7 @@ static void writer_init(avro_writer_t wr
 	avro_refcount_set(&writer->refcount, 1);
 }
 
-avro_reader_t avro_reader_file(FILE * fp)
+avro_reader_t avro_reader_file_fp(FILE * fp, int should_close)
 {
 	struct _avro_reader_file_t *file_reader =
 	    avro_new(struct _avro_reader_file_t);
@@ -100,11 +102,17 @@ avro_reader_t avro_reader_file(FILE * fp
 	}
 	memset(file_reader, 0, sizeof(struct _avro_reader_file_t));
 	file_reader->fp = fp;
+	file_reader->should_close = should_close;
 	reader_init(&file_reader->reader, AVRO_FILE_IO);
 	return &file_reader->reader;
 }
 
-avro_writer_t avro_writer_file(FILE * fp)
+avro_reader_t avro_reader_file(FILE * fp)
+{
+	return avro_reader_file_fp(fp, 1);
+}
+
+avro_writer_t avro_writer_file_fp(FILE * fp, int should_close)
 {
 	struct _avro_writer_file_t *file_writer =
 	    avro_new(struct _avro_writer_file_t);
@@ -113,10 +121,16 @@ avro_writer_t avro_writer_file(FILE * fp
 		return NULL;
 	}
 	file_writer->fp = fp;
+	file_writer->should_close = should_close;
 	writer_init(&file_writer->writer, AVRO_FILE_IO);
 	return &file_writer->writer;
 }
 
+avro_writer_t avro_writer_file(FILE * fp)
+{
+	return avro_writer_file_fp(fp, 1);
+}
+
 avro_reader_t avro_reader_memory(const char *buf, int64_t len)
 {
 	struct _avro_reader_memory_t *mem_reader =
@@ -402,7 +416,9 @@ void avro_reader_free(avro_reader_t read
 	if (is_memory_io(reader)) {
 		avro_freet(struct _avro_reader_memory_t, reader);
 	} else if (is_file_io(reader)) {
-		fclose(avro_reader_to_file(reader)->fp);
+		if (avro_reader_to_file(reader)->should_close) {
+			fclose(avro_reader_to_file(reader)->fp);
+		}
 		avro_freet(struct _avro_reader_file_t, reader);
 	}
 }
@@ -412,7 +428,9 @@ void avro_writer_free(avro_writer_t writ
 	if (is_memory_io(writer)) {
 		avro_freet(struct _avro_writer_memory_t, writer);
 	} else if (is_file_io(writer)) {
-		fclose(avro_writer_to_file(writer)->fp);
+		if (avro_writer_to_file(writer)->should_close) {
+			fclose(avro_writer_to_file(writer)->fp);
+		}
 		avro_freet(struct _avro_writer_file_t, writer);
 	}
 }



Mime
View raw message