TIP: Click on subject to list as thread! ANSI
echo: power_bas
to: LAWRENCE GORDON
from: ERIC SCHONNING
date: 1996-06-27 00:00:00
subject: Re: Reading Pascal files

LG> Anyone know how to read a Pascal data file and convert the file struct
LG> PB?  The first data byte of a string is usually the length of the stri
LG> I'm supposing that numeric variables don't contain a string length.
 
I used to do this extensively when I first learned how to program,
writing utilities for Phoenix and Wildcat BBS software.  I always used
the random file and field statement method.  For example, if the pascal
structure called out for a length 12 string and 2 integers, you know
the record length is 17 (12 + 1 for the string, and 2 * 2 for the
integers).  The corresponding field statement would be
 
open "pascal.dat" as #1 len=17
field #1, 1 as str1_len$, 12 as str1$, 2 as int1$, 2 as int2$
get #1,1
 
you can use CVI to convert the integers to number values, and I always
used LEFT$ to read the strings, for example 
 
print left$(str1$,asc(str1_len$))
print cvi(int1$)
 
now that PB 3.2 supports all the other data types (like words), you can
easily convert these too.  You know that words are two bytes, but use
CVWD (I think thats it, you can look it up) and that long
words/integers are 4 bytes, and can use the CLNG and CDWRD to make them
into numbers.  
 
The only data types I had problems with are Turbo Pascal 3.0 floats,
CVS would not properly convert them, but I think they (Borland) have
since switched to standard IEEE format for their floats so you should
be able to use CVS, CVD, etc. to convert the floating point numbers.
 
The biggest trick is just knowing how many bytes each data type uses (2
for integer, 2 for words, 4 for single precision floats/double
words/long integers, etc.) and then being careful when making your field
statement.  I used to print out the pascal structure then carefully
mark off each line as I made the corresponding field statement.  
 
and lastly, don't forget to GET a record before you try to print out
the values or you'll just get junk.... 
 
others helps are also first calculating the record size based on the
pascal structure (like I did at top of message).  I would always figure
that out first, to make sure my idea of how to convert it was right. 
Once you calculate this then take the data filesize and divide by the
record size and you should get an integer (no remainders), then you know
at least you are on the right track.   
 
well that should be enough to get you going.   have fun!  :)  the long
ones were always a bear....           Eric
 
ps: I am assuming once you read it you can convert it to PB?  I have
yet to use PB's type structures, so can't give you any help there.  For
the field statement, it would be pretty much the same as for the Pascal
field statement except that strings don't have that 1 byte length at
the start of them, and PB pad's extra characters in the string with
whatever you specify, whereas Pascal would leave the old data there,
which is why its important to use the length byte.  To convert a number
back into a string use the MKI$, MKS$, etc depending on the data type.
I was able to add to the pascal data file just fine using this method
(use LSET to set field strings) and the the pascal program read new
data with no problems.
--- QM v1.00
---------------
* Origin: Creekside Manor (805) 484-8016 CdCom Support BBS (1:206/2512.0)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.