From 1d3395cad167f88d41839b1e18da44d70b4b9304 Mon Sep 17 00:00:00 2001 From: Kp Date: Tue, 3 Dec 2013 23:21:36 +0000 Subject: [PATCH] Improve strlen usage --- common/ui/menubar.cpp | 2 +- similar/editor/group.cpp | 2 +- similar/editor/kgame.cpp | 2 +- similar/main/credits.cpp | 4 ++-- similar/main/menu.cpp | 28 +++++++++++++++++----------- similar/main/mission.cpp | 4 ++-- similar/main/multi.cpp | 2 +- 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/common/ui/menubar.cpp b/common/ui/menubar.cpp index 0750318d8..9f8b764e1 100644 --- a/common/ui/menubar.cpp +++ b/common/ui/menubar.cpp @@ -827,7 +827,7 @@ void menubar_init( const char * file ) } CommaParse( 4, buf1, buffer ); - if (strlen(buf1)) + if (buf1[0]) { Menu[menu].Item[item].user_function = func_get(buf1, &np); diff --git a/similar/editor/group.cpp b/similar/editor/group.cpp index 41b097794..5097a96f8 100644 --- a/similar/editor/group.cpp +++ b/similar/editor/group.cpp @@ -1420,7 +1420,7 @@ static void checkforgrpext( char * f ) { int i; - for (i=1; i FILENAME_LEN-1 || strlen(*f) < 5) // sorry guys, can only have up to eight chars for the player name + size_t lenf = strlen(*f); + if (lenf > FILENAME_LEN-1 || lenf < 5) // sorry guys, can only have up to eight chars for the player name { NumItems--; continue; @@ -1397,7 +1398,7 @@ static int select_file_handler(listbox *menu, d_event *event, browser *b) rval = newmenu_do( NULL, "Enter drive letter", 1, m, NULL, NULL ); text[1] = '\0'; snprintf(newpath, sizeof(char)*PATH_MAX, "%s:%s", text, sep); - if (!rval && strlen(text)) + if (!rval && text[0]) { select_file_recursive(b->title, newpath, b->ext_list, b->select_dir, b->when_selected, b->userdata); // close old box. @@ -1418,12 +1419,14 @@ static int select_file_handler(listbox *menu, d_event *event, browser *b) { char *p; - if ((p = strstr(&newpath[strlen(newpath) - strlen(sep)], sep))) + size_t len_newpath = strlen(newpath); + size_t len_sep = strlen(sep); + if ((p = strstr(&newpath[len_newpath - len_sep], sep))) if (p != strstr(newpath, sep)) // if this isn't the only separator (i.e. it's not about to look at the root) *p = 0; - p = newpath + strlen(newpath) - 1; - while ((p > newpath) && strncmp(p, sep, strlen(sep))) // make sure full separator string is matched (typically is) + p = newpath + len_newpath - 1; + while ((p > newpath) && strncmp(p, sep, len_sep)) // make sure full separator string is matched (typically is) p--; if (p == strstr(newpath, sep)) // Look at root directory next, if not already @@ -1432,8 +1435,8 @@ static int select_file_handler(listbox *menu, d_event *event, browser *b) if (!d_stricmp(p, "/Volumes")) return 1; #endif - if (p[strlen(sep)] != '\0') - p[strlen(sep)] = '\0'; + if (p[len_sep] != '\0') + p[len_sep] = '\0'; else { #if defined(__MACH__) && defined(__APPLE__) @@ -1451,12 +1454,14 @@ static int select_file_handler(listbox *menu, d_event *event, browser *b) return !(*b->when_selected)(b->userdata, ""); else { - if (strncmp(&newpath[strlen(newpath) - strlen(sep)], sep, strlen(sep))) + size_t len_newpath = strlen(newpath); + size_t len_sep = strlen(sep); + if (strncmp(&newpath[len_newpath - len_sep], sep, len_sep)) { - strncat(newpath, sep, PATH_MAX - 1 - strlen(newpath)); + strncat(newpath, sep, PATH_MAX - 1 - len_newpath); newpath[PATH_MAX - 1] = '\0'; } - strncat(newpath, list[citem], PATH_MAX - 1 - strlen(newpath)); + strncat(newpath, list[citem], PATH_MAX - 1 - len_newpath); newpath[PATH_MAX - 1] = '\0'; } @@ -1545,7 +1550,8 @@ static int select_file_recursive(const char *title, const char *orig_path, const while (!PHYSFS_addToSearchPath(b->view_path, 0)) { - while ((p > b->view_path) && strncmp(p, sep, strlen(sep))) + size_t len_sep = strlen(sep); + while ((p > b->view_path) && strncmp(p, sep, len_sep)) p--; *p = '\0'; diff --git a/similar/main/mission.cpp b/similar/main/mission.cpp index 63b14a3dc..95a5f6350 100644 --- a/similar/main/mission.cpp +++ b/similar/main/mission.cpp @@ -891,7 +891,7 @@ static int load_mission(mle *mission) else if (istok(buf,"briefing")) { if ((v = get_value(buf)) != NULL) { add_term(v); - if (strlen(v) < FILENAME_LEN && strlen(v) > 0) + if (v[0] && strlen(v) < FILENAME_LEN) { char *tmp, *ptr; MALLOC(tmp, char, FILENAME_LEN); @@ -916,7 +916,7 @@ static int load_mission(mle *mission) else if (istok(buf,"ending")) { if ((v = get_value(buf)) != NULL) { add_term(v); - if (strlen(v) < FILENAME_LEN && strlen(v) > 0) + if (v[0] && strlen(v) < FILENAME_LEN) { char *tmp, *ptr; MALLOC(tmp, char, FILENAME_LEN); diff --git a/similar/main/multi.cpp b/similar/main/multi.cpp index 0d5f05ad9..d689b50ca 100644 --- a/similar/main/multi.cpp +++ b/similar/main/multi.cpp @@ -1508,7 +1508,7 @@ multi_send_message_dialog(void) nm_set_item_input(&m[0], MAX_MESSAGE_LEN-1, Network_message); choice = newmenu_do( NULL, TXT_SEND_MESSAGE, 1, m, NULL, NULL ); - if ((choice > -1) && (strlen(Network_message) > 0)) { + if ((choice > -1) && (Network_message[0])) { Network_message_reciever = 100; #if defined(DXX_BUILD_DESCENT_II) HUD_init_message(HM_MULTI, "%s '%s'", TXT_SENDING, Network_message);