From 22ddf92e89b19c30ff77b368cab60cc95315db35 Mon Sep 17 00:00:00 2001 From: zlg Date: Thu, 26 Sep 2013 07:17:51 -0500 Subject: Solve Exercise 5-11: `entab` and `detab` remixed! This one was pretty rough. At first I wanted to include a bunch of error-catching and "smart" stuff. When I took a second look at it and realized `itoa` returns 0 on a string, things became a bit easier. I may have been able to outsource a few things to a function or two, but overall I think it worked out. --- ch5/5-11_detab-remixed.c | 61 ++++++++++++++++++++++++++++++++++++++ ch5/5-11_entab-remixed.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 ch5/5-11_detab-remixed.c create mode 100644 ch5/5-11_entab-remixed.c (limited to 'ch5') diff --git a/ch5/5-11_detab-remixed.c b/ch5/5-11_detab-remixed.c new file mode 100644 index 0000000..43bffb4 --- /dev/null +++ b/ch5/5-11_detab-remixed.c @@ -0,0 +1,61 @@ +#include + +/* The C Programming Language: 2nd Edition + * + * Exercise 5-11: Modify the programs entab and detab (written as exercises in + * Chapter 1) to accept a list of tab stops as arguments. Use the default tab + * settings if there are no arguments. + */ + +#define TABWIDTH 8 + +int main(int argc, char *argv[]) { + int column, c, tabnum, stop; + column = 0; + + if (argc > 1) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } else { + tabnum = 0; + } + + while ((c = getchar()) != EOF) { + if (c == '\t') { + if (argc > 1) { + // advance the argument if we're ahead of the last one + if (column > stop && tabnum < (argc -1)) { + stop = atoi(argv[++tabnum]); + } + // insert our spaces up to the tabstop + while (column <= stop) { + putchar(' '); + column++; + } + // advance the argument (again) if needed. + if (tabnum < (argc - 1)) { + stop = atoi(argv[++tabnum]); + } + } else { + // default tabstopping + while (column % TABWIDTH != 1) { + putchar(' '); + column++; + } + } + } else { + // reset counters and the arglist + if (c == '\n') { + column = 0; + if (tabnum > 0) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } + } + putchar(c); + column++; + } + } + + return 0; +} diff --git a/ch5/5-11_entab-remixed.c b/ch5/5-11_entab-remixed.c new file mode 100644 index 0000000..8f6f7d4 --- /dev/null +++ b/ch5/5-11_entab-remixed.c @@ -0,0 +1,76 @@ +#include +#include + +/* The C Programming Language: 2nd Edition + * + * Exercise 5-11: Modify the programs entab and detab (written as exercises in + * Chapter 1) to accept a list of tab stops as arguments. Use the default tab + * settings if there are no arguments. + */ + +/* The directions for this exercise aren't terribly clear. The way I see it, + * each argument is a character column that all tabs should align to. For the + * entab portion, I should count 2+ spaces forward and, if I reach a tabstop, + * insert a tab. Then move onto the next argument. If 2+ spaces are present, + * but don't reach the tabstop threshold, the spaces are left alone. This is + * the best I could manage with the ambiguous instructions. + */ + +#define TABWIDTH 8 + +int main(int argc, char *argv[]) { + int column, c, spaces, tabnum, stop; + spaces = column = 0; + + if (argc > 1) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } else { + tabnum = 0; + } + + while ((c = getchar()) != EOF) { + column++; + + if (c == ' ') { + spaces++; + if (argc > 1) { + if (column == stop) { + if (tabnum < (argc - 1)) { + stop = atoi(argv[++tabnum]); + } + if (spaces > 1) { + putchar('\t'); + spaces = 0; + } + } else if (column > stop && tabnum < (argc -1)) { + stop = atoi(argv[++tabnum]); + } + } else { + // we need to do default tabstopping + if (column % TABWIDTH == 0 && spaces > 1) { + putchar('\t'); + spaces = 0; + } + } + } else { + // output all extra spaces first + while (spaces > 0) { + putchar(' '); + spaces--; + } + // reset the counts and argument position on a newline + if (c == '\n') { + column = 0; + spaces = 0; + if (tabnum > 0) { + tabnum = 1; + stop = atoi(argv[tabnum]); + } + } + putchar(c); + } + } + + return 0; +} -- cgit v1.2.3-70-g09d2 08 19:07:36 -0700'>2018-09-08Add remaining filters to vgstash packagezlg1-2/+11 2018-09-04Update LICENSE to match setup.pyzlg1-80/+67 Whoops. 2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779 This commit is huge, but contains everything needed for a "proper" build system built on pytest + tox and a CLI built with click. For now, this branch will contain all new vgstash development activity until it reaches feature parity with master. The CLI is installed to pip's PATH. Only the 'init', 'add', and 'list' commands work, with only two filters. This is pre-alpha software, and is therefore not stable yet. 2018-03-18Flesh out filter types and ownership statuszlg3-82/+144 It's time for a refactor to a module; the functionality and interface are clashing. 2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 This filter is used to get an idea of which games you're currently playing through, so you can prioritize games to play when you're bored and detect it when you've beaten a game but haven't marked it as such. 2018-03-13Update helpers a bitzlg1-2/+9 At present, user modification is needed to make these seamless. vgup() may need to be axed in favor of telling the user to make an alias. 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 It used to point to a directory, which would then look for .vgstash.db. This behavior was kind of backwards and I don't remember why I did it that way. This change gives users more control over where they put their DB. Be sure to update your environment variable if you have it set! 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 Sourcing them in .bash_profile screws up login if they're set. 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 Loops and functions -- oh my, what a useful combination. :) 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 Also correct a typo.