mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Modernize various files
* range-based for loops * emplace_back instead of push_back * code style * C++ headers instead of C headers * Default operators * empty stl function
This commit is contained in:
parent
13e995b811
commit
55ab4264dc
12 changed files with 100 additions and 118 deletions
|
@ -20,9 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "filesys.h"
|
||||
#include "util/string.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
#include <fstream>
|
||||
#include "log.h"
|
||||
#include "config.h"
|
||||
|
@ -246,7 +246,7 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
|||
If so, try stat().
|
||||
*/
|
||||
if(isdir == -1) {
|
||||
struct stat statbuf;
|
||||
struct stat statbuf{};
|
||||
if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
|
||||
continue;
|
||||
isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
|
||||
|
@ -262,22 +262,20 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
|||
bool CreateDir(const std::string &path)
|
||||
{
|
||||
int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
if(r == 0)
|
||||
{
|
||||
if (r == 0) {
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If already exists, return true
|
||||
if(errno == EEXIST)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// If already exists, return true
|
||||
if (errno == EEXIST)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
bool PathExists(const std::string &path)
|
||||
{
|
||||
struct stat st;
|
||||
struct stat st{};
|
||||
return (stat(path.c_str(),&st) == 0);
|
||||
}
|
||||
|
||||
|
@ -288,7 +286,7 @@ bool IsPathAbsolute(const std::string &path)
|
|||
|
||||
bool IsDir(const std::string &path)
|
||||
{
|
||||
struct stat statbuf;
|
||||
struct stat statbuf{};
|
||||
if(stat(path.c_str(), &statbuf))
|
||||
return false; // Actually error; but certainly not a directory
|
||||
return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
|
||||
|
@ -347,19 +345,19 @@ bool RecursiveDelete(const std::string &path)
|
|||
|
||||
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
|
||||
{
|
||||
if(IsDir(path)){
|
||||
if (IsDir(path)) {
|
||||
bool did = (rmdir(path.c_str()) == 0);
|
||||
if(!did)
|
||||
errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
|
||||
<<std::endl;
|
||||
return did;
|
||||
} else {
|
||||
bool did = (unlink(path.c_str()) == 0);
|
||||
if(!did)
|
||||
errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
|
||||
<<std::endl;
|
||||
if (!did)
|
||||
errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
|
||||
<< std::endl;
|
||||
return did;
|
||||
}
|
||||
|
||||
bool did = (unlink(path.c_str()) == 0);
|
||||
if (!did)
|
||||
errorstream << "unlink errno: " << errno << ": " << strerror(errno)
|
||||
<< std::endl;
|
||||
return did;
|
||||
}
|
||||
|
||||
std::string TempPath()
|
||||
|
@ -385,8 +383,7 @@ std::string TempPath()
|
|||
void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst)
|
||||
{
|
||||
std::vector<DirListNode> content = GetDirListing(path);
|
||||
for(unsigned int i=0; i<content.size(); i++){
|
||||
const DirListNode &n = content[i];
|
||||
for (const auto &n : content) {
|
||||
std::string fullpath = path + DIR_DELIM + n.name;
|
||||
dst.push_back(fullpath);
|
||||
if (n.dir) {
|
||||
|
@ -414,15 +411,13 @@ bool RecursiveDeleteContent(const std::string &path)
|
|||
{
|
||||
infostream<<"Removing content of \""<<path<<"\""<<std::endl;
|
||||
std::vector<DirListNode> list = GetDirListing(path);
|
||||
for(unsigned int i=0; i<list.size(); i++)
|
||||
{
|
||||
if(trim(list[i].name) == "." || trim(list[i].name) == "..")
|
||||
for (const DirListNode &dln : list) {
|
||||
if(trim(dln.name) == "." || trim(dln.name) == "..")
|
||||
continue;
|
||||
std::string childpath = path + DIR_DELIM + list[i].name;
|
||||
std::string childpath = path + DIR_DELIM + dln.name;
|
||||
bool r = RecursiveDelete(childpath);
|
||||
if(r == false)
|
||||
{
|
||||
errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
|
||||
if(!r) {
|
||||
errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -510,10 +505,10 @@ bool CopyDir(const std::string &source, const std::string &target)
|
|||
bool retval = true;
|
||||
std::vector<DirListNode> content = fs::GetDirListing(source);
|
||||
|
||||
for(unsigned int i=0; i < content.size(); i++){
|
||||
std::string sourcechild = source + DIR_DELIM + content[i].name;
|
||||
std::string targetchild = target + DIR_DELIM + content[i].name;
|
||||
if(content[i].dir){
|
||||
for (const auto &dln : content) {
|
||||
std::string sourcechild = source + DIR_DELIM + dln.name;
|
||||
std::string targetchild = target + DIR_DELIM + dln.name;
|
||||
if(dln.dir){
|
||||
if(!fs::CopyDir(sourcechild, targetchild)){
|
||||
retval = false;
|
||||
}
|
||||
|
@ -526,9 +521,8 @@ bool CopyDir(const std::string &source, const std::string &target)
|
|||
}
|
||||
return retval;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PathStartsWith(const std::string &path, const std::string &prefix)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue