Beispiel #1
0
        public void parse(string input)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(input);

            MemoryStream ms = new MemoryStream(bytes);
            StreamReader sr = new StreamReader(ms);

            StringBuilder sb = new StringBuilder();

            while (!sr.EndOfStream)
            {
                int c = sr.Read();

                //MarkUp chars are usless - skip them
                if (c < 32)
                {
                    continue;
                }

                //c == ; - comment
                if (sr.Peek() == 59 || c == 59)
                {
                    //Skip comment
                    sr.ReadLine();
                    continue;
                }

                //c == [ - SECTION name
                if (c == 91)
                {
                    //new SECTION


                    // sb = SECTIONNAME]
                    sb.Append(sr.ReadLine());

                    //if true - wrong format
                    if ((int)sb[sb.Length - 1] != 93)
                    {
                        throw new Exceptions.InvalidFileFormatException("Wrong format. Section name is not closed by ] bracket.");
                    }


                    //remove ] bracket
                    sb.Remove(sb.Length - 1, 1);

                    var sectionName = sb.ToString();

                    if (!isCorrectSectionName(sectionName))
                    {
                        throw new Exceptions.InvalidFileFormatException($"Wrong format. Section name {sb.ToString()} has wrong format.");
                    }


                    //check if this section already exists
                    Dictionary <string, string> dict = null;
                    foreach (var item in this.db)
                    {
                        if (item.Item2 == sectionName)
                        {
                            dict = item.Item1;
                            break;
                        }
                    }
                    if (dict == null)
                    {
                        this.sectionCounter++;
                        this.db.Add(new Tuple <Dictionary <string, string>, string>(new Dictionary <string, string>(), sectionName));
                    }
                    else
                    {
                        throw new Exceptions.InvalidFileFormatException($"Wrong file format. Two sections with name {sectionName}");
                    }



                    //clear sb for next SECTION name
                    sb.Clear();

                    continue;
                }

                //c == other chars
                if (c != 32)
                {
                    //NAME
                    string leftPart = LParser.ReadNameMember(ref sr, c);

                    //_=_ - skip 3 chars
                    c = sr.Read(); //" "
                    if (!((int)c == 32))
                    {
                        throw new Exceptions.InvalidFileFormatException($"Wrong format. After Name member must be space(32) char, but not '{(char)c}'");
                    }
                    c = sr.Read(); //"="
                    if (!((int)c == 61))
                    {
                        throw new Exceptions.InvalidFileFormatException($"Wrong format. After Name member and space char must be '=' char, but not '{(char)c}'");
                    }
                    c = sr.Read(); //" "
                    if (!((int)c == 32))
                    {
                        throw new Exceptions.InvalidFileFormatException($"Wrong format. After '=' char must be space(32) char, but not '{(char)c}'");
                    }

                    //VALUE
                    c = sr.Read();
                    string rightPart = LParser.ReadValueMember(ref sr, c);

                    //Adding pair <key=NAME, value=VALUE> to dictionary db
                    try
                    {
                        this.db[sectionCounter].Item1.Add(leftPart, rightPart);
                    }
                    catch (ArgumentException e)
                    {
                        throw new Exceptions.InvalidFileFormatException($"Invalid file format. Duplicated Keys in section {this.db[sectionCounter].Item2}.");
                    }
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            string input = "";

            //Console.Write("Enter file name: ");
            //input = Console.ReadLine();

            try
            {
                input = File.ReadAllText("LostValues.ini");
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine($"An exception occurred, message: {e.Message}");
                return;
            }

            LParser.LParser lParser = new LParser.LParser();

            try
            {
                lParser.parse(input);
            }
            catch (Exceptions.InvalidFileFormatException e)
            {
                Console.WriteLine($"An exception occurred, message: {e.Message}");
                return;
            }


            string sectionName;
            string memberName;

            while (true)
            {
                Console.Write("Enter section name: ");
                sectionName = Console.ReadLine();
                Console.Write("Enter member name: ");
                memberName = Console.ReadLine();
                Console.Write("Choose value type you want to get (1-double,2-integer and 3-string): ");
                char option = (char)Console.Read();


                try
                {
                    switch (option)
                    {
                    case '1':
                        Console.WriteLine($"{memberName} = {lParser.getDblValue(sectionName, memberName)}");
                        break;

                    case '2':
                        Console.WriteLine($"{memberName} = {lParser.getIntValue(sectionName, memberName)}");
                        break;

                    case '3':
                        Console.WriteLine($"{memberName} = {lParser.getStrValue(sectionName, memberName)}");
                        break;

                    default:
                        Console.WriteLine("Wrong option. Try again. Use 1 for double,2 for integer and 3 for string");
                        break;
                    }
                }
                catch (Exceptions.InvalidSectionException e)
                {
                    Console.WriteLine($"An exception occurred, message: {e.Message}");
                }
                catch (Exceptions.InvalidNameException e)
                {
                    Console.WriteLine($"An exception occurred, message: {e.Message}");
                }
                catch (Exceptions.InvalidParameterTypeException e)
                {
                    Console.WriteLine($"An exception occurred, message: {e.Message}");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"An exception occurred, message: {e.Message}");
                }
                Console.ReadLine();
            }
        }