Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C++11

Extending boost::filesystem for Windows and Linux: Part 1

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
19 Mar 2013CPOL16 min read 31.2K   977   33  
Extending boost::filesystem for Windows and Linux.
/************************************************************
 *                                                          *
 *              Written by Stanic Igor 2012/2013            *
 *  Declaration of basic container for file system item     *
 *  informations(F_INFO).                                   *
 *                                                          *
 *                                                          *
 ************************************************************/

#ifndef F_INFO_H
#define F_INFO_H

#include "BaseDecl.h"

#ifdef WINDOWS
#include <Windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shlguid.h>
#include <shlguid.h>
#include <shlwapi.h>
#include <direct.h>
#include <objbase.h>

#elif defined(LINUX)
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <unistd.h>
#include <mntent.h>
#endif

#include <string>
#include <vector>
#include <cstdio>

#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/regex.hpp>

#include "Helpers.h"

#ifdef WINDOWS
#define REGEX boost::wregex
#elif defined(LINUX)
#define REGEX boost::regex
#endif


#ifdef WINDOWS
enum SizeValue
{
    Kb = 0,
    Mb = 1,
    Gb = 2
};
#elif defined(LINUX)
enum class SizeValue : int
{
    Kb = 0,
    Mb = 1,
    Gb = 2
};
#endif

#ifdef WINDOWS
enum _SortInfo
{
    none,
    asc,
    desc,
    asc_size,
    desc_size,
    dirFirst_asc,
    dirFirst_desc,
    dirFirst_asc_size,
    dirFirst_desc_size
};
#elif defined(LINUX)
enum class _SortInfo
{
    none,
    asc,
    desc,
    asc_size,
    desc_size,
    dirFirst_asc,
    dirFirst_desc,
    dirFirst_asc_size,
    dirFirst_desc_size
};
#endif



#ifdef WINDOWS

inline void GetFileAttributesWindows(const std::wstring& path, WIN32_FILE_ATTRIBUTE_DATA& fileAttrData) throw(DWORD)
{
    //std::wstring stemp = std::wstring(path.begin(), path.end());
    LPCWSTR sw = path.c_str();//stemp.c_str();

    if(GetFileAttributesExW(sw, GetFileExInfoStandard, &fileAttrData) != TRUE)
    {
        throw GetLastError();
    }
}

struct F_INFO_WINDOWS
{
private:
    bool m_permission_denied;
public:

    template<typename T1, typename T2>
    F_INFO_WINDOWS(T1&& name, T1&& path, ULLONG len, bool isDir, T2&& time, bool isSymLink = false)
        :name(std::forward<T1>(name)),
        path(std::forward<T1>(path)),
        length(len),
        isDirectory(isDir),
        time(std::forward<T2>(time)),
        isSymLink(isSymLink),
        m_fileAttrData()
    {
        m_permission_denied = false;
        try
        {
            if(exists(boost::filesystem::path (this->path)))
            {
                //m_fileAttrData = {0};
                GetFileAttributesWindows(this->path, this->m_fileAttrData);
            }
        }
        catch(DWORD err)
        {
            if(err == EACCES || err == EPERM)
                m_permission_denied = true;
        }
        catch(...)
        {}
    }

    //F_INFO_WINDOWS(const STRING_TYPE& name, const STRING_TYPE& path, ULLONG len, bool isDir, boost::posix_time::ptime time);
    F_INFO_WINDOWS(const F_INFO_WINDOWS& other);
    F_INFO_WINDOWS(F_INFO_WINDOWS&& other);
    F_INFO_WINDOWS& operator=(F_INFO_WINDOWS);

    STRING_TYPE name;
    STRING_TYPE path;
    ULLONG length;
    bool isDirectory;
    boost::posix_time::ptime time;
    bool isSymLink;

    bool isArchive(void) const;
    bool isCompressed(void) const;
    bool isEncrypted(void) const;
    bool isHidden(void) const;
    bool isNormal(void) const;
    bool isOffline(void) const;
    bool isReadOnly(void) const;
    bool isSystem(void) const;
    bool isTemporary(void) const;
    bool permission_denied(void) const;

    WIN32_FILE_ATTRIBUTE_DATA m_fileAttrData;
    inline bool checkAttribute(DWORD) const;
};
typedef F_INFO_WINDOWS F_INFO;

#elif defined(LINUX)

typedef struct stat statistics;
typedef struct stat64 statistics64;

struct F_INFO_LINUX
{
private:
    bool m_permission_denied;
public:
    //F_INFO_LINUX(const STRING_TYPE& name, const STRING_TYPE& path, ULLONG len, bool isDir, boost::posix_time::ptime time);
    template <typename T1, typename T2> //PERFECT FORWARDING C-TOR
    F_INFO_LINUX(T1&& name, T1&& path, ULLONG len, bool isDir, T2&& time, bool issymlink = false, T1&& symLinkPath = EMPTYSTR)
        :name(std::forward<T1>(name)),
        path(std::forward<T1>(path)),
        length(len),
        isDirectory(isDir),
        isSymLink(issymlink),
        time(std::forward<T2>(time)),
        sym_Link_Path(forward<T1>(symLinkPath)),
        file_statistics(),
        orgTrashPath(EMPTYSTR)
    {
        m_permission_denied =  EACCES == stat(path.c_str(), &file_statistics);
        /*
        if(time.is_not_a_date_time())
        {
            auto _time = file_statistics.st_mtime;
            time = Helpers::LocalFromUTC(_time);
            //time = boost::posix_time::from_time_t(_time);
        }*/
    }

    F_INFO_LINUX(const F_INFO_LINUX& other);
    F_INFO_LINUX(F_INFO_LINUX&& other);
    F_INFO_LINUX& operator=(const F_INFO_LINUX&);
    F_INFO_LINUX& operator=(F_INFO_LINUX&&);

    STRING_TYPE name;
    STRING_TYPE path;
    ULLONG length;
    bool isDirectory;
    bool isSymLink;
    boost::posix_time::ptime time;
    STRING_TYPE sym_Link_Path;
    statistics file_statistics; // not authomatically typedef'd to name since there is stat() f-ction also present!!!
    STRING_TYPE orgTrashPath;

    inline bool isHidden( void ) const
    {
        return *name.begin() == '.';
    }
    inline bool permission_denied() const
    {
        return m_permission_denied;
    }
};
typedef F_INFO_LINUX F_INFO;
#endif

typedef std::vector<F_INFO>* fInfosPtr;


#endif // F_INFO_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Serbia Serbia
Software developer with couple of years of experience mostly with .NET programming and MS SQL databases currently interested in expanding knowledge to C++ and other operating systems.

Comments and Discussions