Click here to Skip to main content
15,895,283 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 bu Stanic Igor 2012/2013                *
 *              Implementation of helper fuctions               *
 *                                                              *
 *                                                              *
 ***************************************************************/

#include "Helpers.h"
typedef unsigned int UINT;

std::string Helpers::URIEnc(const std::string& source )
{
    static const std::map<char,std::string> reservedURIMappings =
    {PAIR('!', "%21"), PAIR('#', "%23"), PAIR('$', "%24"), PAIR('&', "%26"), /*PAIR('\'', "%27"),*/ PAIR('(', "%28"), PAIR(')', "%29"), PAIR('*', "%2A"), PAIR('+', "%2B"),
     PAIR(',', "%2C"), /*PAIR('/', "%2F"),*/ PAIR(':', "%3A"), PAIR(';', "%3B"), PAIR('=', "%3D"), PAIR('?', "%3F"), PAIR('@', "%40"), PAIR('[', "%5B"), PAIR(']', "%5D")};
    static const std::map<char, std::string> commonCharURIMappings =
    {PAIR(' ', "%20"), PAIR('\"', "%22"),PAIR('%', "%25"),/*PAIR('-', "%2D"),PAIR('.', "%2E"),*/PAIR('<', "%3C"),PAIR('>', "%3E"),
     PAIR('\\', "%5C"),PAIR('^', "%5E"),/*PAIR('_', "%5F"),*/
     PAIR('`', "%60"),PAIR('{', "%7B"),PAIR('|', "%7C"),PAIR('}', "%7D"),PAIR('~', "%7E")};


    std::string result;
    int len = source.length();
    std::stringstream ss;

    for(int i = 0; i < len; ++i)
    {
        //char c = source[i];
        int j = (int)source[i];
        //char seems to be negative number for unstandard chars
        if(j >= 0 && j <= 127)
        {
            char c = (char)j;
            std::map<char,std::string>::const_iterator reservedMapFind = reservedURIMappings.find(c);
            if(reservedMapFind != reservedURIMappings.end())
            {
                ss << (*reservedMapFind).second;
                continue;
            }

            std::map<char,std::string>::const_iterator commonMapFind = commonCharURIMappings.find(c);
            if(commonMapFind != commonCharURIMappings.end())
            {
                ss << (*commonMapFind).second;
                continue;
            }

            ss << c;
        }
        else
        {
            int ii = j;
            if (ii < 0)
                ii = 256 + ii;
            ss << "%";
            ss << hex << ii;
        }
    }
    result = ss.str();

    return result;
}

std::string Helpers::URIDec(const std::string source)
{
    static const std::map<std::string, char> reservedURIMappings =
    {PAIR("%21",'!'), PAIR("%23",'#'), PAIR("%24",'$'), PAIR("%26",'&'), PAIR("%27",'\''), PAIR("%28",'('), PAIR("%29",')'), PAIR("%2A",'*'), PAIR("%2B",'+'),
     PAIR("%2C",','), PAIR("%2F",'/'), PAIR("%3A",':'), PAIR("%3B",';'), PAIR("%3D",'='), PAIR("%3F",'?'), PAIR("%40",'@'), PAIR("%5B",'['), PAIR("%5D",']')};
    static const std::map<std::string, char> commonCharURIMappings =
    {PAIR("%20",' '), PAIR("%22",'\"'),PAIR("%25",'%'),/*PAIR("%2D",'-'),PAIR("%2E",'.'),*/PAIR("%3C",'<'),PAIR("%3E",'>'),
     PAIR("%5C",'\\'),PAIR("%5E",'^'),/*PAIR("%5F",'_'),*/
     PAIR("%60",'`'),PAIR("%7B",'{'),PAIR("%7C",'|'),PAIR("%7D",'}'),PAIR("%7E",'~')};

    std::stringstream ss;
    for(std::string::const_iterator it = source.cbegin(), end = source.cend(); it != end; ++it)
    {
        if(*it == '%')
        {
            std::string temp;
            std::string::const_iterator current = it;
            std::string::const_iterator limit = it + 2;
            do
            {
                temp += *current++;
            }while(current <= limit && limit < source.cend());

            auto iter_map = commonCharURIMappings.find(temp);
            if(iter_map != commonCharURIMappings.end())
            {
                ss << iter_map->second;
                it = limit;
                continue;
            }

            auto iter_map2 = reservedURIMappings.find(temp);
            if(iter_map2 != reservedURIMappings.end())
            {
                ss << iter_map2->second;
                it = limit;
            }

        }
        else
            ss << *it;
    }

    return ss.str();
}

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