home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PASCAL / TBTREE16.ZIP / BYTEDATA.PAS < prev    next >
Pascal/Delphi Source File  |  1989-07-13  |  13KB  |  290 lines

  1. (* TBTree16             Copyright (c)  1988,1989       Dean H. Farwell II    *)
  2.  
  3. unit ByteData;
  4.  
  5. (*****************************************************************************)
  6. (*                                                                           *)
  7. (*                   B Y T E   D A T A   A R R A Y   R O U T I N E S         *)
  8. (*                                                                           *)
  9. (*****************************************************************************)
  10.  
  11. (* This unit implements a new data type, the byte array, which is an array of
  12.    bytes similar to a string.  It is similar in that it can hold between 0 and
  13.    255 bytes of data.  It can have a maximum length of 256 bytes. The first
  14.    byte in the array (byte 0) contains the number of data bytes in the array
  15.    (0 - 255).
  16.  
  17.    One big difference between a byte array and a string is that you can
  18.    declare a string variable to be whatever size you want.  You can not do
  19.    this with other types, including this one.  However, you can declare your
  20.    own type to be an array of bytes the length you want.  These routines use
  21.    untyped parameters so that they will handle an array of between 0 and 255
  22.    bytes of data (length of 1 to 256 bytes including the 0th byte, the length
  23.    byte).
  24.  
  25.    The only other real difference between a string and a byte array is that
  26.    the byte array contains bytes which are just raw data and do not represent
  27.    characters.
  28.  
  29.    You can use this unit in two ways.  You can declare variables of the type
  30.    ByteData which will take 256 bytes of memory and you can use the routines
  31.    as desired.  Or, you can declare a  variable to be an array of bytes the
  32.    length you need (plus one byte for the length).  You can still use the
  33.    routines.  You must ensure that you use variables that are big enough.  If
  34.    you use a routine that produces a byte array of length 10 (eleven bytes
  35.    required) you better have a variable big enough.  If not, this will write
  36.    over some other variable in memory and really bad things are bound to
  37.    happen.  If you do not understand how this works, do not get fancy.
  38.  
  39.    This unit can be used to implement concatenated indexes, but this is not
  40.    obvious.  You do this by creating the index with type ByteArrayValue and
  41.    using these routines to concatenate the values together.  It really only
  42.    makes sense to do retrievals on equality if this method is used.  Someday,
  43.    I'll get around to developing an example of how to use this.  This was
  44.    developed for use with TBASE.
  45.  
  46.    Note - To use the 8087 types you must compile this unit using {$N+}       *)
  47.  
  48. (*\*)
  49. (* Version Information
  50.  
  51.    Version 1.1 - Unit did not exist.
  52.  
  53.    Version 1.2 - Unit did not exist.
  54.  
  55.    Version 1.3 - Unit did not exist.
  56.  
  57.    Version 1.4 - Unit created with this version.
  58.  
  59.    Version 1.5 - Changed code internally to use newly added FastMove unit
  60.  
  61.    Version 1.6 - No Changes                                                  *)
  62.  
  63.  
  64. (*////////////////////////// I N T E R F A C E //////////////////////////////*)
  65.  
  66. interface
  67.  
  68. uses
  69.     FastMove,
  70.     Numbers,
  71.     Strings;
  72.  
  73. type
  74.     ByteArrayRange = 0 .. MAXBYTE;
  75.  
  76.     ByteArray = Array [ByteArrayRange] of Byte;    (* This handy type is used
  77.                                                       to store a from 1 to
  78.                                                       255 bytes.  It is much
  79.                                                       a string in that the
  80.                                                       first element is the
  81.                                                       number of bytes in the
  82.                                                       array. All bytes after
  83.                                                       the significant number
  84.                                                       of bytes are not
  85.                                                       significant.  This is
  86.                                                       used for concatenated
  87.                                                       indexes                *)
  88.  
  89. (*\*)
  90. (* This routine will take an input string and create a byte array.  The number
  91. of bytes held in the byte array is equal to the maximum number of characters
  92. which the string will handle.  For example if a string is passed in which was
  93. declared as a type String[20] then the new byte array will contain 20 bytes of
  94. information (plus the 0th byte which contains the length).  The maximum length
  95. is passed in using the maxLength parameter.  If the string passed in is
  96. shorter than the maximum allowed, the end of the string is filled with as many
  97. 0's as required.                                                             *)
  98.  
  99. procedure ConvertStringToByteArray(str : String;
  100.                                    maxLength : StringLengthRange;
  101.                                    var resultArray);
  102.  
  103.  
  104. (* This routine will take two values and will create a new byte array which
  105. is a concatenation of the two input values.  The input values are of type
  106. ValueType.  All the ValueType(s)s are supported.  The size of the resulting
  107. byte array will be equal to the sum of the sizes of the two input values.
  108. However, if the resulting size will exceed 255 byte, the resulting array will
  109. be truncated and only the first 255 bytes will be returned.
  110.  
  111. Note: Be sure that your variable which is used for the returned array is big
  112. enough!!!                                                                    *)
  113.  
  114. procedure ConcatenateTwoValues(var val1;
  115.                                vType1 : ValueType;
  116.                                var val2;
  117.                                vType2 : ValueType;
  118.                                var resultArray);
  119.  
  120.  
  121. (* This routine will take three values and will create a new byte array which
  122. is a concatenation of the three input values.  The input values are of type
  123. ValueType.  All the ValueType(s)s are supported.  The size of the resulting
  124. byte array will be equal to the sum of the sizes of the three input values.
  125. However, if the resulting size will exceed 255 byte, the resulting array will
  126. be truncated and only the first 255 bytes will be returned.
  127.  
  128. Note: Be sure that your variable which is used for the returned array is big
  129. enough!!!                                                                    *)
  130.  
  131. procedure ConcatenateThreeValues(var val1;
  132.                                  vType1 : ValueType;
  133.                                  var val2;
  134.                                  vType2 : ValueType;
  135.                                  var val3;
  136.                                  vType3 : ValueType;
  137.                                  var resultArray);
  138.  
  139. (*!*)
  140. (*\*)
  141. (*///////////////////// I M P L E M E N T A T I O N /////////////////////////*)
  142.  
  143. implementation
  144.  
  145. (* This routine will take an input string and create a byte array.  The number
  146. of bytes held in the byte array is equal to the maximum number of characters
  147. whcih the string will handle.  For example if a string is passed in which was
  148. declared as a type String[20] then the new byte array will contain 20 bytes of
  149. information (plus the 0th byte which contains the length).  The maximum length
  150. is passed in using the maxLength parameter.  If the string passed in is
  151. shorter than the maximum allowed, the end of the string is filled with as many
  152. 0's as required.                                                             *)
  153.  
  154. procedure ConvertStringToByteArray(str : String;
  155.                                    maxLength : StringLengthRange;
  156.                                    var resultArray);
  157.  
  158. var
  159.     resultByteArray : ByteArray absolute resultArray;
  160.  
  161.     begin
  162.     FastMover(str,resultByteArray,Length(str) + 1);
  163.     FillChar(resultByteArray[resultByteArray[0] + 1],
  164.              maxLength - resultByteArray[0],0);
  165.     resultByteArray[0] := maxLength;
  166.     end;                                   (* end of NormalizeString routine *)
  167.  
  168. (*\*)
  169. (* This routine will take two values and will create a new byte array which
  170. is a concatenation of the two input values.  The input values are of type
  171. ValueType.  All the ValueType(s)s are supported.  The size of the resulting
  172. byte array will be equal to the sum of the sizes of the two input values.
  173. However, if the resulting size will exceed 255 byte, the resulting array will
  174. be truncated and only the first 255 bytes will be returned.
  175.  
  176. Note: Be sure that your variable which is used for the returned array is big
  177. enough!!!                                                                    *)
  178.  
  179. procedure ConcatenateTwoValues(var val1;
  180.                                vType1 : ValueType;
  181.                                var val2;
  182.                                vType2 : ValueType;
  183.                                var resultArray);
  184.  
  185. var
  186.     byteArray1 : ByteArray absolute val1;
  187.     byteArray2 : ByteArray absolute val2;
  188.     resultBytearray : ByteArray absolute resultArray;
  189.     size1,
  190.     size2,
  191.     size3 : ByteArrayRange;   (* always equal to the number of bytes of data
  192.                                  not counting the byte required to keep the
  193.                                  length (BYTEARRAYVALUE and STRINGVALUE)     *)
  194.  
  195.     begin
  196.     case vType1 of
  197.         BYTEARRAYVALUE : size1 := byteArray1[0];
  198.         BYTEVALUE : size1 := BYTESIZE;
  199.         SHORTINTVALUE : size1 := SHORTINTSIZE;
  200.         INTEGERVALUE : size1 := INTEGERSIZE;
  201.         LONGINTVALUE : size1 := LONGINTSIZE;
  202.         WORDVALUE : size1 := WORDSIZE;
  203.         STRINGVALUE : size1 := byteArray1[0];
  204.         REALVALUE : size1 := REALSIZE;
  205. (*   The following types are only for 8087 - and are compiled only if the unit
  206.      is compiled using {$N+}                                                 *)
  207.  
  208. {$IFOPT N+}
  209.         SINGLEVALUE : size1 := SINGLESIZE;
  210.         DOUBLEVALUE : size1 := DOUBLESIZE;
  211.         EXTENDEDVALUE : size1 := EXTENDEDSIZE;
  212.         COMPVALUE : size1 := COMPSIZE;
  213. {$ENDIF}
  214.         end;                                       (* end of case statement *)
  215.     if vType1 in [BYTEARRAYVALUE,STRINGVALUE] then
  216.         begin
  217.         FastMover(byteArray1[1],resultByteArray[1],size1);
  218.         end
  219.     else
  220.         begin
  221.         FastMover(val1,resultByteArray[1],size1);
  222.         end;
  223.     case vType2 of
  224.         BYTEARRAYVALUE : size2 := byteArray2[0];
  225.         BYTEVALUE : size2 := BYTESIZE;
  226.         SHORTINTVALUE : size2 := SHORTINTSIZE;
  227.         INTEGERVALUE : size2 := INTEGERSIZE;
  228.         LONGINTVALUE : size2 := LONGINTSIZE;
  229.         WORDVALUE : size2 := WORDSIZE;
  230.         STRINGVALUE : size2 := byteArray2[0];
  231.         REALVALUE : size2 := REALSIZE;
  232. (*   The following types are only for 8087 - and are compiled only if the unit
  233.      is compiled using {$N+}                                                 *)
  234.  
  235. {$IFOPT N+}
  236.         SINGLEVALUE : size2 := SINGLESIZE;
  237.         DOUBLEVALUE : size2 := DOUBLESIZE;
  238.         EXTENDEDVALUE : size2 := EXTENDEDSIZE;
  239.         COMPVALUE : size2 := COMPSIZE;
  240. {$ENDIF}
  241.  
  242.         end;                                       (* end of case statement *)
  243.     if (size1 + size2) > MAXBYTE then
  244.         begin
  245.         size2 := MAXBYTE - size1;
  246.         end;
  247.     if vType2 in [BYTEARRAYVALUE,STRINGVALUE] then
  248.         begin
  249.         FastMover(byteArray2[1],resultByteArray[size1 + 1],size2);
  250.         end
  251.     else
  252.         begin
  253.         FastMover(val2,resultByteArray[size1 + 1],size2);
  254.         end;
  255.     resultByteArray[0] := size1 + size2;
  256.     end;                              (* end of ConcatenateTwoValues routine *)
  257.  
  258. (*\*)
  259. (* This routine will take three values and will create a new byte array which
  260. is a concatenation of the three input values.  The input values are of type
  261. ValueType.  All the ValueType(s)s are supported.  The size of the resulting
  262. byte array will be equal to the sum of the sizes of the three input values.
  263. However, if the resulting size will exceed 255 byte, the resulting array will
  264. be truncated and only the first 255 bytes will be returned.
  265.  
  266. Note: Be sure that your variable which is used for the returned array is big
  267. enough!!!                                                                    *)
  268.  
  269. procedure ConcatenateThreeValues(var val1;
  270.                                  vType1 : ValueType;
  271.                                  var val2;
  272.                                  vType2 : ValueType;
  273.                                  var val3;
  274.                                  vType3 : ValueType;
  275.                                  var resultArray);
  276.  
  277. var
  278.     byteArray1 : ByteArray;
  279.     resultBytearray : ByteArray absolute resultArray;
  280.  
  281.     begin
  282.     ConcatenateTwoValues(val1,vType1,val2,vType2,byteArray1);
  283.     ConcatenateTwoValues(byteArray1,BYTEARRAYVALUE,
  284.                          val3,vType3,
  285.                          resultByteArray);
  286.     end;                            (* end of ConcatenateThreeValues routine *)
  287.  
  288.  
  289. end.                                                 (* end of ByteData unit *)
  290.