D
Hier eigene Beispielimplementierung die unter debian und windoof funzen. Die Schnittstelle entspricht weitgehend der File Klasse in Java. Einige Methoden sind noch zu implementieren, bzw zu verbessern. Falls jemand bock hat, kann er ja was dazu sagen.
Header sio.h
#ifndef CFILE_H_
#define CFILE_H_
#if defined(__unix__)
#include <dirent.h>
#include <unistd.h>
#define MODUS 0711
#define EXISTS F_OK
#define EXEC X_OK
#define WRITE W_OK
#define READ R_OK
#elif defined(WIN32)
#include <direct.h>
#include <windows.h>
#include <io.h>
#define EXISTS S_IREAD
#define EXEC S_IEXEC
#define WRITE S_IWRITE
#define READ S_IREAD
#endif
#include <sys/stat.h>
#include <string>
#include <vector>
class CFile {
private:
std::string m_strFileName;
std::string m_strParentFileName;
public:
CFile(std::string strFileName, std::string strParentFileName = "");
CFile(const CFile& rclFile);
~CFile();
CFile& operator=(const CFile& rclFile);
bool operator==(const CFile& rclFile) const;
bool operator!=(const CFile& rclFile) const;
bool IsFile() const;
bool IsDirectory() const;
bool IsHidden() const;
long lastModified() const;
bool RenameTo(const CFile& rclFile);
bool SetReadOnly() const;
std::vector<std::string> ListFiles() const;
std::string GetName() const;
std::string GetParent() const;
std::string GetPath() const;
CFile GetParentFile() const;
bool Exists() const;
bool Delete() const;
bool CreateNewFile() const;
bool CreateNewDir() const;
};
#endif /*CFILE_H_*/
Implementierung sio.cpp
#ifdef WIN32
// Disable visual c++ warning C4786
//"... identifier was truncated to '255' character in the debug information
#pragma warning (disable: 4786)
#endif
#include "sio.h"
CFile::CFile(std::string strFileName, std::string strParentFileName) {
m_strFileName = strFileName;
if(strParentFileName == "") {
char buffer[256];
if(getcwd(buffer, sizeof(buffer)) != NULL)
strParentFileName = std::string(buffer);
}
m_strParentFileName = strParentFileName;
}
CFile::CFile(const CFile& rclFile) {
m_strParentFileName = rclFile.m_strParentFileName;
m_strFileName = rclFile.m_strFileName;
}
CFile::~CFile() {
}
CFile& CFile::operator=(const CFile& rclFile) {
if(this != &rclFile) {
m_strParentFileName = rclFile.m_strParentFileName;
m_strFileName = rclFile.m_strFileName;
}
return *this;
}
bool CFile::IsFile() const {
if(!Exists())
return false;
bool bIsFile = false;
struct stat file_status;
if(stat(m_strFileName.c_str(), &file_status) == 0) {
#if defined(__unix__)
if(S_ISREG(file_status.st_mode) || S_ISCHR(file_status.st_mode))
#elif defined(WIN32)
if(file_status.st_mode & S_IFREG || file_status.st_mode & S_IFCHR)
#endif
bIsFile = true;
}
return bIsFile;
}
bool CFile::IsDirectory() const {
if(!Exists())
return false;
bool bIsDir = false;
struct stat file_status;
if(stat(m_strFileName.c_str(), &file_status) == 0) {
#if defined(__unix__)
if(S_ISDIR(file_status.st_mode))
#elif defined(WIN32)
if(file_status.st_mode & S_IFDIR)
#endif
bIsDir = true;
}
return bIsDir;
}
bool CFile::IsHidden() const {
// TODO Implement it
return false;
}
bool CFile::operator==(const CFile& rclFile) const {
// TODO Implement it
return false;
}
bool CFile::operator!=(const CFile& rclFile) const {
// TODO Implement it
return false;
}
std::vector<std::string> CFile::ListFiles() const {
std::vector<std::string> vecList;
if(!IsDirectory() || !Exists())
return vecList;
#if defined(__unix__)
DIR* dir = opendir(m_strFileName.c_str());
struct dirent* entry;
while((entry = readdir(dir)) != NULL)
vecList.push_back(std::string(entry->d_name));
closedir(dir);
#elif defined(WIN32)
WIN32_FIND_DATA dir;
HANDLE fhandle;
char directory[512];
sprintf(directory, "%s\\*.*", m_strFileName.c_str());
if((fhandle = FindFirstFile(directory, &dir)) != INVALID_HANDLE_VALUE) {
do {
vecList.push_back(std::string(dir.cFileName));
} while(FindNextFile(fhandle, &dir));
}
FindClose(fhandle);
#endif
return vecList;
}
std::string CFile::GetName() const {
return m_strFileName;
}
std::string CFile::GetParent() const {
return m_strParentFileName;
}
CFile CFile::GetParentFile() const {
return CFile(m_strParentFileName);
}
bool CFile::Exists() const {
if(access(m_strFileName.c_str(), EXISTS) == -1)
return false;
return true;
}
bool CFile::Delete() const {
if(!Exists())
return false;
if(access(m_strFileName.c_str(), WRITE | READ) == -1)
return false;
if(rmdir(m_strFileName.c_str()) == -1)
return false;
return true;
}
bool CFile::CreateNewFile() const {
if(!Exists()) {
FILE* file = fopen(m_strFileName.c_str(), "w+");
if(file == 0)
return false;
return true;
}
return false;
}
bool CFile::CreateNewDir() const {
if(!Exists()) {
#if defined(__unix__)
if(mkdir(m_strFileName.c_str(), MODUS) != -1)
#elif defined(WIN32)
if(mkdir(m_strFileName.c_str()) != -1)
#endif
return true;
}
return false;
}
Viel Spass
Grüße
Dimitrij