From 49ed20c8cbe48838af74a7f95c440733e18ceab1 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sun, 30 Oct 2016 12:38:56 -0700 Subject: [PATCH] lint: Use early exit/continue --- src/wgetopt.cpp | 99 +++++++++++++++++++++++++------------------------ src/wutil.cpp | 61 +++++++++++++++--------------- 2 files changed, 81 insertions(+), 79 deletions(-) diff --git a/src/wgetopt.cpp b/src/wgetopt.cpp index 091bceeab..dbb0d4393 100644 --- a/src/wgetopt.cpp +++ b/src/wgetopt.cpp @@ -381,59 +381,62 @@ int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *opts } // Look at and handle the next short option-character. - { - wchar_t c = *nextchar++; - wchar_t *temp = const_cast(my_index(optstring, c)); + wchar_t c = *nextchar++; + wchar_t *temp = const_cast(my_index(optstring, c)); - // Increment `woptind' when we start to process its last character. - if (*nextchar == '\0') ++woptind; + // Increment `woptind' when we start to process its last character. + if (*nextchar == '\0') ++woptind; - if (temp == NULL || c == ':') { - if (wopterr) { - fwprintf(stderr, _(L"%ls: Invalid option -- %lc\n"), argv[0], (wint_t)c); - } - woptopt = c; - - if (*nextchar != '\0') woptind++; - - return '?'; - } - if (temp[1] == ':') { - if (temp[2] == ':') { - // This is an option that accepts an argument optionally. - if (*nextchar != '\0') { - woptarg = nextchar; - woptind++; - } else - woptarg = NULL; - nextchar = NULL; - } else { - // This is an option that requires an argument. - if (*nextchar != '\0') { - woptarg = nextchar; - // If we end this ARGV-element by taking the rest as an arg, we must advance to - // the next element now. - woptind++; - } else if (woptind == argc) { - if (wopterr) { - // 1003.2 specifies the format of this message. - fwprintf(stderr, _(L"%ls: Option requires an argument -- %lc\n"), argv[0], - (wint_t)c); - } - woptopt = c; - if (optstring[0] == ':') - c = ':'; - else - c = '?'; - } else - // We already incremented `woptind' once; increment it again when taking next - // ARGV-elt as argument. - woptarg = argv[woptind++]; - nextchar = NULL; - } + if (temp == NULL || c == ':') { + if (wopterr) { + fwprintf(stderr, _(L"%ls: Invalid option -- %lc\n"), argv[0], (wint_t)c); } + woptopt = c; + + if (*nextchar != '\0') woptind++; + return '?'; + } + + if (temp[1] != ':') { return c; } + + if (temp[2] == ':') { + // This is an option that accepts an argument optionally. + if (*nextchar != '\0') { + woptarg = nextchar; + woptind++; + } else { + woptarg = NULL; + } + nextchar = NULL; + } else { + // This is an option that requires an argument. + if (*nextchar != '\0') { + woptarg = nextchar; + // If we end this ARGV-element by taking the rest as an arg, we must advance to + // the next element now. + woptind++; + } else if (woptind == argc) { + if (wopterr) { + // 1003.2 specifies the format of this message. + fwprintf(stderr, _(L"%ls: Option requires an argument -- %lc\n"), argv[0], + (wint_t)c); + } + woptopt = c; + if (optstring[0] == ':') { + c = ':'; + } else { + c = '?'; + } + } else { + // We already incremented `woptind' once; increment it again when taking next + // ARGV-elt as argument. + woptarg = argv[woptind++]; + } + nextchar = NULL; + } + return c; } int wgetopter_t::wgetopt_long(int argc, wchar_t **argv, const wchar_t *options, diff --git a/src/wutil.cpp b/src/wutil.cpp index b8897de3b..572f8e510 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -50,40 +50,39 @@ bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &ou if (!d) return false; out_name = str2wcstring(d->d_name); - if (out_is_dir) { - // The caller cares if this is a directory, so check. - bool is_dir = false; + if (!out_is_dir) return true; - // We may be able to skip stat, if the readdir can tell us the file type directly. - bool check_with_stat = true; + // The caller cares if this is a directory, so check. + bool is_dir = false; + // We may be able to skip stat, if the readdir can tell us the file type directly. + bool check_with_stat = true; #ifdef HAVE_STRUCT_DIRENT_D_TYPE - if (d->d_type == DT_DIR) { - // Known directory. - is_dir = true; - check_with_stat = false; - } else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN) { - // We want to treat symlinks to directories as directories. Use stat to resolve it. - check_with_stat = true; - } else { - // Regular file. - is_dir = false; - check_with_stat = false; - } -#endif // HAVE_STRUCT_DIRENT_D_TYPE - if (check_with_stat) { - // We couldn't determine the file type from the dirent; check by stat'ing it. - cstring fullpath = wcs2string(dir_path); - fullpath.push_back('/'); - fullpath.append(d->d_name); - struct stat buf; - if (stat(fullpath.c_str(), &buf) != 0) { - is_dir = false; - } else { - is_dir = static_cast(S_ISDIR(buf.st_mode)); - } - } - *out_is_dir = is_dir; + if (d->d_type == DT_DIR) { + // Known directory. + is_dir = true; + check_with_stat = false; + } else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN) { + // We want to treat symlinks to directories as directories. Use stat to resolve it. + check_with_stat = true; + } else { + // Regular file. + is_dir = false; + check_with_stat = false; } +#endif // HAVE_STRUCT_DIRENT_D_TYPE + if (check_with_stat) { + // We couldn't determine the file type from the dirent; check by stat'ing it. + cstring fullpath = wcs2string(dir_path); + fullpath.push_back('/'); + fullpath.append(d->d_name); + struct stat buf; + if (stat(fullpath.c_str(), &buf) != 0) { + is_dir = false; + } else { + is_dir = static_cast(S_ISDIR(buf.st_mode)); + } + } + *out_is_dir = is_dir; return true; }