home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code2 / tag_env / tagenv.bas < prev    next >
BASIC Source File  |  1995-02-27  |  7KB  |  247 lines

  1.  
  2. '   TAGENV.BAS
  3.  
  4. '   REQUIRES:
  5. '             STRTOK.BAS
  6.  
  7.  
  8. '   TagString subsystem:
  9. '
  10. '   This set of routines provides support for tagged string fields
  11. '   in a VB Form or Control Tag property.
  12. '
  13. '   The Tag property, under this support, consists of a string
  14. '   of keyword=value pairs, delimited by semicolons;  for instance,
  15. '   the following might be a tag string:
  16. '
  17. '   formname=myForm;myname="Thomas A. Dacon";graphsize=large
  18. '
  19. '   You delete a string from a tagged string field by setting it
  20. '   to a null string, just like the SET command in DOS.
  21. '
  22. '   Keywords and contents fields are stored in mixed case, as supplied,
  23. '   but searches for keywords are case-insensitive.
  24.  
  25. '   The API:
  26. '
  27. '   SetFormTagString <form>,    key$, contents$
  28. '   GetFormTagString <form>,    key$, contents$
  29. '
  30. '   SetCtlTagString  <control>, key$, contents$
  31. '   GetCtlTagString  <control>, key$, contents$
  32. '
  33.  
  34.  
  35. Function ExtractKey$ (theSubString As String)
  36. '
  37. '   Returns the keyword portion of a
  38. '   keyword=value string "kkk=vvvvv"
  39. '
  40.     Dim i As Integer
  41.     Dim theKey As String
  42.  
  43.     i = InStr(theSubString, "=")
  44.     If i <> 0 Then
  45.         theKey = Left$(theSubString, i - 1)
  46.     Else
  47.         theKey = ""
  48.     End If
  49.  
  50.     ExtractKey$ = theKey
  51.  
  52. End Function
  53.  
  54. Function ExtractKeyValue$ (theSubString As String)
  55. '
  56. '   Returns the value portion of a
  57. '   keyword=value string "kkk=vvvvv"
  58. '
  59.  
  60.     Dim i As Integer
  61.     Dim theContents As String
  62.  
  63.     i = InStr(theSubString, "=")
  64.     If i <> 0 Then
  65.         theContents = Mid$(theSubString, i + 1)
  66.     Else
  67.         theContents = ""
  68.     End If
  69.  
  70.     ExtractKeyValue$ = theContents
  71.  
  72. End Function
  73.  
  74. Sub GetCtlTagString (c As Control, key As String, contents As String)
  75. '
  76. '   Get the current value of a key=contents field
  77. '   in a Control's Tag property.  A null string is
  78. '   returned if the key is not found.
  79. '
  80.     GetTagSubstring (c.Tag), key, contents
  81.  
  82. End Sub
  83.  
  84. Sub GetFormTagString (f As Form, key As String, contents As String)
  85. '
  86. '   Get the current value of a key=contents field
  87. '   in a Form's Tag property.  A null string is
  88. '   returned if the key is not found.
  89. '
  90.     GetTagSubstring (f.Tag), key, contents
  91.  
  92. End Sub
  93.  
  94. Sub GetTagSubstring (theTagString As String, key As String, contents As String)
  95. '
  96. '   Internal routine to retrieve the contents of a key=contents
  97. '   field in a string variable.
  98. '
  99.     Dim thisString As String
  100.     Dim subString As String
  101.  
  102.     contents = ""   'in case we don't find the key
  103.  
  104.     If theTagString <> "" Then
  105.         thisString = theTagString
  106.         Do
  107.             subString = StrTok$(thisString, ";")
  108.             thisString = ""
  109.             If subString <> "" Then
  110.                 If UCase$(ExtractKey$(subString)) = UCase$(key) Then
  111.                     contents = ExtractKeyValue$(subString)
  112.                     Exit Do
  113.                 End If
  114.             End If
  115.         Loop Until subString = ""
  116.     End If
  117.  
  118. End Sub
  119.  
  120. Function ParseKeywordValue (text As String, keyword As String, keyvalue As String) As Integer
  121. '
  122. '   Given a text string of the form:
  123. '           keyword = value
  124. '       or  keyword = "value"
  125. '   parses the keyword and value into the output arguments,
  126. '   stripping leading and trailing blanks, and removing the
  127. '   optional double quotes from the value field.
  128. '
  129. '   Returns Boolean("=" character present, following a non-blank field)
  130. '
  131.     Dim eqPos As Integer
  132.     Dim quotes As String * 1
  133.  
  134.     eqPos = InStr(text, "=")
  135.     If eqPos > 0 Then
  136.         keyword = LTrim$(RTrim$(Left$(text, eqPos - 1)))
  137.         keyvalue = LTrim$(RTrim$(Mid$(text, eqPos + 1)))
  138.         quotes = Chr$(34)
  139.         If Left$(keyvalue, 1) = quotes And Right$(keyvalue, 1) = quotes Then
  140.             keyvalue = Mid$(keyvalue, 2, Len(keyvalue) - 2)
  141.         End If
  142.     End If
  143.  
  144.     ParseKeywordValue = (eqPos > 0) And (keyword <> "")
  145.  
  146. End Function
  147.  
  148. Sub SetCtlTagString (c As Control, key As String, contents As String)
  149. '
  150. '   Insert, replace, or delete a key=contents field
  151. '   in a Control's Tag property.
  152. '
  153.     Dim theTagString As String
  154.  
  155.     theTagString = c.Tag
  156.     SetTagSubstring theTagString, key, contents
  157.     c.Tag = theTagString
  158.  
  159. End Sub
  160.  
  161. Sub SetFormTagString (f As Form, key As String, contents As String)
  162. '
  163. '   Insert, replace, or delete a key=contents field
  164. '   in a Form's Tag property.
  165. '
  166.     Dim theTagString As String
  167.  
  168.     theTagString = f.Tag
  169.     SetTagSubstring theTagString, key, contents
  170.     f.Tag = theTagString
  171.  
  172. End Sub
  173.  
  174. Sub SetTagSubstring (theTagString As String, key As String, contents As String)
  175. '
  176. '   Internal routine to insert, replace, or delete
  177. '   a key=contents field in a string variable.
  178. '
  179.     Dim tagStringAccumulator As String
  180.     Dim thisString As String
  181.     Dim subString As String
  182.     Dim theKey As String
  183.     Dim substringToAdd As String
  184.  
  185.     tagStringAccumulator = ""
  186.  
  187.     If theTagString <> "" Then
  188.         thisString = theTagString
  189.         foundIt = False
  190.         Do
  191.             subString = StrTok$(thisString, ";")
  192.             thisString = ""              'for subsequent strtok calls
  193.             If subString <> "" Then
  194.                 If Not foundIt Then
  195.                     theKey = ExtractKey$(subString)
  196.                     If theKey <> key Then
  197.                         substringToAdd = subString
  198.                         GoSub AddSubstring
  199.                     Else    'this deletes if new contents = ""
  200.                         foundIt = True
  201.                         If contents <> "" Then
  202.                             substringToAdd = key + "=" + contents
  203.                             GoSub AddSubstring
  204.                         End If
  205.                     End If
  206.                 Else
  207.                     substringToAdd = subString
  208.                     GoSub AddSubstring
  209.                 End If
  210.             End If
  211.         Loop Until subString = ""
  212.  
  213.         '   If we didn't find the key, we need to add the
  214.         '   substring as a new one (providing there's content).
  215.  
  216.         If Not foundIt Then
  217.             If contents <> "" Then
  218.                 substringToAdd = key + "=" + contents
  219.                 GoSub AddSubstring
  220.             End If
  221.         End If
  222.  
  223.     Else                                         'no current contents in tag string
  224.         If contents <> "" Then                   'if user supplied contents,
  225.             substringToAdd = key + "=" + contents
  226.             GoSub AddSubstring
  227.         End If
  228.     End If
  229.  
  230.     '   Return the resulting tag string.
  231.  
  232.     theTagString = tagStringAccumulator
  233.     Exit Sub
  234.  
  235.  
  236. '   Add a substring to the end of the tag string accumulator.
  237.  
  238. AddSubstring:
  239.     If tagStringAccumulator <> "" Then
  240.         tagStringAccumulator = tagStringAccumulator + ";"
  241.     End If
  242.     tagStringAccumulator = tagStringAccumulator + substringToAdd
  243.     Return
  244.  
  245. End Sub
  246.  
  247.