public void CommandLineArgs()
{
    String[] args=
    {
        "-SingleHyphen=12",
        "--DoubleHyphen=yes",
        "--Empty",
        "--Whitespaces   =  Hello Test  ",
        "--integer =  42",
        "--double =  3.14",
        "-Home=overwritten",
        "--ALIB_TEST=passed",
    };

    Configuration cfg= new Configuration();
    cfg.SetCommandLineArgs( args );
    Variable var= new Variable();
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "SingleHyphen" )) );   UT_EQ( "12",            var.GetString()       );
                                         cfg.Load   ( var.Define( "",      "DoubleHyphen" ));     UT_EQ( true,            var.IsTrue()    );
    UT_EQ( Configuration.PrioCmdLine,    var.Priority);

    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "Empty"        )) );   UT_EQ( "",              var.GetString()       );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "Whitespaces"  )) );   UT_EQ( "Hello Test",    var.GetString()       );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "HOME"         )) );   UT_EQ( "overwritten",   var.GetString()       );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "integer"      )) );   UT_EQ( 42,              var.GetInteger()    );
    UT_EQ( 0,                            cfg.Load   ( var.Define( "",      "notexistent"  )) );   UT_EQ( 0,               var.GetInteger()    );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "integer"      )) );   UT_EQ( 42,              var.GetInteger()    );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "double"       )) );   UT_EQ( 3.14,            var.GetFloat() );
    UT_EQ( 0,                            cfg.Load   ( var.Define( "",      "notexistent"  )) );   UT_EQ( 0.0,             var.GetFloat() );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "",      "double"       )) );   UT_EQ( 3.14,            var.GetFloat() );
    UT_EQ( Configuration.PrioCmdLine,    cfg.Load   ( var.Define( "ALIB",  "test"         )) );   UT_EQ( "passed",        var.GetString()       );
}
 /** ********************************************************************************************
  * Creates an AnsiConsoleLogger.
  * @param name        (Optional) The name of the \e Logger, defaults to "ANSI_CONSOLE".
  **********************************************************************************************/
 public AnsiConsoleLogger( String name= null )
 : base( Console.Out, true, name, "ANSI_CONSOLE" )
 {
     // evaluate environment variable "ALOX_CONSOLE_HAS_LIGHT_BACKGROUND"
     Variable variable= new Variable( ALox.CONSOLE_HAS_LIGHT_BACKGROUND );
     variable.Load();
     if ( variable.Size() > 0 )
         IsBackgroundLight=  variable.IsTrue();
     else
     {
         ConsoleColor fgCol= Console.ForegroundColor;
         IsBackgroundLight=      fgCol == ConsoleColor.Black
                             ||  fgCol == ConsoleColor.DarkBlue
                             ||  fgCol == ConsoleColor.DarkCyan
                             ||  fgCol == ConsoleColor.DarkGray
                             ||  fgCol == ConsoleColor.DarkGreen
                             ||  fgCol == ConsoleColor.DarkMagenta
                             ||  fgCol == ConsoleColor.DarkRed
                             ||  fgCol == ConsoleColor.DarkYellow   ;
     }
 }
    // #############################################################################################
    // Constructor
    // #############################################################################################

        /** ****************************************************************************************
         * Creates an AnsiLogger.
         * @param textWriter     The TextWriter object to write into.
         * @param usesStdStreams Denotes whether this logger writes to the
         *                       <em>standard output streams</em>.
         * @param name           The name of the \e Logger, defaults to what is provided with
         *                       parameter \p typeName.
         * @param typeName       The type of the \e Logger, defaults to "ANSI".
         ******************************************************************************************/
        public AnsiLogger( System.IO.TextWriter textWriter, bool usesStdStreams,
                           String name= null, String typeName= "ANSI" )
            : base( name, typeName, usesStdStreams )
        {
            this.textWriter= textWriter;

            // evaluate environment variable "ALOX_CONSOLE_HAS_LIGHT_BACKGROUND"
            Variable variable= new Variable( ALox.CONSOLE_HAS_LIGHT_BACKGROUND );
            variable.Load();
            if ( variable.Size() > 0 )
                IsBackgroundLight=  variable.IsTrue();
            else
            {
                // on ANSI terminals we can only guess. Our guess is: console windows have dark background
                IsBackgroundLight= false;
            }

            // remove verbosity information and colorize the whole line
            MetaInfo.Format.SearchAndReplace( "]%V[", "][" );
            if ( IsBackgroundLight )
            {
                MsgPrefixError           = ANSI_RED;
                MsgPrefixWarning         = ANSI_BLUE;
                MsgPrefixVerbose         = ANSI_GRAY;
            }
            else
            {
                MsgPrefixError           = ANSI_LIGHT_RED;
                MsgPrefixWarning         = ANSI_LIGHT_BLUE;
                MsgPrefixVerbose         = ANSI_LIGHT_GRAY;
            }

            // set source file background to gray
            AString ansiBGGray= new AString( ESC.BG_GRAY ); ansiBGGray._( "%SF(%SL):" )._( ANSI_BG_STD_COL );
            MetaInfo.Format.SearchAndReplace( "%SF(%SL):", ansiBGGray.ToString() );
        }
