/* Replace lines from line1 through line2 with the lines in the input buffer. For single argument, change that one line. */
        public unsafe void Change(uint line1, uint line2, DoubleLinkedList2 inputBuffer, uint totalLine, uint *mainbufferLine)
        {
            if (line1.Equals(1) && line2.Equals(0)) //if loaction is at 1 and second number is 0
            {
                Append(line1, inputBuffer, 0, mainbufferLine);
                Delete(1, 0, mainbufferLine);
                numberLine     = textBufferList.GetIndex(); // Get the current line number
                currentAddress = totalLine + 1;             //Current Address is the last line entered
                *mainbufferLine = textBufferList.GetIndex();
            }
            else if (line1 > 1 && line2.Equals(0)) //if loaction is great then 1 and second number is 0
            {
                Delete(line1, 0, mainbufferLine);
                Append(line1 - 1, inputBuffer, 0, mainbufferLine);
                numberLine     = textBufferList.GetIndex(); // Get the current line number
                currentAddress = totalLine + line1;         //Current Address is the last line entered
                *mainbufferLine = textBufferList.GetIndex();
            }

            else if (line2 >= line1)
            {
                Delete(line1, line2, mainbufferLine);
                Append(line1 - 1, inputBuffer, 0, mainbufferLine);
                numberLine     = textBufferList.GetIndex(); // Get the current line number
                currentAddress = totalLine + line1;         //Current Address is the last line entered
                *mainbufferLine = textBufferList.GetIndex();
            }
        }
 public void printLine(uint line1, uint line2, DoubleLinkedList2 inputBuffer) //Print everything from text buffer
 {
     if (line2 <= textBufferList.GetIndex())
     {
         textBufferList.PrintList(headOfList, line1, line2, 3);
         currentAddress = line2;
     }
     else
     {
         Console.WriteLine("Line index error");
     }
 }
