[PATCH 4 of 7 c-hglib] examples: rewrite import example

Giovanni Gherdovich g.gherdovich at gmail.com
Sun Nov 23 11:54:48 CST 2014


# HG changeset patch
# User Giovanni Gherdovich <g.gherdovich at gmail.com>
# Date 1416752369 -3600
#      Sun Nov 23 15:19:29 2014 +0100
# Branch refactor examples
# Node ID 5f990286dc351c4b9bd3382a0805a8fa409e4ca2
# Parent  df66d1af4e114c9ca28ad222ddfc9c2f211729e9
examples: rewrite import example

Rewrite the import example to account for the change
in hg_read_header() that returns NULL if no header on pipe
and to use a switch statement to make the channel-based
behaviour more explicit.

diff -r df66d1af4e11 -r 5f990286dc35 examples/import.c
--- a/examples/import.c	Thu Nov 20 10:58:14 2014 +0100
+++ b/examples/import.c	Sun Nov 23 15:19:29 2014 +0100
@@ -26,34 +26,46 @@
 	char *comm[] = {"import", "-", NULL};
 	char buff[BUFF_SIZE];
 	int exitcode = 0;
-	int fd, ns;
+	int exit = 0;
+	int fd, n, length;
+	hg_header *header;
 
 	fd = open(import_patch, O_RDONLY);
 	if (fd < 0) {
 		printf("The '%s' file couldn't be open\n", import_patch);
 		return 1;
 	}
+
 	hg_rawcommand(handle, comm);
 
-	hg_header *head;
-	while (head = hg_read_header(handle), head != NULL &&
-	       head->channel != r) {
-		if (head->channel == o) {
-			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+	while (!exit) {
+		header = hg_read_header(handle);
+		switch (header->channel) {
+		case o:
+		case e:
+			while (n = hg_rawread(handle, buff, BUFF_SIZE), n > 0)
 				printf("%s", buff);
-			}
-		} else if (head->channel == e) {
-			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
-				printf("error data: %s", buff);
-			}
-		} else if (head->channel == L || head->channel == I) {
-			int length = read(fd, buff, head->length);
+			break;
+		case I:
+		case L:
+			/* length = 0 sent by the client
+			 * is interpreted as EOF by the server.
+			 * So this case works also to end
+			 * the input session.
+			 */
+			length = read(fd, buff, header->length);
 			hg_rawwrite(handle, buff, length);
+			break;
+		case r:
+			exitcode = hg_exitcode(handle);
+			printf("exitcode = %d \n", exitcode);
+			exit = 1;
+			break;
+		case unknown_channel:
+			break;
 		}
 	}
 
-	exitcode = hg_exitcode(handle);
-	printf("exitcode = %d \n", exitcode);
 	return exitcode;
 }
 


More information about the Mercurial-devel mailing list