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 | 24 | MenuRow::MenuRow(const char * columnName, MenuRowCallback callback) : |
| 25 | 25 | callback(callback), |
| 26 | 26 | stateNameCallback(NULL) { |
| 27 | − int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); | |
| 28 | − strncpy(this->rowName, columnName, length); | |
| 29 | − memcpy(this->rowName + length, " \0", 5); | |
| 27 | + this->nameLength = strlen(columnName); | |
| 28 | + strncpy(this->rowName, columnName, MAX_COLUMN_NAME); // copy the base name | |
| 29 | + strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state | |
| 30 | 30 | rowWidth = 0; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | MenuRow::MenuRow(const char * columnName, MenuRowCallback callback, MenuStateNameCallback stateNameCallback) : |
| 34 | 34 | callback(callback), |
| 35 | 35 | stateNameCallback(stateNameCallback) { |
| 36 | − int length = std::min(MAX_COLUMN_NAME - 5,(int) strlen(columnName)); | |
| 37 | − strncpy(this->rowName, columnName, length); | |
| 38 | − | |
| 39 | − // note: it would be good to also include the initial state | |
| 40 | − memcpy(this->rowName + length, " \0", 5); | |
| 36 | + this->nameLength = strlen(columnName); | |
| 37 | + strncpy(this->rowName, columnName, MAX_COLUMN_NAME); | |
| 38 | + strncpy(this->rowName, this->getName(), MAX_COLUMN_NAME); // now add in state | |
| 41 | 39 | rowWidth = 0; |
| 42 | 40 | } |
| 43 | 41 | |
| @@ -48,32 +46,27 @@ void MenuRow::call() { · 4 unmodified lines | ||
| 48 | 46 | callback(MENU_ROW_PICKED); |
| 49 | 47 | } |
| 50 | 48 | |
| 51 | −char* MenuRow::getName() { | |
| 52 | − int length = (int) strlen(this->rowName) - 4; | |
| 49 | +const char* MenuRow::getStateName() { | |
| 53 | 50 | int currentValue = callback(MENU_ROW_GET_VALUE); |
| 54 | − | |
| 51 | + const char* stateName; | |
| 55 | 52 | // If the MenuRow has a custom stateNameCallback function, then call it to get a string |
| 56 | 53 | // to display in the menu. Otherwise, use the default implementation. |
| 57 | 54 | if (stateNameCallback != NULL) { |
| 58 | − const char* 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 | − | |
| 55 | + stateName = stateNameCallback(currentValue); | |
| 68 | 56 | } else { |
| 69 | 57 | if (currentValue == 0) { |
| 70 | − memcpy(this->rowName + length, " OFF\0", 5); | |
| 58 | + stateName = " OFF "; | |
| 71 | 59 | } else if (currentValue == 1) { |
| 72 | − memcpy(this->rowName + length, " ON \0", 5); | |
| 60 | + stateName = " ON "; | |
| 73 | 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 | 70 | return this->rowName; |
| 78 | 71 | } |
| 79 | 72 | |