Use c++17 filesystem as alternative to dirent
parent
1d71b081d2
commit
a6135beeef
|
@ -3,7 +3,7 @@
|
|||
#
|
||||
cmake_minimum_required (VERSION 3.8)
|
||||
|
||||
set (CMAKE_CXX_STANDARD 11)
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
|
||||
set(sources
|
||||
PixelAttributes.cpp
|
||||
|
|
|
@ -1,25 +1,36 @@
|
|||
/*
|
||||
* =====================================================================
|
||||
* Version: 1.0
|
||||
* Created: 01.09.2012 14:38:05
|
||||
* Author: Miroslav Bendík
|
||||
* Company: LinuxOS.sk
|
||||
* =====================================================================
|
||||
*/
|
||||
#if __cplusplus >= 201703L || _MSVC_LANG >= 201703L
|
||||
#if __has_include(<filesystem>)
|
||||
#define HAVE_FILESYSTEM
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_FILESYSTEM
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
#include <dirent.h>
|
||||
//#include <experimental/filesystem>
|
||||
#endif // HAVE_FILESYSTEM
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "PlayerAttributes.h"
|
||||
|
||||
using namespace std;
|
||||
//namespace fs = std::experimental::filesystem::v1;
|
||||
|
||||
|
||||
PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory)
|
||||
{
|
||||
string playersPath = sourceDirectory + "players";
|
||||
const string playersPath = sourceDirectory + "players";
|
||||
|
||||
#ifdef HAVE_FILESYSTEM
|
||||
for (const auto &dirEntry : fs::directory_iterator(playersPath)) {
|
||||
cout << dirEntry << std::endl;
|
||||
//dirEntry.path().filename();
|
||||
|
||||
extractPlayer(dirEntry.path().string());
|
||||
}
|
||||
#else
|
||||
DIR *dir;
|
||||
dir = opendir(playersPath.c_str());
|
||||
if (dir == NULL) {
|
||||
|
@ -32,63 +43,42 @@ PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory)
|
|||
continue;
|
||||
}
|
||||
|
||||
string path = playersPath + '/' + ent->d_name;
|
||||
const string path = playersPath + '/' + ent->d_name;
|
||||
|
||||
ifstream in;
|
||||
in.open(path.c_str(), ifstream::in);
|
||||
string buffer;
|
||||
string name;
|
||||
string position;
|
||||
while (getline(in, buffer)) {
|
||||
if (buffer.find("name = ") == 0) {
|
||||
name = buffer.substr(7);
|
||||
}
|
||||
else if (buffer.find("position = ") == 0) {
|
||||
position = buffer.substr(12, buffer.length() - 13);
|
||||
}
|
||||
}
|
||||
char comma;
|
||||
Player player;
|
||||
istringstream positionStream(position, istringstream::in);
|
||||
positionStream >> player.x;
|
||||
positionStream >> comma;
|
||||
positionStream >> player.y;
|
||||
positionStream >> comma;
|
||||
positionStream >> player.z;
|
||||
player.name = name;
|
||||
|
||||
m_players.push_back(player);
|
||||
extractPlayer(path);
|
||||
}
|
||||
closedir(dir);
|
||||
//for (auto& dirEntry : fs::directory_iterator(playersPath)) {
|
||||
// cout << dirEntry << std::endl;
|
||||
// //dirEntry.path().filename();
|
||||
#endif // HAVE_FILESYSTEM
|
||||
|
||||
// ifstream in;
|
||||
// in.open(dirEntry.path().string(), ifstream::in);
|
||||
// string buffer;
|
||||
// string name;
|
||||
// string position;
|
||||
// while (getline(in, buffer)) {
|
||||
// if (buffer.find("name = ") == 0) {
|
||||
// name = buffer.substr(7);
|
||||
// }
|
||||
// else if (buffer.find("position = ") == 0) {
|
||||
// position = buffer.substr(12, buffer.length() - 13);
|
||||
// }
|
||||
// }
|
||||
// char comma;
|
||||
// Player player;
|
||||
// istringstream positionStream(position, istringstream::in);
|
||||
// positionStream >> player.x;
|
||||
// positionStream >> comma;
|
||||
// positionStream >> player.y;
|
||||
// positionStream >> comma;
|
||||
// positionStream >> player.z;
|
||||
// player.name = name;
|
||||
|
||||
}
|
||||
|
||||
// m_players.push_back(player);
|
||||
//}
|
||||
void PlayerAttributes::extractPlayer(const std::string &path)
|
||||
{
|
||||
ifstream in;
|
||||
in.open(path, ifstream::in);
|
||||
string buffer;
|
||||
string name;
|
||||
string position;
|
||||
while (getline(in, buffer)) {
|
||||
if (buffer.find("name = ") == 0) {
|
||||
name = buffer.substr(7);
|
||||
}
|
||||
else if (buffer.find("position = ") == 0) {
|
||||
position = buffer.substr(12, buffer.length() - 13);
|
||||
}
|
||||
}
|
||||
char comma;
|
||||
Player player;
|
||||
istringstream positionStream(position, istringstream::in);
|
||||
positionStream >> player.x;
|
||||
positionStream >> comma;
|
||||
positionStream >> player.y;
|
||||
positionStream >> comma;
|
||||
positionStream >> player.z;
|
||||
player.name = name;
|
||||
|
||||
m_players.push_back(player);
|
||||
}
|
||||
|
||||
PlayerAttributes::Players::iterator PlayerAttributes::begin()
|
||||
|
|
|
@ -1,14 +1,4 @@
|
|||
/*
|
||||
* =====================================================================
|
||||
* Version: 1.0
|
||||
* Created: 01.09.2012 14:38:08
|
||||
* Author: Miroslav Bendík
|
||||
* Company: LinuxOS.sk
|
||||
* =====================================================================
|
||||
*/
|
||||
|
||||
#ifndef PLAYERATTRIBUTES_H_D7THWFVV
|
||||
#define PLAYERATTRIBUTES_H_D7THWFVV
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
@ -19,7 +9,7 @@ struct Player
|
|||
double x;
|
||||
double y;
|
||||
double z;
|
||||
}; /* ----- end of struct Player ----- */
|
||||
};
|
||||
|
||||
class PlayerAttributes
|
||||
{
|
||||
|
@ -27,12 +17,12 @@ public:
|
|||
typedef std::list<Player> Players;
|
||||
|
||||
PlayerAttributes(const std::string &sourceDirectory);
|
||||
|
||||
Players::iterator begin();
|
||||
Players::iterator end();
|
||||
|
||||
private:
|
||||
Players m_players;
|
||||
}; /* ----- end of class PlayerAttributes ----- */
|
||||
|
||||
#endif /* end of include guard: PLAYERATTRIBUTES_H_D7THWFVV */
|
||||
void extractPlayer(const std::string &path);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue