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.49 : STinC++ - archive

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

And so, my friends, we’re into the final stretch of chapter 3. Chapter 3 broadly follows the pattern of its predecessors - presenting several small programs exploring various aspects of the matter under examination, before pulling the threads together into a slightly larger program. This chapter is all about files and file handling - opening files, reading their contents, creating and writing new files - and the last program of the chapter brings all that together in archive, a file archiving utility not unlike tar or cpio.

PROGRAM

archive maintain file archive

USAGE

archive -cmd aname [ file …​ ]

FUNCTION

archive manages any number of member files in a single files, aname, with sufficient information that members may be selectively added, extracted, replaced, or deleted from the collection. -cmd is a code that determines the operation to be performed.

-c create a new archive with the named members

-d delete named members from archive

-p print named members on standard output

-t print table of archive contents

-u update named members or add at end

-x extract named members from archive

In each case, the "named members" are the zero or more filenames given as arguments following aname. If no arguments follow, then the "named members" are taken as all of the files in the archives, except for the delete command -d, which is not so rash. archive complains if a file is named twice or cannot be accessed.

The -t command writes one line to the output for each named member, consisting of the member name and a string representation of the file lenght, separated by a blank.

The create command -c makes a new archive containing the named files. The update command -u replaces existing named members and adds new files onto the end of an existing archive. Create and update read from, and extract writes to, files whose names are the same as the member names in the archive. An intermediate version of the new archive file is first written to artemp; hence this filename should be avoided.

An archive is a concatentation of zero or more entities, each consisting of a header and an exact copy of the original file. The header format is
-h- name length

EXAMPLE

To replace two files in an existing archive, add a new one, then print the table of contents:

 archive -u archfile old1 old2 new1
 archive -t archfile

It is, frankly, a bit of beast. The discussion of archive takes as many pages as does all the preceding programs in the chapter. I suspect I’ll probably end up with a similar ratio, but rather than write one monstrous great article (which would be tiring and, frankly, dispiriting for all of us) I’m going to take the excellent advice Kernighan and Plauger give at the start of the section.

The archive program is a natural for what we like to call "left-corner" construction. The idea is to nibble off a small, manageable corner of the program - a part that does something useful - and make that work. Once it does, more and more pieces are added until the whole thing is done. If care is taken with the original design, later pieces should fit in relatively smoothly. Debugging and testing are easier, for the pieces are only added one at a time. Furthermore, if you decide to scrap the whole thing at some point, you are scrapping only that fraction built so far.

They continue …​

The beauty of left-corner construction is that the progam does some part of its job very early in the game. By implementing the most useful functions first, you get an idea of how valuable the program will be before investing any time in the difficult or esoteric services (which often prove to be unnecessary or unwanted anyway). You also ensure that the simpler and more common functions are handled simply, which leads to greater efficiency in the end.

Again, Software Tools in Pascal was written in the 1970s and published in 1981. Collectively it took us another twenty years for this kind of thing to become remotely mainstream, and even now there are people who’ll tell you this kind of incremental development, focusing on software that actually does stuff, is dangerous nonsense and what we really need is more design documents and planning meetings.

So anyway, I guess it makes sense to start with -c create an archive. Join me again tomorrow - same C++ time, same C++ channel - as I dive in and have a go.


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