Patchwork [3,of,7,c-hglib] examples: rewrite export/import example

login
register
mail settings
Submitter Giovanni Gherdovich
Date Nov. 23, 2014, 5:54 p.m.
Message ID <df66d1af4e114c9ca28a.1416765287@tosh>
Download mbox | patch
Permalink /patch/6832/
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 1416477494 -3600
#      Thu Nov 20 10:58:14 2014 +0100
# Branch refactor examples
# Node ID df66d1af4e114c9ca28ad222ddfc9c2f211729e9
# Parent  e7ee8936e3389c091036ccc1790eb140d1cafaa1
examples: rewrite export/import example

Rewrite the export/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.

Patch

diff -r e7ee8936e338 -r df66d1af4e11 examples/export-import.c
--- a/examples/export-import.c	Thu Nov 20 11:39:17 2014 +0100
+++ b/examples/export-import.c	Thu Nov 20 10:58:14 2014 +0100
@@ -25,48 +25,99 @@ 
 	char *import_comm[] = {"import", "-", NULL};
 	char ebuff[BUFF_SIZE], ibuff[BUFF_SIZE];
 	int eexitcode, iexitcode;
+	int eexit = 0, iexit = 0;
+	hg_header *eheader, *iheader;
+	int n;
 
 	hg_rawcommand(ehandle, export_comm);
 	hg_rawcommand(ihandle, import_comm);
 
-	hg_header *ehead, *ihead;
-	while (ehead = hg_read_header(ehandle), ehead->channel != r) {
-		if (ehead->channel == o) {
-			if (hg_rawread(ehandle, ebuff, BUFF_SIZE) > 0) {
-				ihead = hg_read_header(ihandle);
-				while (ihead->channel == o ||
-				       ihead->channel == e) {
-					if (hg_rawread(ihandle, ibuff,
-                                                       BUFF_SIZE) > 0) {
-						printf("%s\n", ibuff);
-					}
-					ihead = hg_read_header(ihandle);
-				}
-				if (ihead->channel == L) {
-					hg_rawwrite(ihandle, ebuff,
-						    strlen(ebuff));
+	while (!eexit || !iexit) {
+		eheader = hg_read_header(ehandle);
+		switch (eheader->channel) {
+		case o:
+			while (iheader = hg_read_header(ihandle),
+			      iheader->channel != L) {
+				switch (iheader->channel) {
+				case o:
+				case e:
+					while (n = hg_rawread(ihandle,
+							      ibuff,
+							      BUFF_SIZE),
+					       n > 0)
+						printf("%s", ibuff);
+					break;
+				case I:
+				case L:
+					break;
+				case r:
+					iexitcode = hg_exitcode(ihandle);
+					iexit = 1;
+					break;
+				case unknown_channel:
+					break;
 				}
 			}
-		} else if (ehead->channel == e) {
-			if (hg_rawread(ehandle, ebuff, BUFF_SIZE) > 0) {
-				printf("err = %s", ebuff);
+			while (n = hg_rawread(ehandle, ebuff, BUFF_SIZE),
+			       n > 0) {
+				switch (iheader->channel) {
+				case o:
+				case e:
+				case I:
+					break;
+				case L:
+					hg_rawwrite(ihandle,
+						    ebuff,
+						    strlen(ebuff));
+					break;
+				case r:
+					iexitcode = hg_exitcode(ihandle);
+					iexit = 1;
+					break;
+				case unknown_channel:
+					break;
+				}
 			}
+			break;
+		case e:
+			while (n = hg_rawread(ehandle, ebuff, BUFF_SIZE), n > 0)
+				printf("%s\n", ibuff);
+			break;
+		case I:
+		case L:
+			break;
+		case r:
+			eexitcode = hg_exitcode(ehandle);
+			while (!iexit) {
+				iheader = hg_read_header(ihandle);
+				switch (iheader->channel) {
+				case o:
+				case e:
+					while (n = hg_rawread(ihandle,
+							      ibuff,
+							      BUFF_SIZE),
+					       n > 0)
+						printf("%s", ibuff);
+					break;
+				case I:
+				case L:
+					hg_rawwrite(ihandle, "", 0);
+					break;
+				case r:
+					iexitcode = hg_exitcode(ihandle);
+					iexit = 1;
+					break;
+				case unknown_channel:
+					break;
+				}
+			}
+			eexit = 1;
+			break;
+		case unknown_channel:
+			break;
 		}
 	}
 
-	ihead = hg_read_header(ihandle);
-	hg_rawwrite(ihandle, ebuff, 0);
-
-	while (ihead = hg_read_header(ihandle), ihead->channel != r) {
-		if (ihead->channel == o || ihead->channel == e) {
-			if (hg_rawread(ihandle, ibuff, BUFF_SIZE) > 0) {
-				printf("%s", ibuff);
-			}
-		}
-	}
-
-	eexitcode = hg_exitcode(ehandle);
-	iexitcode = hg_exitcode(ihandle);
 	printf("exitcode for export process is %d \n", eexitcode);
 	printf("exitcode for import process is %d \n", iexitcode);