Sarà molto imbarazzante per me confessare che non ho capito il tuo codice xD
In compenso il mio codice funzionare a parte che per un solo comando, quello che sposta il cursore alle coordinate (^##).
codice:
void ExecCommand()
{
//Una volta letto il ^ che introduce il comando
//leggo il carattere che rappresenta il comando
cin.get(ch);
if (isdigit(ch))
{
//Se il carattere trovato è un numero, allora
//il comando è ^##, perciò leggo un altro carattere
//che rappresenta la seconda coordinata
char ch2;
cin.get(ch2);
//Sposto il cursore alle coordinate lette
MoveCursor(ch, ch2);
}
else { //altri comandi semplici }
}
Questo comando viene eseguito senza problemi, MA: qualsiasi (qualsiasi) cosa venga scritta dopo questo comando causa un segmentation fault. Tutti i comandi funzionano alla perfezione. Notare che se eseguo questo comando non ricevo il sigsev.
Esempio:
funziona, ma
va in segmentation fault.
Sotto c'è il codice completo:
codice:
#include <iostream>
#include <ctype.h>
using namespace std;
void ExecCommand();
void Output(char c);
void outIns(char c);
void outOW(char c);
void MoveCursor(int x, int y);
void ClampCursor();
void cls();
void EraseLine();
void PrintTerminal();
typedef enum OutputMode { insert, overWrite } OutputMode;
char scrBoxPart[] = "+----------+";
const int screenHeigth = 10;
const int screenWidth = 10;
int testCase = 0;
int cursorX;
int cursorY;
char** screen;
char ch;
OutputMode outMode;
int main()
{
int lnNumber, lnCount;
//Creates screen
screen = new char*[screenHeigth];
for (int i = 0; i < screenHeigth; i++)
screen[i] = new char[screenWidth];
//and clears it.
cls();
//Keeps running until the end of the file
while (true)
{
//resets the terminal.
testCase++;
lnNumber = lnCount = 0;
cursorX = cursorY = 0;
outMode = overWrite;
cls();
//Reads the number of input lines
//and exits if there is no data to process
cin >> lnNumber;
if (!lnNumber) return 0;
cin.get(ch);
//Keeps processing data until all
//the lines have been processed.
while (lnCount < lnNumber)
{
//Reads a char
cin.get(ch);
switch (ch)
{
case '^':
//it's a command
ExecCommand();
break;
case '\n':
//end of line
lnCount++;
break;
default:
//ch has to be output
Output(ch);
break;
}
}
PrintTerminal();
}
return 0;
}
//Executes the commands
void ExecCommand()
{
cin.get(ch);
/*If the command is a digit, the command
is in the form ^##, so you have to read the
second digit to have the full command*/
if (isdigit(ch))
{
char ch2;
cin.get(ch2);
//Moves the cursor
MoveCursor(ch, ch2);
}
else
{
//Runs the proper command.
switch (ch)
{
case 'b':
//move to line beginning
MoveCursor(0, cursorY);
break;
case 'c':
cls();
break;
case 'd':
//move down
MoveCursor(cursorX, (cursorY + 1));
break;
case 'e':
//erase everything at the right of the cursor
EraseLine();
break;
case 'h':
//move to the top left corner
MoveCursor(0, 0);
break;
case 'i':
//enters insert mode
outMode = insert;
break;
case 'l':
//move left
MoveCursor((cursorX - 1), cursorY);
break;
case 'o':
//enters overwrite mode
outMode = overWrite;
break;
case 'r':
//move right
MoveCursor((cursorX + 1), cursorY);
break;
case 'u':
//move up
MoveCursor(cursorX, (cursorY - 1));
break;
case '^':
//print a ^
Output('^');
break;
}
}
}
//Prints a character to the screen
void Output(char c)
{
switch (outMode)
{
case insert:
outIns(c);
break;
case overWrite:
outOW(c);
break;
}
}
void MoveCursor(int x, int y)
{
cursorX = x;
cursorY = y;
ClampCursor();
}
//Clamps the cursor to the screen
void ClampCursor()
{
if (cursorX < 0) cursorX = 0;
if (cursorX > (screenWidth - 1)) cursorX = (screenWidth - 1);
if (cursorY < 0) cursorY = 0;
if (cursorY > (screenHeigth - 1)) cursorX = (screenHeigth - 1);
}
void cls()
{
for (int y = 0; y < screenHeigth; y++)
for (int x = 0; x < screenWidth; x++)
screen[y][x] = ' ';
}
void EraseLine()
{
for (int x = cursorX; x < screenWidth; x++)
screen[cursorY][x] = ' ';
}
void outIns(char c)
{
for (int x = screenWidth - 1; x > cursorX; x--)
{
screen[cursorY][x] = screen[cursorY][x - 1];
}
screen[cursorY][cursorX] = c;
MoveCursor((cursorX + 1), cursorY);
}
void outOW(char c)
{
screen[cursorY][cursorX] = c;
MoveCursor((cursorX + 1), cursorY);
}
void PrintTerminal()
{
cout << "Case " << testCase << endl;
cout << scrBoxPart << endl;
for (int y = 0; y < screenHeigth; y++)
{
cout << '|' << screen[y] << '|' << endl;
}
cout << scrBoxPart << endl;
}