diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-01-03 12:29:06 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-01-03 12:33:48 -0800 |
commit | 6f8e9bcff3046dc7abedea731427f7c307ba3258 (patch) | |
tree | 7c4263cd0836eb2a587f6023ba094425e4a89087 /gtk-gui.c | |
parent | 8dbe3528b4ae6ab01d913d41d696c20319004508 (diff) | |
download | subsurface-6f8e9bcff3046dc7abedea731427f7c307ba3258.tar.gz |
Rewrite the nickname replacement function
This function had a couple of bugs. Two different off by one errors and on
top of that it was matching only the deviceid instead of model and
deviceid.
So I simply rewrote it to match against the full pattern and take a much
more straight forward approach to replacing the entry for the divecomputer
under consideration. If the new nickname is entry this implementation
allocates one extra byte - but that didn't seem worth the extra code to
fix it.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'gtk-gui.c')
-rw-r--r-- | gtk-gui.c | 31 |
1 files changed, 10 insertions, 21 deletions
@@ -2190,40 +2190,29 @@ static char *cleanedup_nickname(const char *nickname, int len) void replace_nickname_nicknamestring(const char *model, int deviceid, const char *nickname) { - char buf[11]; - char *entry, *comma1, *comma2, *brace, *new_nn; + char pattern[160]; + char *entry, *brace, *new_nn; int len; if (!nickname) nickname = ""; - snprintf(buf, sizeof(buf), "{%08x,", deviceid); - entry = strstr(nicknamestring, buf); + snprintf(pattern, sizeof(pattern), "{%08x,%s", deviceid, model); + entry = strstr(nicknamestring, pattern); if (!entry) /* this cannot happen as we know we have an entry for this deviceid */ goto bail; + len = strlen(entry); - comma1 = g_utf8_strchr(entry, len, ','); - if (!comma1) - goto bail; - len = strlen(comma1); - comma2 = g_utf8_strchr(comma1, len, ','); - brace = g_utf8_strchr(comma1, len, '}'); + brace = g_utf8_strchr(entry, len, '}'); if (!brace) goto bail; - if (!comma2 || brace < comma2) { - /* didn't have a nickname, so add one */ - len = strlen(nicknamestring) + strlen(nickname) + 2; - *brace = '\0'; - } else { - /* replace the nickname */ - len = strlen(nicknamestring) + strlen(nickname) - (brace - comma2) + 1; - *comma2 = '\0'; - } + *entry = *brace = '\0'; + len = strlen(nicknamestring) + strlen(brace + 1) + strlen(pattern) + strlen(nickname) + 3; new_nn = malloc(len); if (strlen(nickname)) - snprintf(new_nn, len, "%s,%s}%s", nicknamestring, nickname, brace + 1); + snprintf(new_nn, len, "%s%s,%s}%s", nicknamestring, pattern, nickname, brace + 1); else - snprintf(new_nn, len, "%s}%s", nicknamestring, brace + 1); + snprintf(new_nn, len, "%s%s}%s", nicknamestring, pattern, brace + 1); free(nicknamestring); nicknamestring = new_nn; return; |