From 97de91380c589b5a9294ff214ccddbd42820a662 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Fri, 10 Nov 2006 08:54:00 +0000 Subject: [PATCH] More robust technique for reading from pipeline --- Sources/VLAppController.mm | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Sources/VLAppController.mm b/Sources/VLAppController.mm index e935133..cfbfd6a 100644 --- a/Sources/VLAppController.mm +++ b/Sources/VLAppController.mm @@ -64,18 +64,28 @@ - (NSString*)getLineFromCommand:(NSString*)command { - char line[1000]; - FILE * output = popen([command UTF8String], "r"); - if (fgets(line, 1000, output)) { - size_t len = strlen(line); - if (len && line[len-1]=='\n') { - line[len-1] = 0; - return [NSString stringWithUTF8String:line]; - } - } else - NSLog(@"Failed command: %@ %s (%d)\n", command, feof(output) ? "EOF" : "Error", errno); + char line[1000]; + FILE * output = popen([command UTF8String], "r"); + NSString * outLine = nil; + for (int attempts=0; attempts<5; ++attempts) + if (fgets(line, 1000, output)) { + size_t len = strlen(line); + if (len && line[len-1]=='\n') { + line[len-1] = 0; + outLine = [NSString stringWithUTF8String:line]; + } + } else if (feof(output)) + break; + else + clearerr(output); + + if (!outLine) + NSLog(@"Failed command: %@ %s (%d)\n", command, + feof(output) ? "EOF" : "Error", errno); + pclose(output); - return nil; + + return outLine; } - (NSString *)lilypondVersion:(NSString *)path