home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / SOURCE / ALL / CW / FILES.ASM < prev    next >
Assembly Source File  |  1993-09-15  |  8KB  |  399 lines

  1. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  2. OpenFile    proc    near
  3. ;
  4. ;Open a file and if succesful, add its handle to the list.
  5. ;
  6. ;On Entry:-
  7. ;
  8. ;DS:EDX    - File name pointer as normal.
  9. ;
  10. ;On Exit:-
  11. ;
  12. ;If carry clear,
  13. ;AX    - Handle for file.
  14. ;
  15. ;else file not opened.
  16. ;
  17.     mov    ax,3d02h        ;Open with read & write access.
  18.     int    21h
  19.     jc    @@9
  20.     ;
  21.     push    ebx
  22.     push    ecx
  23.     mov    ebx,offset OpenFilesList    ;Point at the list.
  24.     mov    ecx,60
  25. @@0:    cmp    w[ebx],-1        ;free entry?
  26.     jz    @@1
  27.     add    ebx,2        ;next entry.
  28.     loop    @@0
  29.     jmp    @@8
  30. @@1:    mov    [ebx],ax        ;store this handle.
  31.     pop    ecx
  32.     pop    ebx
  33.     clc            ;restore sucess flag.
  34. @@9:    ret
  35.     ;
  36. @@8:    mov    bx,ax
  37.     mov    ah,3eh        ;close again cos not enough table space.
  38.     int    21h
  39.     pop    ecx
  40.     pop    ebx
  41.     stc
  42.     ret
  43. OpenFile    endp
  44.  
  45.  
  46. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  47. CreateFile    proc    near
  48. ;
  49. ;Create a file and if succesful, add its handle to the list.
  50. ;
  51. ;On Entry:-
  52. ;
  53. ;DS:EDX    - File name pointer as normal.
  54. ;
  55. ;On Exit:-
  56. ;
  57. ;If carry clear,
  58. ;AX    - Handle for file.
  59. ;
  60. ;else file not created.
  61. ;
  62.     mov    ah,3ch        ;Create function.
  63.     xor    cx,cx        ;normal attributes.
  64.     int    21h
  65.     jc    @@9
  66.     ;
  67.     push    ebx
  68.     push    ecx
  69.     mov    ebx,offset OpenFilesList    ;Point at the list.
  70.     mov    ecx,60
  71. @@0:    cmp    w[ebx],-1        ;free entry?
  72.     jz    @@1
  73.     add    ebx,2        ;next entry.
  74.     loop    @@0
  75.     jmp    @@8
  76. @@1:    mov    [ebx],ax        ;store this handle.
  77.     pop    ecx
  78.     pop    ebx
  79.     clc            ;restore sucess flag.
  80. @@9:    ret
  81.     ;
  82. @@8:    mov    bx,ax
  83.     mov    ah,3eh        ;close again cos not enough table space.
  84.     int    21h
  85.     pop    ecx
  86.     pop    ebx
  87.     stc
  88.     ret
  89. CreateFile    endp
  90.  
  91.  
  92. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  93. SetFilePointer    proc    near
  94. ;
  95. ;Set the file pointer position for a file.
  96. ;
  97. ;On Entry:-
  98. ;
  99. ;AL    - Method
  100. ;BX    - Handle
  101. ;DX:CX    - 32-bit position.
  102. ;
  103. ;Methods are:-
  104. ;
  105. ;0    - Absolute offset from start.
  106. ;1    - signed offset from current position.
  107. ;2    - signed offset from end of file.
  108. ;
  109.     xchg    dx,cx        ;DOS has them in a stupid order.
  110.     push    bx
  111.     mov    ah,42h        ;set pointer function.
  112.     int    21h
  113.     pop    bx
  114.     mov    cx,ax        ;fetch small result.
  115.     ret
  116. SetFilePointer    endp
  117.  
  118.  
  119. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  120. ReadFile    proc    near
  121. ;
  122. ;Read some data from a file.
  123. ;
  124. ;On Entry:-
  125. ;
  126. ;BX    - Handle
  127. ;ECX    - Length
  128. ;EDI    - Position.
  129. ;
  130.     xor    edx,edx
  131. @@0:    pushm    bx,ecx,edx,edi
  132.     cmp    ecx,65535        ;size of chunks to load.
  133.     jc    @@2
  134.     mov    ecx,65535        ;as close to 64k as can get.
  135. @@2:    mov    edx,edi
  136.     mov    ah,3fh
  137.     int    21h        ;read from the file.
  138.     popm    bx,ecx,edx,edi
  139.     jc    @@9
  140.     movzx    eax,ax        ;get length read.
  141.     add    edx,eax        ;update length read counter.
  142.     sub    ecx,eax        ;update length counter.
  143.     add    edi,eax        ;move memory pointer.
  144.     or    ecx,ecx
  145.     jz    @@8        ;read as much as was wanted.
  146.     or    eax,eax        ;did we read anything?
  147.     jz    @@8
  148.     jmp    @@0
  149. @@8:    mov    eax,edx        ;get accumulated length read.
  150.     clc
  151.     ret
  152.     ;
  153. @@9:    stc
  154.     ret
  155. ReadFile    endp
  156.  
  157.  
  158. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  159. WriteFile    proc    near
  160. ;
  161. ;Write some data to a file.
  162. ;
  163. ;On Entry:-
  164. ;
  165. ;BX    - Handle
  166. ;ECX    - Length
  167. ;ESI    - Position.
  168. ;
  169.     xor    edx,edx
  170. @@0:    pushm    bx,ecx,edx,esi
  171.     cmp    ecx,65535        ;size of chunks to load.
  172.     jc    @@2
  173.     mov    ecx,65535        ;as close to 64k as can get.
  174. @@2:    mov    edx,esi
  175.     mov    ah,40h
  176.     int    21h        ;read from the file.
  177.     popm    bx,ecx,edx,esi
  178.     jc    @@9
  179.     movzx    eax,ax        ;get length read.
  180.     add    edx,eax        ;update length read counter.
  181.     sub    ecx,eax        ;update length counter.
  182.     add    esi,eax        ;move memory pointer.
  183.     or    ecx,ecx
  184.     jz    @@8        ;read as much as was wanted.
  185.     or    eax,eax        ;did we write anything?
  186.     jz    @@9
  187.     jmp    @@0
  188. @@8:    mov    eax,edx        ;get accumulated length read.
  189.     clc
  190.     ret
  191.     ;
  192. @@9:    stc
  193.     ret
  194. WriteFile    endp
  195.  
  196.  
  197. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  198. CloseFile    proc    near
  199. ;
  200. ;Close a file and delete its handle from the list.
  201. ;
  202. ;On Entry:-
  203. ;
  204. ;BX    - Handle for file to close.
  205. ;
  206.     pushm    ax,ebx,ecx
  207.     mov    ax,bx
  208.     mov    ebx,offset OpenFilesList    ;Point at the list.
  209.     mov    ecx,60
  210. @@0:    cmp    [ebx],ax        ;right entry?
  211.     jz    @@1
  212.     add    ebx,2        ;next entry.
  213.     loop    @@0
  214.     jmp    @@9
  215. @@1:    mov    w[ebx],-1        ;clear this handle.
  216.     popm    ax,ebx,ecx
  217.     mov    ah,3eh        ;close file function.
  218.     int    21h
  219.     ret
  220. @@9:    popm    ax,ebx,ecx
  221.     stc
  222.     ret
  223. CloseFile    endp
  224.  
  225.  
  226. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  227. CloseAllFiles    proc    near
  228. ;
  229. ;Close all files in the list.
  230. ;
  231.     push    ax
  232.     push    ebx
  233.     push    ecx
  234.     mov    ebx,offset OpenFilesList    ;Point at the list.
  235.     mov    ecx,60        ;size OpenFilesList /2
  236. @@0:    cmp    w[ebx],0        ;used entry?
  237.     jnz    @@2
  238. @@1:    add    ebx,2        ;next entry.
  239.     loop    @@0
  240.     pop    ecx
  241.     pop    ebx
  242.     pop    ax
  243.     ret
  244. @@2:    push    ebx
  245.     push    ecx
  246.     mov    bx,[ebx]        ;get this handle.
  247.     mov    ah,3eh        ;close file function.
  248.     int    21h
  249.     pop    ecx
  250.     pop    ebx
  251.     jmp    @@1
  252. OpenFilesList    dw 60 dup (-1)    ;Enough for 60 files.
  253. CloseAllFiles    endp
  254.  
  255.  
  256. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  257. DeleteFile    proc    near
  258. ;
  259. ;Delete a file.
  260. ;
  261. ;On Entry:-
  262. ;
  263. ;DS:EDX    - File name to delete.
  264. ;
  265.     mov    ah,41h
  266.     int    21h
  267.     ret
  268. DeleteFile    endp
  269.  
  270.  
  271. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  272. ReadLine    proc    near
  273. ;
  274. ;Read a line of text from input file specified.
  275. ;
  276. ;On Entry:-
  277. ;
  278. ;BX    - File handle.
  279. ;DS:EDI    - Buffer address.
  280. ;
  281. ;On Exit:-
  282. ;
  283. ;AX    - Status.
  284. ;    0 - Line is in the buffer.
  285. ;    1 - EOF.
  286. ;
  287. ;CX    - Line length.
  288. ;
  289.     xor    cx,cx        ;reset bytes so far count.
  290. @@0:
  291. ;    pushm    bx,cx,edi
  292.  
  293.     call    ReadBufferByte
  294.  
  295. ;    mov    edx,edi
  296. ;    mov    cx,1
  297. ;    mov    ah,3fh
  298. ;    int    21h        ;read a byte.
  299.  
  300. ;    popm    bx,cx,edi
  301.     jc    @@9
  302.     or    ax,ax        ;read anything?
  303.     jz    @@CheckEOF
  304.     cmp    b[edi],13        ;EOL?
  305.     jz    @@CheckLF
  306.     cmp    b[edi],26
  307.     jz    @@SoftEOF        ;move to the end of the file, then do an EOL.
  308.     inc    edi
  309.     inc    cx
  310.     cmp    cx,1024        ;line getting a bit long?
  311.     jnc    @@SoftEOF
  312.     jmp    @@0        ;keep going.
  313.     ;
  314. @@CheckLF:    mov    b[edi],0        ;terminate this line.
  315.     inc    edi
  316. ;    pushm    bx,cx,edi
  317.  
  318.     call    ReadBufferByte
  319.  
  320. ;    mov    edx,edi
  321. ;    mov    cx,1
  322. ;    mov    ah,3fh
  323. ;    int    21h        ;read a byte.
  324.  
  325. ;    popm    bx,cx,edi
  326.     or    ax,ax        ;read anything?
  327.     jz    @@9        ;no LF here is an error.
  328.     cmp    b[edi],10        ;/
  329.     jnz    @@9        ;/
  330.     ;
  331. @@EOL:    mov    b[edi],0        ;terminate the line.
  332.     xor    ax,ax
  333.     clc
  334.     ret
  335.     ;
  336. @@SoftEOF:    pushm    bx,cx,edi
  337.     xor    cx,cx
  338.     mov    dx,cx
  339.     mov    ax,4202h        ;move to the end of the file.
  340.     int    21h
  341.     popm    bx,cx,edi        ;fall through to treat like hard EOF.
  342.     mov    FileBufferCount,0
  343.     ;
  344. @@CheckEOF:    or    cx,cx        ;read anything on this line yet?
  345.     jnz    @@EOL        ;do EOL this time.
  346.     ;
  347. @@EOF:    mov    b[edi],0        ;terminate the line.
  348.     mov    ax,1
  349.     clc
  350.     ret
  351.     ;
  352. @@9:    stc
  353.     ret
  354. ReadLine    endp
  355.  
  356.  
  357. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  358. InitFileBuffer    proc    near
  359.     mov    FileBufferCount,0
  360.     ret
  361. InitFileBuffer    endp
  362.  
  363.  
  364. ;==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==
  365. ReadBufferByte    proc    near
  366.     mov    ax,0
  367.     cmp    FileBufferCount,0
  368.     jnz    @@0
  369.     ;
  370.     ;Need to re-fill the buffer.
  371.     ;
  372.     pushm    eax,ebx,ecx,edx,esi,edi,ebp
  373.     mov    edi,offset FileBuffer
  374.     mov    ecx,1024
  375.     call    ReadFile
  376.     mov    FileBufferCount,eax
  377.     mov    FileBufferPosition,offset FileBuffer
  378.     popm    eax,ebx,ecx,edx,esi,edi,ebp
  379.     jc    @@9
  380.     ;
  381. @@0:    cmp    FileBufferCount,0    ;still zero?
  382.     jz    @@8
  383.     inc    ax        ;getting a byte then.
  384.     pushm    esi,edi
  385.     mov    esi,FileBufferPosition
  386.     movsb
  387.     mov    FileBufferPosition,esi
  388.     dec    FileBufferCount
  389.     popm    esi,edi
  390.     ;
  391. @@8:    clc
  392. @@9:    ret
  393. ReadBufferByte    endp
  394.  
  395.  
  396. FileBufferCount dd 0
  397. FileBufferPosition dd 0
  398. FileBuffer    db 2048 dup (?)
  399.