From cb197e459c381591f9cb0a92a0cbd720bdbda5a7 Mon Sep 17 00:00:00 2001 From: zlg Date: Wed, 21 May 2014 05:54:51 -0500 Subject: Refactor flag handling --- ch5/5-14_rsort.c | 20 ++++++++++++++------ ch5/5-15_sort-fold.c | 25 +++++++++++++++++-------- ch5/5-16_dir-order.c | 30 ++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/ch5/5-14_rsort.c b/ch5/5-14_rsort.c index 4c03191..b39a20c 100644 --- a/ch5/5-14_rsort.c +++ b/ch5/5-14_rsort.c @@ -129,12 +129,20 @@ int main (int argc, char *argv[]) { int reverse = 0; /* 1 if reverse sort */ if (argc > 1) { - int i; - for (i = 1; --argc; i++) { - if (strcmp(argv[i], "-n") == 0) { - numeric = 1; - } else if (strcmp(argv[i], "-r") == 0) { - reverse = 1; + int i, j; + for (i = 1, j = 0; --argc; i++) { + if (argv[i][j++] == '-') { + while (argv[i][j] != '\0') { + switch(argv[i][j]) { + case 'n': + numeric = 1; + break; + case 'r': + reverse = 1; + break; + } + j++; + } } } } diff --git a/ch5/5-15_sort-fold.c b/ch5/5-15_sort-fold.c index 3b508b9..90b567b 100644 --- a/ch5/5-15_sort-fold.c +++ b/ch5/5-15_sort-fold.c @@ -142,14 +142,23 @@ int main (int argc, char *argv[]) { int fold = 0; /* 1 if folding upper and lower case */ if (argc > 1) { - int i; - for (i = 1; --argc; i++) { - if (strcmp(argv[i], "-n") == 0) { - numeric = 1; - } else if (strcmp(argv[i], "-r") == 0) { - reverse = 1; - } else if (strcmp(argv[i], "-f") == 0) { - fold = 1; + int i, j; + for (i = 1, j = 0; --argc; i++) { + if (argv[i][j++] == '-') { + while (argv[i][j] != '\0') { + switch(argv[i][j]) { + case 'n': + numeric = 1; + break; + case 'r': + reverse = 1; + break; + case 'f': + fold = 1; + break; + } + j++; + } } } } diff --git a/ch5/5-16_dir-order.c b/ch5/5-16_dir-order.c index eabf755..7533b3b 100644 --- a/ch5/5-16_dir-order.c +++ b/ch5/5-16_dir-order.c @@ -162,16 +162,26 @@ int main (int argc, char *argv[]) { int nlines; /* number of input lines read */ if (argc > 1) { - int i; - for (i = 1; --argc; i++) { - if (strcmp(argv[i], "-n") == 0) { - numeric = 1; - } else if (strcmp(argv[i], "-r") == 0) { - reverse = 1; - } else if (strcmp(argv[i], "-f") == 0) { - fold = 1; - } else if (strcmp(argv[i], "-d") == 0) { - dir = 1; + int i, j; + for (i = 1, j = 0; --argc; i++) { + if (argv[i][j++] == '-') { + while (argv[i][j] != '\0') { + switch(argv[i][j]) { + case 'n': + numeric = 1; + break; + case 'r': + reverse = 1; + break; + case 'f': + fold = 1; + break; + case 'd': + dir = 1; + break; + } + j++; + } } } } -- cgit v1.2.3-54-g00ecf 2018-09-29Bump to 0.3alpha5 for PyPIzlg1-1/+1 2018-09-29cli: Add pretty printing to 'list' commandzlg3-17/+107 2018-09-08setup.py: Bump to alpha4 for PyPIzlg1-1/+1 2018-09-08cli: add '--raw' option to list commandzlg2-9/+45 2018-09-08Add remaining filters to vgstash packagezlg1-2/+11 2018-09-04Update LICENSE to match setup.pyzlg1-80/+67 2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779 2018-03-18Flesh out filter types and ownership statuszlg3-82/+144 2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 2018-03-13Update helpers a bitzlg1-2/+9 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 2016-11-15Correct phrasing in README.Ze Libertine Gamer1-4/+4 2016-11-13DerpZe Libertine Gamer1-0/+1 2016-11-03Improve error handling in shell scriptsZe Libertine Gamer4-3/+23 2016-10-24Correct run_again, add recursionZe Libertine Gamer1-0/+4 2016-10-21Add quotes to correct behavior for arglistZe Libertine Gamer1-1/+1 2016-10-14updater.sh: add recursion, error handlingZe Libertine Gamer1-43/+101 2016-10-14Correct pipe-handling behaviorZe Libertine Gamer1-1/+9 2016-10-12Clarify a method to move between platformsZe Libertine Gamer1-2/+5