;SUBJECT.INC Formats subject headers for Offline.was.

;*****************************************************************************
;*                                                                           *
;* SUBJECT.WAS                                                               *
;* Copyright (C) 1996 Datastorm Technologies, Inc.                           *
;* All rights reserved.                                                      *
;*                                                                           *
;* This script compiles a list of subject headers for a chosen article file. *
;*                                                                           *
;* This ASPECT SCRIPT is intended only as a sample of ASPECT programming.    *
;* DATASTORM makes no warranty of any kind, express or implied, including    *
;* without limitation, any warranties of merchantability and/or fitness      *
;* for a particular purpose.  Use of this program is at your own risk.       *
;*****************************************************************************

proc Subject

;*****************************************************************************
;*                                                                           *
;* SUBJECT                                                                   *
;* The Subject procedure creates a list of SUBJECT: lines for the current    *
;* article file.                                                             *
;*                                                                           *
;* Calls: CleanUp                                                            *
;* Called by:  NewsRead                                                      *
;* Modifies globals: CancelFlag, SubjectFile, ControlFile, InputFile         *
;*                                                                           *
;*****************************************************************************

param integer Extension                ;Numeric sub-folder index.
   
   long ControlLength                  ;Length of the Control file.
   long ArticleStart                   ;Holds position of articles in file.
   string ReadControl                  ;String read from current Control file.
   string Match = "Subject:"           ;Target string.
   string ControlField[4]              ;Array for STRTOK of Control lines.
   string ReadHeader                   ;String read from current Article file.
   string SubjectLine                  ;Line to write to the Subject file.
   string DateStamp                    ;Date and time of subject creation.
   string Message                      ;Text line of message dialog.
   integer ControlLoop                 ;Loop for STRTOK of Control entries.
   integer Reading                     ;Loop for reading of Control entries.
   integer Searching                   ;Loop for reading Article headers.
   integer NotSubject                  ;Result of string comparison.
   integer SubjectLength               ;Length of the Subject line.
   integer ValidControl                ;Test on Control line.
   integer StampShort                  ;Amount short of length on DateStamp.
   integer EndOfSearch                 ;FEOF test on Control file.
   integer StampLength                 ;Length of the DateStamp
   integer AddDate                     ;Loop to add length to DateStamp.
   integer Choice                      ;User choice at message dialog.
   integer LineLength                  ;Length of the Subject line.
   integer AddLength                   ;Loop counter for adding length.
   integer AddLoop                     ;Loop controller for adding length.
   
   CancelFlag = 3
   strfmt SubjectFile "%03i.SUB" Extension
                                       ;Format the name of the subject file.
   strfmt ControlFile "%03i.CTL" Extension
                                       ;Format the name of the Control file.
   strfmt DateStamp "%s %s" $DATE $TIME
                                       ;Format the date stamp.
   strlen DateStamp StampLength        ;Get the length of the date stamp.
   if StampLength < 17                 ;If the length is less than 17 bytes,
      StampShort = 17 - StampLength    ;Pad the string to make it 17 bytes.
      for AddDate = 1 upto StampShort
         strcat DateStamp " "
      endfor
   endif
   strcat DateStamp "`r`n"             ;Add CR/LF pair to end of date stamp.
                                       
   fopen FID_CONTROL ControlFile READ TEXT      ;Open the control file.
   while FAILURE                       ;If the Control file could not be opened,
                                       ; loop until the user exits or the 
                                       ; file opens.
      strfmt Message "Unable to open file:`r`n`r`n%s" ControlFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice = 2                    ;Exit on Cancel.
         CleanUp()
      endif
      fopen FID_CONTROL ControlFile READ TEXT
   endwhile
   
   fopen FID_ARTICLE InputFile READ TEXT         
                                       ;Open the Article file.
   while FAILURE                       ;If the Article file could not be opened,
                                       ; loop until the user exits or the 
                                       ; file opens.
      strfmt Message "Unable to open file:`r`n`r`n%s" ControlFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice = 2                    ;Exit on Cancel.
         CleanUp()
      endif
      fopen FID_ARTICLE InputFile READ TEXT
   endwhile
   
   fopen FID_SUBJECT SubjectFile CREATE         
                                       ;Open the new Subject file.
   while FAILURE                       ;If the Article file could not be opened,
                                       ; loop until the user exits or the 
                                       ; file opens.
      strfmt Message "Unable to open file:`r`n`r`n%s" ControlFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice = 2                    ;Exit on Cancel.
         CleanUp()
      endif
      fopen FID_SUBJECT SubjectFile CREATE
   endwhile
   
   flength FID_CONTROL ControlLength   ;Get the length of the Control file.
   if ControlLength == 0               ;If the Control file is empty,
      strfmt SubjectLine "%s No New Articles" DateStamp
                                       ;Create an "empty" subject file.
      fputs FID_SUBJECT SubjectLine
   else                                ;If the control file is NOT empty,
      fputs FID_SUBJECT DateStamp      ;Insert the date stamp.
      rewind FID_CONTROL               ;Go to the start of the Control file.
      Reading = 1                      ;Initialize search loop.
      while Reading                    ;While the Control file is not eof,
         fgets FID_CONTROL ReadControl ;Get the next Control string.
         strlen ReadControl ValidControl
                                       ;Get the length of the control string.
         if ValidControl == 0          ;If this is not a valid string,
            Reading = 0                ;Exit this loop.
         else
            for ControlLoop = 0 upto 3 ;Initialize the STRTOK loop.
               strtok ControlField[ControlLoop] ReadControl ""
                                       ;Parse Control string into tokens.
            endfor
            atol ControlField[1] ArticleStart
                                       ;Convert file-offset into long format.
            fseek FID_ARTICLE ArticleStart 0    
                                       ;Move to file-offset in Article file.
            for Searching = 0 upto 20
               fgets FID_ARTICLE ReadHeader      
                                       ;Get the next line from the Article file.
               strnicmp ReadHeader Match 8 NotSubject
                                       ;Check for Subject: line.
               if NotSubject == 0      ;If this is the Subject line,
                  Searching = 30       ;Exit search loop.
               endif
            endfor
            if Searching == 21
               strfmt SubjectLine "%s	No subject line found for this \
article" ControlField[0]
            else
               strlen ReadHeader SubjectLength
                                       ;Get the length of the subject line.
               subjectLength -= 9      ;Subtract <Subject: > from length.
               substr ReadHeader ReadHeader 9 SubjectLength
                                       ;Subtract <Subject: > from string.
               strfmt SubjectLine "%s	%s" ControlField[0] ReadHeader
                                       ;Format the Subject file entry.
            endif
            strlen SubjectLine LineLength
                                       ;Get the length of the subject line.
            if LineLength < 98      ;If the line is not long enough,
               AddLength = 98 - LineLength
                                       ;See how many bytes short the line is.
               for AddLoop = 1 upto AddLength
                                       ;Add the required amount of whitespace.
                  strcat SubjectLine " "
               endfor
            elseif LineLength > 98
                                       ;If the line is too long,
               substr SubjectLine SubjectLine 0 98
                                       ;Cut it off at 98 bytes.
            endif
            strcat SubjectLine "`r`n"
                                       ;Add a CR/LF pair to the line.
            fputs FID_SUBJECT SubjectLine 
                                       ;Insert the formatted entry.
            feof FID_CONTROL EndOfSearch        
                                       ;Test for end of the Control file.
            if EndOfSearch             ;If Control file is eof,
               Reading = 0             ;Exit the Read loop.
            endif
         endif
      endwhile
   endif
   fclose FID_SUBJECT                  ;Close the subject file.
   fclose FID_ARTICLE                  ;Close the article file.
   fclose FID_CONTROL                  ;Close the control file.
endproc
