;Text column into HTML table.                v1.00

;***********************************************************************
;* Written by:  Symantec ASPECT Assist                                       *
;*                                                                           *                                                                             
;* This script file will give the user the option of either capturing data   *
;* in half hour increments, or in 24 hour increments. The Capture file will  *
;* close after a predefined period of inactivity on the port.                *
;*                                                                           *
;* This ASPECT script is intended only as a sample of ASPECT programming.    *
;* Symantec 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.       *
;*                                                                           *
;* Technical Support is not provided on this script.                         *                                                                          *
;*****************************************************************************



;***********************************************************************
;*   GLOBAL VARIABLES                                                  *
;***********************************************************************

string StatInfo                       ;Text data for status dialog box.
string HTMfile                        ;Output HTML file.
string Ctrlfile                       ;Data list derived from original 
                                      ;   file.
string OneColFile                     ;Source file.
long Counter                          ;Number of entries in source file.



;***********************************************************************
;*                                                                     *
;* MAIN                                                                *
;* The Main procedure calls PrepareFiles to open the source and        *
;* destination files needed by this script.  It throws up a status box *
;* for the user by calling the ShowStatus routine, then reads the      *
;* source file with ReadSourceFile and writes the destination with     *
;* WriteHTMLFile.  Finally, CleanUp is called to exit the script.      *
;*                                                                     *
;* Calls: PrepareFiles, ShowStatus, ReadSourceFile, WriteHTMLFile,     *
;*        CleanUp                                                      *
;* Modifies globals: None                                              *
;*                                                                     *
;***********************************************************************

proc Main

   OneColFile=$NULLSTR                ;Initialize string variable.
   PrepareFiles()                     ;Open all necessary files.
   ShowStatus()                       ;Display the status dialog box.
   ReadSourceFile()                   ;Get data from single-column file.
   WriteHTMLFile()                    ;Output formatted HTML document.
   CleanUp(1)                         ;Close all files and exit script.
     
endproc



;***********************************************************************
;*                                                                     *
;* PREPAREFILES                                                        *
;* The procedure PrepareFiles opens all files necessary for the        *
;* conversion.                                                         *
;*                                                                     *
;* Calls: None                                                         *
;* Called by: Main                                                     *
;* Modifies globals: HTMFile, CtrlFile                                 *
;*                                                                     *
;***********************************************************************

proc PrepareFiles

   string Message                     ;Text data for dialog boxes.
   integer Choice                     ;User input from dialog boxes.

   fetch aspect path CtrlFile         ;Get path for control file.
   chdir CtrlFile
   
   sdlgfopen "Select file containing list to be sorted:" "*.txt" \
