home *** CD-ROM | disk | FTP | other *** search
/ Arcade Explosion / ae.mdf / arcexp / descent / levels / utils / dthogexe / splithog.asm < prev    next >
Assembly Source File  |  1995-04-06  |  14KB  |  530 lines

  1. ;=============================================================================
  2. ;FILE:    SPLITHOG.ASM
  3. ;
  4. ;DESC:    Splits a Descent .HOG file into separate files
  5. ;
  6. ;NOTES:   Uses Spontaneous Assembly 3.0.  Can be modified to not use it
  7. ;         at all....
  8. ;
  9. ;         Built with MASM 6.1 and Spontaneous Assembly 3.0.
  10. ;         Use the /cp:1 link option for automatic memory footprint reduction
  11. ;         at load time:
  12. ;
  13. ;         ml /c splithog.asm
  14. ;         link /cp:1 starts splithog,splithog,,sas;
  15. ;
  16. ;         This source code is placed into the public domain.
  17. ;
  18. ; Version 1.0, 03/25/95:  Initial release
  19. ;         1.1, 04/06/95:  Added ESC checking
  20. ;=============================================================================
  21.  
  22.  
  23. _model    equ  <small>
  24. include        MODEL.INC
  25. include        CONSOLE.INC
  26. include        FILECNTL.INC
  27. include        FILEIO.INC
  28. include        IMATH.INC
  29.  
  30. READWRITEBUFR  equ  63 * 1024
  31. NAMELENGTH     equ  13
  32.  
  33.  
  34. .codeseg    lib
  35.  
  36. .extrn    start:auto, exit:auto, exit_ok:auto
  37. .extrn    console_init:auto
  38. .extrn    get_version:auto
  39. .extrn    clr_region:auto
  40. .extrn    goto_xy:auto
  41. .extrn    openex_h:auto
  42. .extrn    cput_str:auto
  43. .extrn    lseekb_h:auto
  44. .extrn    read_h:auto
  45. .extrn    create_h:auto
  46. .extrn    write_h:auto
  47. .extrn    word_to_asc:auto
  48. .extrn    dword_to_dec:auto
  49. .extrn    arg_count:auto
  50. .extrn    arg_next:auto
  51. .extrn    str_len:auto
  52.  
  53. .ends
  54.  
  55.  
  56. .dataseg
  57.  
  58. .extrn    e_code:word
  59.  
  60. SizeOfCreate   dword     0              ; bytes in file to be created
  61. MemSeg         word      0              ; read/write buffer segment
  62. HogHandle      word      0              ; handle of HOG file
  63. NewHandle      word      0              ; handle of FileToCreate file
  64. LogHandle      word      0              ; handle of results log file
  65. BigHogFile     byte      "DESCENT.HOG",0,0,0
  66. FileToCreate   byte      13 dup(0),0
  67. CrLf           byte      13,10,0
  68. ScratchBuffer  byte      130 dup(0)     ; oversized buffer in case user
  69.                                         ; really screws up command line
  70. Spaces1        byte      6 dup(" "),0
  71. Spaces2        byte      17 dup(" ")
  72. LogFile        byte      13 dup(0)      ; name of results log file
  73. ModulesOf      byte      13,10,"Modules of file ",0
  74. ColonAndEnd    byte      ":",13,10,"(file name, size in bytes)",13,10,10,0
  75. ResultsAlsoIn  byte      13,10,"Results logged to file ",0
  76. PressESC       byte      "SPLITHOG.EXE  1.1   by Bob Clarke",13,10
  77.                byte      "Splits Registered Descent HOG file into components"
  78.                byte      13,10,10
  79.                byte      "You may press ESC to interrupt program run."
  80.                byte      13,10,"Press a key to begin.",13,10,10,0
  81. Interrupted    byte      13,10,"*** Processing interrupted by user ***",13,10,0
  82.  
  83.  
  84. ; Error messages
  85.  
  86. MemAllocError  byte      "Error allocating read/write buffer.",13,10,0
  87. NoHogOpen      byte      "Can't open .HOG file.", 13,10,0
  88. ErrorCodeMsg   byte      "Error code:      ",0
  89. Overflow       byte      "Overflow subtracting word from dword!",13,10,0
  90. HogReadError   byte      "Error reading HOG file.",13,10,0
  91. HogWriteError  byte      "Error writing to new file.",13,10,0
  92. CreateError    byte      "Error creating ",0
  93. LogWriteError  byte      "Error writing to log file.",13,10,0
  94. LogCreateError byte      "Error creating log results file.",0
  95.  
  96. .ends
  97.  
  98.  
  99. IF NOT __TINY__
  100. .stackseg
  101. .public        stack_start
  102. .label        stack_start    word
  103.         db    1024 dup(?)    ;define a 1024 byte stack
  104. .ends
  105. ENDIF
  106.  
  107.  
  108. ;=============================================================================
  109. ;FUNC:    MAIN
  110. ;
  111. ;DESC:    Main body of program.
  112. ;
  113. ;IN:      DX        segment address of PSP
  114. ;
  115. ;ASUMS:   DS,ES     @DATASEG  (same as @CODESEG in TINY model)
  116. ;         SS        @STACKSEG (same as @CODESEG in TINY model)
  117. ;=============================================================================
  118.  
  119. .codeseg
  120.  
  121. IF __TINY__
  122. assume        cs:@codeseg, ds:@dataseg, es:@dataseg, ss:@dataseg
  123. ELSE
  124. assume        cs:@codeseg, ds:@dataseg, es:@dataseg, ss:@stackseg
  125. ENDIF
  126.  
  127. .public        main
  128. .proc        main        auto
  129.  
  130.           cld
  131.           .call console_init
  132.           .call get_version             ;; init for use of file sharing flags
  133.  
  134.  
  135. ; read the command line, looking for a .HOG file name
  136.  
  137.           .call arg_count
  138.           or   cx,cx
  139.           jz   UseDescentHog
  140.  
  141.           mov  si,offset ScratchBuffer
  142.           .call arg_next
  143.           mov  di,offset BigHogFile
  144.           mov  cx,13
  145.           rep  movsb
  146.  
  147. ; clear the screen and home the cursor
  148.  
  149. UseDescentHog:
  150.           .call clr_region
  151.           xor  ax,ax
  152.           .call goto_xy
  153.           mov  si,offset PressESC
  154.           .call cput_str
  155.           xor  ah,ah
  156.           int  16h
  157.  
  158. ; allocate read/write buffer
  159.  
  160.           mov  ah,48h
  161.           mov  bx,READWRITEBUFR shr 4
  162.           int  21h
  163.           jnc  GotMem
  164.           mov  [e_code],ax
  165.           mov  si,offset MemAllocError
  166.           .call cput_str
  167.           call ErrorOut
  168.           jmp  NoRelease
  169. GotMem:   mov  [MemSeg],ax
  170.  
  171. ; open .HOG file
  172.  
  173.           mov  si,offset BigHogFile
  174.           mov  al,O_RDONLY+O_DENYNO
  175.           .call openex_h
  176.           jnc  @F
  177.           mov  si,offset NoHogOpen
  178.           .call cput_str
  179.           jmp  AllDone
  180.  
  181. Oops:     call ErrorOut
  182.           jmp  AllDone
  183.  
  184. ; create the log file
  185.  
  186. @@:       mov  [HogHandle],bx
  187.           mov  si,offset BigHogFile
  188.           mov  di,offset LogFile
  189. LogLoop:  lodsb
  190.           stosb
  191.           cmp  al,"."
  192.           je   LoopOut
  193.           or   al,al               ; shouldn't happen...
  194.           jnz  LogLoop
  195. LoopOut:  mov  ax,"OL"
  196.           stosw
  197.           mov  al,"G"
  198.           stosb
  199.           xor  al,al
  200.           stosb
  201.           mov  si,offset LogFile
  202.           mov  ax,FA_NORM
  203.           .call create_h
  204.           jnc  @F
  205.           mov  si,offset LogCreateError
  206.           .call cput_str
  207.           jmp  short Oops
  208. @@:       mov  [LogHandle],bx
  209.  
  210. ; write header to log file
  211.  
  212.           mov  si,offset ModulesOf
  213.           .call str_len
  214.           .call write_h
  215.           jnc  @F
  216. LogWErr:  mov  si,offset LogWriteError
  217.           .call cput_str
  218.           jmp  short Oops
  219.  
  220. @@:       mov  si,offset BigHogFile
  221.           .call str_len
  222.           .call write_h
  223.           jc   LogWErr
  224.           mov  si,offset ColonAndEnd
  225.           .call str_len
  226.           .call write_h
  227.           jc   LogWErr
  228.  
  229. ; move past 3-byte header in .HOG file
  230.  
  231.           mov  dx,0
  232.           mov  ax,3
  233.           mov  bx,[HogHandle]
  234.           .call lseekb_h
  235.           jc   Oops
  236.  
  237. ; start loop that reads file name, creates file, transfers bytes, closes file
  238.  
  239. ; get the name of the file
  240.  
  241. BigLoop:  mov  ah,1
  242.           int  16h                 ; check for key waiting
  243.           jz   NoKey
  244.           cmp  al,27               ; ESC?
  245.           jne  NoKey
  246.           mov  si,offset Interrupted
  247.           .call cput_str
  248.           jmp  AllDone
  249.  
  250. NoKey:    mov  si,offset FileToCreate
  251.           mov  cx,NAMELENGTH
  252.           mov  bx,[HogHandle]
  253.           .call read_h
  254.           ja   AllDone             ; all done
  255.           je   @F                  ; read was okay...
  256.           mov  si,offset HogReadError  ;read failed
  257.           .call cput_str
  258.           jmp  Oops
  259.  
  260. ; create the new file
  261.  
  262. @@:       mov  ax,FA_NORM          ; normal file attributes
  263.           .call create_h           ; destroys earlier BX
  264.           jnc  @F
  265.           mov  si,offset CreateError
  266.           .call cput_str
  267.           mov  si,offset FileToCreate
  268.           .call cput_str
  269.           mov  si,offset CrLf
  270.           .call cput_str
  271.           jmp  Oops
  272. @@:       mov  [NewHandle],bx
  273.  
  274. ; display the file being written
  275.  
  276.           mov  si,offset FileToCreate
  277.           .call cput_str
  278.           mov  si,offset Spaces1
  279.           .call cput_str
  280.  
  281. ; read the length of the file
  282.  
  283.           mov  si,offset SizeOfCreate
  284.           mov  cx,4
  285.           mov  bx,[HogHandle]
  286.           .call read_h
  287.           ja   AllDone             ; shouldn't happen...
  288.           je   SizeToCRT
  289.           mov  si,offset HogReadError
  290.           .call cput_str
  291.           jmp  Oops
  292.  
  293. ; dislay the number of bytes in the file
  294.  
  295. SizeToCRT:
  296.           lodsw                    ; low-order
  297.           mov  dx,ax
  298.           lodsw                    ; high-order
  299.           xchg dx,ax
  300.           mov  si,offset ScratchBuffer
  301.           .call dword_to_dec
  302.           .call cput_str
  303.           mov  si,offset CrLf
  304.           .call cput_str
  305.  
  306. ; write file info to log file
  307.  
  308.           call LogWrite
  309.           jc   AllDone
  310.  
  311. ; read as many bytes as told by the file length just read, and place into
  312. ; the newly created file
  313.  
  314. ; compare what has been read to what we need to read
  315.  
  316. HogCompare:
  317.           mov  dx,word ptr [SizeOfCreate+2]
  318.           mov  ax,word ptr [SizeOfCreate]
  319.           mov  bx,ax
  320.           or   bx,dx               ; if 0, all done with this new file
  321.           jnz  @F
  322.  
  323. ; close the output file, get the next one
  324.  
  325.           mov  ah,3eh
  326.           mov  bx,[NewHandle]
  327.           int  21h
  328.           mov  [NewHandle],0
  329.           jmp  BigLoop
  330.  
  331. @@:       mov  cx,READWRITEBUFR
  332.           .cmp_dw dx,ax,cx
  333.  
  334. ; JB  if dword (DX:AX) < word (CX)
  335. ; JBE if dword <= word
  336. ; JE  if dword = word
  337. ; JAE if dword >= word
  338. ; JA  if dword > word
  339.  
  340. ; If what we need to read is >= READWRITEBUFR, read a full buffer, else
  341. ; read what's left.
  342. ; Since DX = 0 if DX:AX <= READWRITEBUFR, AX is amount for the read operation
  343.  
  344.           ja   FullRead
  345.           mov  word ptr [SizeOfCreate],0
  346.           mov  word ptr [SizeOfCreate+2],0
  347.           mov  cx,ax
  348.           jmp  short DoTheRead
  349.  
  350. ; subtract the amount we're about to read from what is left to read
  351.  
  352. FullRead: .sub_dw dx,ax,cx
  353.           jnc  NoOverflow          ; there should be no overflow...
  354.  
  355. ; error, close all and exit
  356.  
  357.           mov  si,offset Overflow
  358.           .call cput_str
  359.           jmp  short AllDone
  360.  
  361. NoOverflow:
  362.           mov  word ptr [SizeOfCreate+2],dx
  363.           mov  word ptr [SizeOfCreate],ax
  364.  
  365. ; read from the source file, write it to the target file
  366.  
  367. DoTheRead:
  368.           mov  bx,[HogHandle]
  369.           xor  si,si
  370.           push ds
  371.           mov  ds,[MemSeg]
  372.           .call read_h
  373.           pop  ds
  374.           ja   AllDone             ; already at EOF (shouldn't happen...)
  375.           je   ReadOkay
  376.           mov  si,offset HogReadError
  377.           .call cput_str
  378.           jmp  Oops
  379. ReadOkay: mov  bx,[NewHandle]
  380.           push ds
  381.           mov  ds,[MemSeg]
  382.           .call write_h
  383.           pop  ds
  384.           jae  HogCompare          ; okay, get more
  385.           mov  si,offset HogWriteError
  386.           .call cput_str
  387.           jmp  Oops
  388.  
  389.  
  390. ; release any allocated memory
  391.  
  392. AllDone:  mov  ax,[MemSeg]
  393.           or   ax,ax
  394.           jz   NoRelease
  395.           mov  es,ax
  396.           mov  ah,49h
  397.           int  21h
  398. NoRelease:
  399.           mov  bx,[NewHandle]
  400.           or   bx,bx
  401.           jz   @F
  402.           mov  ah,3eh
  403.           int  21h
  404. @@:       mov  bx,[HogHandle]
  405.           or   bx,bx
  406.           jz   @F
  407.           mov  ah,3eh
  408.           int  21h
  409. @@:       mov  bx,[LogHandle]
  410.           or   bx,bx
  411.           jz   @F
  412.           mov  ah,3eh
  413.           int  21h
  414.           mov  si,offset ResultsAlsoIn
  415.           .call cput_str
  416.           mov  si,offset LogFile
  417.           .call cput_str
  418.           mov  si,offset CrLf
  419.           .call cput_str
  420. @@:       ret
  421.  
  422. .endp        main
  423.  
  424.  
  425.  
  426. ;=============================================================================
  427. ;FUNC:    ErrorOut
  428. ;
  429. ;DESC:    Displays DOS error code message
  430. ;
  431. ;IN:      nothing
  432. ;
  433. ;OUT:     nothing
  434. ;
  435. ;=============================================================================
  436.  
  437. .public        ErrorOut
  438. .proc          ErrorOut     auto uses ax bx si
  439.  
  440.           mov  ax,[e_code]
  441.           mov  bl,10
  442.           mov  si,offset ErrorCodeMsg+12
  443.           .call word_to_asc
  444.           mov  si,offset ErrorCodeMsg
  445.           .call cput_str
  446.           ret
  447.  
  448. .endp          ErrorOut
  449.  
  450.  
  451.  
  452. ;=============================================================================
  453. ;FUNC:    LogWrite
  454. ;
  455. ;DESC:    Write HOG module info to log file
  456. ;
  457. ;IN:      nothing
  458. ;
  459. ;OUT:     CF set if error, else CF clear
  460. ;
  461. ;=============================================================================
  462.  
  463. .public        LogWrite
  464. .proc          LogWrite  auto uses ax bx cx dx si
  465.  
  466.           mov  bx,[LogHandle]
  467.           mov  si,offset FileToCreate
  468.           .call str_len
  469.           .call write_h
  470.           jc   LWError
  471.           mov  dx,17
  472.           sub  dx,cx
  473.           xchg dx,cx
  474.           mov  si,offset Spaces2
  475.           .call write_h
  476.           jc   LWError
  477.           mov  si,offset ScratchBuffer
  478.           .call str_len
  479.           .call write_h
  480.           jc   LWError
  481.           mov  si,offset CrLf
  482.           mov  cx,2
  483.           .call write_h
  484.           jc   LWError
  485.           ret
  486.  
  487. LWError:  mov  si,offset LogWriteError
  488.           .call cput_str
  489.           call ErrorOut
  490.           stc
  491.           ret
  492.  
  493. .endp          LogWrite
  494.  
  495.  
  496. .ends
  497.  
  498.  
  499. ;=============================================================================
  500. ; Stack normalization and memory management initialization labels
  501. ;
  502. ; NOTE: These declarations must remain after the declaration of the stack
  503. ; and anything in the stack segment. These labels define the end of the
  504. ; stack and the program, which is where the near and far heaps are placed
  505. ; by default. These declarations do not affect the size of the program and
  506. ; may be left here even if the stack is not normalized and the heaps are
  507. ; not used.
  508. ;=============================================================================
  509.  
  510. .public        nheap_default, fheap_default
  511. IF NOT __TINY__
  512. .stackseg
  513.   IF __SMALL__ OR __MEDIUM__
  514. .public        stack_end        ;used by START to normalize stack
  515. .label        stack_end    word    ;must be defined past entire stack
  516.   ENDIF
  517. .label        nheap_default    word    ;used by the near heap
  518. .label        fheap_default    word    ;used by the far heap
  519. .ends
  520. ELSE
  521. _BSSEND        segment    byte public 'STACK'
  522. .label        nheap_default    word    ;used by the near heap
  523. .label        fheap_default    word    ;used by the far heap
  524. _BSSEND        ends
  525. % @codeseg    group    _BSSEND
  526. ENDIF
  527.  
  528.  
  529.         end    start        ;specify START as starting address
  530.