
/*
**
**  $VER: prefs.c 1.6 (28.8.98)
**  mpegsystem.datatype 1.6
**
**  Preferences
**
**  Written 1997/1998 by Roland 'Gizzy' Mainz
**  Original example source from David N. Junod
**
*/

/* main includes */
#include "classbase.h"
#include "classdata.h"


/****** mpegsystem.datatype/preferences *****************************************
*
*   NAME
*       preferences
*
*   DESCRIPTION
*       The "ENV:Classes/DataTypes/mpegsystem.prefs" file contains global
*       settings for the datatype.
*       The preferences file is an ASCII file containing one line where the
*       preferences can be set.
*       It can be superset by a local variable with the same name.
*
*       Each line can contain settings, special settings for some projects
*       can be set using the MATCHPROJECT option.
*       Lines beginning with a '#' or ';' chars are treated as comments.
*       Lines are limitted to 256 chars.
*
*   TEMPLATE
*       MATCHPROJECT/K,VERBOSE/S,NOVERBOSE/S,VERBOSESYNTAX/S,
*       NOVERBOSESYNTAX/S,VERBOSEDEBUG=DEBUG/S,NOVERBOSEDEBUG=NODEBUG/S,
*       IGNOREERRORS/S,NOIGNOREERRORS/S,AUDIO/S,NOAUDIO/S
*
*       MATCHPROJECT -- The settings in this line belongs only to this
*           project(s), e.g. if the case-insensitive pattern does not match,
*           this line is ignored.
*           The maximum length of the pattern is 128 chars.
*           Defaults to #?, which matches any project.
*
*       VERBOSE -- Print information about the animation. Currently
*           the frame numbers and the used compression are printed, after all
*           number of scanned/loaded frames, set FPS rate, dimensions (width/
*           height/depth), sample information etc.
*
*       NOVERBOSE -- Turns VERBOSE mode off.
*           Be _carefull_ with this options, this turns really ALL error 
*           messages OFF !
*
*       VERBOSESYNTAX -- Print information about syntax errors
*
*       NOVERBOSESYNTAX -- Turns the VERBOSESYNTAX option off.
*
*       DEBUG
*       VERBOSEDEBUG -- Turns debugging mode on to get internal debugging
*           info without recompiling the whole source.
*
*       NODEBUG
*       NOVERBOSEDEBUG -- Turns the DEBUG option off.
*
*       IGNOREERRORS -- Ignores errors if system stream parsing fails;
*           the video+audio parts attempts to load the data until the
*           last scanned packet occurs.
*
*       NOIGNOREERRORS -- Turns the IGNOREERRORS option off.
*
*       AUDIO -- Decode audio stream (default on)
*
*       NOAUDIO -- Do not decode audio
*
*   NOTE
*       - An invalid prefs file line will be ignored and forces the VERBOSE
*         output.
*
*   BUGS
*       - Low memory may cause that the prefs file won't be parsed.
*
*       - Lines are limitted to 256 chars
*
*       - An invalid prefs file line will be ignored.
*
*       - The sample path length is limitted to 200 chars. A larger
*         value may crash the machine if an error occurs.
*
******************************************************************************
*
*/


static
STRPTR GetPrefsVar( struct ClassBase *cb, STRPTR name )
{
          STRPTR buff;
    const ULONG  buffsize = 16UL;

    if( buff = (STRPTR)AllocVec( (buffsize + 2UL), (MEMF_PUBLIC | MEMF_CLEAR) ) )
    {
      if( GetVar( name, buff, buffsize, GVF_BINARY_VAR ) != (-1L) )
      {
        ULONG varsize = IoErr();

        varsize += 2UL;

        if( varsize > buffsize )
        {
          FreeVec( buff );

          if( buff = (STRPTR)AllocVec( (varsize + 2UL), (MEMF_PUBLIC | MEMF_CLEAR) ) )
          {
            if( GetVar( name, buff, varsize, GVF_BINARY_VAR ) != (-1L) )
            {
              return( buff );
            }
          }
        }
        else
        {
          return( buff );
        }
      }

      FreeVec( buff );
    }

    return( NULL );
}


static
BOOL matchstr( struct ClassBase *cb, STRPTR pat, STRPTR s )
{
    TEXT buff[ 512 ];

    if( pat && s )
    {
      if( ParsePatternNoCase( pat, buff, (sizeof( buff ) - 1) ) != (-1L) )
      {
        if( MatchPatternNoCase( buff, s ) )
        {
          return( TRUE );
        }
      }
    }

    return( FALSE );
}


