home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / dos_util / pcmcvt.zip / PCMCVT.DOC < prev   
Text File  |  1993-06-01  |  32KB  |  596 lines

  1. PCMCVT.COM (VERSION 1.0)                     Copyright (c) 1993 Jay Munro
  2. -------------------------------------------------------------------------
  3.                   First Published in PC Magazine June 29,1993 (Utilities)
  4. -------------------------------------------------------------------------
  5.  
  6. PCMCVT.COM:
  7.  
  8.            PCMCVT strips out all the formatting codes of today's most
  9. popular word processors, leaving you with an editable ASCII text. PCMCVT
  10. recognizes and converts the binary files produced by Word for DOS,
  11. Word for Windows, Windows Write, WordPerfect for DOS, WordPerfect for
  12. Windows, and Ami Pro into ASCII files.
  13.  
  14.      What do you do when you receive a document prepared on a word processing
  15. program you don't happen to have?  Dozens of formatting codes for every font
  16. change, margin, and mode are embedded in the manuscript, and every one of
  17. them is worse than useless if you have to do any work on the text. 
  18.  
  19.      This utility eliminates this hassle. If you ever need to turn text
  20. formatted with today's most popular word processors into straight ASCII,
  21. all you need is PCMCVT.  PCMCVT recognizes and converts the binary files
  22. produced by Word for DOS, Word for Windows, Windows Write, WordPerfect for
  23. DOS, WordPerfect for Windows, and Ami Pro into ASCII files.
  24.  
  25.      Keeping all these different word processors on your machine would be
  26. prohibitive both in cost and disk space.  Moreover, you'd have to learn
  27. enough of each of their commands to open the files you receive and then
  28. resave them in ASCII format.  And you'd have to know which program to use
  29. on the document you received.  PCMCVT does all this for you automatically,
  30. making it the perfect answer to word processor overload.
  31.  
  32.      PCMCVT is not a memory-resident program, and it takes up very little
  33. space when it runs.  This can be very handy when you want to shell out to
  34. DOS and convert an additional document without having to close the
  35. document on which you're working.  Most word processors today provide a
  36. shell-to-DOS capability, and since PCMCVT weighs in at under 10K, it runs
  37. very comfortably in a DOS shell.  Some word processors, such as Microsoft
  38. Word for DOS, can accept a DOS command without even requiring you to shell
  39. out to DOS, making it possible to convert a file with PCMCVT without even
  40. seeing a DOS prompt.
  41.  
  42.      If you make any changes in the PCMCVT source code, you'll need
  43. MASM 6.0 or later to reassemble the program.
  44.  
  45. USING PCMCVT
  46.  
  47.      You should put PCMCVT.COM in a directory on your DOS path.  That way
  48. you can then go to any directory and start converting files without having
  49. to move them around.  You enter PCMCVT with one or more parameters, from
  50. the DOS prompt.  Its complete syntax is
  51.  
  52.  
  53. PCMCVT /S source [/D destination] [/Wxx] [/O] [Tx] [/?]
  54.  
  55.  
  56. The only required PCMCVT parameter is /S source, the filename of the
  57. word-processed document you want to convert into ASCII text.  Spaces can
  58. be included between the /S and the filename but are not required.  The
  59. source filename must be complete, with no wildcards or missing extensions;
  60. the Quick Reference Guide explains how to process a batch of files.
  61.  
  62.      When creating a file for the converted text, PCMCVT uses the same
  63. filename as that of the source file by default, adding only the extension
  64. .TXT.  Thus, if you enter
  65.  
  66.  
  67.                           PCMCVT /S MYLETTER.DOC
  68.  
  69.  
  70. the source file will remain unchanged and the converted file will be
  71. called MYLETTER.TXT.  By adding the optional /D destination parameter
  72. on the command line, you can specify any legal DOS filename (including
  73. a path, if desired) for the converted file.
  74.  
  75.      If a same-named file exists in the destination directory, PCMCVT
  76. will warn you and give you the choice to overwrite the file or not.
  77. If you answer No, the easiest way to reenter the command up to the point
  78. of typing in a new destination file name is to hit F3.  If you want PCMCVT
  79. to overwrite same-named files automatically and without a warning prompt,
  80. simply include the /O switch parameter on the command line.  This can be
  81. useful when you are running PCMCVT from a batch file.
  82.  
  83.       To tell PCMCVT to word-wrap lines at a certain length, you use
  84. the /Wxx parameter, where xx is the maximum number of characters desired.
  85. With a setting of /W60, PCMCVT will force a new line by inserting a
  86. carriage return/line-feed (CRLF) after a maximum of 60 characters.
  87. When converting a line longer than xx characters, PCMCVT tries to find
  88. a convenient space to break the line.  Thus in this example, although
  89. the specified length is 60, the line may break at 45, 52, or 59
  90. characters, depending on where the last word space occurred.  If a
  91. file contains a long, unbroken character string, PCMCVT has no choice
  92. but to break the line at the specified length.
  93.  
  94.      If you enter the /W switch parameter without specifying a value
  95. for xx, PCMCVT defaults to a line length of 78 characters and replaces
  96. soft carriage returns in the original document with hard carriage returns.
  97.  
  98.      By default, PCMCVT passes on the tab character (ASCII 9) to the
  99. destination file.  This is not always desirable.  The /Tx switch replaces
  100. tab characters with the number of spaces you specify as x.  Thus, for
  101. example, /T5 replaces all tab characters with 5 spaces, which you might
  102. use to approximate the tab spacing in the original document.  If no value
  103. of x is specified, the tab is removed.
  104.  
  105.      If you forget the syntax details, you can get a listing of the
  106. PCMCVT options by entering the command with no parameter or with
  107. the /? switch.
  108.  
  109. CONVERTING FILES
  110.  
  111.      PCMCVT can create two kinds of ASCII files.  The most common
  112. (and the default) format is called ``one-line-per-paragraph.''  In this
  113. format, everything up to a hard carriage return is considered part of a
  114. single line, no matter how many lines it may occupy on the screen.  The
  115. other format inserts hard carriage returns and line-feeds at the end of
  116. each screen line, whose length is specified by the word-wrap setting as
  117. explained above.
  118.  
  119.      Which format is more appropriate in a given situation depends on
  120. how the file will eventually be used.  If you wish to import the
  121. converted text into a word processor, the one-line-per-paragraph format
  122. makes the most sense, as it leaves the word processor free to impose its
  123. own margin settings and reformatting on the text.
  124.  
  125.      Fixed-length lines delimited by a carriage return/line-feed, on the
  126. other hand, are  perfect for text files that are to be typed or browsed
  127. from a DOS prompt.  README files are a good example.  With the trend in
  128. shareware toward using the Windows Write format for instructions, you
  129. could preview a newly downloaded file without having to fire up Windows.
  130.  
  131.  FILE FORMATS
  132.  
  133.      In addition to the text that you want and the formatting codes that
  134. you don't, word-processed files contain information that uniquely
  135. identifies the word processor that created them.  For this reason, during
  136. the conversion process PCMCVT is able to display the name of the
  137. originating word processing system.  Identifying the native document
  138. format also enables PCMCVT to use the appropriate routines to extract
  139. the text from the file.
  140.  
  141.      Among the document formats PCMCVT supports--the DOS or Windows
  142. versions of WordPerfect 5.1 or later; Word for DOS, Version 5.0 or
  143. later; Word for Windows (WinWord), Version 1.0 or later; Windows
  144. Write 3.x;  and Ami Pro--the three from Microsoft are very similar,
  145. so I'll discuss them first.
  146.  
  147. WORD/WINWORD/WINDOWS WRITE
  148.  
  149.      Since its introduction in 1983, the file format for Microsoft Word
  150. has remained unchanged.  Microsoft had the foresight at that time to
  151. define structures larger than were immediately needed so that new
  152. features could be added without changing the basic format. A Word
  153. document file consists of three sections:  the header, or file
  154. information block; the text section; and the formatting section.
  155.  This basic file structure is shared by the three Microsoft file
  156. formats with but slight differences, which I'll discuss as they arise.
  157.  
  158.      PCMCVT is concerned only with the first two sections of the
  159. Microsoft document format, that is, the FIB or file information block,
  160. and the text section.  In identifying which word processor was used to
  161. create the original file, however, PCMCVT also makes use of a second
  162. identifying characteristic, called the signature.  A file signature is
  163. usually a unique byte sequence at a specific position in the file that
  164. sets it apart from other files.  In the Microsoft document files, for
  165. example, the signature is contained in the first 2 bytes, as shown below:
  166.  
  167.  
  168.  
  169. File format                   Signature
  170. -----------                   ---------
  171.  
  172. Word 5.5                        31BEh
  173. WinWord 1.x                     9BA5h
  174. WinWord 2.0                     DBA5h
  175. Windows Write 3.1               31BEh
  176.  
  177.  
  178.     From the signatures alone we can tell WinWord and Word for DOS apart,
  179. but Windows Write presents a problem, as it shares the same signature
  180. with Word for DOS.  This is where the reserved areas of the FIB come
  181. into play. Starting from the beginning of the file, if we look at the
  182. value of the 2-byte word located at offset 96, we'll get our answer.
  183. If the value is 0, then the file is written in Word for DOS; if it has
  184. any other value, it's Windows Write.  With the type of file nailed down,
  185. we can move on to figuring out the size of the text portion.
  186.  
  187.      The text follows the file information block.  In both Word for DOS
  188. and Windows Write the text starts 128 bytes from the beginning of the
  189. file. The starting offset of the text in WinWord is specified by a word
  190. stored at offset 24 from the start of the file. Now that we know the file
  191. type and the start of the text, with one more piece of information and a
  192. bit of math, we can move on to read the text.
  193.  
  194.      The actual length of the text plus the header size is stored as a
  195. DWord (double word) at offset 28 from the start of the file.  By
  196. subtracting the header size (128 for Word and Write, 384 for WinWord),
  197. we get the length of the text to be converted.
  198.  
  199.      Of the formats handled by PCMCVT, Microsoft's are the easiest to
  200. convert.  Once the header information is gathered and the text length is
  201. determined, what is left to be read is essentially pure ASCII text.
  202. Unlike the other formats, only a few characters in the text demand
  203. special treatment during conversion.
  204.  
  205.      In Windows Write, Word for DOS, and WinWord, the pair of ASCII
  206. character codes 13 and 10 (carriage return and line-feed) are used for
  207. paragraph ends; character code 12 is used for explicit page breaks; and
  208. character code 9 is used for tab characters.  PCMCVT keeps all these
  209. characters unchanged in the converted text unless, of course, the /Tx
  210. switch is specified to expand tabs into spaces.
  211.  
  212.      Several other special codes are used in Word for DOS and WinWord.
  213. Hard line breaks are stored as character 11, which PCMCVT replaces with
  214. a carriage return/line-feed pair.  PCMCVT replaces ASCII 15, the
  215. character that Word uses for the em-hyphen, or em-dash, with two normal
  216. hyphens.  Word for DOS defines the 196 as a nonbreaking hyphen and 255
  217. as a nonbreaking space; WinWord uses ASCII 30 for a nonbreaking hyphen
  218. and ASCII 160 for a nonbreaking space.  So in these files, depending on
  219. the original format of the file--WinWord or Word--PCMCVT replaces the
  220. 30 or 196 with a character 45 (hyphen), and 160 or 255 with a 32
  221. (space).  The utility ignores all other characters with a value of ASCII
  222. 31 or less.
  223.  
  224. WORDPERFECT
  225.  
  226.      WordPerfect file formats are essentially the same in Versions 5.1
  227. for DOS and in 5.1 and 5.2 for Windows.  The format goes back only to
  228. WordPerfect 5.0, which represented a considerable departure from that
  229. of the earlier 4.2 version.
  230.  
  231.      WordPerfect 5.x files contain a 16-byte file header, called the
  232. prefix area, that identifies the file and provides certain basic file
  233. information.  The first 4 bytes consist of the hexadecimal value 0FFh
  234. and the three letters WPC, which comprise the signature.  Although
  235. PCMCVT uses only the signature and the next 4 bytes (which point to
  236. the start of the text), the prefix area also contains information
  237. about the type of document, the version of WordPerfect that created
  238. the file, and so forth.
  239.  
  240.      After the header,  WordPerfect document files are not segmented
  241. in the manner of Microsoft files.  Except for some initial formatting
  242. information stored in an additional prefix area immediately after the
  243. 16-byte header, all formatting information is embedded in the document
  244. area itself.  Having the formatting codes and data in the text area
  245. makes the conversion process somewhat more difficult.
  246.  
  247.      WordPerfect uses both single and multibyte codes to identify
  248. functions.  Single-byte codes employ some of the same control
  249. characters (ASCII values of less than 32) used in Word files.
  250. In WordPerfect, such control characters are used to signal carriage
  251. returns, page feeds, page numbers, and special merge codes.  Single-byte
  252. codes in the hexadecimal range from 80h to 0BFh (128 to 191 decimal) are
  253. used to turn such functions as justification, columns, and footnotes on
  254. and off.  PCMCVT traps the single-byte codes for hard hyphens and hard
  255. spaces. Hard hyphens occur in three types (in-line hyphens, end-of-line
  256. hyphens, and end-of-page hyphens) and use codes 0A9h, 0AAh, abd 0ABh.
  257. Hard spaces use the code 0A0h. PCMCVT looks for these codes and replaces
  258. them with the standard hyphen (ASCII 45) and space (ASCII 32) characters.
  259. Except for hard hyphens, hard spaces, carriage returns, and page feeds,
  260. PCMCVT strips out all the rest of the single-byte code characters when
  261. it converts a file.
  262.  
  263.      Multibyte functions are where the real fun starts.  There are over
  264. 50 pages of documentation on the multibyte functions in the WordPerfect
  265. developers guide! These functions are of two kinds.  Fixed-length
  266. functions use the range from hexadecimal 0C0h to 0CFh (192 to 207
  267. decimal), and variable-length functions lie in the range of hexadecimal
  268. 0D0h to 0FFh (208 to 255 decimal).
  269.  
  270.      The fixed-length functions begin with a code (for example, 0C1h for
  271. tab-align), which is followed by a series of bytes of data.  The end of
  272. the function is signaled by a repetition of the initial code. PCMCVT
  273. recognizes only one of these functions, 0C1h, the tab-align code. While
  274. most word processors use the tab character (ASCII 9), WordPerfect signals
  275. a tab with the tab-align function. The function contains a byte that
  276. distinguishes between standard tabs and left, right, or center
  277. alignment, depending on which bits are set. PCMCVT retrieves the byte
  278. and, if the code is for tab, replaces the functions with either an
  279. ASCII 9 character, or the number of spaces if the /T parameter was used.
  280. In all other functions, PCMCVT ignores everything between the two
  281. appearances of WordPerfect's multibyte fixed-length codes. 
  282.  
  283.      Variable-length multibyte functions have a slightly different
  284. format.  The actual size of the function is stored in the codes.
  285. These functions start with a 1-byte function code followed by a
  286. 1-byte function code.  This combination ensures over 12,000 possible
  287. variations, giving WordPerfect a lot of room for expansion.
  288.  
  289.      Immediately following the function code is a word (2 bytes) that
  290. contains the length of the function.  The value represents the size of
  291. the whole function, including the beginning and the ending codes.  After
  292. the length word comes the data for the function.  Multibyte functions can
  293. cover a wide range of different features, such as margin commands, font
  294. changes, printer codes, and graphics information.
  295.  
  296.      At the end of the code sequence, the first section repeats itself,
  297. only in reverse order.  That is to say, the size word of the function
  298. is duplicated, followed by the function code, and finally the function
  299. code.  In its conversions, PCMCVT must ignore (that is, strip out) all
  300. the multibyte functions.  Instead of examining each byte in search of
  301. the ending code, however, PCMCVT figures out the length from the size
  302. word and jumps past the function, saving the time it might take to
  303. scan through possibly hundreds of bytes.
  304.  
  305. AMI PRO
  306.  
  307.      Compared with the Microsoft and WordPerfect products, Ami Pro
  308. presents an unusual format:  Its files consist almost completely of 
  309. ASCII text.  Like its competitors, Ami Pro does have a header to store
  310. information, followed by the document area.  Like WordPerfect, Ami Pro
  311. also embeds formatting commands in the text area.  However, the 
  312. ifference is that the formatting codes are set off by certain low ASCII
  313. characters such as < and @, rather than high-bit ASCII characters.
  314.  
  315.      The Ami Pro signature used by PCMCVT is the text string [ver] at
  316. the very start of the file.  Following the signature comes line after
  317. line of information about the file--printer last used, font information,
  318. title of file, and so forth.  PCMCVT skips through all this information
  319. and searches straightaway for the Ami Pro start-of-document text string,
  320. [edoc].
  321.  
  322.      The text that follows the [edoc] string stores formatting
  323. information both at the beginning and in the middle of the text lines.
  324. Simple formatting information, such as bold on and off, is signaled
  325. with <> signs surrounding a code.  For example, if the single word bold
  326. were supposed to appear in boldface, the code below would be used:
  327.  
  328.  
  329. The next word is <+!>bold<-!>
  330.  
  331. Such formatting is humanly readable.  The code for boldface is the
  332. exclamation point.  It is turned on with a plus sign and turned off with
  333. a minus sign.
  334.  
  335.      To distinguish between  formatting codes and characters that are
  336. part of the user's text, Ami Pro  usually places a less-than sign (<)
  337. before the character.  For example, if the text contains a less-than
  338. sign, Ami Pro prefixes it with another less-than sign. So if Ami Pro
  339. (or PCMCVT, for that matter) sees two less-than signs (<<) in a row,
  340. it ignores one and prints the other as a regular character. To signify
  341. a greater-than sign, Ami Pro uses the three characters less-than,
  342. semicolon, and greater-than (< ; >).
  343.  
  344.      Ami Pro uses a similar coding format for many features, even page
  345. breaks.  Although in most word processors hard page breaks are usually
  346. an ASCII character 12, Ami Pro puts the code <:P> at the beginning of
  347. the first line of a new page instead.  About the only ASCII control codes
  348. that Ami Pro uses are the carriage return/line-feeds and tab characters
  349. (ASCII 9).  And again, as with all but the tab, carriage return/line-feed,
  350. and page feeds, PCMCVT reads the Ami Pro codes and skips over them,
  351. extracting only the actual text of the file.
  352.  
  353. HOW PCMCVT WORKS
  354.  
  355.      PCMCVT was written in assembly language, using Microsoft Macro
  356. Assembler 6.0.  (Version 6.1 would work as well.)  A modular programming
  357. approach was implemented for each conversion routine, so each conversion
  358. algorithm could be designed and tested independently.  What follows is a
  359. broad overview of how PCMCVT works; add the complete commented assembly
  360. code (available on PC MagNet) and you'll have everything you need to
  361. understand the PCMCVT utility.
  362.  
  363.       The main module of the program contains the routines for file
  364. handling, identifying headers, parsing the command line, word wrapping,
  365. and other support functions.
  366.  
  367.      Program execution starts by revectoring INT 24h.  This allows PCMCVT
  368. to handle critical errors itself rather than by means of the classic
  369. ``Abort, Retry, Ignore'' message.  The HookInt24h and UnHookInt24h
  370. functions simply intercept critical errors and send program execution
  371. back to the calling program.  Because the carry flag is set on the
  372. return, PCMCVT sees the error and can process it as needed.
  373.  
  374.      As a command line program, PCMCVT needs to read and parse the
  375. parameters the user passes to it.  It thus reads offset 80h of the
  376. Program Segment Prefix (PSP), which contains the number of command line
  377. parameters.  If none are found, PCMCVT simply displays a help screen
  378. and terminates.  If it finds any user-supplied parameters, it calls the
  379. ParseCommandLine function.  In order to simplify the parsing process,
  380. PCMCVT first capitalizes the incoming command line string. The parsing
  381. function steps through each parameter, extracts the filenames and
  382. switches, and then stores them for later use.
  383.  
  384.      The next step is to identify the source file's word processing
  385. format by examining the header and signature.  PCMCVT opens the source
  386. file and reads a 256-byte chunk of the file into the HeaderBuffer.  This
  387. is more than sufficient to identify the five formats supported here, and
  388. it leaves room for expansion later.  The function HeadCheck steps through
  389. the beginning of the HeaderBuffer, looking first for a WordPerfect
  390. signature, then for the signature of Word for DOS, WinWord, and finally
  391. AMI Pro.  The function StrCmp is called with pointers to the HeaderBuffer
  392. looking for matches.  If no matching signatures are found, PCMCVT displays
  393. the message ``Sorry, unknown format'' and exits.
  394.  
  395.     The HeadCheck function returns a value that corresponds to the file
  396. type found.  This determines which conversion routine will be called.
  397. If the value is a valid format that PCMCVT can handle, the destination
  398. file is opened and execution moves on to the appropriate conversion
  399. routine. The code shown (below) in Figure 1 is the signature-checking
  400. routine that HeadCheck uses for WordPerfect. The routine is duplicated
  401. in PCMCVT, using the unique signature for each file type.
  402.  
  403. CALLING CONVERSION ROUTINES
  404.  
  405.      The PCMCVT conversion routines are essentially integral units that
  406. call external routines for file support and that use global data.  The
  407. way text is extracted from its native file varies with the specific word
  408. processor.  However, the conversion routines use similar steps, starting
  409. with finding the beginning of the text in the file.
  410.  
  411.      Each module begins by getting (or calculating) the start of the text
  412. section, to which the file pointer is then reset.  (The file pointer needs
  413. to be reset because it was left 256 bytes from the start by the
  414. header-reading procedure.)  Once the pointer is reset, the first block of
  415. text is read into a 2,024-byte (2K) input buffer.  This buffer size was
  416. chosen as a compromise between the demands of adequate workspace and
  417. the danger of hogging too much memory.  If the file is less than 2K,
  418. the buffer size is adjusted accordingly.  If nothing is left to read
  419. from the file, the routine will exit with an error.  The file read
  420. routine is self-contained, handling errors and monitoring the amount
  421. of text input by itself.
  422.  
  423.      The actual conversion process consists of a byte-by-byte comparison
  424. between the source file characters and the codes and special characters
  425. that PCMCVT recognizes.  PCMCVT performs a trio of tests--Use it,
  426. Replace it, Ignore it--on each successive character (or series of
  427. characters, in the case of a multibyte function).  For each file format,
  428. the decision may be different.  In Microsoft Word, for example, the
  429. carriage return/line-feed (CRLF) characters are passed on to the
  430. destination (output) file.  However, in Ami Pro, because each line
  431. is CRLF delimited, PCMCVT needs to check whether there is one or more
  432. than one CRLF.  If there is only one, it is ignored.  If there is more
  433. than one CRLF, the first is ignored but each additional one is passed
  434. on the output.
  435.  
  436.      All the conversion modules handle tabs in the same manner.  When a
  437. tab character (ASCII 9) is found, the DoTabs routine is called.  DoTabs,
  438. a simple routine as shown (below) in Figure 2, determines whether PCMCVT
  439. is to pass the tab character on or replace it with spaces.  If the /Txx
  440. switch is in effect, DoTabs writes the correct number of spaces to the
  441. output file.  Otherwise it simply writes out a tab character.
  442.  
  443.      To write data, whether characters or control codes, PCMCVT calls
  444. the WriteIt function.  WriteIt is also self-contained. WriteIt accepts
  445. a character in AL and puts it into the output buffer.  The output buffer,
  446. also 2,024 bytes (2K) long, is used exclusively by WriteIt.  WriteIt
  447. monitors the current character position and, when the buffer is full,
  448. writes the buffer's contents to the destination file.  WriteIt can be
  449. forced to write (or flush) its buffer by calling it with a -1 in AL.
  450. The buffer is flushed when PCMCVT gets to the end of the source file.
  451.  
  452.      PCMCVT reads the source file in sections.  When the conversion
  453. routine comes to the end of the input buffer, a call is made to read
  454. in another piece of the file.  The file-reading routine preserves all
  455. registers and updates the input buffer pointer after the file is read.
  456. The input buffer points at the current character the conversion routine
  457. is to consider.  Thus, when the file-reading routine returns to the
  458. conversion module, execution can pick up exactly where it left off,
  459. except that now the input buffer has a new block of text to convert.
  460. This is important in cases like that of the WordPerfect conversion
  461. module, where the input buffer can run out of characters while in the
  462. middle of a multibyte-function-scanning loop.
  463.  
  464.      If the /Wxx option was specified, the self-contained word-wrap
  465. module is called when WriteIt writes the buffer to disk.  Working with
  466. the output buffer, the word-wrap routines scan the text in a two-step
  467. process.  After saving the current pointer into the output buffer, the
  468. first scan looks for carriage return/line-feeds, which signal the end
  469. of a string of characters.  While scanning, the word-wrap routine counts
  470. the characters at which it looks.  When it finds a CRLF, or if it comes
  471. to the end of the buffer, the number of characters is compared with the
  472. value specified on the command line.  If a CRLF was found in fewer
  473. characters than the specified value, the line is printed as is and the
  474. scanning starts over again.  But if the CRLF is found beyond the desired
  475. line length (/Wxx value), the second scan is performed.
  476.  
  477.      The second scan, in Figure 4, actually consists of a series of small
  478. scans.  The current string or line is scanned for a space (ASCII 32) or
  479. a tab (ASCII 9), which signals a break between two words.  Each time a
  480. space is found, its position in relation to the start of the current
  481. string is compared with the specified line length value.  If the space's
  482. position is less than the line length, the position value is saved and
  483. the string is further scanned.  This continues until either a space is
  484. found with its position greater than the requested length of the line,
  485. or the end of the buffer is encountered.
  486.  
  487.      Several things happen when the scanning process finds a space in
  488. the string at a position greater than the line length value.  First, the
  489. value of the starting position of the current scan is retrieved.  The
  490. starting value and the last space position are subtracted, yielding the
  491. number of characters in the string.  This number of characters is used
  492. for the length of the line  when the string is written to the output
  493. file.
  494.  
  495.      Once the string of characters is written, the program writes a
  496. CRLF combination (ASCII 13 and 10) to the destination file, completing
  497. the line.  When the CRLF writing is done, the program clears the current
  498. character counter and stores the last space's position in the starting
  499. position variable, and then the scanning process starts all over again.
  500.  
  501.      A slight variation on the previous procedure is required when the
  502. routine runs out of characters at the end of the output buffer before
  503. either a CRLF or the specified line length is reached.  In this case,
  504. the current line is written to the destination file without a CRLF.
  505. The length is stored and the word-wrap procedure returns to the main
  506. program.  Since the files are read and written in small sections, the
  507. next time the word-wrap function is called, it picks up where it left
  508. off and maintains the correct line length throughout the conversion.
  509.  
  510.      When PCMCVT finishes converting the file, it closes all files and
  511. returns to the command prompt.  The utility also returns errorlevel
  512. codes for use in batch files.  If the conversion was successful, the
  513. return code is a 0.  The other errorlevel exit codes are shown below:
  514.  
  515.  
  516. Error- level code                    Meaning
  517. -----------------                    -------
  518.  
  519.        1                             Source file not found
  520.        2                             Destination existed,not overwritten
  521.        3                             Drive not ready
  522.        4                             Disk full
  523.  
  524.  
  525. CAVEATS
  526.  
  527.       PCMCVT is designed for quick-and-dirty text conversion.  Given the
  528. complexity of today's word processors, you may occasionally run into a
  529. file that either will not convert, or will convert  very oddly.  One
  530. format that does not convert well with PCMCVT, for example, is WinWord's
  531. so-called fast save format. This format keeps text in the original text
  532. area, but it also incrementally saves text in the area after the
  533. formatting area.  While you're unlikely to receive a Winword document
  534. you need to convert that has not been saved in the regular manner, if
  535. you hit one saved in the fast save format you'll have to consult
  536. WinWord help.
  537.  
  538.      Other casualties of stripping out formatting codes are headers and
  539. footers. The loss of formatting on outlines or tables could render the
  540. text unreadable. Fortunately, PCMCVT will prove entirely adequate for
  541. the vast majority of your conversion needs.
  542. ---------------------------------------------------------------------
  543. JAY MUNRO IS A FREQUENT CONTRIBUTOR TO  PC MAGAZINE. HE CAN BE REACHED
  544. ON PC MAGNET (72241,554) IN THE UTILITIES/TIPS FORUM (GO ZNT:TIPS),
  545. THE TECHNICAL SUPPORT AREA FOR PC MAGAZINE UTILITIES.
  546. ---------------------------------------------------------------------
  547.              The Signature-Checking Routine
  548.  
  549. ;-- WordPerfect
  550.   Invoke StrCmp, Addr HeaderBuffer, Addr WPerfID,4
  551.   Or   AX,AX                    ;StrCmp = 0 if a match
  552.   Jnz  @F                       ;no match
  553.   Mov  AX,1                     ;1 = Word Perf
  554.   Lea  DX,WPerfName             ;DX contains address
  555.                                 ; source file name
  556.   Jmp  HeadOut
  557. @@:                             ;next comparison
  558.  
  559. Figure 1: The HeadCheck function uses this routine to determine if the word processing format of a file is one that PCMCVT supports.
  560. ===========================================================================
  561.                      The DoTabs Procedure
  562.  
  563. DoTabs Proc Near Uses CX              ;expands tabs
  564.    Mov   CX, TabWidth                 ;get tab size
  565.  
  566. TabLoop:
  567.    Mov   AL,32                        ;put space in AL
  568.    Call  WriteIt                      ;write it to dest file
  569.    Loop  TabLoop                      ;go back for more
  570.    Ret
  571. DoTabs EndP
  572.  
  573. Figure 2: This procedure determines whether PCMCVT is to pass the tab
  574. character on or replace it with spaces.  If the /Txx switch is in effect,
  575. DoTabs writes the correct number of spaces to the output file; otherwise
  576. it simply writes out a tab character.
  577. ==========================================================================
  578.                  How Word Wrap Detects Spaces
  579.  
  580. ScanLoop:
  581.    Mov   AL, 32          ;load AL with space character
  582.    RepNe ScaSb           ;scan for that first space
  583.    Mov   AX, BX          ;get length total length (BX)
  584.    Sub   AX, CX          ;see difference
  585.    Mov   CurLine,AX      ;save current length
  586.    Add   AX, LastLen     ;add in the last length
  587.    Cmp   AX, DX          ;check length against maximum (DX)
  588.    Jg    Decision2Make   ;jump if line is longer than width
  589.    Mov   AX,CurLine      ;get current line length back
  590.    Mov   LastSpc,AX      ;save it as last space found
  591.    Jmp   ScanLoop        ;go back for more
  592.  
  593. Figure 3: The word-wrap routines (invoked when you use the /Wxx option)
  594. scan the text in a two-step process. This is the code for the second scan;
  595. it actually consists of a series of small scans.
  596. =========================================================================