home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / BEEHIVE / UTILITYS / DWIM.ARC / DWIM.MAC < prev   
Text File  |  1990-07-21  |  5KB  |  264 lines

  1. ;DWIM - Do What I Mean
  2. ;
  3. ;given a file name which doesn't match any known file name
  4. ;DWIM will attempt to automatically correct for 
  5. ;the four most commonly occurring typographic errors -
  6. ;    1) single character substitution
  7. ;    2) single character omission
  8. ;    3) single character inclusion
  9. ;    4) adjacent character transposition
  10. ;
  11. ;as DWIM generates each possible alternative, it tests
  12. ;for validity against the "dictionary" of the cp/m file
  13. ;directory. When a (possibly empty) set of valid files
  14. ;has been generated, the user is prompted to choose from
  15. ;a menu of possible commands, one of which hopefully will
  16. ;be what the user meant to type.
  17.  
  18. ;the file is assumed to have the correct extension (probably .COM)
  19. ;which gives 4n-3 possible corrections for a file length 8.
  20. ;This requires 29 searches of the directory which may make DWIM
  21. ;slower than just retyping the erroneous command
  22. ;this depends entirely upon the speed of your disk.
  23.  
  24. FCB    equ    5Ch
  25. FNAM    equ    FCB+1
  26. FCBsize    equ    36
  27. namLen    equ    12
  28. BDOS    equ    5
  29. PSTR    equ    9
  30. srchFirst equ    17
  31. srchNext equ    18
  32. setDMA    equ    26
  33.  
  34. ;DDMA    equ    ???
  35.  
  36. start:    ld    (stSav),sp
  37.     ld    hl,stack
  38.     ld    sp,hl
  39.  
  40.     ld    c,setDMA
  41.     ld    de,DDMA
  42.     call    BDOS
  43.  
  44.     call    subst
  45.     ld    sp,(stSav)
  46.     ret
  47.  
  48. ;Substitute '?' for each character in turn, to catch the typo
  49. ;        of substitution ie: "tha cut sitz un yhe met"
  50.  
  51. subst:    ld    hl,FCB        ;point to file name
  52.     ld    de,tFCB        ;point to subsidiary file name
  53.     ld    bc,FCBsize    ;file name size
  54.     ldir            ;duplicate file name
  55.  
  56.     ld    hl,tFCB+1    ;start of file name
  57.     ld    a,9        ;only want first
  58.     ld    (charCnt),a    ; 8 characters
  59.  
  60. subNxt:    dec    a        ;test
  61.     ld    (charCnt),a    ; end of
  62.     jr    z,insrt        ;  name
  63.  
  64.     ld    a,(hl)        ;store
  65.     ld    (currChar),a    ; current
  66.  
  67.     and    7fh        ;blank
  68.     cp    ' '        ; terminates
  69.     jr    z,insrt        ;  name
  70.     
  71.     ld    a,'?'        ;substitute '?'
  72.     ld    (hl),a        ; for current
  73.  
  74.     push    hl
  75.     call    srch
  76.     pop    hl
  77.  
  78.     ld    a,(currChar)    ;restore
  79.     ld    (hl),a        ; original
  80.  
  81.     ld    a,(charCnt)    ;recover length
  82.     inc    hl        ;increment ptr
  83.     jr    subNxt
  84.  
  85. ;Insert    '?' between each pair of characters to catch typos
  86. ;        like eg "te ct st n th ma"
  87.  
  88. insrt:    ld    hl,FCB+1    ;shift
  89.     ld    de,tFCB+2    ; name
  90.     ld    bc,7        ;  right
  91.     ldir            ;   one char
  92.     ;overwrites to the right - hence no problem with garbage
  93.  
  94.     ld    hl,tFCB+1    ;point to file name
  95.     ld    de,FCB+1
  96.     ld    a,8        ;only 7 insertions
  97.     ld    (charCnt),a    ; possible
  98.  
  99. insNxt:    dec    a        ;test
  100.     ld    (charCnt),a    ; end of
  101.     jr    z,delet        ;  name
  102.  
  103.     ld    a,'?'        ;simulate
  104.     ld    (hl),a        ; insertion
  105.  
  106.     push    hl
  107.     push    de
  108.     call    srch
  109.     pop    de
  110.     pop    hl
  111.  
  112.  
  113.     ld    a,(de)        ;restore original
  114.     ld    (hl),a        ; character
  115.  
  116.     inc    hl        ;increment pointers
  117.     inc    de        ;/
  118.     ld    a,(hl)        ;test next char
  119.     and    7fh        ; blank
  120.     cp    ' '        ;  terminates
  121.     jr    z,delet        ;   file name
  122.  
  123.     ld    a,(charCnt)    ;recover count
  124.     jr    insNxt
  125.  
  126. delet:    ld    hl,FCB+2    ;point to file name
  127.     ld    de,tFCB+1    ;point to subsidiary file name
  128.     ld    bc,7        ;file name size
  129.     ldir            ;duplicate file name
  130.  
  131.     ld    a,'?'        ; garbage
  132.     ld    (de),a        ;  to right
  133.  
  134.     ld    hl,tFCB+1    ;point to file name
  135.     ld    de,FCB+1    ;point to original
  136.     ld    a,8        ;only 7 deletions
  137.     ld    (charCnt),a    ; possible
  138.  
  139. delNxt:    dec    a        ;test
  140.     ld    (charCnt),a    ; end of
  141.     jr    z,transp    ;  name
  142.  
  143.     push    hl
  144.     push    de
  145.     call    srch
  146.     pop    de
  147.     pop    hl
  148.  
  149.     ld    a,(de)        ;overwrite char
  150.     ld    (hl),a        ; to simulate deletion
  151.  
  152.     and    7fh        ;test char -
  153.     cp    ' '        ; blank terminates
  154.     jr    z,transp    ;   file name
  155.  
  156.     inc    hl        ;increment pointer
  157.     inc    de        ;/
  158.     ld    a,(charCnt)    ;recover count
  159.     jr    delNxt
  160.  
  161. transp:    ld    hl,FCB        ;point to file name
  162.     ld    de,tFCB        ;point to subsidiary file name
  163.     ld    bc,FCBsize    ;file name size
  164.     ldir            ;duplicate file name
  165.  
  166.     ld    hl,tFCB+1    ;start of file name
  167.     ld    a,8        ;only want
  168.     ld    (charCnt),a    ; 7 transpositions
  169.  
  170. traNxt:    dec    a        ;test
  171.     ld    (charCnt),a    ; end of
  172.     jr    z,done        ;  name
  173.  
  174.     ld    a,(hl)        ;store
  175.     ld    (currChar),a    ; current
  176.  
  177. ;swap characters
  178.     ld    b,(hl)        ;get curr char
  179.     inc    hl        ;get next
  180.     ld    a,(hl)        ; char
  181.     ld    (hl),b        ;swap current char
  182.     dec    hl        ; and
  183.     ld    (hl),a        ;  next char
  184.  
  185.     and    7fh        ;blank
  186.     cp    ' '        ; terminates
  187.     jr    z,done        ;  name
  188.  
  189.     push    hl
  190.     call    srch
  191.     pop    hl
  192.  
  193. ;unswap chars
  194.     ld    b,(hl)        ;get curr char
  195.     inc    hl        ;get next
  196.     ld    a,(hl)        ; char
  197.     ld    (hl),b        ;swap current char
  198.     dec    hl        ; and
  199.     ld    (hl),a        ;  next char
  200.  
  201.     ld    a,(charCnt)    ;recover length
  202.     inc    hl        ;increment ptr
  203.     jr    traNxt
  204.  
  205. ;completed all variations
  206. done:    ret
  207.  
  208. ;perform search - plug your favourite file search in here
  209. srch:    ld    de,tFCB        ;look for file
  210.     ld    c,srchFirst    ; in directory
  211.     call    BDOS        ;/
  212.  
  213. srch1:    or    a        ;set status
  214.     ret    M        ;match not found
  215.     call    action
  216.     ld    de,tFCB        ;look for
  217.     ld    c,srchNext    ; next
  218.     call    BDOS        ;  match
  219.     jr    srch1        ;keep looking
  220.  
  221. ;print the attempted file name
  222. action:    ld    hl,DDMA
  223.     add    a,a
  224.     add    a,a
  225.     add    a,a
  226.     add    a,a
  227.     add    a,a
  228.     ld    e,a
  229.     ld    d,0
  230.     add    hl,de
  231.     ld    de,prtBlk
  232.     ld    bc,namLen
  233.     ldir
  234.  
  235.     ld    a,10
  236.     ld    (de),a
  237.     inc    de
  238.     ld    a,13
  239.     ld    (de),a
  240.     inc    de
  241.     ld    a,'$'
  242.     ld    (de),a
  243.  
  244.     ld    c,PSTR
  245.     ld    de,prtBlk
  246.     call    BDOS
  247.     ret
  248.  
  249. ;Variable area
  250. charCnt:    defb    0
  251. currChar:    defb    0
  252. stSav:    defw    0
  253.  
  254. tFCB:    defs    FCBsize
  255.  
  256. prtBlk:    defs    FCBsize
  257.  
  258. DDMA:    defs    128
  259.  
  260.     defs    40
  261. stack    equ    $
  262.  
  263.     END
  264.