THE BYTE MECHANIC
Tune Up Those Misfiring Files
by William A. Schneider
Sometimes you have to lift the hood on your files to fix something or just to take a look around inside. Text ediors and word processors work fine for ASCII files, but not all files are ASCII and not all editors can show you the whole story. That's when you need to call on a professional—The Byte Mechanic--to get the job done.
Learn all about your files--byte by byte--with MECHANIC.ARC on your START disk.
Ever wonder what a file really looks like as it's stored on disk? Ever try to view or print a file, only to find that every line prints on top of the previous line or prints with no spaces between words? Ever need to change a file from single- to double-spacing or from double to triple? Then this program is for you! Byte Mechanic is designed to make it easy to study the contents and layout of any type of file and make byte-by-byte changes if you want.
LET'S GET STARTED
To get started, un-ARC file MECHANIC.ARC from your START disk, following the disk instructions located elsewhere in this issue. Byte Mechanic runs in medium or high resolution. Start out by running MECHANIC.PRG and then selecting SAMPLE1.DOC by double-clicking on its filename. (This is an example of a file created with 1st Word from Atari.)
Byte Mechanic can read any type of file, but its maximum file size is determined by the amount of free memory in your machine; if you don't have enough memory for the selected file, an alert box gives you a choice of selecting a new file or splitting the file up. If you select Split, you can load a segment of the file by providing the start and end byte positions of the file. Then when you save the segment, you can save it to an existing filename. One of your choices then is to append the file segment in memory to the end of the selected file. By repeating this procedure, you can restore any size file on disk.
The contents of SAMPLE1.DOC should be on your screen with the menu bar at the bottom. Press the Help key for a brief description of all the options, cursor movement keys and some important notes. Note: some commands described on the help screens are not covered in this article.
Byte Mechanic uses the Atari character set to show all ASCII codes from 1 to 255. This display is called Text mode. Press the A key for <A>scii/Codes, and only characters that you can print on a typewriter are visible. Spaces replace ASCII characters with values below 32 and above 126. Pressing A again displays the full character set. If it's still hard to read the file, select <V>iew by pressing V. In this mode, printable characters are displayed with wordwrap. All non-printable characters are replaced with a space.
If you are in the View mode, return to the Text mode with <H>ex/Text. If all of the special characers are not shown, use the <A>scii/Codes option to display them. If you have a color system, try the <C>olr option--all of the characters with ASCII codes under 32 are displayed in red and those with codes over 126 are displayed in blue.
There is one other display mode available. Press H to toggle to the Hex mode; the hex codes for each character are displayed on the left side of the screen with the corresponding ASCII character on the right. The <A>scii/ Codes and <C>olr options work on this display also.
FINDING YOUR WAY AROUND THE FILE
Moving through a file is easy. In Hex or Text modes, a number in the lower left corner of the menu area always indicates the position of the byte under the cursor from the beginning of the file. In View mode there is no cursor; the number shown in the lower left corner of the menu is the byte number for the character displayed in the top left corner of the screen. (You use the arrow keys to move around the file in View mode. Press Help to see the other movement command keys.)
Now read the SAMPLE1.DOC file. This file describes how 1st Word uses special characters in its files. The file is written so that you can see the special characters as they are explained. You will probably want to read the text in the View mode and examine the codes in the Hex mode with color on.
Let's look at another example. Choose the <Q>uit option, click on New File and then select SAMPLE2.STW. This is an example of an ST Writer file. Try the <V>iew option. The file is still not easy to read. Let's use Byte Mechanic to modify the file for easier viewing. But before we modify the file, you need to know some of the other commands we will be using.
FINDING A STRING
Finding specific characters or strings of characters in a file is easy. The
The <T>rade option replaces all occurrences of an ASCII or hexadecimal string with another string. Both the find and replace strings are case sensitive. You are asked for the search range; by pressing the Return key twice, the entire file is selected.
Any time you are asked for an input, either numeric or alphanumeric, you can use the standard editing keys: Escape, arrows, Backspace, Delete and Insert. A thin cursor means you're in insert mode, while a thick cursor is for overwrite mode. Use the keypad to enter hex strings: ( is A, ) is B, / is C, * is D, - is E and + is F.
EDITING A FILE
To edit, place the cursor over the first byte you want to change. Type E, then select <O>verwrite, <I>nsert or <D>elete. In Overwrite mode, you type over the characters; in Insert mode, you can insert up to 16 characters without erasing any. Delete lets you mark characters for deletion or asks for a range of bytes to delete. Before any permanent changes are made, you are given a final opportunity to abort any changes with the Escape key.
You can create a macro of up to either 30 (or 60) keystrokes and assign it to the F1-F10 keys for later playback using the <K>ey option. For example, you may want to set up a macro to convert from one word processing format to another. To define a macro, press the function key you wish to define, enter the keystrokes and then press the same function key again to save it. If you decide not to save it, press Undo to cancel. To delete a character while defining it, press Control-D. To edit a previously defined macro, refer to the online help. A macro can be assigned to the function keys at any time, but to replay a macro you must first enable the type-ahead buffer by selecting <B>uffer and click on On. You can also save and load macro files to disk.
Additionally, you can use STARTKey from the Winter 1987 issue of START to save a series of keystrokes. As with the internal F1-F10 macros, the buffer must be enabled. Some Byte Mechanic operations, once started, can be stopped in the middle by pressing any key. Even if you are in the middle of a STARTKey or Byte Mechanic macro playback, this will still work. The macro will be temporarily halted and then resumed when the operation is complete.
THE ST WRITER EXAMPLE
Now, let's get back to the ST Writer example. First, be sure that you are at the top of the file by pressing the Clr Home key. Now change to the Hex mode. Notice that the byte 00 seems to represent the end of a line. This is clearer if you use the <C>olr option. To change all of the 00 characters to a carriage return and a line feed, press T and type: H 00 <Return> H 0D0A <Return> <Return> <Return> Y
Now change to the <V>iew mode. It's still not perfect, but at least we can read the file now. Notice that ST Writer also has a file header. To remove the header, go to Hex mode, then type: <Home> E D <Down arrow> <Down arrow> <Down arrow> <Return>
A FEW MORE PROGRAM USES
With Byte Mechanic, you can make many changes in a file to control its format onscreen or on the printer. Here are some examples of problems and their solutions:
- Every line prints on top of the previous line: Use <T>rade to change every carriage return (hex 0D) to carriage return and linefeed (hex 0D0A).
- There are no spaces between words, such as in a 1st Word or Word Writer file. Look at the file and use <T>rade to change the special character between words to a space (hex 20).
- Add page breaks to a file: First add 6 pairs of hex 0D0A (CR/LF) to the beginning of the file for a 1 inch top margin. With the cursor after the 6th 0D0A pair, use the <F>ind option followed by
ultiple find 53 to find the 54th occurrence of 0D0A, then insert 12 pairs of 0D0A for a 1 inch top and bottom margin at the page break. Or enter a form feed (hex 0C) plus 6 pairs of 0D0A. Repeat the pattern to paginate the entire document.
- Add a left margin of 12 spaces: Use <T>rade to replace every 0D0A with 0D0A and 12 spaces.
- Change from double- to triple-space: Use <T>rade to replace every 0D0A0D0A with 0D0A0D0A0D0A or 0D0D0D0A.
- Analyze a database file: You can study the way the database's fields and records are delimited and then use that information to convert the DB to another format.
- Rename a .PRG file and its accompanying.RSC file: Load the.PRG file into Byte Mechanic, then <F>ind the filename for the RSC file and change all occurrences of the name to the new name. Important: make sure the new name is the same length of the original name and don't use spaces to pad the name. Save the modified .PRG file under a new name and then change the.RSC filename itself from the desktop.
- Fix a program you've just un-ARC'd that won't run. Look through the file to find any filenames. If they are preceeded with the pathname of a subdirectory, you can then configure your directories in the same manner. Note: Never change the length of a .PRG file; always use overwrite mode while you are editing. If you accidentally delete or add a byte of code, the program will most likely crash, so always work from a backup copy.
The <L>[i]st option will send the screen to the printer in either Text or Hex modes. The output is 80 characters wide and two screens can be printed on one page with automatic page advance. Only characters with ASCII codes between 32 and 126 will print; all other codes will be spaces.
Press <L>st to choose among three printers and to see or change the current settings for the left margin and printer setup strings. Click on OPTIONS to select a printer initialization string to send to your printer. Byte Mechanic includes two strings for Epson-compatible printers: string 1 resets the printer and then sets it to 12 characters per inch and string 2 resets the printer, sets it to 12 cpi and enables double-strike mode. Use the SPECIAL choice to enter your own string of control character codes. Remember, the string should begin with the code sequence to reset your printer or your printout may begin with leftovers from your last print job. If you wish to change the control strings and descriptions permanently, you can use Byte Mechanic to modify the MECHANIC.PRG file itself. <Q>uit your current file and select MECHANIC.PRG. Be sure that you are working on a backup copy! Use the <F>ind option to locate the string "printer string" You will see the following: Printer String 1=027064027077000000 000000000000000000000000000End
The string of characters between "=" and "End" is the printer initialization string for "Str 1". Every 3 digits represent the decimal Ascii code for a particular character or control code. Use <E>dit and <O>verwrite in ASCII mode to change this string. Fill the bytes between the last code and the "End" with 0's. The default codes in Str 1 represent the following on an Epson printer:
027 064 = escape @ Printer reset
027 077 = escape M 12 char/inch
000 000 . . . Filler
Then you will see the following string:
The space between "=" and "End" can be any characters you wish. Overwrite these characters to describe what the Str 1 codes do to the printer. You can change the codes and descnption for Str 2 in a similar manner. If you make changes that you want to be permanent, save the modified file to disk for later use; you will need to <Q>uit the program and rerun it for the codes to be active.
Select the Ge<M> option to use desk accessories. All of the accessories I have tried seem to work OK. Use this option with caution; if your accessory requires too much additional memory, you could crash the computer. Be sure to close any open windows before returning to Byte Mechanic.
SAVING A FILE
You can save your modified file under the original name or a new name by selecting <S>ave.
The <Q>uit option warns you that the file in memory will be abandoned. If you click on New File you can select and load a new file to examine without exiting Byte Mechanic. To exit the program, click on Exit.
William Schneider is a chemical engineer and plant manager for a Fortune 500 company. He learned Fortran IV at the University of Illinois, and has since used many languages on a variety of computers.