Example #4
0
        public static void AddDebugLogger( Lox lox= null,
        [CallerLineNumber] int cln= 0,[CallerFilePath] String csf="",[CallerMemberName] String cmn="" )
        {
            #if ALOX_DBG_LOG

                if ( lox == null )
                    lox= LOX;

                ALIB.ASSERT_ERROR( DebugLogger == null, "Illeagal repeated invocation." );

                // add a CLR logger if this a debug session
                if( System.Diagnostics.Debugger.IsAttached )
                {
                    Variable variable= new Variable(ALox.NO_IDE_LOGGER);
                    variable.Load();
                    if( !variable.IsTrue() )
                    {
                        IDELogger= new CLRDebuggerLogger( "IDE_LOGGER" );

                        // add logger
                        lox.SetVerbosity( IDELogger  , Verbosity.Verbose, "/"                 , Configuration.PrioDefault ,cln,csf,cmn );
                        lox.SetVerbosity( IDELogger  , Verbosity.Warning, ALox.InternalDomains, Configuration.PrioDefault ,cln,csf,cmn );
                    }
                }

                // add a default console logger
                DebugLogger= Lox.CreateConsoleLogger("DEBUG_LOGGER");

                lox.SetVerbosity( DebugLogger, Verbosity.Verbose, "/"                 , Configuration.PrioDefault ,cln,csf,cmn );
                lox.SetVerbosity( DebugLogger, Verbosity.Warning, ALox.InternalDomains, Configuration.PrioDefault ,cln,csf,cmn );

                // replace the ReportWriter
                Log.AddALibReportWriter( lox );

            #endif
        }
