Jez Higgins

Freelance software grandad
software created
extended or repaired


Follow me on Twitter
My code on GitHub
Talks & Presentations

Hire me
Contact

Older posts are available in the archive or through tags.

Feed

Tuesday 14 April 2020 The Forest Road Reader, No 2.45 : STinC++ - concat

STinC++ rewrites the programs in Software Tools in Pascal using C++

Having made particular reference to test-driven development when talking about include, I’m going to turn around and say I didn’t bother for this next program, concat. It’s so simple, I just went straight in. Now this is dangerous talk, because even the simplest seeming code can have awkward corners, but in this case I think I’m ok and in a few moments, I hope you’ll agree.

concat concatenates a set of named input files onto the console. Kernighan and Plauger cite the example of combining multiple files together to be a piped into a program that can only read its standard input. You’ve used cat - you know what’s going on here.

We can put concat together using two functions we’ve already written - make_arguments from echo mid-way through Chapter 2 and used frequently since, combined with copy from waaay back last August in the very first program of the first chapter.

#include <iostream>
#include <fstream>
#include "../../lib/arguments.h"
#include "../../lib/copy.h"

int main(int argc, char const* argv[]) {
  auto filenames = stiX::make_arguments(argc, argv);

  for (auto filename : filenames) {
    std::ifstream file(filename);
    if (!file) {
      std::cerr << "Could not open '" << filename << "'\n";
      continue;
    }

    stiX::copy(file, std::cout);
  }
}

Bish, bosh. We’re done.

Source code

Source code for this program, indeed the whole project, is available in the stiX GitHub repository. concat is third program of Chapter 3.

Endnotes

This whole endeavour relies Software Tools in Pascal and Software Tools, both by Brian W Kernighan and PJ Plauger. I love these books and commend them to you. They’re both still in print, but new copies are, frankly, just ridiculously expensive. Happily, here are plenty of second-hand copies floating round, or you can borrow them from the The Internet Archive using the links above.

For this project I’ve been using JetBrain’s CLion, which I liked enough to buy a license. CLion uses CMake to build projects. My previous flirtations with CMake, admittedly many years ago, weren’t a huge success. Not so this time - it’s easy to use and works a treat.

The test harness I’m using is Catch. I’ve been aware of Catch pretty much since it was first released, but this is my first time really using it. I like it and plan to use it again.


Tagged code, and software-tools-in-c++


Jez Higgins

Freelance software grandad
software created
extended or repaired

Follow me on Twitter
My code on GitHub
Talks & Presentations

Hire me
Contact

Older posts are available in the archive or through tags.

Feed