SNR

A small console utility to automatically replace the fragments of lines in text files. I did not think that once again have to reinvent the wheel, but I had to ...

I was need to make a simple thing, to put the prefix "hwg_" to all the names of HwGUI functions, which wasn't prefixed yet, and, consequently, make automatic search and replace - modify the source code for all calls to these functions. It would seem that there are a lot of ready tools to do this. Any text editor does it, and there are many utilities for this. But, as often happens, after a closer examination it became clear that it isn't so easy, every toll miss something important.

What I actually need:
    - operation to be performed automatically with a group of files that are possibly in sub-directories;
    - to be able to specify an arbitrary number of pairs find/replace, for to not make this operation for each pair separately;
    - to change exactly what is needed, for example, the string cSelectFile := SelectFile(par1, ...), has been replaced by cSelectFile := hwg_SelectFile(par1, ...), it is usually adjusted option "Whole words".

I downloaded a few utilities, using the links from Yandex and tried them. Some had to install, that itself for such a simple program looks weird and therefore a bit annoying. But everywhere there was something missing, mostly paragraph 2 of my claims. In one of them, sort of, was all that was needed, but it was shareware, and the interface was, hmm..., not very good - I took time to deal with it and in some odd way it made a find/replace the files that I was not going to touch.

I guess I just did not show enough patience, but in the end, thought it best write such a tool myself. Then, once begun, added the options that are not needed to perform the task, but can need later - to me, or maybe someone else.

So, Snr2, to love and to pray ( download link, the source code and executable file ).

       snr2 [-tTemplateName] | [-hheaderName] | [-sSearchString-iInsertString]
            [-r[+|-]] [-c[+|-]] [-w[+|-]] [-f[+|-]] [-a[+|-]] [-v[+|-]] [-b[ext]] mask
    
    -tTemplateName - specifies the name of the text file (TemplateName) with the separated by spaces couples find/replace, line by line;
    -hheaderName - specifies the name of the header file (headerName) - more on this later;
    -sSearchString - fragment, which must be found;
    -iInsertString - fragment, which must replace the found SearchString;
    -r - recursive search in sub-directories, the default is off;
    -c - option "Case sensitive", is disabled by default;
    -w - option "Whole words" - "Whole", is on by default, to turn off, use the-w-;
    -f - this option allows to replave function names only (it checks, does the parenthesis follow the name), is off by default,;
    -a - automatic replacement by default. If this option is disabled with-a-, then before each change displays appropriate fragment and the question: to replace or not, to which you must respond by pressing Enter, or "y" (yes) or "n" - No ;
    -v - a more detailed change log is disabled by default;
    -b[ext] . By default, the old file is left intact and a new file is created with the extension .new. If you set option -b, the old file is saved with the extension you specified (ext, if you do not specify, the default extension - orig), and the new takes its place;
    mask - mask of files that should be changed, it can be a single file name. If you need to use multiple masks, you can separate them by ";": *.prg;*.ch .

At the command prompt, or you must specify the -tTemplateName, or -hheaderName, or a pair of values -sSearchString and -iInsertString. If you have one pair of values for the substitution, for example, SelectFile on hwg_SelectFile, you can set it in the command line:

       snr2 -sSelectFile -ihwg_SelectFile *.prg
    

If several pairs, you need to create a text file of this kind here:

    SelectFile hwg_SelectFile
    SaveFile hwg_SaveFile
    Printersetup hwg_Printersetup
    
and run snr2:
       snr2 -tshablon.txt *.prg
    
where shablon.txt - the name of the file.

Especially for Harbour programmers: as a template, you can use the header (*.h, *.ch) file, where the pairs find/replace are declared with the directive #xtranslate:

    #xtranslate Selectfile([]) => hwg_Selectfile()
    

So, to replace the function calls to new in all of your source files, written in [x]Harbour+HwGUI, it is necessary to do the following:

       snr2 -hhwgcompat.ch -f -r *.prg
    
because it is the file hwgcompat.ch where matching directives #xtranslate are collected.

And a couple words more about the text template, defined by the option -t. If the text fragments that you want to find/replace has spaces inside, the corresponding line must be enclosed in single or double quotes, for example:

    "do while .T." "DO WHILE TRUE"
    
The same applies to the command line options -s, -i:
       snr2 -s"do while .t." -i"DO WHILE TRUE" *.prg
    

Snr2 creates a log snr2.log. At the beginning there are listed the find/replace pairs, options, and then a list of changed files. If the -w option was specified, then the log includes fragments of replaced lines.


There is a GUI version of this utility - Fileagent