[PATCH 5 of 7 c-hglib] examples: rewrite log example

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


# HG changeset patch
# User Giovanni Gherdovich <g.gherdovich at gmail.com>
# Date 1416480095 -3600
#      Thu Nov 20 11:41:35 2014 +0100
# Branch refactor examples
# Node ID c0b9754029daf740bbf4ee09cb6183f46d7f344f
# Parent  5f990286dc351c4b9bd3382a0805a8fa409e4ca2
examples: rewrite log example

Rewrite the log 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 5f990286dc35 -r c0b9754029da examples/log.c
--- a/examples/log.c	Sun Nov 23 15:19:29 2014 +0100
+++ b/examples/log.c	Thu Nov 20 11:41:35 2014 +0100
@@ -21,26 +21,34 @@
 	char buff[BUFF_SIZE];
 	char *comm[] = {"log", "-v", NULL};
 	int exitcode;
-	int ns;
+	int exit = 0;
+	int n;
+	hg_header *header;
 
 	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);
-			}
+			break;
+		case I:
+		case L:
+			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