INCREDIBLE INCREMENTER - finally solved the "file exist

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
DOS386
Posts: 798
Joined: Jul 02, 2005 20:55

INCREDIBLE INCREMENTER - finally solved the "file exist

Post by DOS386 »

Code: Select all

SUB SSINCREMCORE (BYVAL VL32ADR AS UINT32, BYREF VR8POS AS UINT8)

'' Input: VL32ADR - address of the input string
''        VR8POS  - position to increment, ZERO-based (1 to 255, not ZERO)

'' Output: - on success string incremented
''         - on failure VR8POS set to ZERO and string is trashed

'' - Works on a raw string (no size prefix, no ZERO termination)
'' - Doesn't care about string length (must be VR8POS < "length")
'' - Position VR8POS must be >= 1 (we never touch the ZERO'th character)
'' - Only numbers "0" to "9" and uppercase "A" to "Z" are incrementable,
''   lowercase letters and other junk are not
'' - ASM code uses only 3 registers EAX ECX EDX

DIAS UINT8 VL8POSI

  VL8POSI=VR8POS

    ASM

        xor    ecx, ecx       '' MOVNTQ ECX,0
        mov    eax, [VL32ADR]
        mov    cl, [VL8POSI]  '' May NOT be ZERO on entry !!!
        test   cl, cl         '' CMPNTQ CL,0
        jz     jj5            '' Check for ZERO, no check for too high
        add    eax, ecx       '' But we still need CL below !!!
jj0:    mov    dl, [eax]      '' Pick
        cmp    dl, 48
        jb     jj1            '' NOT incrementable nor wrappable
        cmp    dl, 90
        ja     jj1            '' NOT incrementable nor wrappable
        cmp    dl, 64
        ja     jj2            '' Letter, OK, we can increment
        cmp    dl, 57
        ja     jj1            '' NOT incrementable nor wrappable
jj2:    mov    dh, 1          '' Pre-ASS'ume "evil" wrap
        mov    ch, 48
        cmp    dl, 57
        je     jj3            '' Hit "9", wrap to "0"
        mov    ch, 65
        cmp    dl, 90
        je     jj3            '' Hit "Z", wrap to "A"
        inc    dl             '' Safe to increment
        mov    ch, dl
        dec    dh             '' MOVNTQ DH,0 | OK, done, not "evil" wrap
jj3:    mov    [eax], ch      '' POKE it back (incremented or wrapped)
        test   dh, dh         '' CMPNTQ DH,0
        jz     jj5            '' Done !
jj1:    dec    cl
        jz     jj4            '' F**K !!! (don't touch ZERO'th character)
        dec    eax            '' Safe to decrement
        jmp    jj0
        '' -------

jj4:    mov    [VL8POSI], cl  '' CL==0 | Failure | The string is trashed !!!

jj5:

    END ASM

  VR8POS=VL8POSI

END SUB '' SSINCREMCORE
Image

Download now : http://users.freebasic-portal.de/dos386/incr.zip (19 KiB) (full code with test)

Problem: an application tries to save a file but the filename is already occupied by an existing file

Scenarios:

1. Miserably fail with an ENOENT - BAD :-(

2. Silently kick the existing file (examples: FBC and most other compilers, OBJCONV, old versions of NCONVERT, FFMPEG2THEORA) - BAD :-(

3. Ask the user whether to overwrite or not (example: FFMPEG) (but what then ???)

4. Auto-rename. This is what my code is about :-) You can try some conversion many 1000 times and the previous results are kept, so you can compare (except you specify a "very bad" name with no or few possible increments at start).

EDITED 2014-Feb-01
Last edited by DOS386 on Feb 01, 2014 1:54, edited 1 time in total.
MilesAhead
Posts: 26
Joined: Jul 18, 2010 19:06

VMS file system built-in versioning

Post by MilesAhead »

One of the things I did like about the VMS operating system, was the file system had a simple versioning scheme built in. It was automatic. If you were using any text editor as example, and saved a file as MyFile.txt if it was the first one, only MyFile.txt would exist. If you saved it again, MyFile.txt would automatically be renamed to MyFile.txt;1 and the new save would be simply MyFile.txt. This would chain on as filename;1 filename;2 etc.. on and on until you ran the purge command(or the sysadmin may have set some limit I don't know.)

This way every time you saved all the previous ones were backed up without you doing anything special. To get rid of all but the 4 newest versions you ran this command on the command line:

purge MyFile.txt /keep=4

or just

purge /keep=2 to keep only the 2 newest versions of all user created files in that directory.

A guy did command line tools for Windows to try to simulate the effect. It's called Glindra. But not being built-in, it's not as cool as how the VMS did it of course. Instead of using command line Copy you would use cp command and it did the renaming. It was cool for copying files into another folder and any name collisions were handled using the rename and numbering. I think he used a tilde character instead of the VMS semicolon. It includes C++ source.

I just mentioned it because it's somewhat similar to what you are doing.
After having used the VMS scheme I always thought the action of editors to create one backup file with .bak extension was pretty lame, esp. since it changed the file type. :)

Tidy for AutoIt3 does something similar by numbering backups into a backup folder.. but that's just for source files.

Windows Ultimate Edition tries to do something similar by using Shadow Copy service to have previous versions of files. But this problem has already been solved like 40 years ago. It should be built in to NTFS by now.
DOS386
Posts: 798
Joined: Jul 02, 2005 20:55

update

Post by DOS386 »

MilesAhead wrote:the VMS operating system, was the file system had a simple versioning scheme built in. It was automatic. If you were using any text editor as example, and saved a file as MyFile.txt if it was the first one, only MyFile.txt would exist. If you saved it again, MyFile.txt would automatically be renamed to MyFile.txt;1 and the new save would be simply MyFile.txt. This would chain on as filename;1 filename;2 etc.. on and on until you ran the purge command

But this problem has already been solved like 40 years ago. It should be built in to NTFS by now.
Interesting :-) ... updated my code (see above)
Post Reply