?? preform.c
字號:
else
putch(' '); // Yes, do not echo a carriage return.
for ( ; num_digits; num_digits--)
putch(BACK_SPACE); // Get the cursor back to where it began.
}
// ****************************************************************************
// To read in a decimal number, the digits must be entered. That is what this
// routine does. The digit input string will be terminated with a carriage return.
// This routine will only accept and echo characters of 0 to 9.
// Assumed the ASCII string of more than MAX_DEC_DIGITS BYTEs (one for the CR).
//
// Input: Pointer to where the digit input string should be placed.
// It is assumed to have at least MAX_DEC_DIGITS+1 BYTES of space.
// The maximum value allowed for this input.
// The minimum value allowed for this input.
// Return: BOOLEAN - TRUE, characters input.
// FALSE, ESE entered to exit.
// Side effects: Length BYTES at the ASCII string might be altered.
static BYTE inp_decimal(BYTE* digits_here, WORD min_value, WORD max_value)
{
WORD cur_value; // Hold the current value being read in.
WORD dig_pos; // The string position (index) of the next character.
WORD num_digts; // Number of digits in the string, Maximum entered.
BYTE ascii_string[MAX_DEC_DIGITS + 2]; // Hold digits being input.
BYTE inp_char; // Last character input.
num_digts = dig_pos = cur_value = 0; // No valid digits input yet.
memset(digits_here, ' ', MAX_DEC_DIGITS); // Fill with spaces.
digits_here[MAX_DEC_DIGITS] = CR; // Be sure it is carriage return terminated.
do
{
if ((inp_char = getch_cont()) EQ ESC) // This echos the character.
return(FALSE); // Exit on ESC. Confused user?
if (inp_char EQ ARROW_HI_BYTE) // It might be a valid control key.
{ // Then, if the next input makes it a valid control continue the do loop.
if ((inp_char = getch_cont()) EQ LEFT_ARROW)
{
if (dig_pos) // Some digits have been input to back up over.
{
putch(BACK_SPACE); // Back up on the screen.
dig_pos = dig_pos - 1; // Backing up over a character.
} else
putch(BELL); // ERROR beep, if can.
} else if (inp_char EQ RIGHT_ARROW)
{
if ((dig_pos < MAX_DEC_DIGITS - 1) AND (dig_pos < num_digts))
putch(digits_here[dig_pos++]); // Print again what is in the buffer.
else
putch(BELL); // ERROR beep, if can.
} else if (inp_char EQ DELETE)
{
if (num_digts AND (num_digts NE dig_pos))
num_digts = num_digts - 1;
del_digit(&digits_here[dig_pos]); // and update the display.
} else
putch(BELL); // ERROR beep, if can.
} else if (inp_char EQ BACK_SPACE)
{
if (dig_pos) // Some digits have been entered to back up over.
{
putch(BACK_SPACE); // Backup.
dig_pos = dig_pos - 1; // New cursor position.
if (num_digts AND (num_digts NE dig_pos))
num_digts = num_digts - 1;
del_digit(&digits_here[dig_pos]); // and update the display.
} else
putch(BELL); // ERROR beep, if can.
} else if (inp_char NE CR) // A carriage return is used to terminate
{ // The CR terminator will be added later.
if (dig_pos < MAX_DEC_DIGITS)
{
if (inp_char >= '0' AND inp_char <= '9')
{ // Valid character input. Put it in string copy and check for
memcpy(ascii_string, digits_here, MAX_DEC_DIGITS + 1);
ascii_string[dig_pos] = inp_char; // Put in buffer for to check it.
if (dec_word_val(ascii_string, &cur_value))
{
if (cur_value <= max_value)
{
putch(inp_char); // Echo what was just input.
digits_here[dig_pos++] = inp_char; // Save in input buffer.
if (dig_pos > num_digts)
num_digts = dig_pos; // Sting getting longer.
} else
putch(BELL); // ERROR beep, if can.
} else // The new digit entered would make an overflow.
putch(BELL); // ERROR beep, if can.
} else
putch(BELL); // ERROR beep, if can.
}
} else if ((cur_value < min_value) AND (num_digts NE 0)) // Invalid ENTER key?
putch(BELL); // ERROR beep, if can.
dec_word_val(digits_here, &cur_value); // Set to current value in string.
if ((inp_char EQ CR) AND (num_digts EQ 0)) // No digits just ENTER.
cur_value = min_value; // force exit.
} while ((inp_char NE CR) OR (cur_value < min_value)); // Until CR and big enough.
digits_here[num_digts] = CR; // Terminate with a carriage return.
return(TRUE);
}
// ****************************************************************************
// To read in a number the digits must be entered. That is what this routine does.
// It actually just reads in a string of bytes input at the keypad. This
// is to support exiting with ESC and use of the back space or left and right
// arrow keys. The digit input string will be terminated with the carriage return.
// It will only accept and echo characters for the base being input for.
// Assumed the ASCII string has points to at least (number_length + 1) data bytes.
// Hexadecimal, decimal, and binary are all supported. Although, (because of
// the Del key) this should only be used for hexadecimal and binary input.
//
// Input: Pointer to where the digit input string should be placed.
// Maximum number of digits to be input for this number.
// The *ASCII string must have one more byte, for the carriage return.
// Base (HEX, DEC,or BIN ) the digits are being input for.
// The character to 'fill' the empty digit string with, before digits.
// Return: BOOLEAN - TRUE, characters input.
// FALSE, ESE entered to exit.
// Side effects: Length BYTES at the ASCII string might be altered.
static BYTE inp_digstring(BYTE* ascii_string, WORD number_length,
enum data_type base, BYTE fill_char)
{
int num_digs; // Number of digits that have been entered.
WORD dig_pos; // The string position (index) of the next character.
BYTE inp_char; // Last character input.
num_digs = dig_pos = 0; // No valid digits input yet.
memset(ascii_string, fill_char, number_length); // Fill with spaces.
ascii_string[number_length] = CR; // Terminate with a carriage return.
do
{
if ((inp_char = getch_cont()) EQ ESC) // This echos the character.
return(FALSE); // Exit on ESC. Confused user?
if (inp_char EQ ARROW_HI_BYTE)
{
if ((inp_char = getch_cont()) EQ LEFT_ARROW)
{
if (dig_pos) // Some digits have been input to back up over.
{
putch(BACK_SPACE); // Back up on the screen.
dig_pos = dig_pos - 1; // Backing up over a character.
} else
putch(BELL); // ERROR beep, if can.
} else if (inp_char EQ RIGHT_ARROW)
{
if (dig_pos < number_length - 1)
putch(ascii_string[dig_pos++]); // Print again what is in the buffer.
else
putch(BELL); // ERROR beep, if can.
} else if (inp_char EQ DELETE)
{
if (dig_pos < number_length) // At the end of the bigest number?
{ // NO, there is something to delete.
if (ascii_string[dig_pos] NE fill_char) // Is a digit being deleted?
num_digs = num_digs - 1; // A digit deleted.
ascii_string[dig_pos] = fill_char; // Last character is gone.
putch(fill_char); // Erase from screen last character.
putch(BACK_SPACE); // Backup over the ' ' just printed.
}
} else
putch(BELL); // ERROR beep, if can.
} else if (inp_char EQ BACK_SPACE)
{
if (dig_pos) // Some digits have been input to back up over.
{
putch(inp_char); // Echo what was just input.
dig_pos = dig_pos - 1; // Backing up over a character.
if (ascii_string[dig_pos] NE fill_char) // Is a digit being deleted?
num_digs = num_digs - 1; // A digit deleted.
ascii_string[dig_pos] = fill_char; // Last character is gone.
putch(fill_char); // Erase from screen last character.
putch(BACK_SPACE); // Backup over the ' ' just printed.
} else
putch(BELL); // ERROR beep, if can.
} else if (inp_char NE CR)
{ // Leave at least one space (' ') to terminate the number.
if (dig_pos < number_length)
{
switch (base)
{
case HEX:
inp_char = toupper(inp_char); // Convert to upper case.
if ((inp_char >= '0' AND inp_char <= '9') OR
(inp_char >= 'A' AND inp_char <= 'F'))
{
putch(inp_char); // Echo what was just input.
ascii_string[dig_pos++] = inp_char; // Save in input buffer.
num_digs = num_digs + 1; // Count another digit entered.
} else
putch(BELL); // ERROR beep, if can.
break;
case DEC:
if (inp_char >= '0' AND inp_char <= '9')
{
putch(inp_char); // Echo what was just input.
ascii_string[dig_pos++] = inp_char; // Save in input buffer.
num_digs = num_digs + 1; // Count another digit entered.
} else
putch(BELL); // ERROR beep, if can.
break;
case BIN:
if (inp_char >= '0' AND inp_char <= '1')
{
putch(inp_char); // Echo what was just input.
ascii_string[dig_pos++] = inp_char; // Save in input buffer.
num_digs = num_digs + 1; // Count another digit entered.
} else
putch(BELL); // ERROR beep, if can.
break;
default:
return(FALSE); // ERROR! this should never happen
}
}
} else if (base EQ BIN) // It is a CR, but Binary must input all digits.
if ((NOT eight_binary_digits(ascii_string)) AND // If all binary digitse
(num_digs NE 0)) // And some digits have been entered.
{
inp_char = 0; // Not all a 1 or 0, then not done. Ignore CR.
putch(BELL); // ERROR beep, if can.
}
} while (inp_char NE CR);
if (num_digs <= 0)
ascii_string[0] = CR; // ENTER key with no digits.
printf("\n");
return(TRUE);
}
// ****************************************************************************
// Input a decimal number, character by character.
// sscanf() would be used, but that waits for a CR.
// ESC must be detected, to exit.
// ENTER alone should also be recognized as a special case.
// As a result, inp_num() is returning an unsigned number that will have a value
// that will fit in a signed number. ALL valid numbers (not a flag)
// will have the sign bit cleared. Flag values, for things like ESC or ENTER,
// will have the sign bit set (0x8000).
//
// Input: The maximum value allowed for this input.
// Return: Number input, TOO_BIG_VAL if ESC was entered to exit,
// or ENTER_ALONE if no digits entered, but the ENTER key was pressed.
// Side effects: NONE.
WORD inp_num(WORD max_value)
{
BYTE ascii_number[MAX_DEC_DIGITS + 2]; // Hold digits being input.
WORD this_value; // Build the value being input here.
this_value = 0; // Initialize working variables.
if (NOT inp_decimal(ascii_number, 0, max_value))
return(TOO_BIG_VAL); // Use is trying to ESCape out.
printf("\n"); // Echo the terminating carriage return.
if (ascii_number[0] EQ CR) // No digits, only the ENTER key.
return(ENTER_ALONE); // Thus, done with entering data.
if (dec_word_val(ascii_number, &this_value))
return(this_value); // Valid decimal number entered. Return it!
return(0); // Should never get here, but be safe.
}
// ****************************************************************************
// Input a hexadecimal number, character by character. sscanf() would be used,
// but that waits for a CR. ESC must be detected, to exit.
//
// Input: NONE.
// Return: Number input, TOO_BIG_VAL if ESC was entered to exit,.
// or ENTER_ALONE if no digits entered, but the ENTER key was pressed.
// Side effects: NONE.
WORD inp_hex(void)
{
WORD incom_value; // Build the value being input here.
BYTE ascii_number[MAX_HEX_DIG + 2]; // Hold digits being input and a CR.
BYTE* digit_ptr; // Point to where the next digit goes.
incom_value = 0;
if (NOT inp_digstring(ascii_number, MAX_HEX_DIG, HEX, ' '))
return(TOO_BIG_VAL); // Use is trying to ESCAPE out.
digit_ptr = ascii_number; // Point at the character just input.
if (*digit_ptr EQ CR) // No digits, only the ENTER key.
return(ENTER_ALONE); // Return to caller for a try again, or default.
while (*digit_ptr NE CR) // Loop until done and time to returns.
{
if ((*digit_ptr >= '0') AND (*digit_ptr <= '9')) // Another digit entered?
incom_value = (incom_value << 4) + *digit_ptr - '0'; // Add in new value.
else if ((*digit_ptr >= 'A') AND (*digit_ptr <= 'F')) // Hex dig?
incom_value = (incom_value << 4) + *digit_ptr - 'A' + 0x0A; // Update value.
digit_ptr++; // Count another valid digit entered.
}
return(incom_value); // Done imputing, return the input value.
}
// ****************************************************************************
// Input a binary number, character by character. sscanf() would be used,
// but that waits for a CR. ESC must be detected, to exit.
//
// Input: NONE.
// Return: Number input, TOO_BIG_VAL if ESC was entered to exit,
// or ENTER_ALONE if no digits entered, but the ENTER key was pressed.
// Side effects: NONE.
WORD inp_bin(void)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -