
// -------- Copyright 1995, C.K.Hung, Hong Kong, All Rights Reserved. ------
//
// lwRules.Doc : output description of .TXT Format    (LazyWriter 1.20)
//
// lwRules.TXT include the following macro functions :
//
//    init()       -----  initialize a output process
//    end()        -----  end a output process
//    filelist()   -----  generate file list to documentation
//    index()      -----  generate index section
//    database()   -----  generate database section
//    funclist1()  -----  generate function list I
//    funclist2()  -----  generate function list II
//    control()    -----  generate control sentence section.
//
// You can also modify these functions to change the output format or create
// new section by writting macro function and call it using :
//        # section( 'YourSectionName', [<para>,...] )
//
// LazyWriter extends 3 macro commands to output message, there are :
//
//     \\ <cMsg>    => lw_stdout( <cMsg> )  => send cMsg to output file
//     \? <cExp>    => lw_stdout( &<cExp> ) => send the value of cExp.
//     \: <cMsg>    => lw_txtout( <cMsg> )  => send cMsg as Text (no margin checking)
//
// To get more information about macro program, please refer to the norton
// guide of Casual-Kit 2.01
//


// ================ init(),End() :: initialize/end a output process ===========

\ Function Init
    set( 20 , 'PRINTER' )                          // set device to printer
    set( 24 , output + if('.'$output,'','.doc') )  // set printer to output
    devpos( margin[1]-1, 0 )                       // reset printer
\ Return

