* * DBASCIIT.KEX * * When exporting database records from something like Access or dBASE to a * tab-delimited ASCII text file it can be difficult to view the file with * KEDIT since fields are not lined up in specific columns. This macro will * go through the file and determine the appropriate widths needed for each * field (the widest element plus 2 for spacing) and then align the fields * in vertical columns with the EXPAND command. * * By default, the macro also assumes that the first record of the * file lists the field names. If this is not the case then set the * variable below called "FieldNames" to zero. * * This macro also strips out the double quotes around strings when it is * done since doing so results in somewhat more readable output. If you do * NOT want this to happen then set the variable "StripQuotes" to zero. * * If the macro determines that the current WIDTH setting would be exceeded * by aligning the fields then an error is issued and the process is * aborted. You can then restart KEDIT and specify a wider WIDTH setting * and try again. * * After determining the field locations the macro issues a SET TABS command * for these columns. Since SET TABS can only accept 32 discrete tab * positions only records with 32 or less fields can be handled. * * Given a source file that looks like this (where tab characters are indicated * by plus signs): * * "Name"+"Address"+"Age" * "John"+"Hartford, CT"+26 * "Alice"+"Provo, UT"+33 * "Bill"+"San Diego, CA"+51 * * the macro will convert it to this: * * Name |Address |Age * ------------------------------ * John |Hartford, CT |26 * Alice |Provo, UT |33 * Bill |San Diego, CA |51 * * Works with: * * KEDIT for Windows 1.5 * KEDIT 5.0 for DOS * KEDIT 5.0 for OS/2 * * Original author: Kent Downs, MSG, 8/96 * * Change this to 0 if your original file does not begin with a list of field * names. FieldNames = 1 * Change this to 0 if you do not want double quotes stripped from the file. StripQuotes = 1 "preserve" "set autosave off" * ASCII tab character tab = d2c(9) * The first record should have tab characters. Count them to determine how * many fields are in each record. ":1" "nomsg count /"tab NumFields = word(lastmsg.1(), 1) if upper(NumFields) = "NO" then do say "DBASCIIT Error 1: Could not find any tab characters in line 1" exit 1 end if NumFields > 32 then do say "DBASCIIT Error 2: Too many fields per record for this macro." say " The limit is 32 fields." exit 2 end * Init our table of widths do i = 1 to NumFields + 1 FieldWidth.i = 0 end * This might take a while prompt = "Determining optimal field widths. Please wait..." say prompt "refresh" * Initialize the table of field widths call ProcessLine n = size.1() RefreshInterval = n % 20 j = 1 do while \focuseof() line = curline.3() do i = 1 to NumFields + 1 n = pos(tab, line) if n = 0 then field = line else do field = substr(line, 1, n - 1) line = substr(line, n + 1) end if FieldWidth.i < length(field) then FieldWidth.i = length(field) end "down 1" if j >= RefreshInterval then do say prompt "refresh" j = 1 end else j = j + 1 end * It's quite possible that we'll exceed the current WIDTH setting n = 0 do i = 1 to NumFields + 1 * Add some extra space for readability FieldWidth.i = FieldWidth.i + 2 * Need to account for the "|" divider characters between columns n = n + FieldWidth.i + 1 end if n > width.1() then do say "DBASCIIT Error 3: Expanded text will exceed the current WIDTH setting." say " Restart KEDIT with a WIDTH of at least" n"." exit 3 end * Build a SET TABS command using the columns we've determined TabCommand = "set tabs" DisplayWidth = 0 do i = 1 to NumFields + 1 * Put in an extra space between fields DisplayWidth = DisplayWidth + FieldWidth.i + 1 TabCommand = TabCommand DisplayWidth end * Prefix each field with the "|" character to aid readability (optional) "nomsg c/"tab"/"tab"|/ all *" TabCommand "expand all" ":1" if FieldNames Then do * Generally the first line lists the field names. Put a divider in * (optional) "i" copies("-", DisplayWidth) end "top" if StripQuotes then 'nomsg c/"// all *' * We probably don't want to save this file so reset the alteration count to 0 "set alt 0 0" exit 0 * Determine the field widths for the current line ProcessLine: do i = 1 to NumFields + 1 n = pos(tab, line) if n = 0 then field = line else do field = substr(line, 1, n - 1) line = substr(line, n + 1) end FieldWidth.i = max(length(field), FieldWidth.i) end return