Jez Higgins

Freelance software grandad
software created
extended or repaired


Follow me on Mastodon
Applications, Libraries, Code
Talks & Presentations

Hire me
Contact

Older posts are available in the archive or through tags.

Feed

The Forest Road Reader, No 2.71 : STinC++ - edit, five minute insert

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

I had a few minutes last night, so cracked open the laptop[1] and implemented the insert command.

(.) i insert text before line (text follows)

Text entered with a, c and i is terminated with a line containing just a ..

I popped another branch into the if ladder in editor::process …​

void editor::process(std::istream& in, std::ostream& out) {
  while(in.peek() != eof) {
    auto line = stiX::getline(in);

    if (line == "=")
      out << buffer_.dot() << "\n";
    else if (line == "i")
      do_insert(in, buffer_);
    else
      out << "?\n";
  }
}

... then implemented the function I’d just imagined.

void do_insert(std::istream& in, edit_buffer& buffer) {
  while(in.peek() != eof) {
    auto line = stiX::getline(in);

    if (line == ".")
      return;

    buffer.insert_before(buffer.dot(), line);
  }
}

And that was it.

I was actually a little startled, because I was expecting it to take slightly longer, but I just typed it in and it worked.

But, but, but …​

There’s a lot not to like here. The if ladder is ugly. The gross structures of process and do_insert are identical. do_insert is not a great name. Within the if ladder there are commands implemented both in-line and out-of-line.

But it doesn’t matter.

At least, it doesn’t matter at the moment.

We know there’s more functionality to add, we can even expect to be able to reuse parts of the code we already have - how different is append from insert really - but until we actually write that code we don’t know what those changes are, how they will affect the structure of the code, and what the future state of the program will be.

With that in mind, I’d be liar if I said I didn’t have expectations about that future state. There are certain familiar shapes I expect to emerge, but the details? No, I have no real clue.

I’ll take six lines that work now over any number of imagined future lines.

Source Code

The full source code, with tests and what-not, for edit, indeed the whole project, is available in the stiX GitHub repository. Forgive me if I suggest that you might find the history more interesting that the naked source.

Endnotes

This whole endeavour relies on 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 are both still in print, but new copies are, frankly, ridiculously expensive. Happily, there are plenty of second-hand copies around, or you can borrow them from The Internet Archive using the links above.

For this project I’ve been using JetBrain’s CLion, which I liked enough to buy and keep renewing a license for. I use it all the time now.


1. A Dell XPS 13 running Ubuntu Linux, since you’re asking. It’s terrific.

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


Jez Higgins

Freelance software grandad
software created
extended or repaired

Follow me on Mastodon
Applications, Libraries, Code
Talks & Presentations

Hire me
Contact

Older posts are available in the archive or through tags.

Feed