auto-sync

This commit is contained in:
rusEfi 2014-10-04 13:02:53 -05:00
parent a629132ef1
commit 19389bc033
7 changed files with 114 additions and 22 deletions

View File

@ -132,6 +132,10 @@ LEElement *LEElementPool::next() {
return &pool[index++];
}
bool isNumeric(const char* line) {
return line[0] >= '0' && line[0] <= '9';
}
const char *processToken(const char *line, char *buffer) {
while (line[0] != 0 && line[0] == ' ') {
line++;
@ -151,3 +155,48 @@ const char *processToken(const char *line, char *buffer) {
return line;
}
le_action_e parseAction(const char * line) {
if (strEqualCaseInsensitive("or", line)) {
return LE_OPERATOR_OR;
} else if (strEqualCaseInsensitive("AND", line)) {
return LE_OPERATOR_AND;
}
return LE_UNDEFINED;
}
static char parsingBuffer[64];
LEElement * parseExpression(LEElementPool *pool, const char * line) {
LEElement *first = NULL;
LEElement *last = NULL;
while (true) {
line = processToken(line, parsingBuffer);
if (line == NULL) {
/**
* No more tokens in this line
*/
return first;
}
LEElement *n = pool->next();
if (isNumeric(parsingBuffer)) {
n->init(LE_NUMERIC_VALUE, atoff(parsingBuffer));
} else {
le_action_e action = parseAction(parsingBuffer);
n->init(action);
}
if (first == NULL) {
first = n;
last = n;
} else {
last->next = n;
last = last->next;
}
}
return first;
}

View File

@ -73,5 +73,8 @@ private:
};
const char *processToken(const char *line, char *buffer);
bool isNumeric(const char* line);
le_action_e parseAction(const char * line);
LEElement * parseExpression(LEElementPool *pool, const char * line);
#endif /* LOGIC_EXPRESSION_H_ */

View File

@ -317,24 +317,6 @@ int tokenLength(const char *msgp) {
return result;
}
bool strEqualCaseInsensitive(const char *str1, const char *str2) {
// todo: actual implementation!
return strEqual(str1, str2);
}
bool strEqual(const char *str1, const char *str2) {
// todo: there must be a standard function?!
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 != len2) {
return false;
}
for (int i = 0; i < len1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
void initConsoleLogic() {
#if EFI_PROD_CODE || EFI_SIMULATOR
initLogging(&logging, "rfi console");

View File

@ -60,8 +60,6 @@ typedef void (*VoidCharPtrCharPtrCharPtr)(const char *, const char *, const char
typedef void (*VoidCharPtrCharPtrCharPtrCharPtrCharPtr)(const char *, const char *, const char *, const char *, const char *);
char *validateSecureLine(char *line);
bool strEqualCaseInsensitive(const char *str1, const char *str2);
bool strEqual(const char *str1, const char *str2);
void resetConsoleActions(void);
void helpCommand(void);
void initConsoleLogic(void);

View File

@ -180,3 +180,30 @@ float atoff(const char *param) {
}
return integerPart + decimal / divider;
}
#define TO_LOWER(x) (((x)>='A' && (x)<='Z') ? (x) - 'A' + 'a' : (x))
bool strEqualCaseInsensitive(const char *str1, const char *str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 != len2) {
return false;
}
for (int i = 0; i < len1; i++)
if (TO_LOWER(str1[i]) != TO_LOWER(str2[i]))
return false;
return true;
}
bool strEqual(const char *str1, const char *str2) {
// todo: there must be a standard function?!
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 != len2) {
return false;
}
for (int i = 0; i < len1; i++)
if (str1[i] != str2[i])
return false;
return true;
}

View File

@ -49,6 +49,9 @@ float maxF(float i1, float i2);
char* itoa10(char *p, int num);
bool isSameF(float v1, float v2);
bool strEqualCaseInsensitive(const char *str1, const char *str2);
bool strEqual(const char *str1, const char *str2);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -12,11 +12,13 @@
#include "logic_expression.h"
#include "cli_registry.h"
void testLogicExpressions(void) {
printf("*************************************************** testLogicExpressions\r\n");
static void testParsing(void) {
char buffer[64];
assertTrue(strEqualCaseInsensitive("hello", "HELlo"));
assertFalse(strEqualCaseInsensitive("hello", "HElo2"));
const char *ptr;
ptr = processToken(" hello ", buffer);
assertTrue(strEqual("hello", buffer));
@ -29,6 +31,34 @@ void testLogicExpressions(void) {
ptr = processToken(ptr, buffer);
assertTrue(strEqual("world", buffer));
assertTrue(isNumeric("123"));
assertFalse(isNumeric("a123"));
LEElementPool pool;
LEElement *element;
element = parseExpression(&pool, "1 3 AND");
assertTrue(element != NULL);
assertEquals(element->action, LE_NUMERIC_VALUE);
assertEquals(element->fValue, 1.0);
element = element->next;
assertEquals(element->action, LE_NUMERIC_VALUE);
assertEquals(element->fValue, 3.0);
element = element->next;
assertEquals(element->action, LE_OPERATOR_AND);
element = element->next;
assertTrue(element == NULL);
}
void testLogicExpressions(void) {
printf("*************************************************** testLogicExpressions\r\n");
testParsing();
LECalculator c;
LEElement value1;