Example #1
0
        static void InputBufferDynamic()
        {
            String input          = ""; //User input1
            uint   currentAddress = 0;
            uint   line1          = 0;
            uint   line2          = 0;
            uint   line3          = 0;

            //Stopwatch to test execution time
            Stopwatch stopWatch = Stopwatch.StartNew();

            Parser            myParser       = new Parser("", 0);
            TextBufferDynamic tempTextBuffer = new TextBufferDynamic(); //Create temp buffer
            Doublylinked      inputList      = new Doublylinked();      // My text buffer
            DoubleLinkedList  headOfList     = new DoubleLinkedList();  //First node

            while (!input.Equals("q", StringComparison.InvariantCultureIgnoreCase))
            {
                inputList.ResetIndex();
                Console.Write("");
                input = Console.ReadLine();                   //Read input string
                input = input.Replace(" ", "");               //remove space between words

                myParser = new Parser(input, currentAddress); //Sent input token to parser
                //Console.WriteLine("Line 1 is " + myParser.line1 + "Line 2 is " + myParser.line2 + "Line 3 is " + myParser.line3);
                //Console.WriteLine("Current Address is " + currentAddress);

                if (myParser.accept != true && myParser.line1 != 0) // if only one address is enter
                {
                    line1 = myParser.line1;
                }
                else if (myParser.accept)
                {
                    if (line1 != 0)
                    {
                        line2 = myParser.line1;
                    }
                    else
                    {
                        line1 = myParser.line1;
                        line2 = myParser.line2;
                        line3 = myParser.line3;
                    }
                }

                //After parser read the command the switch statement help it choose what to do
                if (myParser.accept)
                {
                    switch (myParser.command)
                    {
                    case 'a':

                        while (input[0] != '.')
                        {
                            input = Console.ReadLine();

                            if (input[0] == '.')
                            {
                                uint totalLine = inputList.GetIndex();
                                unsafe
                                {
                                    tempTextBuffer.Append(line1, headOfList, totalLine, &currentAddress);
                                }
                                headOfList.head = null;

                                break;
                            }

                            inputList.InsertLast(headOfList, input);
                        }
                        break;

                    case 'c':

                        while (input[0] != '.')
                        {
                            input = Console.ReadLine();

                            if (input[0] == '.')
                            {
                                uint totalLine = inputList.GetIndex();
                                unsafe
                                {
                                    tempTextBuffer.Change(line1, line2, headOfList, totalLine, &currentAddress);
                                }

                                headOfList.head = null;

                                break;
                            }

                            inputList.InsertLast(headOfList, input);
                        }
                        break;

                    case 'd':

                        unsafe
                        {
                            tempTextBuffer.Delete(line1, line2, &currentAddress);
                        }
                        headOfList.head = null;

                        break;

                    case 'j':
                        unsafe
                        {
                            tempTextBuffer.Join(line1, line2, &currentAddress);
                        }
                        headOfList.head = null;

                        break;

                    case 'l':
                        unsafe
                        {
                            tempTextBuffer.List(line1, line2, &currentAddress);
                        }
                        headOfList.head = null;

                        break;

                    case 'm':
                        unsafe
                        {
                            tempTextBuffer.Move(line1, line2, line3, &currentAddress);
                        }
                        headOfList.head = null;

                        break;

                    case 'n':
                        unsafe
                        {
                            tempTextBuffer.number(line1, line2, &currentAddress);
                        }
                        headOfList.head = null;

                        break;

                    case 'p':

                        if (line1 != 0 && line2 != 0)
                        {
                            tempTextBuffer.PrintLine(line1, line2, headOfList);
                            headOfList.head = null;
                        }

                        break;

                    case 't':
                        unsafe
                        {
                            tempTextBuffer.Transfer(line1, line2, line3, &currentAddress);
                        }
                        headOfList.head = null;

                        break;

                    case 'w':

                        tempTextBuffer.Write(myParser.para);
                        headOfList.head = null;

                        break;

                    case 'e':
                        stopWatch.Start();
                        tempTextBuffer.Edit(myParser.para);
                        stopWatch.Stop();
                        Console.WriteLine("File load time is " + stopWatch.ElapsedMilliseconds + "ms");
                        headOfList.head = null;

                        break;

                    default:
                        Console.WriteLine("Wrong command");
                        break;
                    }

                    line1 = 0;
                    line2 = 0;
                    line3 = 0;
                }
            }
        }
        uint numberLine = 1;                                  /* How many lines are currently in the buffer if the DoubleLinkedList does not store its own size */

        /* Appends contents of input buffer after the input line */
        public unsafe void Append(uint line, DoubleLinkedList inputBuffer, uint totalLine, uint *mainbufferLine)
        {
            if (line == 9999)
            {
                DynamicStringLinkNode temp = inputBuffer.head;

                if (temp != null)
                {
                    textBufferList.InsertFront(headOfList, temp.memory.ToString()); // copy first line from input buffer to text buffer
                }
                DynamicStringLinkNode temp2 = headOfList.head;
                temp = temp.next;

                while (temp != null)
                {
                    textBufferList.InsertAfter(temp2, temp.memory.ToString());
                    temp  = temp.next;
                    temp2 = temp2.next;
                }
                numberLine     = textBufferList.GetIndex(); // Get the current line number
                currentAddress = textBufferList.GetIndex(); //Current Address is the last line entered
                *mainbufferLine = textBufferList.GetIndex();
            }
            else if (line == 0 || line == textBufferList.GetIndex())  //append at the end
            {
                DynamicStringLinkNode temp = inputBuffer.head;

                if (temp != null)
                {
                    textBufferList.InsertLast(headOfList, temp.memory.ToString()); // copy first line from input buffer to text buffer
                }

                temp = temp.next;

                while (temp != null)
                {
                    textBufferList.InsertLast(headOfList, temp.memory.ToString());
                    temp = temp.next;
                }
                numberLine     = textBufferList.GetIndex(); // Get the current line number
                currentAddress = textBufferList.GetIndex(); //Current Address is the last line entered
                *mainbufferLine = textBufferList.GetIndex();
            }
            else //append after a selecte line
            {
                DynamicStringLinkNode temp  = inputBuffer.head;
                DynamicStringLinkNode temp2 = headOfList.head;

                if (line == 1) // Insert after the head
                {
                    if (temp != null)
                    {
                        textBufferList.InsertAfter(headOfList.head, temp.memory.ToString());
                    }

                    temp  = temp.next;
                    temp2 = temp2.next;

                    while (temp != null)
                    {
                        textBufferList.InsertAfter(temp2, temp.memory.ToString());
                        temp  = temp.next;
                        temp2 = temp2.next;
                    }
                    numberLine     = textBufferList.GetIndex(); // Get the current line number
                    currentAddress = totalLine + 1;             //Current Address is the last line entered
                    *mainbufferLine = textBufferList.GetIndex();
                }
                else if (textBufferList.GetIndex() >= line)
                {
                    for (int i = 0; i < line - 1; i++)
                    {
                        temp2 = temp2.next;
                    }

                    if (temp != null)
                    {
                        textBufferList.InsertAfter(temp2, temp.memory.ToString());
                    }

                    temp  = temp.next;
                    temp2 = temp2.next;

                    while (temp != null)
                    {
                        textBufferList.InsertAfter(temp2, temp.memory.ToString());
                        temp  = temp.next;
                        temp2 = temp2.next;
                    }
                    *mainbufferLine = textBufferList.GetIndex();
                    numberLine     = textBufferList.GetIndex(); // Get the current line number
                    currentAddress = totalLine + numberLine;    //Current Address is the last line entered
                }

                else
                {
                    Console.WriteLine("Erro wrong input lines number ");
                }
            }
        }