From: H.G.Muller Date: Thu, 6 Dec 2018 20:23:49 +0000 (+0100) Subject: Do all input through one ReadLine() routine X-Git-Tag: v4.0~25 X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=29739720e2b8d549906e235e88bdc603491e9aa3;p=uci2wb.git Do all input through one ReadLine() routine Some duplicate code now is made into a subroutine, to which we pass the stream from which it should read a line, and the buffer to hold the result. It returns whether the read was succesful (i.e. no EOF). As this can also easily fake engine output from a file, the special routine GetChar()we had for this could be abolished. --- diff --git a/UCI2WB.c b/UCI2WB.c index 178cdcb..c0ace0f 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -243,14 +243,11 @@ Move4GUI(char *m) } int -GetChar() +ReadLine (FILE *f, char *line) { - int c; - if(fromF) { - if((c = fgetc(fromF)) != EOF) return c; - fclose(fromF); fromF = 0; printf("# end fake\n"); - } - return fgetc(fromE); + int x, i = 0; + while((x = fgetc(f)) != EOF && (line[i] = x) != '\n') i++; line[++i] = 0; + return (x != EOF); } void * @@ -263,9 +260,8 @@ Engine2GUI() int i=0, x; char *p, dummy, len; fflush(stdout); fflush(toE); - while((line[i] = x = GetChar()) != EOF && line[i] != '\n') i++; - line[++i] = 0; - if(x == EOF) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0); + if(fromF && !ReadLine(fromF, line)) fromF = 0, printf("# end fake\n"); + if(!fromF && !ReadLine(fromE, line)) printf("tellusererror UCI2WB: %s died on me\n", binary), exit(0); DPRINT("# engine said: %s", line), fflush(stdout); if(sscanf(line, "%s", command) != 1) continue; if(!strcmp(command, "bestmove")) { @@ -440,7 +436,7 @@ GUI2Engine() char line[256], command[256], *p, *q, *r, mySide, type[99]; while(1) { - int i, x, difficult, think=0; + int i, difficult, think=0; if((computer == stm || computer == ANALYZE && !searching) && !suspended) { DPRINT("# start search\n"); @@ -459,8 +455,7 @@ GUI2Engine() nomove: for(difficult=0; !difficult; ) { // read and handle commands that can (or must) be handled during thinking fflush(toE); fflush(stdout); - i = 0; while((x = getchar()) != EOF && (line[i] = x) != '\n') i++; - line[++i] = 0; if(x == EOF) { printf("# EOF\n"); sprintf(line, "quit -1\n"); } + if(!ReadLine(stdin, line)) printf("# EOF\n"), sprintf(line, "quit -1\n"); if(think && !pause) Sync(PAUSE), think = 0, Release(); // if no longer thinking, take dummy pause sscanf(line, "%s", command); DPRINT("# '%s' think=%d pause=%d log=%d sent=%d\n", command, think, pause, logLen, sentLen); if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first