Hilfe bei eigener getline() Funktion...

partitionist

Erfahrenes Mitglied
Hallo Leute, ich möchte eine eigene getline() Funktion schreiben, d.h. ich muss alles manuell mit getch() machen und auf einzelne Tasten reagieren wie ENTER, LEERTASTE usw.
Soweit so gut, einzelne Zeichen werden am ende zum string angehängt, dabei wird immer die textposition des Cursor hoch oder runtergezählt (BACKSPACE).

Ich habe noch paar Probleme mit meinem Code, z.B. wenn ich BACKSPACE bis zum anfang der zeile alles lösche bekomme ich ein Laufzeitfehler sowie bei der LEERTASTE, außerdem wird der string nicht richtig angezeigt, sondern versetzt.

Schauts euch bitte mal an und hoffentlich könnt ihr mir weiterhelfen:

Code:
#include <windows.h>
#include <string>
#include <iostream>
#include <conio.h>

#define ENTER            13
#define BACKSPACE        8
#define TAB                 9
#define LEERTASTE        32
#define ESCAPE            27
#define STEUERZEICHEN    224
#define PFEIL_LINKS        75
#define PFEIL_RECHTS    77
#define PFEIL_OBEN        72
#define PFEIL_UNTEN        80
#define DELETE            83

using namespace std;


void clearLine(int LineSize)
{
    cout.put( '\r' );

    for( string::size_type i = 0; i < LineSize; ++i )
    {
        cout.put( ' ' );
    }
}

void gotoxy(short x, short y)
{
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hCon, pos);
}

string GetKeyInputString(string Prompt)
{
    int x, y, key, currentPosition, textpos = 0;
    string input;
    int LineSize;

    //Handle für x,y Koordinaten
    HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
    CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info;  

    do
    {
        key = _getch( );
        switch(key)
        {            

        case BACKSPACE:
            if(textpos > 0)
            {                
                input.erase(textpos-1, 1);            
                cout.put(' ');
                                
                GetConsoleScreenBufferInfo( std_output, &console_screen_buffer_info );
                y = console_screen_buffer_info.dwCursorPosition.Y;

                currentPosition = Prompt.length() + textpos;
                gotoxy(currentPosition-1, y); 
                textpos--;
                

            }break;

        case ENTER:        

            if(input.length() != 0)
                input.append("\0");
            break;
            
        case LEERTASTE:
            
            input.insert(textpos, " "); 
            cout << input << flush;

            GetConsoleScreenBufferInfo( std_output, &console_screen_buffer_info );
            y = console_screen_buffer_info.dwCursorPosition.Y;

            currentPosition = Prompt.length() + textpos;
            gotoxy(currentPosition+1, y);  
            textpos++;                           

            break;

        case ESCAPE:

            LineSize = Prompt.length() + input.length();
            clearLine(LineSize);
            cout << '\r' << Prompt << flush;   
            textpos = 0;
            input.clear();
            break;

        case STEUERZEICHEN:

            switch( _getch() )
            {    
                
            case DELETE:
                if(input.length() > 0)
                {                    
                    input.erase(textpos, 1 );
                    
                    GetConsoleScreenBufferInfo( std_output, &console_screen_buffer_info );
                    y = console_screen_buffer_info.dwCursorPosition.Y;

                    currentPosition = Prompt.length() + textpos;
                    gotoxy(currentPosition, y);

                }break;

            }break;

        default:

            string tmp;
            GetConsoleScreenBufferInfo( std_output, &console_screen_buffer_info );
            y = console_screen_buffer_info.dwCursorPosition.Y;

            //An bestimmter Stelle im String wird neues Zeichen eingefügt
            if(textpos < input.length() )
            {        
                tmp = key;
                input.insert(textpos, tmp);
                cout << input << flush;
            }
            
            else
            {                    
                input += key;
                cout.put( key );
            }
            currentPosition = Prompt.length() + textpos;
            gotoxy(currentPosition+1, y);  
            textpos++;
        }
    }while( key != ENTER );

    cout << endl;

    if( input.length() != 0)
    {                
        cout << "Input: [" << input << "] CursorPos: " << textpos << endl << endl; 
        return input;        
    }
    return "";
}

int main()
{    
    bool do_exit = false;
    string Prompt = ":\\>";
    string GetInput;

    do
    {
        cout << Prompt;
        GetInput = GetKeyInputString(Prompt);
        if(GetInput == "exit")
            do_exit = true;        

    }while( !do_exit );


    return 0;
}
 
Zurück