Programming style improvement: following Cppcheck suggestions.

This commit is contained in:
brunoherbelin
2021-04-04 13:13:06 +02:00
parent b4627a1613
commit f443720319
51 changed files with 269 additions and 271 deletions

View File

@@ -55,11 +55,11 @@ long SystemToolkit::memory_usage()
FILE *file = fopen("/proc/self/statm", "r");
if (file) {
unsigned long m = 0;
int ret = 0;
int ret = 0, ret2 = 0;
ret = fscanf (file, "%lu", &m); // virtual mem program size,
ret = fscanf (file, "%lu", &m); // resident set size,
ret2 = fscanf (file, "%lu", &m); // resident set size,
fclose (file);
if (ret>0)
if (ret>0 && ret2>0)
size = (size_t)m * getpagesize();
}
return (long)size;
@@ -102,7 +102,7 @@ string SystemToolkit::byte_to_string(long b)
while(numbytes >= 1024.0 && i != list.end())
{
i++;
++i;
numbytes /= 1024.0;
}
oss << std::fixed << std::setprecision(2) << numbytes << *i;
@@ -119,7 +119,7 @@ string SystemToolkit::bits_to_string(long b)
while(numbytes >= 1000.0 && i != list.end())
{
i++;
++i;
numbytes /= 1000.0;
}
oss << std::fixed << std::setprecision(2) << numbytes << *i;
@@ -189,18 +189,17 @@ string SystemToolkit::extension_filename(const string& filename)
std::string SystemToolkit::home_path()
{
// 1. find home
char *mHomePath;
string homePath;
// try the system user info
// NB: avoids depending on changes of the $HOME env. variable
struct passwd* pwd = getpwuid(getuid());
if (pwd)
mHomePath = pwd->pw_dir;
homePath = std::string(pwd->pw_dir);
else
// try the $HOME environment variable
mHomePath = getenv("HOME");
homePath = std::string(getenv("HOME"));
return string(mHomePath) + PATH_SEP;
return homePath + PATH_SEP;
}
@@ -216,17 +215,16 @@ std::string SystemToolkit::cwd_path()
std::string SystemToolkit::username()
{
// 1. find home
char *user;
string userName;
// try the system user info
struct passwd* pwd = getpwuid(getuid());
if (pwd)
user = pwd->pw_name;
userName = std::string(pwd->pw_name);
else
// try the $USER environment variable
user = getenv("USER");
userName = std::string(getenv("USER"));
return string(user);
return userName;
}
bool SystemToolkit::create_directory(const string& path)
@@ -307,7 +305,7 @@ bool SystemToolkit::file_exists(const string& path)
return access(path.c_str(), R_OK) == 0;
// TODO : WIN32 implementation
// TODO : WIN32 implementation (see tinyfd)
}
@@ -330,18 +328,18 @@ list<string> SystemToolkit::list_directory(const string& path, const string& fil
list<string> ls;
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path.c_str())) != NULL) {
// list all the files and directories within directory
while ((ent = readdir (dir)) != NULL) {
if ( ent->d_type == DT_REG)
{
string filename = string(ent->d_name);
if ( extension_filename(filename) == filter)
ls.push_back( full_filename(path, filename) );
}
}
closedir (dir);
// list all the files and directories within directory
struct dirent *ent;
while ((ent = readdir (dir)) != NULL) {
if ( ent->d_type == DT_REG)
{
string filename = string(ent->d_name);
if ( extension_filename(filename) == filter)
ls.push_back( full_filename(path, filename) );
}
}
closedir (dir);
}
return ls;
@@ -354,11 +352,11 @@ void SystemToolkit::open(const string& url)
#elif defined APPLE
char buf[2048];
sprintf( buf, "open '%s'", url.c_str() );
system( buf );
(void) system( buf );
#else
char buf[2048];
sprintf( buf, "xdg-open '%s'", url.c_str() );
int r = system( buf );
(void) system( buf );
#endif
}
@@ -367,9 +365,9 @@ void SystemToolkit::execute(const string& command)
#ifdef WIN32
ShellExecuteA( nullptr, nullptr, url.c_str(), nullptr, nullptr, 0 );
#elif defined APPLE
int r = system( command.c_str() );
(void) system( command.c_str() );
#else
int r = system( command.c_str() );
(void) system( command.c_str() );
#endif
}
// example :
@@ -388,15 +386,15 @@ std::string SystemToolkit::transliterate(std::string input)
icu::Transliterator *firstTrans = icu::Transliterator::createInstance(
"any-NFKD ; [:Nonspacing Mark:] Remove; NFKC; Latin", UTRANS_FORWARD, status);
firstTrans->transliterate(ucs);
delete firstTrans;
icu::Transliterator *secondTrans = icu::Transliterator::createInstance(
"any-NFKD ; [:Nonspacing Mark:] Remove; [@!#$*%~] Remove; NFKC", UTRANS_FORWARD, status);
secondTrans->transliterate(ucs);
delete secondTrans;
delete firstTrans;
std::ostringstream output;
output << ucs;
return output.str();
}