void ReadENVPrefs( struct ClassBase *cb, struct MPEGSystemInstData *msid )
{
    struct RDArgs envvarrda =
    {
      NULL,
      256L,
      0L,
      0L,
      NULL,
      0L,
      NULL,
      RDAF_NOPROMPT
    };

    struct
    {
      STRPTR  matchproject;
      long   *verbose;
      long   *noverbose;
      long   *dosyntax;
      long   *nodosyntax;
      long   *dodebug;
      long   *nododebug;
      long   *ignoreerrors;
      long   *noignoreerrors;
      long   *audio;
      long   *noaudio;
    } mpegsystemargs;

    TEXT   varbuff[ 258 ];
    STRPTR var;

    if( var = GetPrefsVar( cb, "Classes/DataTypes/mpegsystem.prefs" ) )
    {
      STRPTR prefsline      = var,
             nextprefsline;
      ULONG  linecount      = 1UL;

      /* Be sure that "var" contains at least one break-char */
      strcat( var, "\n" );

      while( nextprefsline = strpbrk( prefsline, "\n" ) )
      {
        stccpy( varbuff, prefsline, (int)MIN( (sizeof( varbuff ) - 2UL), (((ULONG)(nextprefsline - prefsline)) + 1UL) ) );

        /* be sure that this line isn't a comment line or an empty line */
        if( (varbuff[ 0 ] != '#') && (varbuff[ 0 ] != ';') && (varbuff[ 0 ] != '\n') && (strlen( varbuff ) > 2UL) )
        {
          /* Prepare ReadArgs processing */
          strcat( varbuff, "\n" );                                       /* Add NEWLINE-char            */
          envvarrda . RDA_Source . CS_Buffer = varbuff;                  /* Buffer                      */
          envvarrda . RDA_Source . CS_Length = strlen( varbuff ) + 1UL;  /* Set up input buffer length  */
          envvarrda . RDA_Source . CS_CurChr = 0L;
          envvarrda . RDA_Buffer = NULL;
          envvarrda . RDA_BufSiz = 0L;
          memset( (void *)(&mpegsystemargs), 0, sizeof( mpegsystemargs ) );          /* Clear result array          */

          if( ReadArgs( "MATCHPROJECT/K,"
                        "VERBOSE/S,"
                        "NOVERBOSE/S,"
                        "VERBOSESYNTAX/S,"
                        "NOVERBOSESYNTAX/S,"
                        "VERBOSEDEBUG=DEBUG/S,"
                        "NOVERBOSEDEBUG=NODEBUG/S,"
                        "IGNOREERRORS/S,"
                        "NOIGNOREERRORS/S,"
                        "AUDIO/S,"
                        "NOAUDIO/S", (LONG *)(&mpegsystemargs), (&envvarrda) ) )
          {
            BOOL noignore = TRUE;

            if( (mpegsystemargs . matchproject) && (msid -> msid_ProjectName) )
            {
              noignore = matchstr( cb, (mpegsystemargs . matchproject), (msid -> msid_ProjectName) );
            }

            if( noignore )
            {
              if( mpegsystemargs . verbose )
              {
                OpenLogfile( cb, msid );
              }

              if( mpegsystemargs . noverbose )
              {
                if( (msid -> msid_VerboseOutput) && ((msid -> msid_VerboseOutput) != -1L) )
                {
                  Close( (msid -> msid_VerboseOutput) );
                }

                msid -> msid_VerboseOutput = -1L;
              }

              if( mpegsystemargs . dosyntax )
              {
                msid -> msid_DoSyntax = TRUE;
              }

              if( mpegsystemargs . nodosyntax )
              {
                msid -> msid_DoSyntax = FALSE;
              }

              if( mpegsystemargs . dodebug )
              {
                msid -> msid_DoDebug = TRUE;
              }

              if( mpegsystemargs . nododebug )
              {
                msid -> msid_DoDebug = FALSE;
              }

              if( mpegsystemargs . ignoreerrors )
              {
                msid -> msid_IgnoreErrors = TRUE;
              }

              if( mpegsystemargs . noignoreerrors )
              {
                msid -> msid_IgnoreErrors = FALSE;
              }

              if( mpegsystemargs . audio )
              {
                msid -> msid_NoAudio = FALSE;
              }

              if( mpegsystemargs . noaudio )
              {
                msid -> msid_NoAudio = TRUE;
              }
            }
            else
            {
              verbose_printf( cb, msid, "prefs line %lu ignored\n", linecount );
            }

            FreeArgs( (&envvarrda) );
          }
          else
          {
            LONG ioerr = IoErr();
            TEXT errbuff[ 256 ];

            Fault( ioerr, "Classes/DataTypes/mpegsystem.prefs", errbuff, (LONG)sizeof( errbuff ) );

            error_printf( cb, msid, "preferences \"%s\" line %lu\n", errbuff, linecount );
          }
        }

        prefsline = ++nextprefsline;
        linecount++;
      }

      FreeVec( var );
    }
}