SINGLE OneColFile                     ;Get the source path.
   if FAILURE
      CleanUp(0)
   endif
   
   fopen 0 OneColFile READWRITE       ;Attempt to open the source file, 
   while FAILURE                      ;error catching if failure.
      strfmt Message "Unable to open file:  `"%s`"`r`nDo you wish to \
try again?" OneColFile
      sdlgmsgbox "File Error" Message STOP YESNO Choice 1 BEEP;
      if Choice == 7
         fopen 0 OneColFile READWRITE ;Get the source path. 
      else
         CleanUp(0)
      endif
   endwhile 
          
          
   chdir CtrlFile
   strcat CtrlFile "`\CONTROL"
         
   fopen 1 CtrlFile CREATE            ;Attempt to open the control 
   while FAILURE;                     ;file, error catching if failure.
      strfmt Message "Unable to open file:  `"%s`"`r`nDo you wish to \
try again?" CtrlFile
      sdlgmsgbox "File Error" Message STOP YESNO Choice 1 BEEP
      if Choice == 7
         fopen 0 CtrlFile READWRITE   ;Get the source path. 
      else
         CleanUp(0)
      endif
   endwhile


   sdlgfopen "Select destination file:" "*.htm;*.html" SINGLE HTMFile 
                                      ;Get dest path.
   if FAILURE
      CleanUp(0)
   endif
   
   if isfile HTMFile
      sdlgmsgbox "File Creation Error" "This file already exists; do \
you wish to overwrite?" STOP YESNO Choice 2 BEEP
      if Choice == 7
         CleanUp(0)
      endif
   endif
      
   fopen 2 HTMfile CREATE             ;Attempt to open the dest file,
   while FAILURE                      ;error catching if failure.
      strfmt Message "Unable to open file:`r`n%s`r`nWould you like to \
try again?" HTMfile
      sdlgmsgbox "File Error" Message STOP YESNO Choice 1 BEEP
      if Choice == 6
         fopen 0 OneColFile READWRITE
      else
         CleanUp(0)
      endif
   endwhile
   
endproc



;***********************************************************************
;*                                                                     *
;* READSOURCEFILE                                                      *
;* The procedure ReadSourceFile reads the data from the source file    *
;* and writes a new file with the data formatted preparatory to being  *
;* written into the HTML file.                                         *
;*                                                                     *
;* Calls:  None                                                        *
;* Called by: Main                                                     *
;* Modifies globals: None                                              *
;*                                                                     *
;***********************************************************************

proc ReadSourceFile

   integer True                       ;Loop exit value.
   integer AddLoop                    ;Loop for adding characters to 
                                      ;line.
   integer CharCount                  ;Number of valid characters in 
                                      ;string.
   integer LineLength                 ;Length of data to be written.
   string ReadString                  ;Data read from source file.
   string WriteString                 ;Data written to destination 
                                      ;file.

   Counter = 0                        ;Initialize loop counter.
   True = 1                           ;Setup loop exit.
   rewind 0                           ;File pointer to start of file.
   while True                         ;Loop until forced to exit.
      if not feof 0                   ;While source file is not 
                                      ;exhausted,
         fgets 0 ReadString           ;Read the first element of the 
                                      ;list.
         strreplace ReadString "`r" ""  
                                      ;Get rid of CR, LF, and TAB 
                                      ;elements.
         strreplace ReadString "`n" ""
         strreplace ReadString "	" ""
         strlen ReadString CharCount  ;Get the length of the resulting 
                                      ;string.
         if CharCount > 0
            strfmt WriteString "%i``%s" CharCount ReadString
                                      ;Format the control string entry.
            strlen WriteString LineLength
                                      ;Get the length of the formatted 
                                      ;entry.
            if LineLength > 98        ;Make line 100 chars long with 
                                      ;CR/LF.
               strcpy WriteString WriteString 98
            elseif LineLength < 98
               for AddLoop = 1 upto (98 - LineLength)
                  strcat WriteString " "
               endfor
            endif
            strcat WriteString "`r`n" ;Add CR/LF pair to end of each 
                                      ;line.
            fputs 1 WriteString       ;Insert entry into control file.
            Counter += 1              ;Take a count of entries added.
            strfmt StatInfo "Reading Source File:  %i lines" Counter
            dlgupdate 0 1             ;Update the status message.
         endif
      else
         True = 0
      endif
   endwhile
   
   fclose 0                           ;Close the source file.
   
endproc
   
   
   
;***********************************************************************
;*                                                                     *
;* WRITEHTMLFILE                                                       *
;* The procedure WriteHTMLFile uses the formatted text in the CtrlFile *
;* to create a two-column table in an HTML document.                   *
;*                                                                     *
;* Calls:  CleanUp                                                     *
;* Called by: Main                                                     *
;* Modifies globals: None                                              *
;*                                                                     *
;***********************************************************************

