home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / TOKEN.AZM / TOKEN.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  6.1 KB  |  201 lines

  1. ;----------------------------------------------------------------
  2. ;         This is a module in the ASMLIB library.
  3. ;
  4. ; This is the token processing module. It is responsible for
  5. ; all the token processing and contains the following code
  6. ;
  7. ; 1) DEFDEL    Define Token delimeters
  8. ; 2) GETTOK    Get a token from a string
  9. ; 3) TABSRC    Search a table for a token
  10. ;
  11. ;            Written        R.C.H.        22/10/83
  12. ;            Last Update    R.C.H.        01/11/83
  13. ;----------------------------------------------------------------
  14. ;
  15.     name    'token'
  16.     extrn    cmpstr,delstr
  17.     public    gettok,defdel,tabsrc
  18.     maclib    z80
  19. ;
  20. ;
  21. defdel:
  22.     sded    delims            ; Save the address of the delimeters
  23.     push    b
  24.     ldax    d            ; get size of the string
  25.     ora    a            ; return a zero if size = 0
  26.     pop    b
  27.     ret
  28. ;
  29. ; This is a major routine which must use the delimeter string to define
  30. ; where a token will end. If no delimeter then the token ends on a string
  31. ; boundary. If no delimeters then return a carry set to indicate an error.
  32. ; On entry DE -> string to extract a token from
  33. ;          HL -> Destination for the token
  34. ;           B -> maximum allowable length for a token.
  35. ;
  36. ; On exit carry set means error.
  37. ;   if destination count too small then carry returned
  38. ;   When a token is returned the delimeter that caused the return is
  39. ;    appended to the end of the string. This is always done.
  40. ;
  41. ; This module has been written with the aid of a pseudo code sketch.
  42. ;
  43. ;  start:
  44. ;       get size of input string
  45. ;        if zero then return
  46. ;       get length of delimeter string
  47. ;           if zero then return
  48. ;  main loop:
  49. ;    get a character 
  50. ;    load into destination string
  51. ;       check if a delimeter
  52. ;           if so thgen return no zer or carry
  53. ;       check if destination full
  54. ;           if so then return a carry
  55. ;       check if source empty.
  56. ;           if so then return a zero
  57. ;        goto main loop.
  58. ;
  59. ;
  60. gettok:
  61.     ldax    d
  62.     ora    a            ; is source string zero length ?
  63.     rz                ; exit with zero for end of job
  64.     shld    dst$adr            ; save destination string address
  65.     sded    src$adr            ; save source string address
  66.     mov    c,a            ; save the original length
  67.     sbcd    siz$str            ; save original size setups
  68. ;
  69. main1:    ; main loop. DE -> source, HL -> dest, B = dest size, C = source size
  70.     inx    d            ; point ot a character in the source
  71.     inx    h            ; point to next character in the dest
  72.     ldax    d            ; get a character
  73.     mov    m,a            ; save it
  74.     dcr    c            ; decrement the source size
  75.     dcr    b            ; decrement the dest size
  76. ; Check if the character was a delimeter.
  77.     call    chk$delim        ; check. return zero true if so
  78.     jrz    delim$end        ; delimetered end return
  79. ; Check if the source is empty. This is a valid return
  80.     mov    a,c
  81.     ora    a
  82.     jrz    delim$end        ; this is a delimetered end, cr did it
  83. ; check if the dest is full
  84.     mov    a,b
  85.     ora    a
  86.     jrz    end$dest        ; set carry to indicate the error
  87. ; All else means that we get another character
  88.     jmp    main1            ; keep on going
  89. ;
  90. ; Here and the destination went full prematurely so we signal the error
  91. ; and exit without saving any string lengths etc.
  92. ;
  93. end$dest:    ; end of destination buffer, signal an error
  94.     stc                ; set the carry flag
  95.     lhld    dst$adr
  96.     lded    src$adr
  97.     ret
  98. ;
  99. ; Here and the string was properly delimetered by either the source string end
  100. ; or there was a match with a token delimeter character. When this happens we 
  101. ; must change the string sizes in both the source and dest strings
  102. ;
  103. delim$end:
  104.     lded    siz$str            ; get original sizes into DE
  105.     mov    a,e            ; get original source string size
  106.     sub    c            ; take decremented size
  107.     lhld    dst$adr            ; get start address of dest string
  108.     mov    m,a            ; save size of the destination string
  109.     mov    b,a            ; save a copy for the delete
  110.     mvi    c,1             ; start at character 1
  111.      lded    src$adr            ; start of source string
  112.     call    delstr            ; delete the substring
  113.     ret
  114. ;
  115. ; This routine returns a zero if the character in A matches a delimeter
  116. ; character in the delimeter string.
  117. ;
  118. chk$delim:
  119.     push    h            ; save
  120.     push    b            ; save maximum string length bytes
  121.     lhld    delims            ; get delimeter string address
  122.     mov    b,m            ; get string size
  123. chk$loop:                ; loop here checking bytes
  124.     inx    h            ; point to next delimeter
  125.     cmp    m
  126.     jrz    chk$match        ; matches a delimeter 
  127.     djnz    chk$loop        ; loop on
  128.     mvi    a,0ffh            ; ensure zero flag off
  129. ;
  130. ; If the following is jumped then the zero flag is not turned off
  131. ; and we return saying that there was a match therefore
  132. ;
  133.     ora    a
  134. chk$match:                ; jump here if a match, zero maintained
  135.     pop    b
  136.     pop    h            ; restore all
  137.     ret
  138. ;
  139. ;----------------------------------------------------------------
  140. ;     This routine must search a table of strings for a match with the
  141. ; input string. If it matches then a return is done with the carry
  142. ; off , zero off and A= index into the table where the match happened.
  143. ;     If there was no match then return with a zero in a and a zero flag.
  144. ;
  145. ; No entry DE -> string
  146. ;          HL -> table end of table has a string size of 00.
  147. ;
  148. ; Note that the routine assumes the table is in alpha order.
  149. ; This routine uses the external referenced compare routine
  150. ;----------------------------------------------------------------
  151. ;
  152. tabsrc:
  153.     ldax    d            ; get source string length
  154.     ora    a
  155.     rz                ; return if a zero 
  156.     mvi    c,01            ; count strings checked.
  157.     shld    dst$adr            ; save table address
  158.     sded    src$adr            ; save string address
  159. ; Now we can check the strings.
  160. ; DE -> the source string
  161. ; HL -> table string
  162. tab$loop:
  163.     push    b
  164.     call    cmpstr            ; compare them
  165.     pop    b
  166.      mov    a,c
  167.     rz                ; exit with a = number checked if =
  168. ; If no carry then the table string > source string so error exit
  169.     jrc    err$exit
  170. ; Else we must index to the next string in the table
  171.     lhld    dst$adr            ; get start of the string
  172.     mov    e,m
  173.     mvi    d,00            ; make an index
  174.     dad    d            ; point to next string
  175.     inx    h            ; make up for counter byte
  176.     mov    a,m            ; is table length = 0
  177.     ora    a
  178.     rz    
  179.     shld    dst$adr            ; save it
  180.     lded    src$adr            ; get address of string again
  181.     inr    c
  182.     jr    tab$loop
  183. ;
  184. ;
  185. err$exit:
  186.     xra    a            ; indicate a not found string
  187.     stc
  188.     ret
  189. ;
  190.     dseg
  191. delims:    db    00,00        ; initialize to zero for checking
  192. src$adr    db    00,00        ; source string address
  193. dst$adr    db    00,00        ; destination string address
  194. siz$str    db    00,00        ; original string setup sizes
  195.     end
  196.  
  197.  
  198.  
  199.