Example #5
0
        /** ****************************************************************************************
         * This method must be called prior to using ALib, e.g. at the beginning of
         * the \c main() method of an application. It is OK, to call this method more than once, which
         * allows independent code blocks (e.g. libraries) to bootstrap %ALIB independently.
         * However, only the first invocation is effective with the exclamation that if
         * command line parameters are provided with a call, those are set.
         * Also, the very first invocation should not be interrupted by a parallel invocation of a
         * second thread. Consequently, it has to be assured that this method is invoked once on
         * the real bootstrap an app.
         *
         * In the C# version of the AWorx library, the invocation of this method is optional.
         * However, it is good practice to invoke this method in the main() method of a process
         * and provide the command line arguments. If no invocation is performed, no
         * configuration plug-ins are set.
         *
         * \note
         *   If other, custom configuration data sources should be used already with this method
         *   (in the current implementation, the only configuration variable read with this method
         *   is \c WAIT_FOR_KEY_PRESS), then such plug-in(s) have to be added to
         *   public, static field #Config prior to invoking this method.
         *
         * @param args    Parameters taken from <em>standard C#</em> method \c main()
         *                (the list of command line arguments).
         *                Defaults to null.
         ******************************************************************************************/
        public static void     Init( String[] args= null )
        {
            Config.SetCommandLineArgs( args );

            if ( isInitialized )
                return;
            isInitialized= true;


            // set the system's locale as the default for our static default number format
            NumberFormat.Global.SetFromLocale();

            // --- determine if we want to wait for a keypress upon termination ---
            {
                Variable variable= new Variable( ALIB.WAIT_FOR_KEY_PRESS );
                variable.Load();
                if ( variable.Size() > 0 )
                    WaitForKeyPressOnTermination= variable.IsTrue();
                else
                {
                    #if ALIB_VSTUDIO
                        WaitForKeyPressOnTermination=  ALIB.SysInfo_HasConsoleWindow && System.Diagnostics.Debugger.IsAttached;
                    #else
                        WaitForKeyPressOnTermination=  false;
                    #endif // WIN32
                }
            }
         }
    /** ********************************************************************************************
     * Creates a ConsoleLogger.
     * @param name  (Optional) The name of the \e Logger, defaults to "COLOR_CONSOLE"
     **********************************************************************************************/
    public ColorConsoleLogger( String name= null )
    : base( name, "COLOR_CONSOLE", true )
    {
        // get actual console foreground color
        ConsoleColor fgCol= Console.ForegroundColor;

        // evaluate environment variable "ALOX_CONSOLE_HAS_LIGHT_BACKGROUND"
        Variable variable= new Variable( ALox.CONSOLE_HAS_LIGHT_BACKGROUND );
        variable.Load();
        if ( variable.Size() > 0 )
            IsBackgroundLight=  variable.IsTrue();
        else
        {
            IsBackgroundLight=      fgCol == ConsoleColor.Black
                                ||  fgCol == ConsoleColor.DarkBlue
                                ||  fgCol == ConsoleColor.DarkCyan
                                ||  fgCol == ConsoleColor.DarkGray
                                ||  fgCol == ConsoleColor.DarkGreen
                                ||  fgCol == ConsoleColor.DarkMagenta
                                ||  fgCol == ConsoleColor.DarkRed
                                ||  fgCol == ConsoleColor.DarkYellow   ;
        }


        // remove verbosity information and colorize the whole line
        MetaInfo.Format.SearchAndReplace( " %V ", " " );
        MsgColorInfo          = fgCol;
        if ( IsBackgroundLight )
        {
            MsgColorError           = ConsoleColor.DarkRed;
            MsgColorWarning         = ConsoleColor.DarkBlue;
            MsgColorVerbose         = ConsoleColor.DarkGray;
        }
        else
        {
            MsgColorError           = ConsoleColor.Red;
            MsgColorWarning         = ConsoleColor.Blue;
            MsgColorVerbose         = ConsoleColor.Gray;
        }
    }
