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

login
register
mail settings
Submitter Giovanni Gherdovich
Date Nov. 23, 2014, 5:54 p.m.
Message ID <8afb0879bad5d4f2682b.1416765291@tosh>
Download mbox | patch
Permalink /patch/6836/
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 1416519204 -3600
#      Thu Nov 20 22:33:24 2014 +0100
# Branch refactor examples
# Node ID 8afb0879bad5d4f2682b0327e94c94f6ec426e69
# Parent  2fcddadba4b91e1f5fb75a1aa42981b48582732d
examples: rewrite verify example

Rewrite the verify 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 2fcddadba4b9 -r 8afb0879bad5 examples/verify.c
--- a/examples/verify.c	Sun Nov 23 15:51:39 2014 +0100
+++ b/examples/verify.c	Thu Nov 20 22:33:24 2014 +0100
@@ -21,27 +21,36 @@ 
 	char *comm[] = {"verify", NULL};
 	char buff[BUFF_SIZE];
 	int exitcode = 0;
-	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:
+			while (n = hg_rawread(handle, buff, BUFF_SIZE), n > 0)
 				printf("out = %s", buff);
-			}
-		} else if (head->channel == e) {
-			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+			break;
+		case e:
+			while (n = hg_rawread(handle, buff, BUFF_SIZE), n > 0)
 				printf("err = %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;
 }