\ Function End
  \ priv ( oldscr )
    lw_stdFF()                              // FormFeed, Reset printer
    set( 24, '' )                           // close output file
    set( 20 , 'SCREEN' )                    // set device to screen
    boxdraw(8,16,16,64,'w+',' ')
    m_say( 10, 20, 'LazyWriter had finished the documentation', 'n/w' )
    m_say( 11, 20, '', 'n+/w' )
    m_say( 12, 23, ' Times   ' + str(second() - lwtime,6,1) + ' Seconds', 'n/w' )
    m_say( 13, 23, ' Page    ' + str(lwpageno,6) + ' Pages', 'n/w' )
    m_say( 14, 23, ' Output  ' + uppe(output) + if('.'$output,'','.DOC'), 'n/w' )
    done( tone(600), tone(300), tone(500), tone(300,2) )
    \if (stdmsg('  Press Esc to Main Menu, Other Key to View File  ', 'mkr' )#27)
        eval( beditfile, uppe(output) + if('.'$output,'','.DOC') )
    \endi
\ Retu


// ===================== File List :: generate file list ====================

\ Function FileList

  \priv ( fdir, fexe, fprg, fdbf, fidx, foth )

  stdmsg( ' Generate File List ......' )

  fexe := lw_dir( path , execute  )              // get file list
  fprg := lw_dir( path , source   )
  fdbf := lw_dir( path , database+', *.dbt' )
  fidx := lw_dir( path , index    )
  fdir := lw_dir( path , files, '*.bak' )        // not include *.bak
  foth := lw_dir( Path , files, ;
            execute+','+Source+','+database+','+index+', *.bak, *.dbt ' )

  \\=========================================================================
  \\                          Files  Section
  \\=========================================================================
  \\
  macrop( 'displist', { 'Execute File', fexe }, lwCommand )
  macrop( 'displist', { 'Source Code ', fprg }, lwCommand )
  macrop( 'displist', { 'Database    ', fdbf }, lwCommand )
  macrop( 'displist', { 'Index File  ', fidx }, lwCommand )
  macrop( 'displist', { 'Others      ', foth }, lwCommand )

  \? 'Total '+str(len(fdir),4)+' files in '+lw_dirsize(fdir)
  \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\Return

\func displist ( title, flst )       // display file list
  \priv ( fi, fl, fj, fs )
  \retu ( nil; ( fl := len( flst ) ) = 0 )  // if empty then return
  \? padr(title,23) + '(' + str(fl,4) + ' files  ' + lw_dirsize(flst) + ' )'
  \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  \for ( fi:=1; fi<=fl; fi:=fi+1 )
       fs := flst[fi,1]
       fj := at( '.', fs )
       fs := if( fj=0, padr(fs,12), padr( left(fs,fj-1), 8 ) + subs( fs, fj ) )
       \? padc( fs, 18 ) + tran( flst[fi,2], '99999,999 Bytes' ) ;
          + '   '+ dtoc( flst[fi,3] ) + '   ' + flst[fi,4]
  \next
  \\
\ retu


// ===================== Index :: Generate index section ==================

\Function Index

 \ priv ( info, rlen, ri )

 stdmsg( ' Generate Index Section ......' )

 info := lw_idxinfo()               // get index info.
 rlen := len(info)

 \\=========================================================================
 \\                          Index  Section
 \\=========================================================================
 \\
 \\
 \\Index          Database       Index Key
 \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 \\
 \ for ( ri:=1; ri<=rlen; ri:=ri+1 )
       \? padr( info[ri,1], 15 ) + padr( info[ri,2], 15 ) + info[ri,3]
 \ next
 \\
\ Return


// =================== Database :: Generate database section ================

\ Function Database ( lPage )

  \ priv ( info, rlen, ri, rj, rs )

  stdmsg( ' Generate Database Section ......' )

  info := lw_dbfinfo()                  // get database info.
  rlen := len(info)

  \ for ( ri := 1; ri <= rlen; ri := ri + 1 )
    \\*========================================================================
    \? '* Database: ' + padr(info[ri,1],16) + 'Size: ' + ;
       tran(info[ri,2],'@B 99,999,999 Bytes') + '   Update: ' + ;
       dtoc(info[ri,3])
  \\*========================================================================
    \\
    if( empty(info[ri,4]), lw_stdout('  * No Index Found !! '), nil )
    \ for ( rj := 1; rj <= len(info[ri,4]); rj := rj + 1 )
      \? '  * INDEX TO ' + info[ri,4,rj,1] + ' ON ' + info[ri,4,rj,2]
    \next
    \\
    \\-------------------------------------------------------------------------
    \\  No.  Fields      Type    Length    Dec    Notes
    \\
    rs := 0
    \ for ( rj := 1; rj <= len(info[ri,5]); rj := rj + 1 )
      \? str(rj,4) + '   ' + padr(info[ri,5,rj,1],12) + info[ri,5,rj,2] + ;
         str(info[ri,5,rj,3],10) + str(info[ri,5,rj,4],8)
      rs := rs + info[ri,5,rj,3]
    \ next
    \\
    \? '  Total ' + ltrim(str(len(info[ri,5]))) + ' fields,  ' + ltrim(str(rs)) + ;
       ' bytes per record,  ' + ltrim(str(info[ri,6]))+' records in database'
    \\--------------------------------------------------------------------------

    rj := if( ri=len(info), 0 , prow()+len(info[ri+1,5])+len(info[ri+1,4])+18 )
    if( empty(lPage) .and. rj < PageLines,  devpos(prow()+5,0), ;
        if( ri=len(info), nil, scrollpage() ) )
  \ next
\retu


// ======================= Function List =======================

\ Function FuncList1
  \ priv ( bSayFunc, fNo, flen, lastfile, lastprow )

  stdmsg( ' Generate File List I ...... ')
  \\=========================================================================
  \\                  Function  List I  ( Sort in File + Line )
  \\=========================================================================
  \\
  dbusearea( , , 'lw' )

  fNo := flen := lastprow := cNo :=0
  lastfile := ''
  bSayFunc := { |s| if(lastfile=lwfile.and.prow()>=lastprow, nil, ;
                { lw_stdout(''),lw_stdout('File = '+lwfile),lw_stdout(repl('~',19))}) ,;
                if(lastfile=lwfile,,cNo:=0),lastfile:=lwfile, lastprow:=prow(), ;
                s:=str(lwline,5)+':'+strtran(str(lwlength,3),' ','0')+'  ',;
                s:=s+left(lwname,14) + uppe(lwtype) + lwnotes, ;
                fno:=fno+1, cNo:=cNo+1, fLen:=flen+lwlength, ;
                lw_stdout(str(cno,3)+' '+trim(s)), dbskip()  }
  dbSetFilter( {||!(left(lwname,1)$'!@#$&+-*/')}, "!(left(lwname,1)$'!@#$%+-*/')" )
  dbgotop()
  \\ No   Pos:Len  Name          Type  Notes
  \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  blk_whil( {|| !eof()}, bSayFunc )
  \\
  dbclearfilter()
  dbclearind()
  dbgobottom()
  \? '<< Total >>    Function = ' + ltrim(str(fno)) + '    Source Lines = ' + ;
     ltrim(str(flen)) + '    File Lines = ' + ltrim(str(lwlength))
  \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  dbcloseall()
\ Return

\ Function FuncList2
  \ priv ( bSayFunc, fNo, flen )
  stdmsg( ' Generate File List II ...... ')
  \\=========================================================================
  \\               Function  List II  ( Sort in Function Name )
  \\=========================================================================
  \\
  dbusearea( , , 'lw' )
  dbcreateindex( 'lw', 'lwname', {||lwname} )

  fNo := flen :=0
  bSayFunc := { |s| s := left(lwname,14) + uppe(lwtype) + lwfile , ;
                s := s + str(lwline,5)+':'+str(lwlength,3)+'  '+lwnotes, ;
                fno := fno + 1, fLen := flen + lwlength, ;
                lw_stdout(str(fno,3)+'  '+trim(s)), dbskip()  }
  dbSetFilter( {||!(left(lwname,1)$'!@#$&+-*/')}, "!(left(lwname,1)$'!@#$%+-*/')" )
  dbgotop()
  \\ No  Name          Type   File         Pos:Len  Notes
  \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  blk_whil( {|| !eof()}, bSayFunc )
  \\
  dbclearfilter()
  dbclearind()
  dbgobottom()
  \? '<< Total >>    Function = ' + ltrim(str(fno)) + '    Source Lines = ' + ;
     ltrim(str(flen)) + '    File Lines = ' + ltrim(str(lwlength))
  \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  dbcloseall()
  ferase( 'lw.ntx' )
\ Return

// ======================= Control Sentence =======================

\ Function Control

\ priv ( rlines )

  stdmsg( ' Pickup Control Sentence of Source Code ...... ')
  \\=========================================================================
  \\                        Control  Sentence  Only
  \\=========================================================================
  \\
  dbusearea( , , 'lw' )
  dbSetFilter( {||left(lwname,1)='-'}, "left(lwname,1)='-'" )
  dbgotop()
  \ While ( !eof() )
    \\
    \\
    \\_________________________________________________________________________
    \? '[' + uppe(trim(lwtype)) + '] ' + subs(lwName,2) + ' [File] ' + lwfile + ;
       ' [Position] '+ltrim(str(lwline)) + '  [Length] ' + ltrim(str(lwlength))
    \\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    \\
    rLines := str_cut( lwText )
    aeval( rLines, { |x| lw_stdout(x) } )
    dbskip()
  \ endd
  dbcloseall()
\ Return

