home *** CD-ROM | disk | FTP | other *** search
/ Datatid 1999 #6 / Datatid_1999-06.iso / internet / Tango352Promo / P.SQL / PTKPKG.1 / UNIVDEMO.SBL < prev    next >
Encoding:
Text File  |  1996-06-18  |  9.7 KB  |  278 lines

  1. ' The following constants define the location of the drive and directory
  2. ' where the Inscribe tutorial was copied.  You should change these values
  3. ' if you copied the tutorial to a directory different from the one listed
  4. ' below.
  5. Const TutorialDrive$ = "C:"
  6. Const TutorialDirectory$ = "\INSCRIBE"
  7.  
  8. ' -------------------------------------------------------------------------
  9. ' The SBL Letter() procedure is called to generate a confirmation or
  10. ' rejection letter for a student who has attempted to register for a class.  
  11. ' A confirmation letter will be generated if the maximum enrollment for
  12. ' the class has not yet been reached.  Otherwise, a rejection letter will
  13. ' be generated.
  14. '
  15. ' 1) Letter is called with arguments based on the following SSQL
  16. '    prototype:
  17. '
  18. '        CREATE PROCEDURE Letter
  19. '                 (IN StudentId  INTEGER(4),
  20. '                  IN ClassName  CHAR(60),
  21. '                  IN Confirm    CHAR(1)
  22. '                  ); EXTERNAL
  23. '        
  24. '    For example, a call might appear as:
  25. '
  26. '        Letter (123456789,          -- Student ID
  27. '                "Accounting 101",   -- Class Name
  28. '                "Y")                -- Confirmation (Y = yes, N = no)
  29. '
  30. ' 2) Read a student's name and address from an address file, which is
  31. '    organized with Student ID, Student Name, Address1, Address2, and
  32. '    Address3 on each line with "%" as the separation character.  The
  33. '    Student Id is used to locate the correct information for the
  34. '    student.
  35. '
  36. ' 3) Read each line of a text file representing the template for the 
  37. '    letter.
  38. '
  39. ' 4) Replace strings in the template of the form %sn% with student name,
  40. '    %cn% with class name, %a1% with address1, etc.
  41. '
  42. ' 5) Write each line to an output file.
  43. ' -------------------------------------------------------------------------
  44.  
  45.  
  46. ' Forward Declarations
  47. Declare Function GetAddr&(StudentId&, StudentName$, Address1$, _
  48.                           Address2$, Address3$)
  49.  
  50. Declare Sub Letter(StudentId&, ClassName$, Confirm$)
  51.  
  52.  
  53. ' -------------------------------------------------------------------------
  54. ' Main is not called from SSQL.  It contains calls to Letter so Letter can
  55. ' be tested independently of SSQL, if desired.
  56. ' -------------------------------------------------------------------------
  57. Sub main
  58.  
  59.   Call Letter(123456789, "Film Making 101", "Y")
  60.   Call Letter(888888888, "Physical Chemistry 200", "N")
  61.   Call Letter(345678901, "General Studies 201", "Y")
  62.  
  63. End Sub
  64.  
  65.  
  66. ' -------------------------------------------------------------------------
  67. ' Generate a confirmation or rejection letter for a student who has
  68. ' requested enrollment in a class.  The address information is extracted
  69. ' from a student address file.
  70. ' -------------------------------------------------------------------------
  71. Sub Letter(StudentId&, ClassName$, Confirm$)
  72.  
  73.   Dim template_line$    ' A line read from the univtmpl.txt template file
  74.   Dim output_line$      ' A processed template_line ready to be written
  75.   Dim output_file$      ' The output file to which output_line is written
  76.   Dim student_name$     ' The student's name
  77.   Dim address1$         ' The first line of the student's address
  78.   Dim address2$         ' The second line of the student's address
  79.   Dim address3$         ' The third line of the student's address
  80.   Dim field$            ' A field of text in template_line
  81.   Dim field_num%        ' The number of a field in template_line
  82.   Dim separators$       ' Separators for the fields in template_line
  83.   Dim newline           ' The newline character
  84.   Dim alldone%          ' Set non-zero when we're done with univtmpl.txt
  85.  
  86.   ' Set the current drive and directory to the location of the template
  87.   ' file and the generated letters.
  88.   On Error GoTo WrapUp
  89.   ChDrive TutorialDrive
  90.   ChDir TutorialDirectory
  91.  
  92.   ' Extract the student address information from an address file
  93.   If GetAddr(StudentId, student_name, address1, address2, address3) < 0 then
  94.     MsgBox "Error: Unable to locate student id " & CStr(StudentId) & _
  95.            " in the address file"
  96.     Stop
  97.   End If
  98.  
  99.   ' Construct the output_file pathname from the student's last name
  100.   output_file = GetField$(student_name, 3, " ")
  101.   If Len(output_file)=0 then
  102.     output_file = GetField$(student_name, 2, " ")
  103.   End If
  104.   If Len(output_file) = 0 then
  105.     output_file = GetField$(student_name, 1, " ")
  106.   End If
  107.   output_file = Left$(output_file, 8) & ".txt"
  108.  
  109.   ' Perform initializations to open files and define variables used below
  110.   Open "univtmpl.txt" For Input As #1
  111.   Open output_file For Output As #2
  112.   alldone = 0
  113.   newline = Chr(10)
  114.   separators = "%" & newline
  115.  
  116.   ' Read each line of the template file and substitute the appropriate
  117.   ' values for fields of the form %sn%, %cn%, %a1%, etc.
  118.   Do Until alldone > 0
  119.  
  120.     ' Read the next template line and set alldone if it's the last line
  121.     Line Input #1, template_line
  122.     If Seek(1) > Lof(1) then
  123.       alldone = 1
  124.     End If
  125.  
  126.     ' Scan the line from the template and process pairs of fields separated
  127.     ' by the characters in 'separators'.  We perform substitution on the
  128.     ' second field of the pair, not the first field.  For example, the line
  129.     ' "Dear %cn%:" will process the fields "Dear " and "cn" on the the
  130.     ' first iteration and process ":" and Null on the second iteration.
  131.     field_num = 1
  132.     output_line = ""
  133.     Do
  134.       ' Get an odd-numbered field -- this is a text field where we don't
  135.       ' look for substitution characters.  Note: this field may be Null,
  136.       ' but we don't exit since non-Null fields may follow this field.
  137.       field = GetField$(template_line, field_num, separators)
  138.       output_line = output_line & field
  139.  
  140.       ' Get an even-numbered field -- this is a text field where we look
  141.       ' for substitution characters.  Note that if this field is Null, we
  142.       ' exit since no more fields will follow. 
  143.       field = GetField$(template_line, field_num+1, separators)
  144.       If Len(field) = 0 then
  145.         Exit Do
  146.       End If
  147.  
  148.       ' Check the 'field' value to determine which string needs to be
  149.       ' placed in 'output_line'
  150.       If StrComp(field, "sn", 1) = 0 then
  151.         ' Student name
  152.         output_line = output_line & student_name
  153.  
  154.       ElseIf StrComp(field, "a1", 1) = 0 then
  155.         ' Address Line 1
  156.         output_line = output_line & address1
  157.  
  158.       ElseIf StrComp(field, "a2", 1) = 0 then
  159.         ' Address Line 2
  160.         output_line = output_line & address2
  161.  
  162.       ElseIf StrComp(field, "a3", 1) = 0 then
  163.         ' Address Line 3
  164.         output_line = output_line & address3
  165.  
  166.       ElseIf StrComp(field, "cn", 1) = 0 then
  167.         ' Class Name
  168.         output_line = output_line & ClassName
  169.  
  170.       ElseIf StrComp(field, "da", 1) = 0 then
  171.         ' Date - Output date with the day of the week stripped off
  172.         field = Format$(Date$, "Long Date")
  173.         output_line = output_line & GetField$(field, 2, " ") & " " & _
  174.                       GetField$(field, 3, " ") & " " & _
  175.                       GetField$(field, 4, " ")
  176.  
  177.       ElseIf StrComp(field, "co", 1) = 0 then
  178.         ' Confirmation
  179.         If StrComp(Confirm, "Y", 1) = 0 then
  180.           output_line = output_line & "has been confirmed"
  181.         Else
  182.           output_line = output_line & _
  183.                         "has been denied because the class " & _ 
  184.                         "has reached its maximum size"
  185.         End If
  186.  
  187.       Else
  188.         ' Unrecognized field name so generate a string of asterisks
  189.         output_line = output_line & "********"
  190.       End If
  191.  
  192.       field_num = field_num + 2
  193.  
  194.     Loop
  195.  
  196.     Print #2, output_line
  197.     
  198.   Loop
  199.  
  200.   Close #1
  201.   Close #2
  202.  
  203. Done:
  204.   Exit Sub
  205.  
  206. WrapUp:
  207.   field = "Error " & Err & " at line " & Erl & ": " & Error$
  208.   MsgBox field
  209.   Resume Done
  210.  
  211. End Sub
  212.  
  213.  
  214. ' --------------------------------------------------------------------------
  215. ' Read an address file to get the address of the specified student.
  216. ' --------------------------------------------------------------------------
  217. Function GetAddr&(StudentId&, StudentName$, Address1$, Address2$, Address3$)
  218.  
  219.   Dim address_line$     ' A line read from the address.txt file
  220.   Dim field$            ' A field of text in address_line
  221.   Dim separators$       ' Separators for the fields in address_line
  222.   Dim newline           ' The newline character
  223.   Dim alldone%          ' Set non-zero when we're done with address.txt
  224.   Dim retcode&          ' Return code
  225.  
  226.   On Error GoTo WrapUp
  227.  
  228.   Open "address.txt" For Input As #1
  229.   GetAddr = 0
  230.   alldone = 0
  231.   newline = Chr(10)
  232.   separators = "%" & newline
  233.  
  234.   ' Read each line of the address file until the StudentId is located
  235.   Do Until alldone > 0
  236.  
  237.     ' Read the next address line
  238.     Line Input #1, address_line
  239.  
  240.     ' Check the line just read to see if the student id is present in the
  241.     ' first field of the record.
  242.     field = GetField$(address_line, 1, separators)
  243.  
  244.     If Val(field) = StudentId then
  245.       ' Found the student so extract the name and address information
  246.       ' from the remaining fields and then exit the loop.
  247.       StudentName = GetField$(address_line, 2, separators)
  248.       Address1 = GetField$(address_line, 3, separators)
  249.       Address2 = GetField$(address_line, 4, separators)
  250.       Address3 = GetField$(address_line, 5, separators)
  251.       Exit Do
  252.     End If
  253.     
  254.     ' Set alldone if it's the last line
  255.     If Seek(1) > Lof(1) then
  256.       alldone = 1
  257.     End If
  258.  
  259.   Loop
  260.  
  261.   Close #1
  262.   If alldone > 0 then
  263.     ' We scanned the entire address file and didn't find the student id.
  264.     GetAddr = -1
  265.   End If
  266.  
  267. Done:
  268.   Exit Function
  269.  
  270. WrapUp:
  271.   field = "Address file error " & Err & " at line " & Erl & ": " & Error$
  272.   MsgBox field
  273.   GetAddr = -1
  274.   Resume Done
  275.  
  276. End Function
  277.  
  278.