mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[apps/calculation] Factorize some code.
Change-Id: Id217a1d898c7100172a70cd1fedcf3573c2f68b9
This commit is contained in:
@@ -65,6 +65,27 @@ bool App::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event e
|
||||
return false;
|
||||
}
|
||||
|
||||
bool App::expressionLayoutFieldDidReceiveEvent(::ExpressionLayoutField * expressionLayoutField, Ion::Events::Event event) {
|
||||
if ((event == Ion::Events::Var || event == Ion::Events::XNT) && ExpressionFieldDelegateApp::expressionLayoutFieldDidReceiveEvent(expressionLayoutField, event)) {
|
||||
return true;
|
||||
}
|
||||
if (expressionLayoutField->isEditing() && expressionLayoutField->expressionLayoutFieldShouldFinishEditing(event)) {
|
||||
if (!expressionLayoutField->hasText()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int bufferLength = TextField::maxBufferSize();
|
||||
char bufferForParsing[bufferLength];
|
||||
expressionLayoutField->writeTextInBuffer(bufferForParsing, bufferLength);
|
||||
|
||||
if (!textInputIsCorrect(bufferForParsing)) {
|
||||
displayWarning(I18n::Message::SyntaxError);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool App::textInputIsCorrect(const char * text) {
|
||||
/* Here, we check that the expression entered by the user can be printed with
|
||||
* less than k_printedExpressionLength characters. Otherwise, we prevent the
|
||||
@@ -85,36 +106,6 @@ bool App::textInputIsCorrect(const char * text) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool App::expressionLayoutFieldDidReceiveEvent(::ExpressionLayoutField * expressionLayoutField, Ion::Events::Event event) {
|
||||
if ((event == Ion::Events::Var || event == Ion::Events::XNT) && ExpressionFieldDelegateApp::expressionLayoutFieldDidReceiveEvent(expressionLayoutField, event)) {
|
||||
return true;
|
||||
}
|
||||
/* Here, we check that the expression entered by the user can be printed with
|
||||
* less than k_printedExpressionLength characters. Otherwise, we prevent the
|
||||
* user from adding this expression to the calculation store. */
|
||||
if (!(expressionLayoutField->isEditing() && expressionLayoutField->expressionLayoutFieldShouldFinishEditing(event))) {
|
||||
return false;
|
||||
}
|
||||
int bufferLength = TextField::maxBufferSize();
|
||||
char bufferForParsing[bufferLength];
|
||||
expressionLayoutField->writeTextInBuffer(bufferForParsing, bufferLength);
|
||||
Expression * exp = Expression::parse(bufferForParsing);
|
||||
if (exp == nullptr) {
|
||||
expressionLayoutField->app()->displayWarning(I18n::Message::SyntaxError);
|
||||
return true;
|
||||
}
|
||||
char buffer[Calculation::k_printedExpressionSize];
|
||||
int length = exp->writeTextInBuffer(buffer, sizeof(buffer));
|
||||
delete exp;
|
||||
/* If the buffer is totally full, it is VERY likely that writeTextInBuffer
|
||||
* escaped before printing utterly the expression. */
|
||||
if (length >= Calculation::k_printedExpressionSize-1) {
|
||||
displayWarning(I18n::Message::SyntaxError);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char * App::XNT() {
|
||||
return "x";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user