proc WriteHTMLFile
   
   integer True                       ;Key for exiting loop.
   integer Index                      ;Number of characters as an 
                                      ;integer.
   integer Column2                    ;Length of data in second column.
   integer Status                     ;Amount of source file copied to 
   				      ;HTML file.
   long Loop                          ;Loop counter.
   long ListCount                     ;Number of table rows to create.
   long FirstStop                     ;First column entry from control 
                                      ;file.
   long SecondStop                    ;Second column entry from control
                                      ;file.
   string IndexString                 ;Character count as string.
   string Row                         ;Data to write to HTML file.
   string FirstItem                   ;First column entry in two-column
                                      ;table row.
   string FirstEntry                  ;Properly formatted version of 
                                      ;FirstItem.
   string SecondItem                  ;Second column entry in two-column
                                      ;row.
   string SecondEntry                 ;Properly formatted verson of 
                                      ;SecondItem.
   
   fputs 2 "<!DOCTYPE HTML PUBLIC `"-//W3C//DTD HTML 3.2//EN`">`r`n"
                                      ;Write the HTML headers into dest 
                                      ;file.
   fputs 2 "<HEAD><TITLE></TITLE></HEAD>`r`n"
   fputs 2 "<BODY BACKGROUND=`"`">`r`n"
   fputs 2 "<TABLE WIDTH=`"100%`">`r`n"
   
   ListCount = Counter / 2            ;Working with two columns, divide
                                      ;total count by two and add one if
   if (Counter % 2) == 1              ;there is a remainder
      ListCount += 1                  ;(first column will be longer.)
   endif 
   True = 1
   if not feof 1                      ;If the control file is not empty,
      for Loop = 0 upto (ListCount - 1)
                                      ;Loop through the available items.
         Column2 = 1                  ;Initialize presence of second 
                                      ;column.
         FirstStop = (Loop * 100)     ;Calculate start of first column 
                                      ;entry.
         fseek 1 FirstStop 0          ;Go to and read the first column 
                                      ;entry.
         fgets 1 FirstItem
         strtok IndexString FirstItem "``"
                                      ;Break string around the back tic.
         atoi IndexString Index       ;First entry is number of 
                                      ;characters.
         substr FirstEntry FirstItem 1 Index
                                      ;Pull out the text from the line.
         SecondStop = ((Loop * 100) + (ListCount * 100))
                                      ;Go to and read the second column 
                                      ;entry.
         fseek 1 SecondStop 0
         fgets 1 SecondItem
         strtok IndexString SecondItem "``"
                                      ;Break entry around the back tic.
         atoi IndexString Index       ;Get the length of the second 
                                      ;entry.
         substr SecondEntry SecondItem 1 Index
                                      ;Pull out text of second entry.
         strlen SecondEntry Column2;  ;Get the length of second column 
                                      ;text.
                                      
         if ! Column2                 ;If there is no text in second 
                                      ;column,
            strfmt Row "<TR><TD>%s</TD></TR>`r`n" FirstEntry;
                                      ;Add first column.
         else                         ;If both columns have text,
            strfmt Row "<TR><TD>%s</TD><TD>%s</TD></TR>`r`n" \
            FirstEntry SecondEntry    ; add data to both columns.
         endif
         fputs 2 Row                  ;Write the new entry to the dest 
                                      ;file.
         Status = ((Loop * 100) / ListCount)
                                      ;Calculate percentage of file 
                                      ;completed.
         strfmt StatInfo "Writing HTML File:  %i%%" Status
                                      ;Format and display new status 
                                      ;string.
         dlgupdate 0 1
      endfor
   else
      usermsg "No data to sort`; exiting script!" ;Exit if source empty.
      CleanUp(0)
   endif
   
   dlgdestroy 0 CANCEL                ;Get rid of the status dialog.
   fputs 2 "</BODY>"                  ;Add end tags to HTML file.
   fputs 2 "</HTML>"
   
endproc
   
   
   
;***********************************************************************
;* SHOWSTATUS                                                          *
;*                                                                     *
;*  This procedure draws the status dialog box that let's the user     *
;*  know how much progress has been made on the conversion.            *
;*                                                                     *
;* Calls:  CheckCancel                                                 *
;* Called by:  Main                                                    *
;* Modifies Globals:  None                                             *
;*                                                                     *
;***********************************************************************

proc ShowStatus

   dialogbox 0 96 31 264 66 15 "Conversion Status" 
                                      ;Draw the status dialog box.
      text 1 31 13 199 11 StatInfo left 
      pushbutton 2 111 41 40 13 "Cancel" CANCEL DEFAULT
   enddialog
   
   when dialog 0 call CheckCancel     ;Call CheckCancel on dialog 
                                      ;events.
   
endproc



;***********************************************************************
;* CHECKCANCEL                                                         *
;*                                                                     *
;*  This procedure checks dialog events to see if a Cancel event has   *
;* occurred.  If so, CleanUp is called.                                *
;*                                                                     *
;* Calls: CleanUp                                                      *
;* Called By:  ShowStatus                                              *
;* Modifies Globals:  None                                             *
;*                                                                     *
;***********************************************************************

proc CheckCancel

   integer Event                      ;Holds the event number.
   integer Choice                     ;User choice in dialog box.
   string Message                     ;Text of dialog box.
   
   dlgevent 0 Event                   ;Get the event from the event 
                                      ;queue.
   if Event == 2                      ;If the CANCEL button was pressed,
                                      ;ask for input and exit if 
                                      ;desired.
      strfmt Message "Do you wish to cancel the current operation?"
      sdlgmsgbox "Conversion Halted!" Message STOP YESNO Choice 6 BEEP
      if Choice == 7
         CleanUp(0)
      endif
   endif
   
endproc



;***********************************************************************
;* CLEANUP                                                             *
;*                                                                     *
;* This procedure closes all open files and exits the script.          *
;*                                                                     *
;* Calls: None                                                         *
;* Called By:  Main, PrepareFiles, WriteHTMLFile, CheckCancel          *
;* Modifies Globals:  None                                             *
;*                                                                     *
;***********************************************************************

proc CleanUp

param integer Completed               ;Status of script completion.

      set aspect errormsg OFF         ;Turn off non-critical errors.
      fclose 0                        ;Close all open files before 
      fclose 1                        ;exiting.
      fclose 2
      delfile Ctrlfile                ;Delete the control file.
      if Completed == 0               ;If the script failed,
         delfile HTMFile              ;Delete the destination file.
      endif
      exit
      
endproc   