Improve strlen usage

This commit is contained in:
Kp 2013-12-03 23:21:36 +00:00
parent 83843f32be
commit 1d3395cad1
7 changed files with 25 additions and 19 deletions

View file

@ -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);

View file

@ -1420,7 +1420,7 @@ static void checkforgrpext( char * f )
{
int i;
for (i=1; i<strlen(f); i++ )
for (i=1; f[i]; i++ )
{
if (f[i]=='.') return;

View file

@ -35,7 +35,7 @@ static void checkforgamext( char * f )
{
int i;
for (i=1; i<strlen(f); i++ )
for (i=1; f[i]; i++ )
{
if (f[i]=='.') return;

View file

@ -130,8 +130,8 @@ static int credits_handler(window *wind, d_event *event, credits *cr)
{
if (p[1] == ALLOWED_CHAR)
{
int i = 0, len = strlen(p);
for (i = 0; i < len; i++)
int i = 0;
for (i = 0; p[i]; i++)
p[i] = p[i+2];
}
else

View file

@ -361,7 +361,8 @@ int RegisterPlayer()
{
char *p;
if (strlen(*f) > 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';

View file

@ -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);

View file

@ -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);