Ejemplo n.º 3
0
        static void InputBufferStatic()
        {
            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);
            TextBufferStatic  tempTextBuffer = new TextBufferStatic();  //Create temp buffer
            doublylinked2     inputList      = new doublylinked2();     // My text buffer
            DoubleLinkedList2 headOfList     = new DoubleLinkedList2(); //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");

                        break;

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

                    line1 = 0;
                    line2 = 0;
                    line3 = 0;
                }
            }
        }
        /* Copies lines from line1 through line2 and inserts them after line3. For single argument, transfer that one line. */
        public unsafe void Transfer(uint line1, uint line2, uint line3, uint *mainbufferLine)
        {
            {
                uint              index          = line1;
                uint              index2         = line2;
                uint              index3         = line3;
                doublylinked2     tempList       = new doublylinked2();            // Temp helper list
                DoubleLinkedList2 tempheadOfList = new DoubleLinkedList2();        // Temp helper list's first node

                if (line1 >= 1 && line2.Equals(0) && line3 >= 0 && line3 != line1) //is line 2 is 0 then only transfer one line
                {
                    if (headOfList.head != null && line1.Equals(1))                //If location is line 1
                    {
                        tempList.InsertLast(tempheadOfList, headOfList.head.memory.ToString());
                        Append(line3 - 1, tempheadOfList, 0, mainbufferLine); //need to rest set memory each time or creat
                        numberLine     = textBufferList.GetIndex();           // Get the current line number
                        currentAddress = line3 + 1;
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                    else if (line3 >= 1)
                    {
                        StaticStringLinkNode temp  = headOfList.head; // use to locate line 1 location
                        StaticStringLinkNode temp2 = headOfList.head;
                        index--;                                      //for line 1 location
                        index3--;
                        while (temp != null && index != 0)            //This while loop is use to locate line 1 string in text buffer
                        {
                            index--;
                            temp = temp.next;                // line1 loaction node, we will copy this node's tring to temp
                        }
                        while (temp2 != null && index3 != 0) //This while loop is use to locate line 3
                        {
                            index3--;
                            temp2 = temp2.next;                                    // line 3 loaction node string
                        }
                        textBufferList.InsertAfter(temp2, temp.memory.ToString()); // Insert line 1 string after line 3
                        numberLine     = textBufferList.GetIndex();                // Get the current line number
                        currentAddress = line3 + 1;
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                    else if (line3.Equals(0))
                    {
                        StaticStringLinkNode temp = headOfList.head; // use to locate line 1 location
                        index--;                                     //for line 1 location
                        index3--;
                        while (temp != null && index != 0)           //This while loop is use to locate line 1 string in text buffer
                        {
                            index--;
                            temp = temp.next;                                           // line1 loaction node, we will copy this node's tring to temp
                        }
                        textBufferList.InsertFront(headOfList, temp.memory.ToString()); // Insert line 1 string after line 3
                        numberLine     = textBufferList.GetIndex();                     // Get the current line number
                        currentAddress = line3;
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                }
                else if (line1 >= 1 && line2.Equals(line1) && line3 >= 1 && line3 != line1) // if line 1 = line2
                {
                    if (headOfList.head != null && line1.Equals(1))                         //If location is line 1
                    {
                        tempList.InsertLast(tempheadOfList, headOfList.head.memory.ToString());
                        Append(line3 - 1, tempheadOfList, 0, mainbufferLine); //need to rest set memory each time or creat
                        numberLine     = textBufferList.GetIndex();           // Get the current line number
                        currentAddress = line3 + 1;
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                    else
                    {
                        StaticStringLinkNode temp  = headOfList.head; // use to locate line 1 location
                        StaticStringLinkNode temp2 = headOfList.head; // use to locate line 3 string
                        index--;                                      //for line 1 location
                        index3--;
                        while (temp != null && index != 0)            //This while loop is use to locate line 1 string in text buffer
                        {
                            index--;
                            temp = temp.next;                // line1 loaction node, we will copy this node's tring to temp
                        }
                        while (temp2 != null && index3 != 0) //Thi1s while loop is use to locate line 3
                        {
                            index3--;
                            temp2 = temp2.next;                                    // line 3 loaction node string
                        }
                        textBufferList.InsertAfter(temp2, temp.memory.ToString()); // Insert line 1 string after line 3
                        numberLine     = textBufferList.GetIndex();                // Get the current line number
                        currentAddress = line3 + 1;
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                }
                else if (line2 > line1) // copy line 1 throut line 2 and inser after line 3
                {
                    if (line3.Equals(0))
                    {
                        index  = line1 - 1;
                        index2 = (line2 - line1 + 1);
                        StaticStringLinkNode temp  = headOfList.head; // use to locate line 1 location
                        StaticStringLinkNode temp2 = headOfList.head; // use to locate line 3 string

                        index3--;

                        while (temp != null && index != 0) //If location is line 1. Copy line 1 thorough line 2
                        {
                            temp = temp.next;
                            index--;
                        }

                        while (temp != null && index2 != 0) //If location is line 1. Copy line 1 thorough line 2
                        {
                            tempList.InsertLast(tempheadOfList, temp.memory.ToString());
                            temp = temp.next; // line1 loaction node, we will copy this node's tring to temp
                            index2--;
                        }
                        Append(9999, tempheadOfList, 0, mainbufferLine);
                        numberLine     = textBufferList.GetIndex(); // Get the current line number
                        currentAddress = line3 + (line2 - line1 + 1);
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                    else
                    {
                        index  = line1 - 1;
                        index2 = (line2 - line1 + 1);
                        StaticStringLinkNode temp  = headOfList.head; // use to locate line 1 location
                        StaticStringLinkNode temp2 = headOfList.head; // use to locate line 3 string

                        index3--;

                        while (temp != null && index != 0) //If location is line 1. Copy line 1 thorough line 2
                        {
                            temp = temp.next;
                            index--;
                        }

                        while (temp != null && index2 != 0) //If location is line 1. Copy line 1 thorough line 2
                        {
                            tempList.InsertLast(tempheadOfList, temp.memory.ToString());
                            temp = temp.next; // line1 loaction node, we will copy this node's tring to temp
                            index2--;
                        }

                        Append(line3, tempheadOfList, 0, mainbufferLine);
                        numberLine     = textBufferList.GetIndex(); // Get the current line number
                        currentAddress = line3 + (line2 - line1 + 1);
                        *mainbufferLine = textBufferList.GetIndex();
                    }
                }
                else
                {
                    Console.WriteLine("Line index error");
                }
            }
        }
        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, DoubleLinkedList2 inputBuffer, uint totalLine, uint *mainbufferLine)
        {
            if (line == 9999)
            {
                StaticStringLinkNode temp = inputBuffer.head;

                if (temp != null)
                {
                    textBufferList.InsertFront(headOfList, temp.memory.ToString()); // copy first line from input buffer to text buffer
                }
                StaticStringLinkNode 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
            {
                StaticStringLinkNode 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
            {
                StaticStringLinkNode temp  = inputBuffer.head;
                StaticStringLinkNode 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 ");
                }
            }
        }