/*
**  $VER: CountLines 1.0 (29 Mar 1996)  **
**
**        © 1996 Timo C. Nentwig
**          all rights reserved !
**
**        tcn@techbase.in-berlin.de
**
** ======================================
**
**  Language:
**  ¯¯¯¯¯¯¯¯
**
**    Program is compiled by SAS/C
**
**
**  Purpose:
**  ¯¯¯¯¯¯¯
**
**    Returns the number of lines in
**    specified file.
**
**    Scans the given file(s) and returns number of
**        · lines
**        · blank lines of lines
**        · bytes of file
**
**    Pattern matching is supported.
**    All arguments are worked up.
**
**
**  Requirements:
**  ¯¯¯¯¯¯¯¯¯¯¯¯
**
**    · AOS 2+
**
**
**  Solve:
**  ¯¯¯¯¯
**
**
**
**  Bugs:
**  ¯¯¯¯
**
**
**
**  ToDo:
**  ¯¯¯¯
**
**
**  Notes:
**  ¯¯¯¯¯
**
**
**
** ======================================
**
**  History:
**  ¯¯¯¯¯¯¯
**
**  29 Mar 1996 - 1.0 : initial release
**
*/

/// include

#include <exec/types.h>
#include <exec/memory.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <strings.h>
#include <stdio.h>

///
/// protos

BOOL ScanFile (STRPTR path, LONG *lines, LONG *blanks, LONG *bytes);

///
/// main

VOID
main (UWORD argc, STRPTR argv[])
{

    if (argc == 1 || *argv[1] == '?')
    {

        printf ("USAGE   : %s <path/file> or <path/pattern>\n", argv[0]);
        printf ("EXAMPLES:\n");
        printf ("\t  %s docs:#?.doc lists:stats/#?.lst <...>\n", argv[0]);
        printf ("\t  %s text:ascii_file docs:*.doc <...>\n",     argv[0]);
        printf ("\t  %s text:ascii_file <...>\n",                argv[0]);

    }
    else
    {

        #define    BUFFSIZE    240

        if (NULL != (DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 37)))
        {

            struct    AnchorPath   *ap;
            LONG      err;


            if (ap = AllocVec (sizeof (struct AnchorPath) + BUFFSIZE, MEMF_CLEAR))
            {

                UWORD    cnt;
                LONG     number    =  1;
                LONG     g_lines   =  0;
                LONG     g_blanks  =  0;
                LONG     g_bytes   =  0;


                printf ("[32mCouting ...[0m\n\n");

                printf ("                             cnt  |  lines   |  blanks  |  bytes\n");
                printf ("                             -----+----------+----------+-----------\n");

                for (cnt = 1; cnt < argc; cnt++)
                {

                    ap -> ap_Strlen = BUFFSIZE;

                    for (err = MatchFirst (argv[cnt], ap); err == 0; err = MatchNext (ap), number++)
                    {

                        LONG    l_lines   =  0;
                        LONG    l_blanks  =  0;
                        LONG    l_bytes   =  0;


                        if (ScanFile (ap -> ap_Buf, &l_lines, &l_blanks, &l_bytes) == TRUE)
                        {

                            g_lines   +=  l_lines;
                            g_blanks  +=  l_blanks;
                            g_bytes   +=  l_bytes;

                            printf ("[32m%25s:[0m ", FilePart (ap -> ap_Buf));
                            printf (" %4ld  |  [33m%6ld[0m  |  [45m%6ld[0m  |  [46m%8ld[0m\n", number, l_lines, l_blanks, l_bytes);

                        }
                        else
                        {

                            printf ("Couldn't access file \"%s\"\n", ap -> ap_Buf);

                        }

                        CloseLibrary ((struct Library *) DOSBase);

                    }

                }
                

                if (number > 2)
                {

                    printf ("==================================+==========+==========+==========\n");
                    printf ("       [1m[32mComplete Statistic:[0m           [1m[33m%6ld[0m  |  [1m[45m%6ld[0m  |  [1m[46m%8ld[0m\n", g_lines, g_blanks, g_bytes);

                }

                MatchEnd (ap);
                FreeVec  (ap);

            }

        }

    }

}

///
/// ScanFile

BOOL
ScanFile (STRPTR path, LONG *lines, LONG *blanks, LONG *bytes)
{

             BPTR    file;
    REGISTER LONG    i;
    REGISTER UBYTE   line[1000];
    REGISTER LONG    c;


    if ((file = Open (path, MODE_OLDFILE)) != NULL)
    {

        for (i = 0; (c = FGetC (file)) != EOF; i++)
        {

            *bytes += 1;

            if (c == '\n')
            {
                line[i] = '\0';
                i = -1;
                *lines += 1;

                if (strlen (line) < 1)
                   *blanks += 1;

            }
            else
            {

               if (i >= 999)
                   i = -1;

               line[i] = c;

            }

        }

        Close (file);
        return (TRUE);

    }
    else
    {

        return (FALSE);

    }

}

///

