getstr.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 201 行

C
201
字号
/*++
Copyright (c) 1997  Microsoft Corporation

Module Name:

    getstr.c

Abstract:

    This program contains the FwGetString function

Author:

    David M. Robinson (davidro) 25-Oct-1991

Revision History:

    Jun Liu 8/6/96 Modified for Puzzle
    John Cooper (johncoop) 98-Oct-20 Added command line recall

--*/

#include <windows.h>
#include <bootldr.h>
#include "fwp.h"


CHAR FwGetChar(void);
void FwPutChar(CHAR c);

GETSTRING_ACTION
FwGetString(
    OUT PCHAR lineBuf,
    IN ULONG bufSize,
    IN PCHAR InitialString OPTIONAL
    )

/*++

Routine Description:

    This routine reads a string from standardin until a carriage return is
    found, StringLength is reached, or ESC is pushed.

Arguments:

    lineBuf - Supplies a pointer to a location where the string is to be stored.

    bufSize - Supplies the Max Length to read.

    InitialString - Supplies an optional initial string.

Return Value:

    If the string was successfully obtained GetStringSuccess is return,
    otherwise one of the following codes is returned.

    GetStringMaximum - bufSize was reached.


--*/

{

    CHAR InData;
    ULONG i;
    ULONG count = 0;
    ULONG Cursor = 0;
    static CHAR LineRecall[10*80];
    PCHAR RecallPtr;
    static ULONG LineNumber = 0;
    ULONG Recall = LineNumber;

    //
    // If an initial string was supplied, update the output string.
    //
    if (ARGUMENT_PRESENT(InitialString)) {
        strcpy(lineBuf, InitialString);
        count = (ULONG)strlen(lineBuf);
    } else {
        *lineBuf = '\0';
    }

    //
    // Check if enough line buffer size
    //
    if (bufSize == 1) return GetStringMaximum;

    do {

        do {
            InData = FwGetChar();
        } while ( InData >= 0x7f );

        switch ( InData ) {

            case ASC_ESC:                  // Ansi excape sequence
                if ((InData = FwGetChar()) == '[') {
                    InData = FwGetChar();

                    if (InData == 'D') {   // left arrow
                        if (Cursor) {
                            FwPutChar(ASC_BS);
                            Cursor--;
                        }
                        break;
                    }
                    if (InData == 'C') {   // right arrow
                        if (Cursor < count) {
                            FwPutChar(lineBuf[Cursor]);
                            Cursor++;
                        }
                        break;
                    }

                    if (InData == 'A') {   // Up arrow
                        if ((Recall) && ((Recall + 10) > LineNumber)) {
                            Recall--;
                        }
                    }
                    if (InData == 'B') {   // down arrow
                        if (Recall+1 < LineNumber) {
                            Recall++;
                        }
                    }

                    for (i = Cursor; i < count; i++) {
                        FwPutChar(ASC_SP);
                    }
                    for (i = 0; i < count; i++) {
                        FwPutChar(ASC_BS);
                        FwPutChar(ASC_SP);
                        FwPutChar(ASC_BS);
                    }
                    RecallPtr = &LineRecall[80 * (Recall % 10)];
                    count = 0;
                    while (*RecallPtr) {
                        lineBuf[count++] = *RecallPtr;
                        FwPutChar(*RecallPtr);
                        RecallPtr++;
                    }
                    Cursor = count;
                    break;
                }

            case ASC_BS:                   // Back a character
                if ( Cursor ) {
                    FwPutChar(ASC_BS);

                    for (i = Cursor; i < count; i++) {
                        FwPutChar(lineBuf[i]);
                        lineBuf[i-1] = lineBuf[i];
                    }
                    FwPutChar(ASC_SP);
                    Cursor--;
                    for (i = Cursor; i < count; i++) {
                        FwPutChar(ASC_BS);
                    }
                    count--;
                }
                break;

            default:
            if ( InData >= ASC_SP ) {
                FwPutChar(InData);
                for (i = Cursor; i < count; i++) {
                    FwPutChar(lineBuf[i]);
                }
                for (i = count; i > Cursor; i--) {
                    lineBuf[i] = lineBuf[i-1];
                    FwPutChar(ASC_BS);
                }
                lineBuf[Cursor] = InData;
                count++;
                Cursor++;
                if ( count >= bufSize-1 ) {
                    lineBuf[bufSize-1] = '\0';
                    return GetStringMaximum;
                }
            }
        }


    } while ( InData != ASC_CR );

    lineBuf[count] = '\0';

    if (count) {
        RecallPtr = &LineRecall[80 * (LineNumber % 10)];
        for (i = 0; i < 79; i++) {
            RecallPtr[i] = lineBuf[i];
        }
        RecallPtr[i] = '\0';
        LineNumber++;
    }

    return GetStringSuccess;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?