![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
After truncating the data values, the program appends the data to the output file. It repeats the process of removing a data name and value from the environment string and adding the data to the file until there is no more information to read.
Option Explicit
Const FILE_NAME = WebData.TXT
Const MAX_FIELD = 8
Const MAX_VALUE = 100
Sub Main()
Dim txt As String
Dim fnum As Integer
Dim field_name As String
Dim field_value As String
We cannot recover if something goes wrong.
On Error GoTo Done
Read the environment variable.
txt = Environ(QUERY_STRING)
Open the file.
fnum = FreeFile
Open FILE_NAME For Append As #fnum
On Error GoTo CloseFile
Print #fnum, Received: & Format$(Now)
Write the segments to the file.
Do While txt <>
Get the next field name.
field_name = RemoveSegment(txt)
field_value = RemoveSegment(txt)
Make sure they are not too long.
field_name = Left$(field_name, MAX_FIELD)
field_value = Left$(field_value, MAX_VALUE)
Save the data.
field_name = & field_name & :
Print #fnum, Format$(field_name, _
!@@@@@@@@@@@@@@) & field_value
Loop
CloseFile:
Close #fnum
Done:
End Sub
Packing and Unpacking DataThe InetXfer ActiveX control uses subroutine AppendSegment to fill a buffer string with data before sending it across the network. Before it adds new data to the buffer, AppendSegment translates certain special characters to protect them during transit. When the server program receives the data, it uses function RemoveSegment to pull data segments out of the data buffer. RemoveSegment reverses the translations performed by AppendSegment to restore the original data. These routines and their helper subroutines are described in the following sections. AppendSegmentTo prevent spaces from truncating the data, AppendSegment converts them into plus signs. If the data already contains plus signs, this could be confusing. The receiving program would not be able to tell which plus signs were in the original data and which represented spaces. To prevent this confusion AppendSegment converts plus signs into the string \+. For example, the string Conversion + Confusion becomes Conversion+\++Confusion. Similarly, AppendSegment converts carriage returns into asterisks and asterisks into the string \*. It performs these conversions using subroutine ProtectChar. This routine converts one character (for example, space) into another (plus) while placing a back slash in front of the second character when it occurs in the original data (\+). This routine is described shortly. After protecting spaces and carriage returns, AppendSegment uses the ConvertToken subroutine to change semi-colons in the data into the string \;. ConvertToken, also described shortly, makes simple replacements of one string for another. Finally, AppendSegment adds the converted data to the buffer string, separating it with a semi-colon. Replacing semi-colons with \; in the original data allows the receiving program to tell which semi-colons delimit data segments and which are part of the data.
Public Sub AppendSegment(txt As String, ByVal new_txt As String)
Protect carriage returns.
ProtectChar new_txt, vbCrLf, *
Protect spaces.
ProtectChar new_txt, , +
Protect semi-colons.
ConvertToken new_txt, ;, \;
Concatenate the result.
txt = txt & new_txt & ;
End Sub
ProtectChar Subroutine ProtectChar converts one character (for example, space) into another (plus) while placing a back slash in front of the second character when it occurs in the original data (\+). It does this using the ConvertToken subroutine described in the next section. First it adds the back slashes. For example, it converts + into \+. Then ProtectChar replaces the first character (space) with the second (plus).
Public Sub ProtectChar(txt As String, fr_ch As String, to_ch As String)
Convert to_ch into \to_ch.
ConvertToken txt, to_ch, \ & to_ch
Convert fr_ch into to_ch.
ConvertToken txt, fr_ch, to_ch
End Sub
ConvertToken ConvertToken replaces one substring with another inside a larger string. The routine loops through the data string looking for occurrences of the first substring. When it finds one, it copies the text before the substring to the end of the output string. It adds the second substring to replace the first, skips over the first substring in the original data, and continues looking.
Public Sub ConvertToken(txt As String, from_str As String, _
to_str As String)
Dim new_txt As String
Dim old_pos As Integer
Dim new_pos As Integer
Dim token As String
Dim txt_len As Integer
Dim from_len As Integer
txt_len = Len(txt)
from_len = Len(from_str)
new_txt =
old_pos = 1
Do
Find the next from_str.
new_pos = InStr(old_pos, txt, from_str)
If new_pos < 1 Then new_pos = txt_len + 1
Add this piece to the new string.
token = Mid$(txt, old_pos, new_pos - old_pos)
new_txt = new_txt & token
If were beyond the end, were done.
If new_pos > txt_len Then Exit Do
Add a space.
new_txt = new_txt & to_str
Move beyond the from_str.
old_pos = new_pos + from_len
Loop
txt = new_txt
End Sub
RemoveSegmentAfter the data arrives at the server program, that program uses RemoveSegment to pull data segments out of the data buffer. RemoveSegment returns the next piece of data and shortens the data buffer so it no longer contains the removed segment. RemoveSegment first searches the data for a semi-colon that is not preceded by a back slash. This marks the end of the next segment within the data. It creates a string that contains the segment and removes the segment from the data buffer.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|