I have to generate a string command to configure a device using a microcontroller, hence the need for C-style strings instead of regular std::string
.
Each step requires an enter press or a Y/N + enter answer and I need a line of code for each one. Code example:
#define YES "Y\n"
#define NO "N\n"
#define ENTER "\n"
#define DEFAULT_COMMAND_SIZE 30
static char command[DEFAULT_COMMAND_SIZE];
if (getChangePassword()) { // just a function that returns true if password has to be changed
if (getTelnetPassword() != nullptr) {
std::strcat(command, YES);
std::strcat(command, getTelnetPassword()); // password is a char*, same as command
std::strcat(command, ENTER);
}
} else {
std::strcat(command, NO);
}
Can I somehow reduce the number of repeating LOC?