Showing the changes to interface/src/MenuRow.cpp (1 of 2 files). Show all
Unified · Split
| @@ -24,20 +24,18 @@ MenuRow::MenuRow() : · 23 unmodified lines | |||
| 24 | MenuRow::MenuRow(const char * columnName, MenuRowCallback callback) : | 24 | MenuRow::MenuRow(const char * columnName, MenuRowCallback callback) : |
| 25 | callback(callback), | 25 | callback(callback), |
| 26 | stateNameCallback(NULL) { | 26 | stateNameCallback(NULL) { |
| 27 | int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); | 27 | this->nameLength = strlen(columnName); |
| 28 | strncpy(this->rowName, columnName, length); | 28 | strncpy(this->rowName, columnName, MAX_COLUMN_NAME); // copy the base name |
| 29 | memcpy(this->rowName + length, " \0", 5); | 29 | strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state |
| 30 | rowWidth = 0; | 30 | rowWidth = 0; |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | MenuRow::MenuRow(const char * columnName, MenuRowCallback callback, MenuStateNameCallback stateNameCallback) : | 33 | MenuRow::MenuRow(const char * columnName, MenuRowCallback callback, MenuStateNameCallback stateNameCallback) : |
| 34 | callback(callback), | 34 | callback(callback), |
| 35 | stateNameCallback(stateNameCallback) { | 35 | stateNameCallback(stateNameCallback) { |
| 36 | int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); | 36 | this->nameLength = strlen(columnName); |
| 37 | strncpy(this->rowName, columnName, length); | 37 | strncpy(this->rowName, columnName, MAX_COLUMN_NAME); |
| 38 | 38 | strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state | |
| 39 | // note: it would be good to also include the initial state | ||
| 40 | memcpy(this->rowName + length, " \0", 5); | ||
| 41 | rowWidth = 0; | 39 | rowWidth = 0; |
| 42 | } | 40 | } |
| 43 | 41 | ||
| @@ -48,32 +46,27 @@ void MenuRow::call() { · 4 unmodified lines | |||
| 48 | callback(MENU_ROW_PICKED); | 46 | callback(MENU_ROW_PICKED); |
| 49 | } | 47 | } |
| 50 | 48 | ||
| 51 | char* MenuRow::getName() { | 49 | const char* MenuRow::getStateName() { |
| 52 | int length = (int) strlen(this->rowName) - 4; | ||
| 53 | int currentValue = callback(MENU_ROW_GET_VALUE); | 50 | int currentValue = callback(MENU_ROW_GET_VALUE); |
| 54 | 51 | const char* stateName; | |
| 55 | // If the MenuRow has a custom stateNameCallback function, then call it to get a string | 52 | // If the MenuRow has a custom stateNameCallback function, then call it to get a string |
| 56 | // to display in the menu. Otherwise, use the default implementation. | 53 | // to display in the menu. Otherwise, use the default implementation. |
| 57 | if (stateNameCallback != NULL) { | 54 | if (stateNameCallback != NULL) { |
| 58 | const char* stateName = stateNameCallback(currentValue); | 55 | stateName = stateNameCallback(currentValue); |
| 59 | int stateNameLength = strlen(stateName); | ||
| 60 | printf("MenuRow::getName() stateName=%s stateNameLength=%d\n",stateName,stateNameLength); | ||
| 61 | |||
| 62 | // XXXBHG - BUG!!! - only 5 characters?? someplace else hard coded? for some reason, we end up | ||
| 63 | // with memory corruption down the line, if we allow these states to be longer than 5 characters | ||
| 64 | // including the NULL termination. | ||
| 65 | strncpy(this->rowName + length, stateName,4); // only 4 chars | ||
| 66 | memcpy(this->rowName + length + 5, "\0", 0); // null terminate!! | ||
| 67 | |||
| 68 | } else { | 56 | } else { |
| 69 | if (currentValue == 0) { | 57 | if (currentValue == 0) { |
| 70 | memcpy(this->rowName + length, " OFF\0", 5); | 58 | stateName = " OFF "; |
| 71 | } else if (currentValue == 1) { | 59 | } else if (currentValue == 1) { |
| 72 | memcpy(this->rowName + length, " ON \0", 5); | 60 | stateName = " ON "; |
| 73 | } else { | 61 | } else { |
| 74 | memcpy(this->rowName + length, " \0", 5); | 62 | stateName = " "; |
| 75 | } | 63 | } |
| 76 | } | 64 | } |
| 65 | return stateName; | ||
| 66 | } | ||
| 67 | |||
| 68 | char* MenuRow::getName() { | ||
| 69 | strcpy(this->rowName + nameLength, getStateName()); | ||
| 77 | return this->rowName; | 70 | return this->rowName; |
| 78 | } | 71 | } |
| 79 | 72 | ||