fix: Custom stick and switch names were being mangled internally (#1770)
* followup to PR1754 * Cleanup * Formatting * Long comment
This commit is contained in:
@@ -91,6 +91,10 @@
|
|||||||
{ "EVT_"#xxx"_LONG", EVT_KEY_LONG(yyy) }, \
|
{ "EVT_"#xxx"_LONG", EVT_KEY_LONG(yyy) }, \
|
||||||
{ "EVT_"#xxx"_REPT", EVT_KEY_REPT(yyy) }
|
{ "EVT_"#xxx"_REPT", EVT_KEY_REPT(yyy) }
|
||||||
|
|
||||||
|
// see strhelpers.cpp for pre-instantiation of function-template
|
||||||
|
// getSourceString() for this parametrization
|
||||||
|
static constexpr uint8_t maxSourceNameLength{16};
|
||||||
|
|
||||||
// Note:
|
// Note:
|
||||||
// - luaRxFifo & luaReceiveData are used only for USB serial
|
// - luaRxFifo & luaReceiveData are used only for USB serial
|
||||||
// - otherwise, the AUX serial buffer is used directly
|
// - otherwise, the AUX serial buffer is used directly
|
||||||
@@ -2301,16 +2305,17 @@ This function is rather time consuming, and should not be used repeatedly in a s
|
|||||||
@status current Introduced in 2.6
|
@status current Introduced in 2.6
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int luaGetSourceIndex(lua_State * L)
|
static int luaGetSourceIndex(lua_State* const L)
|
||||||
{
|
{
|
||||||
const char * name = luaL_checkstring(L, 1);
|
const char* const name = luaL_checkstring(L, 1);
|
||||||
bool found = false;
|
bool found = false;
|
||||||
mixsrc_t idx;
|
mixsrc_t idx;
|
||||||
|
|
||||||
for (idx = MIXSRC_NONE; idx <= MIXSRC_LAST_TELEM; idx++) {
|
for (idx = MIXSRC_NONE; idx <= MIXSRC_LAST_TELEM; idx++) {
|
||||||
if (isSourceAvailable(idx)) {
|
if (isSourceAvailable(idx)) {
|
||||||
char* s = getSourceString(idx);
|
char srcName[maxSourceNameLength];
|
||||||
if (!strncasecmp(s, name, 31)) {
|
getSourceString(srcName, idx);
|
||||||
|
if (!strncasecmp(srcName, name)) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -2341,11 +2346,12 @@ static int luaGetSourceName(lua_State * L)
|
|||||||
{
|
{
|
||||||
mixsrc_t idx = luaL_checkinteger(L, 1);
|
mixsrc_t idx = luaL_checkinteger(L, 1);
|
||||||
if (idx <= MIXSRC_LAST_TELEM && isSourceAvailable(idx)) {
|
if (idx <= MIXSRC_LAST_TELEM && isSourceAvailable(idx)) {
|
||||||
char* name = getSourceString(idx);
|
char srcName[maxSourceNameLength];
|
||||||
lua_pushstring(L, name);
|
getSourceString(srcName, idx);
|
||||||
}
|
lua_pushstring(L, srcName);
|
||||||
else
|
} else {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2368,9 +2374,10 @@ static int luaNextSource(lua_State * L)
|
|||||||
|
|
||||||
while (++idx <= last) {
|
while (++idx <= last) {
|
||||||
if (isSourceAvailable(idx)) {
|
if (isSourceAvailable(idx)) {
|
||||||
char* name = getSourceString(idx);
|
char srcName[maxSourceNameLength];
|
||||||
|
getSourceString(srcName, idx);
|
||||||
lua_pushinteger(L, idx);
|
lua_pushinteger(L, idx);
|
||||||
lua_pushstring(L, name);
|
lua_pushstring(L, srcName);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -512,9 +512,8 @@ char *getSwitchPositionName(char *dest, swsrc_t idx)
|
|||||||
else if (idx <= SWSRC_LAST_MULTIPOS_SWITCH) {
|
else if (idx <= SWSRC_LAST_MULTIPOS_SWITCH) {
|
||||||
div_t swinfo =
|
div_t swinfo =
|
||||||
div(int(idx - SWSRC_FIRST_MULTIPOS_SWITCH), XPOTS_MULTIPOS_COUNT);
|
div(int(idx - SWSRC_FIRST_MULTIPOS_SWITCH), XPOTS_MULTIPOS_COUNT);
|
||||||
char temp[LEN_ANA_NAME + 1];
|
char temp[LEN_ANA_NAME + 2];
|
||||||
getSourceString(temp, MIXSRC_FIRST_POT + swinfo.quot);
|
getSourceString(temp, MIXSRC_FIRST_POT + swinfo.quot);
|
||||||
temp[LEN_ANA_NAME] = '\0';
|
|
||||||
strAppendStringWithIndex(s, temp, swinfo.rem + 1);
|
strAppendStringWithIndex(s, temp, swinfo.rem + 1);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -611,10 +610,8 @@ char *getSourceString(char (&dest)[L], mixsrc_t idx)
|
|||||||
dest[0] = CHAR_POT;
|
dest[0] = CHAR_POT;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
idx -= MIXSRC_Rud;
|
idx -= MIXSRC_Rud;
|
||||||
memcpy(dest + 1, g_eeGeneral.anaNames[idx], L - 1);
|
copyToTerminated(dest, g_eeGeneral.anaNames[idx], offset_t<1>{});
|
||||||
dest[L - 1] = '\0';
|
|
||||||
} else {
|
} else {
|
||||||
idx -= MIXSRC_Rud;
|
idx -= MIXSRC_Rud;
|
||||||
getStringAtIndex(dest, STR_VSRCRAW, idx + 1);
|
getStringAtIndex(dest, STR_VSRCRAW, idx + 1);
|
||||||
@@ -625,8 +622,7 @@ char *getSourceString(char (&dest)[L], mixsrc_t idx)
|
|||||||
} else if (idx <= MIXSRC_LAST_SWITCH) {
|
} else if (idx <= MIXSRC_LAST_SWITCH) {
|
||||||
idx -= MIXSRC_FIRST_SWITCH;
|
idx -= MIXSRC_FIRST_SWITCH;
|
||||||
if (g_eeGeneral.switchNames[idx][0] != '\0') {
|
if (g_eeGeneral.switchNames[idx][0] != '\0') {
|
||||||
strncpy(dest, g_eeGeneral.switchNames[idx], LEN_SWITCH_NAME);
|
copyToTerminated(dest, g_eeGeneral.switchNames[idx], offset_t<1>{});
|
||||||
dest[LEN_SWITCH_NAME] = '\0';
|
|
||||||
} else {
|
} else {
|
||||||
getStringAtIndex(dest, STR_VSRCRAW,
|
getStringAtIndex(dest, STR_VSRCRAW,
|
||||||
idx + MIXSRC_FIRST_SWITCH - MIXSRC_Rud + 1);
|
idx + MIXSRC_FIRST_SWITCH - MIXSRC_Rud + 1);
|
||||||
@@ -647,9 +643,8 @@ char *getSourceString(char (&dest)[L], mixsrc_t idx)
|
|||||||
MAX_GVARS);
|
MAX_GVARS);
|
||||||
} else if (idx <= MIXSRC_LAST_TIMER) {
|
} else if (idx <= MIXSRC_LAST_TIMER) {
|
||||||
if (g_model.timers[idx - MIXSRC_FIRST_TIMER].name[0] != '\0') {
|
if (g_model.timers[idx - MIXSRC_FIRST_TIMER].name[0] != '\0') {
|
||||||
strncpy(dest, g_model.timers[idx - MIXSRC_FIRST_TIMER].name,
|
copyToTerminated(dest, g_model.timers[idx - MIXSRC_FIRST_TIMER].name,
|
||||||
L - 1);
|
offset_t<1>{});
|
||||||
dest[L - 1] = '\0';
|
|
||||||
} else {
|
} else {
|
||||||
getStringAtIndex(dest, STR_VSRCRAW,
|
getStringAtIndex(dest, STR_VSRCRAW,
|
||||||
idx - MIXSRC_Rud + 1 - MAX_LOGICAL_SWITCHES -
|
idx - MIXSRC_Rud + 1 - MAX_LOGICAL_SWITCHES -
|
||||||
|
|||||||
+32
-1
@@ -78,7 +78,7 @@ char *getSwitchPositionName(char *dest, swsrc_t idx);
|
|||||||
char *getSwitchName(char *dest, swsrc_t idx);
|
char *getSwitchName(char *dest, swsrc_t idx);
|
||||||
|
|
||||||
template<size_t L>
|
template<size_t L>
|
||||||
char *getSourceString(char (&dest)[L], mixsrc_t idx);
|
char* getSourceString(char (&dest)[L], mixsrc_t idx);
|
||||||
|
|
||||||
int getRawSwitchIdx(char sw);
|
int getRawSwitchIdx(char sw);
|
||||||
char getRawSwitchFromIdx(int sw);
|
char getRawSwitchFromIdx(int sw);
|
||||||
@@ -113,4 +113,35 @@ template<typename S>
|
|||||||
void clearStruct(S& s) {
|
void clearStruct(S& s) {
|
||||||
memset((void*) &s, 0, sizeof(S));
|
memset((void*) &s, 0, sizeof(S));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <size_t N>
|
||||||
|
using offset_t = std::integral_constant<size_t, N>;
|
||||||
|
|
||||||
|
template <size_t DL, size_t SL, size_t O = 0>
|
||||||
|
void copyToTerminated(char (&dest)[DL], const char (&src)[SL],
|
||||||
|
const offset_t<O> = offset_t<0>{})
|
||||||
|
{
|
||||||
|
// unfortinately std::min() isn't constexpr in C++11
|
||||||
|
// static constexpr size_t len = std::min(DL - O - 1, SL);
|
||||||
|
static constexpr size_t dl{DL - O - 1};
|
||||||
|
static_assert(dl > 0, "wrong sizes or offset");
|
||||||
|
static constexpr size_t len = (dl < SL) ? dl : SL;
|
||||||
|
strncpy(&dest[O], &src[0], len);
|
||||||
|
static_assert((len + O) < DL);
|
||||||
|
dest[len + O] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t L1, size_t L2>
|
||||||
|
int strncasecmp(char (&s1)[L1], const char (&s2)[L2])
|
||||||
|
{
|
||||||
|
static constexpr size_t len = (L1 < L2) ? L1 : L2;
|
||||||
|
return strncasecmp(s1, s2, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t L1>
|
||||||
|
int strncasecmp(char (&s1)[L1], const char *const s2)
|
||||||
|
{
|
||||||
|
return strncasecmp(s1, s2, L1);
|
||||||
|
}
|
||||||
|
|
||||||
#endif // _STRHELPERS_H_
|
#endif // _STRHELPERS_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user