### Login and collect the files we need for testing. [DFK:~] ssh cs50@flume Last login: Fri Apr 15 11:29:11 2016 from 10.31.181.63 [cs50@flume ~/example]$ [cs50@flume ~/example]$ cp ~cs50/public_html/examples/subtemplate.c . [cs50@flume ~/example]$ mygcc -o subtemplate subtemplate.c [cs50@flume ~/example]$ ls subtemplate* subtemplate.c [cs50@flume ~/example]$ cp ~cs50/public_html/Labs/Lab1/templates/* . [cs50@flume ~/example]$ cp ~cs50/public_html/Labs/Lab1/photos/fall/*.txt . [cs50@flume ~/example]$ ls empty.template index.template subtemplate* header.template navigation.bottom.template subtemplate.c header.txt navigation.item.template title.txt image.template navigation.top.template [cs50@flume ~/example]$ head -17 subtemplate.c /* * subtemplate.c - substitute text into a template file * * usage: subtemplate target replacement * where: * target is a string to be replaced wherever it appears in the input; * replacement is a filename for file containing replacement text * output: * read the stdin, copying it to stdout, but wherever the * target text is found, replace it with the contents of the * replacement file. * stdin: the input template * stdout: the modified template * stderr: error messages * * David Kotz, April 2016 */ [cs50@flume ~/example]$ cat title.txt Fall 2015 [cs50@flume ~/example]$ cat header.txt Fall colors at Dartmouth, 2015. Photos courtesy of Dartmouth Flickr site. [cs50@flume ~/example]$ cat header.template @TITLE@

@TITLE@

@HEADCAPTION@

### Test the basic operation of this script, including in a pipeline: [cs50@flume ~/example]$ cat header.template | ./subtemplate @TITLE@ title.txt Fall 2015

Fall 2015

@HEADCAPTION@

[cs50@flume ~/example]$ cat header.template | ./subtemplate @TITLE@ title.txt | ./subtemplate @HEADCAPTION@ header.txt Fall 2015

Fall 2015

Fall colors at Dartmouth, 2015. Photos courtesy of Dartmouth Flickr site.

### Test a simple case where target does not appear in file. [cs50@flume ~/example]$ cat header.template | ./subtemplate @TIXX header.txt @TITLE@

@TITLE@

@HEADCAPTION@

[cs50@flume ~/example]$ cat header.template | ./subtemplate @HUH header.txt @TITLE@

@TITLE@

@HEADCAPTION@

### Test the parameters - missing file, no parameters, one parameter, more than two params: [cs50@flume ~/example]$ cat header.template | ./subtemplate @TITLE@ header.not ./subtemplate: replacement file header.not nonexistent or not readable [cs50@flume ~/example]$ cat header.template | ./subtemplate usage: ./subtemplate targetString replacementFilename [cs50@flume ~/example]$ cat header.template | ./subtemplate @TITLE@ usage: ./subtemplate targetString replacementFilename [cs50@flume ~/example]$ cat header.template | ./subtemplate @TITLE@ title.txt extra usage: ./subtemplate targetString replacementFilename [cs50@flume ~/example]$ cat header.template | ./subtemplate title.txt @TITLE@ ./subtemplate: replacement file @TITLE@ nonexistent or not readable ### Test on a different template [cs50@flume ~/example]$ cat image.template | ./subtemplate @TITLE@ title.txt

@CAPTION@

Date: @DATE@

Credit: @CREDIT@

Copyright: @COPYRIGHT@

### Make some special templates to test special cases. ### Template that is wholly a target string: [cs50@flume ~/example]$ echo @TITLE@ > test.template [cs50@flume ~/example]$ cat test.template | ./subtemplate @TITLE@ title.txt Fall 2015 ### Template that does not end in a newline [cs50@flume ~/example]$ echo -n @TITLE@ > test.template [cs50@flume ~/example]$ cat test.template @TITLE@[cs50@flume ~/exacat test.template | ./subtemplate @TITLE@ title.txt Fall 2015 ### Template file that ends in partial target string [cs50@flume ~/example]$ echo -n @TITLE > test.template [cs50@flume ~/example]$ cat test.template | ./subtemplate @TITLE@ title.txt @TITLE[cs50@flume ~/example]$ ### Substitute for a different target [cs50@flume ~/example]$ cat header.template | ./subtemplate @HEADCAPTION@ title.txt @TITLE@

@TITLE@

Fall 2015

### Substitute for the expected target - multi-line text file [cs50@flume ~/example]$ cat header.template | ./subtemplate @HEADCAPTION@ header.txt @TITLE@

@TITLE@

Fall colors at Dartmouth, 2015. Photos courtesy of Dartmouth Flickr site.

### Pipeline again, but reverse order [cs50@flume ~/example]$ cat header.template | ./subtemplate @HEADCAPTION@ header.txt | ./subtemplate @TITLE@ title.txt Fall 2015

Fall 2015

Fall colors at Dartmouth, 2015. Photos courtesy of Dartmouth Flickr site.

### Empty stdin [cs50@flume ~/example]$ cat /dev/null | ./subtemplate @HEADCAPTION@ header.txt | ./subtemplate @TITLE@ title.txt ### match at end of the file [cs50@flume ~/example]$ echo -n partial match at end @TITLE@ > test.template [cs50@flume ~/example]$ cat test.template | ./subtemplate @TITLE@ title.txt partial match at end Fall 2015 ### partial match at end of the file [cs50@flume ~/example]$ echo -n partial match at end @TITLE > test.template [cs50@flume ~/example]$ cat test.template | ./subtemplate @TITLE@ title.txt partial match at end @TITLE[cs50@flume ~/example]$ [cs50@flume ~/example]$ exit logout Connection to flume closed. [DFK:~]