home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / del2faq.zip / TI2844.ZIP / TI2844.ASC
Text File  |  1996-02-28  |  10KB  |  191 lines

  1.    NUMBER  :  2844
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  All
  4.        OS  :  Windows/Win32
  5.      DATE  :  February 28, 1996                       
  6.  
  7.     TITLE  :  Using The ASCII Driver With Comma-delimited Files
  8.  
  9. Delphi (and the BDE) has the capability to use ASCII files to a limited
  10. degree as tables. The ASCII driver has the capability to translate the
  11. data values in an ASCII fixed-length field or a comma-delimted file into
  12. fields and values that can be displayed through a TTable component. How
  13. this translation of the ASCII file takes place depends on an accompanying
  14. schema file. The schema file for an ASCII data file defines various attri-
  15. butes necessary for parsing the ASCII data file into individual field
  16. values. The field definitions for an ASCII fixed-length field file is
  17. relatively straightforward, the offsets of various fields in the ASCII
  18. file being consistent across all rows in the file. However, for comma-
  19. delimited files, this process is slightly more complicated due to the
  20. fact that not all data values in such a file may be the same length for
  21. all rows in the file. This article, then, concentrates on this more
  22. difficult task of reading data from comma-delimited, or varying-length
  23. field, files.
  24.  
  25. The Schema File
  26. ===============
  27.  
  28. The schema file for an ASCII data file contains information that defines
  29. both the file type (comma-delimited versus fixed-length field), as well as
  30. defining the fields that are represented by the data values in each row of
  31. the ASCII data file. (All of the settings used in a schema file are case
  32. insensitive, so "ascii" is just as valid as "ASCII".) In order that a
  33. schema file be recognized as such, it must have the same filename as the
  34. ASCII data file for which it provides definitions, but with the filename
  35. extension .SCH (for SCHema). The attributes that describe the file are:
  36.  
  37.   File name: Enclosed in square brackets, this setting specifies the
  38.              name of the ASCII data file (sans the filename extension,
  39.              which must be .TXT).
  40.  
  41.   Filetype:  Specifies whether the ASCII data file is structured as a
  42.              fixed-length field file (use a setting of FIXED) or a comma-
  43.              delimited file (with data values of potentially varying
  44.              length (use a setting of VARYING).
  45.  
  46.   Delimiter: Specifies the character that surrounds String type data val-
  47.              ues (typically, the double quotation mark, ASCII decimal 34).
  48.  
  49.   Separator: Specifies the character that is used to separate individual
  50.              data values (typically, a comma). This character must be a
  51.              visible character, i.e., cannot be a space (ASCII decimal
  52.              32).
  53.  
  54.   CharSet:   Specifies the language driver (use a setting of ASCII).
  55.  
  56. Following the file definition settings are field definitions, one for each
  57. data value on each row of the ASCII data file. These field definitions
  58. supply the information Delphi and the BDE will need to create a virtual
  59. field in memory to hold the data value, that virtual field's data type
  60. which will affect how the value is translated after being read from the
  61. ASCII file, and size and positioning attributes. The various settings that
  62. will appear in each field definition are:
  63.  
  64.   Field:              Virtual field name, will always be "Field" followed
  65.                       by an integer number representing that field's ord-
  66.                       inal position in respect to the other fields in the
  67.                       ASCII data file. E.G., the first field is Field1,
  68.                       the second Field2, and so on.
  69.  
  70.   Field name:         Specifies the display name for the field, which
  71.                       appears as the column header in a TDBGrid. Naming
  72.                       convention for ASCII table fields follows that for
  73.                       Paradox tables.
  74.  
  75.   Field type:         Specifies the data tyoe BDE is to use in translating
  76.                       the data value for each field and tells Delphi what
  77.                       type of virtual field to create.
  78.                       
  79.                       Use the setting For values of type
  80.                       --------------- ---------------------
  81.                       CHAR            Character
  82.                       FLOAT           64-bit floating point
  83.                       NUMBER          16-bit integer
  84.                       BOOL            Boolean (T or F)
  85.                       LONGINT         32-bit long integer
  86.                       DATE            Date field.
  87.                       TIME            Time field.
  88.                       TIMESTAMP       Date + Time field.
  89.                       
  90.                       (The actual format for date and time data values
  91.                       will be determined by the current setting in the BDE
  92.                       configuration, Date tab page.)
  93.  
  94.   Data value length:  Maximum length of a field's corresponding data
  95.                       value. This setting determines the length of the
  96.                       virtual field that Delphi creates to receive values
  97.                       read from the ASCII file.
  98.  
  99.   Number of decimals: Applicable to FLOAT type fields; specifies the
  100.                       number of digit positions to the right of the deci-
  101.                       mal place to include in the virtual field defini-
  102.                       tion.
  103.  
  104.   Offset:             Offset from the left that represents the starting
  105.                       position for the field in relation to all of the
  106.                       fields that preceed it.
  107.  
  108. For example, the field definition below is for the first field in the
  109. ASCII table. It defines a String type data value with a name of "Text",
  110. a maximum data value length of three characters (and the field will
  111. appear as only three characters long in Delphi data-aware components such
  112. as the TDBGrid), no decimal places (a String data value will never have
  113. any decimal places), and an offset of zero (because it is the first field
  114. and there would not be any preceeding fields).
  115.  
  116.   Field1=Text,Char,3,00,00
  117.  
  118. Here is an example of a schema file with three fields, the first of String
  119. type and the second and third of type date. This schema file would be
  120. contained in a file named DATES.SCH to provide file and field definitions
  121. for an ASCII data file named DATES.TXT.
  122.  
  123.   [DATES]
  124.   Filetype=VARYING
  125.   Delimiter="
  126.   Separator=,
  127.   CharSet=ascii
  128.   Field1=Text,Char,3,00,00
  129.   Field2=First Contact,Date,10,00,03
  130.   Field3=Second,Date,10,00,13
  131.  
  132. This schema defines a comma-delimited field where all String type data
  133. values can be recognized as being surrounded by the double quotation mark
  134. and where distinct data values are separated by commas (excepting any
  135. commas that may appear within the specified delimiter, inside individual
  136. String data values). The character field has a length of three characters,
  137. no decimal places, and an offset of zero. The first date field has a
  138. length of 10, no decimals, and an offset of three. And the second date
  139. field has a length of 10, no decimals, and an offset of 13.
  140.  
  141. For reading ASCII comma-delimited files, the length and offset parameters
  142. for the field definitions do not apply to data values in the ASCII files
  143. (as is the case for fixed-length field files), but to the virtual fields,
  144. defined in the application, into which the values read will be placed. The
  145. length parameter will need to reflect the maximum length of the data value
  146. for each field -- not counting the delimiting quotation marks or the comma
  147. separators. This is most difficult to estimate for String type data values
  148. as the actual length of such a data value may vary greatly from row to row
  149. in the ASCII data file. The offset parameter for each field will not be
  150. the position of the data value in the ASCII file (as is the case for
  151. fixed-length field files), but the offset as represented by the cumulative
  152. length of all preceding fields (again, the defined fields in memory, not
  153. the data values in the ASCII file.
  154.  
  155. Here is a data file that would correspond to the schema file described
  156. above, in a file named DATES.TXT:
  157.  
  158.   "A",08/01/1995,08/11/1995 
  159.   "BB",08/02/1995,08/12/1995 
  160.   "CCC",08/03/1995,08/13/1995
  161.  
  162. The maximum length of an actual data value in the first field is three
  163. ("CCC"). because this is the first field and there are no preceding
  164. fields, the offset for this field is zero. The length of this first field
  165. (3) is used as the offset for the second field. The length of the second
  166. field, a date value, is 10, reflecting the maximum length of a data value
  167. for that field. The accumulated length of the first and second fields are
  168. then used as the offset for the third field (3 + 10 = 13) .
  169.  
  170. It is only when the proper length for the data values in the ASCII file
  171. are used and each fields length added to any preceding fields to produce
  172. offset values for succeeding fields that this process will correctly read
  173. the data. If data is misread because of improper length settings in the
  174. schema file, most values will suffer adverse translation effects, such
  175. as truncation of character data or numeric values being interpreted as
  176. zeros. Data will usually still be displayed, but no error should occur.
  177. However, values that must be in a specific format in order to be trans-
  178. lated into the appropriate data type will cause errors if the value read
  179. includes characters not valid in a date value. This would include a date
  180. data value which, when incorrectly read may contain extraneous characters
  181. from other surrounding fields. Such a condition will result in a data
  182. translation exception requiring an adjustment of the field length and
  183. offset settings in the schema file.
  184.  
  185.  
  186.  
  187. DISCLAIMER: You have the right to use this technical information
  188. subject to the terms of the No-Nonsense License Statement that
  189. you received with the Borland product to which this information
  190. pertains.
  191.