From: Andreas K. <and...@Ac...> - 2006-01-31 19:35:38
|
> Andreas Kupries wrote: > > > touch PATH > > Creates empty file at PATH. Deletes any pre-existing > > file in this location. > > For Unix users, "touch" would be a _very bad_ name for this operation -- > the shell command `touch` only updates the modification time > of existing files, it doesn't truncate them. See the recent discussion. clearf/truncate, etc. And arguments that we do not need this command at all. > > getfile PATH > > Returns contents of file. Assumes that file is text, > > and in system encoding. > > putfile PATH TEXT > > Writes text as the new content of the file. > > Note: Does _not_ add a traling \n to the data. > > I usually call these "readFile" and "writeFile", after > the analogous Haskell Standard Prelude functions. Ok, and another proposal for the names :) > > insertat PATH AT DATA > > Inserts data at offset AT into the file. > > > > removeat PATH AT N > > Removes N characters from the file, starting at > > offset AT. > > No comment on the names My latest thoughts are to name them 'insert', 'remove', 'replace'. >, but these operations would be > very useful to have in tcllib just so we'd have a short > answer when newbies ask how to do this (which seems to > come up once a month or so on c.l.t. :-) :) > > hack_file PATH KEY VAL ... > > Takes the list of keys and values and applies them to > > the contents of the file, via [string map]. > > hack_file_copy PATHin PATHout KEY VAL ... > > Like hack_file, but writes the result to a different > > file. > These seem rather special-purpose. A useful factor of the > first one might be: > updateInPlace $filename $cmdPrefix -- > Reads the contents of file $filename and passes it > as an additional argument to $cmdPrefix. Overwrites > the original file with the return value of the command. > If the command raises an error, the original file > is not modified. > > Then 'hack_file' could be expressed as: > > updateInPlace PATH [list string map [list KEY VAL...]] The current implementation of hack_file is, in essence: proc hack_file {path args} { putfile $path [string map $args [getfile $path]] } Your proposal strikes me as a nice generalization of this. Thanks. hack_file_copy is a bit redundant anyway. It can be expressed as 'file copy + hack_file', although writing it directly as get/map/put with different paths might be more efficient. Hm. updateInPlace, I was putting some 'patch' commands into the collection, similar to the hack_file, but not quite (only one key to replace, but multiple values, for each occurence of the key). Bianry patching anyone ? Well, your proposal covers this as well, just a different update command ... Yes, this seems to be the generally useful core. Note: slightly OT, because this is not about the new commands, but an existing one ... We have some proposals for extending fileutil::find in the tracker (see RFEs). I thought yesterday that we can handle that by exposing the core of going through a dir-hierarchy via a 'traversal' command taking options. -- Andreas Kupries <and...@Ac...> Developer @ http://www.ActiveState.com, a division of Sophos Tel: +1 604 484 6491 |