From 35fcdd9de9b1f59072d696933f1043383270b527 Mon Sep 17 00:00:00 2001 From: Colum Paget Date: Wed, 21 Feb 2018 19:27:15 +0000 Subject: [PATCH] handle situation where a delimiter cannot be found in input, and we must use the one supplied on the command line --- ccut.c | 26 +++++++++++--------------- check.sh | 8 ++++---- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ccut.c b/ccut.c index be2831f..4ea61a4 100644 --- a/ccut.c +++ b/ccut.c @@ -20,7 +20,7 @@ Copyright (c) 2015 Colum Paget //UTF-8 chars always have the top bit (128) set #define isUTF8ch(ch) (((ch) & 128) ? 1 : 0) -char *Version="2.10"; +char *Version="2.11"; char *Delim=NULL, *OutputDelim=NULL, *OutPath=NULL, *FieldSpec=NULL; @@ -517,7 +517,13 @@ return(fcount); - +void OutputDelimiter(char FoundDelim) +{ + if (Flags & FLAG_REPLACE_DELIM) fputs(OutputDelim, stdout); + else if (Flags & FLAG_DELIMSTR) fputs(Delim, stdout); + else if (FoundDelim !=0) fputc(FoundDelim, stdout); + else if (StrValid(Delim)) fputc(*Delim, stdout); +} void OutputField(const char *start, const char *end, int DelimFlags) @@ -570,12 +576,7 @@ if (start) else { //if we're outputing fields in reverse order than we copy a delimiter to the start - if (DelimFlags & DELIM_PREFIX) - { - if (Flags & FLAG_REPLACE_DELIM) fputs(OutputDelim, stdout); - else if (Flags & FLAG_DELIMSTR) fputs(Delim, stdout); - else fputc(delim, stdout); - } + if (DelimFlags & DELIM_PREFIX) OutputDelimiter(delim); //messy calcluation. We returned the string including delimiter. We can output this by writing end-start bytes //but this clips the delimiter off. If we have a string rather than a character as the delimiter though, then it @@ -589,16 +590,11 @@ if (Flags & FLAG_DELIMSTR) else fwrite(start,ptr-start,1,stdout); //if we're outputing fields in normal order than we copy a delimiter to the end - if (DelimFlags & DELIM_POSTFIX) - { - if (Flags & FLAG_REPLACE_DELIM) fputs(OutputDelim, stdout); - else if (Flags & FLAG_DELIMSTR) fputs(Delim, stdout); - else fputc(delim, stdout); - } + if (DelimFlags & DELIM_POSTFIX) OutputDelimiter(delim); } } //Use the cached 'last delim' if the field doesn't exist -else if (DelimFlags) fputs(OutputDelim, stdout); +else if (DelimFlags) OutputDelimiter(0); OutputNo++; } diff --git a/check.sh b/check.sh index 0fc3df7..7accbd0 100755 --- a/check.sh +++ b/check.sh @@ -52,10 +52,10 @@ TestCut "-Q -d ',' -f 3,6" tests/cut.5 "field3,field6" "Cut honoring quotes but TestCut "-d , -j -f 3" tests/cut.3 "field3" "Cut combining runs of the same delimiter" TestCut "-d ,;: -j -f 6" tests/cut.3 "field6" "Cut combining runs of different delimiters" TestCut "-D ,, -f 2" tests/cut.3 ",field3,field4" "Cut using a string as a delimiter rather than a single char" -TestCut "-Q -d ',' -f 2,3,20" tests/cut.5 "field2,field3," "Cut with a non-existent field at the end" -TestCut "-Q -d ',' -f 2,20,3" tests/cut.5 "field2,,field3" "Cut with a non-existent field in the middle" -TestCut "-Q -d ',' -f 20,2,3" tests/cut.5 ",field2,field3" "Cut with a non-existent field at the start" -TestCut "-Q -d ',' -f 2,3,10-15" tests/cut.5 "field2,field3,,,,,," "Cut with a range of non-existent fields" +TestCut "-Q -d , -f 2,3,20" tests/cut.5 "field2,field3," "Cut with a non-existent field at the end" +TestCut "-Q -d , -f 2,20,3" tests/cut.5 "field2,,field3" "Cut with a non-existent field in the middle" +TestCut "-Q -d , -f 20,2,3" tests/cut.5 ",field2,field3" "Cut with a non-existent field at the start" +TestCut "-Q -d , -f 2,3,10-15" tests/cut.5 "field2,field3,,,,,," "Cut with a range of non-existent fields" TestCut "-c 80-95 --utf8" tests/utf8.txt "Congress‘ infras" "Cut UTF-8 input"