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

login
register
mail settings
Submitter Giovanni Gherdovich
Date Nov. 23, 2014, 5:54 p.m.
Message ID <c0b9754029daf740bbf4.1416765289@tosh>
Download mbox | patch
Permalink /patch/6834/
State Accepted
Delegated to: Pierre-Yves David
Headers show

Comments

Giovanni Gherdovich - Nov. 23, 2014, 5:54 p.m.
# HG changeset patch
# User Giovanni Gherdovich <g.gherdovich@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.

Patch

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;
 }