Example #1
0
    // #############################################################################################
    // file IO
    // #############################################################################################

        /** ****************************************************************************************
         * Clears all configuration data and reads the file. It might happen that lines are
         * ignored or otherwise marked as faulty. All numbers of such lines get collected in
         * field LinesWithReadErrors.
         * @return Returns the #Status of the operation.
         ******************************************************************************************/
        public IniFile.Status  ReadFile()
        {
            Reset();
            LastStatus= Status.OK;

            // read all variables
            StreamReader file;
            try
            {
                file= new StreamReader( FileName.ToString() );
            }
            catch( Exception )
            {
                return LastStatus= Status.ERROR_OPENING_FILE;
            }

            String      lineS;
            AString     name=       new AString();
            AString     value=      new AString();
            AString     comments=   new AString();
            Section     actSection= (IniFile.Section) Sections[0];
            Substring   line=       new Substring();

            int         lineNo= 0;
            bool        fileHeaderRead= false;

            char[]      separatorCharacters= value._( "=" )._( CString.DefaultWhitespaces )
                                                  .ToString().ToCharArray();

            LinesWithReadErrors.Clear();
            while ( (lineS= file.ReadLine()) != null )
            {
                lineNo++;

                bool isEmpty=       line.Set( lineS ).Trim().IsEmpty();
                bool isCommentLine= startsWithCommentSymbol( line );

                if ( isCommentLine )
                {
                    if ( comments.IsNotEmpty() )
                        comments.NewLine();
                    comments._(line);
                    continue;
                }

                // still processing file header?
                if ( !fileHeaderRead )
                {
                    fileHeaderRead= true;
                    FileComments._()._( comments );
                    comments.Clear();
                }

                // empty line?
                if ( isEmpty )
                {
                    if ( comments.IsNotEmpty() )
                        comments.NewLine();
                    continue;
                }

                // section line
                if ( line.Consume( '[' ) )
                {
                    fileHeaderRead= true;

                    // we do not care if there is no closing bracket. But if there is one, we remove it.
                    if( !line.ConsumeFromEnd(']') )
                        LinesWithReadErrors.Add( lineNo );

                    // search the section in our section list (if section existed already, new comments
                    // are dropped)
                    actSection= (IniFile.Section) SearchOrCreateSection( line, comments);
                    comments.Clear();

                    continue;
                }

                // variable line?
                value.Clear();
                int idx= line.IndexOfAny( separatorCharacters, Inclusion.Include );
                if( idx < 0 )
                {
                    name._()._( line );
                    line.Clear();
                }
                else
                {
                    name._()._( line.Buf, line.Start, idx );
                    line.Consume( idx );
                    value._(line);
                }

                // read continues as long as lines end with '\' (must not be '\\')
                while (     line.CharAtEnd()  == '\\'
                        &&  line.CharAtEnd(1) != '\\' )
                {
                    value.NewLine();

                    if ( (lineS= file.ReadLine()) == null  )
                    {
                        // last line of the file ended with '\' !
                        line.Clear();
                        break;
                    }
                    line.Set( lineS ).TrimEnd();
                    value._( line );
                }

                // insert entry with raw value
                {
                    IniFile.Entry entry= (IniFile.Entry) actSection.GetEntry( name, true );
                    entry.Values  .Clear();
                    entry.Comments._()._( comments );
                    entry.RawValue._()._( value );

                    // if there is just no raw value, we add an empty string to the entries' values
                    if ( value.IsEmpty() )
                        entry.Values.Add( new AString() );
                }

                comments.Clear();

            }
            file.Close();

            return LastStatus;
        }
public void FrontEnd()
{
    // empty substring
    {
        Substring subs= new Substring();
        UT_EQ(  '\0',      subs.CharAtStart(   ) );
        UT_EQ(  '\0',      subs.CharAt( 0 ) );
        UT_EQ(  '\0',      subs.CharAt( 1 ) );
        UT_EQ(  '\0',      subs.CharAt(-1 ) );
        UT_EQ(  '\0',      subs.CharAt( 2 ) );
        UT_EQ(  '\0',      subs.CharAt(-2 ) );

        UT_EQ(  '\0',      subs.CharAtEnd (   ) );
        UT_EQ(  '\0',      subs.CharAtEnd ( 0 ) );
        UT_EQ(  '\0',      subs.CharAtEnd ( 1 ) );
        UT_EQ(  '\0',      subs.CharAtEnd (-1 ) );
        UT_EQ(  '\0',      subs.CharAtEnd ( 2 ) );
        UT_EQ(  '\0',      subs.CharAtEnd (-2 ) );
    }

    // empty substring
    {
        Substring subs= new Substring("aaaaaaaaaaaa");
        subs.Start= 5;
        subs.End=   4;
        UT_EQ(  '\0',      subs.CharAtStart(   ) );
        UT_EQ(  '\0',      subs.CharAt( 0 ) );
        UT_EQ(  '\0',      subs.CharAt( 1 ) );
        UT_EQ(  '\0',      subs.CharAt(-1 ) );
        UT_EQ(  '\0',      subs.CharAt( 2 ) );
        UT_EQ(  '\0',      subs.CharAt(-2 ) );

        UT_EQ(  '\0',      subs.CharAtEnd (   ) );
        UT_EQ(  '\0',      subs.CharAtEnd ( 0 ) );
        UT_EQ(  '\0',      subs.CharAtEnd ( 1 ) );
        UT_EQ(  '\0',      subs.CharAtEnd (-1 ) );
        UT_EQ(  '\0',      subs.CharAtEnd ( 2 ) );
        UT_EQ(  '\0',      subs.CharAtEnd (-2 ) );
    }

    // substring of length 1
    {
        Substring subs= new Substring("aaaaaaaaaaaa");
        subs.Start= subs.End= 5;
        UT_EQ('a',      subs.CharAtStart(   ) );
        UT_EQ('a',      subs.CharAt( 0 ) );
        UT_EQ('\0',     subs.CharAt( 1 ) );
        UT_EQ('\0',     subs.CharAt(-1 ) );
        UT_EQ('\0',     subs.CharAt( 2 ) );
        UT_EQ('\0',     subs.CharAt(-2 ) );

        UT_EQ('a',      subs.CharAtEnd (   ) );
        UT_EQ('a',      subs.CharAtEnd ( 0 ) );
        UT_EQ('\0',     subs.CharAtEnd ( 1 ) );
        UT_EQ('\0',     subs.CharAtEnd (-1 ) );
        UT_EQ('\0',     subs.CharAtEnd ( 2 ) );
        UT_EQ('\0',     subs.CharAtEnd (-2 ) );
    }

    // substring of length 2
    {
        Substring subs= new Substring("aaaaabbbbbb");
        subs.End= subs.IndexOf('b');
        subs.Start= subs.End - 1;
        UT_EQ('a',      subs.CharAtStart(   ) );
        UT_EQ('a',      subs.CharAt( 0 ) );
        UT_EQ('b',      subs.CharAt( 1 ) );
        UT_EQ('\0',     subs.CharAt(-1 ) );
        UT_EQ('\0',     subs.CharAt( 2 ) );
        UT_EQ('\0',     subs.CharAt(-2 ) );

        UT_EQ('b',      subs.CharAtEnd (   ) );
        UT_EQ('b',      subs.CharAtEnd ( 0 ) );
        UT_EQ('a',      subs.CharAtEnd ( 1 ) );
        UT_EQ('\0',     subs.CharAtEnd (-1 ) );
        UT_EQ('\0',     subs.CharAtEnd ( 2 ) );
        UT_EQ('\0',     subs.CharAtEnd (-2 ) );
    }

}