public void IniFileTest()
{
    // write sample config file
    //UT_PRINT(""); UT_PRINT( "### Configuration with IniFile ###" );
    String iniFileContents=
     "##########################################################################"  +"\n"
    +"## unit test config file"                                                    +"\n"
    +"##########################################################################"  +"\n"
    +"// this is also a comment"                                                   +"\n"
    +"; and this is as well"                                                       +"\n"
    +""                                                                            +"\n"
    +"HOME= overwritten_by_environment"                                            +"\n"
    +"HOMEPATH= overwritten_by_environment"                                        +"\n"
    +""                                                                            +"\n"
    +"concat=    start =5,          \\"                                            +"\n"
    +"           end   =32,       \\"                                              +"\n"
    +"           \\#no comment,   \\"                                              +"\n"
    +"           \\;nocomment,   \\"                                               +"\n"
    +"           ;a comment,   \\"                                                 +"\n"
    +"           getsLonger,    \\"                                                +"\n"
    +"           getsLongerxxx,   \\"                                              +"\n"
    +"           getsshorter,    \\"                                               +"\n"
    +"           getsLongerxxxxx,  \\"                                             +"\n"
    +"           getsLongerxxxxxxxxx,  \\"                                         +"\n"
    +"           getsshorterxx,    \\"                                             +"\n"
    +"           last"                                                             +"\n"
    +""                                                                            +"\n"
    +""                                                                            +"\n"
    +"CUBA=a country"                                                              +"\n"
    +"# The size "                                                                 +"\n"
    +" SIZE=  25 "                                                                 +"\n"
    +""                                                                            +"\n"
    +"# doble comment line"                                                        +"\n"
    +"# double, i meant"                                                           +"\n"
    +"2Comments= much talk"                                                        +"\n"
    +""                                                                            +"\n"
    +"# A great section"                                                           +"\n"
    +"[Great Section] "                                                            +"\n"
    +"SectionVar=5"                                                                +"\n"
    +"Double=12.3"                                                                 +"\n"
    +"Tricky=  backslash\\\\"                                                      +"\n"
    +"# A 2nd section"                                                             +"\n"
    +"[2nd Section] "                                                              +"\n"
    +"SectionVar=6"                                                                +"\n"
    +""                                                                            +"\n"
    +""                                                                            +"\n"
    +"[Great Section] "                                                            +"\n"
    +"SECTION_CONTINUED   = yEs"                                                   +"\n"
    +""                                                                            +"\n"
    +""                                                                            +"\n"
    +"[ESC] "                                                                      +"\n"
    +"Blanks=  \" x \""                                                            +"\n"
    +"Blanks2= \" x \" \\"                                                         +"\n"
    +"         \" y \" "                                                           +"\n"
    +"Tabs=\t\t\\tx\\t"                                                            +"\n"
    +"nrslash= \"\\n\\r//\\\\\""                                                   +"\n"
   ;

    String fileName= Environment.CurrentDirectory + "/unittest_testiniFile.cfg";

    // write sample config file
    {
        StreamWriter file= new StreamWriter( fileName );
        file.Write( iniFileContents );
        file.Close();
    }

    IniFile iniFile= new IniFile( fileName );
    UT_TRUE( (IniFile.Status.OK == iniFile.LastStatus) );

    // check some values
    Variable var= new Variable();
    iniFile.Load( var.Define( "",    "CUBA") );         UT_EQ( "a country",      var.GetString() );
    iniFile.Load( var.Define( "",    "cUbA") );         UT_EQ( "a country",      var.GetString() );
    iniFile.Load( var.Define( "",    "SIZE") );         UT_EQ( "25",             var.GetString() );
    iniFile.Load( var.Define( "",    "concat", ',') );  UT_EQ( 11 , var.Size());
                                                        UT_EQ( "start =5"       , var.GetString(0) );
                                                        UT_EQ( "end   =32"      , var.GetString(1) );
                                                        UT_EQ( "#no comment"    , var.GetString(2) );
                                                        UT_EQ( ";nocomment"     , var.GetString(3) );

    iniFile.Load( var.Define( "ESC", "Blanks"  ) );   UT_EQ( " x "      , var.GetString() );
    iniFile.Load( var.Define( "ESC", "Blanks2" ) );   UT_EQ( " x  y "   , var.GetString() );
    iniFile.Load( var.Define( "ESC", "Tabs"    ) );   UT_EQ( "\tx\t"    , var.GetString() );
    iniFile.Load( var.Define( "ESC", "nrslash" ) );   UT_EQ( "\n\r//\\" , var.GetString() );

    iniFile.Load( var.Define( "Great Section",  "SectionVar"       ) );   UT_EQ( "5"  , var.GetString() );
    iniFile.Load( var.Define( "2nd Section",    "SectionVar"       ) );   UT_EQ( "6"  , var.GetString() );
    iniFile.Load( var.Define( "Great Section",  "SECTION_CONTINUED") );   UT_EQ( "yEs", var.GetString() );
    iniFile.Load( var.Define( "Great Section",  "Tricky"           ) );   UT_EQ( "backslash\\", var.GetString() );

    // add it to ALIB config
    ALox.Init();
    ALIB.Config.InsertPlugin( iniFile, Configuration.PrioIniFile );
    ALIB.Config.Load( var.Define( "",               "CUBA"              ) );   UT_EQ( "a country"  , var.GetString() );
    ALIB.Config.Load( var.Define( "",               "cUbA"              ) );   UT_EQ( "a country"  , var.GetString() );
    ALIB.Config.Load( var.Define( "",               "SIZE"              ) );   UT_EQ( "25"         , var.GetString() );
    ALIB.Config.Load( var.Define( "",               "concat"            ) );   UT_EQ( 11 , var.Size());
                                                                               UT_EQ( "start =5"   , var.GetString(0) );
                                                                               UT_EQ( "end   =32"  , var.GetString(1) );
    ALIB.Config.Load( var.Define( "Great Section",  "SectionVar"        ) );   UT_EQ( "5"          , var.GetString() );
    ALIB.Config.Load( var.Define( "2nd Section",    "SectionVar"        ) );   UT_EQ( "6"          , var.GetString() );
    ALIB.Config.Load( var.Define( "Great Section",  "SECTION_CONTINUED" ) );   UT_EQ( "yEs"        , var.GetString() );
    ALIB.Config.Load( var.Define( "Great Section",  "Tricky"            ) );   UT_EQ( "backslash\\", var.GetString() );
    ALIB.Config.Load( var.Define( "Great Section",  "SECTION_CONTINUED" ) );   UT_TRUE( var.IsTrue() );


    // check if environment variable "home" overwrites INI file
    AString vIniFile= new AString();   iniFile.Load( var.Define( "", "hOme" ) );               UT_EQ( "overwritten_by_environment", var.GetString() );
    int prio= ALIB.Config.Load( var.Define("", "hOme" ));
    if (prio != Configuration.PrioEnvironment ) // Windows platform?
    {
        prio= ALIB.Config.Load( var.Define("", "hOmePAth") );
        iniFile.Load( var.Define( "", "hOmePAth") );    UT_EQ( "overwritten_by_environment", var.GetString() );
    }
    UT_EQ( Configuration.PrioEnvironment, prio );

    UT_TRUE( var.GetString().Length() > 0 );
    UT_TRUE( !vIniFile.Equals( var.GetString()) );

    // change a value and write a new one
    var.Define( "New Section",  "newvar");
    var.Priority= Configuration.PrioIniFile;
    UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store( var, "new" ) );
    ALIB.Config.Load  ( var.Define("New Section",  "newvar") );  UT_EQ( "new",   var.GetString() );

    var.Define( "",             "newvar");
    var.Priority= Configuration.PrioIniFile;
    UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store( var, "aworx") );
    ALIB.Config.Load  ( var.Define("",             "newvar") );  UT_EQ( "aworx", var.GetString() );


    var.Define( "",   "newvarList", ',');
    var.AddString("val1=5");
    var.AddString("val2=10");
    var.AddString("val3=hello");
    var.Priority= Configuration.PrioIniFile;
    UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store(var) );
    ALIB.Config.Load (  var.Define( "",  "newvarList")   );

    var.Define( "",   "commented", ',', "2lines" );
    var.Priority= Configuration.PrioIniFile;
    UT_EQ( Configuration.PrioIniFile, ALIB.Config.Store(  var,  "this is c-line 1 \nand this line 2" ) );

    // write the file
    iniFile.FileName._(".writeback.txt");
    iniFile.WriteFile();

    // load the written file into another config
    IniFile readBack= new IniFile( iniFile.FileName.ToString() );
    Variable varBack= new Variable();

    // compare all
    UT_TRUE( (IniFile.Status.OK == readBack.LastStatus) );

    {
        AString msg= new AString();
        Substring orig= new Substring();
        Substring back= new Substring();
        foreach ( IniFile.Section section in iniFile.Sections )
        {
            foreach ( IniFile.Entry entry in section.Entries )
            {
                msg.Clear()._( "Reading variable " ).Field()._( section.Name )._( '/' )._( entry.Name );
                UT_PRINT( msg );


                char delim= '\0';
                if(     entry.Name.Equals("concat")
                    ||  entry.Name.Equals("newvarList")       )
                    delim= ',';

                iniFile .Load( var    .Define( section.Name, entry.Name, delim) );
                readBack.Load( varBack.Define( section.Name, entry.Name, delim) );

                UT_EQ( var.Size(), varBack.Size() );
                for ( int i= 0; i< var.Size(); i++ )
                {
                    int idx= var.GetString(i).IndexOf('=');
                    if( idx < 0 )
                    {
                        UT_EQ( var.GetString(i), varBack.GetString(i) );
                    }
                    else
                    {
                        int idxBack= varBack.GetString(i).IndexOf('=');
                        orig.Set( var    .GetString(i), 0, idx     );
                        back.Set( varBack.GetString(i), 0, idxBack );
                        UT_EQ( orig.Trim().ToString(), back.Trim().ToString() );
                        orig.Set( var    .GetString(i), idx     +1 );
                        back.Set( varBack.GetString(i), idxBack +1 );
                        UT_EQ( orig.Trim().ToString(), back.Trim().ToString() );
                    }
                }
            }
        }
    }

    readBack.Load ( var.Define( "New Section",  "newvar" ) );   UT_EQ( "new"  , var.GetString() );
    readBack.Load ( var.Define( "",             "newvar" ) );   UT_EQ( "aworx", var.GetString() );


    ALIB.Config.RemovePlugin( iniFile );


    ALIB.Config.InsertPlugin( readBack, Configuration.PrioIniFile );
    ALIB.Config.Load ( var.Define( "New Section",  "newvar") );   UT_EQ( "new"   , var.GetString() );
    ALIB.Config.Load ( var.Define( "",             "newvar") );   UT_EQ( "aworx" , var.GetString() );

    ALIB.Config.RemovePlugin( readBack );
}