vendredi 22 décembre 2017

Get the size of the directory having unicode filenames in c++

I want to calculate the size(in bytes) of a directory, here is my algorithm using boost::filesystem

#include <boost/filesystem.hpp>
#include <string>
#include <functional>

using namespace boost::filesystem;
using namespace std;

// this will go through the directories and sub directories and when any
// file is detected it call the 'handler' function with the file path
void iterate_files_in_directory(const path& directory,
                                     function<void(const path&)> handler)
{
    if(exists(directory))
        if(is_directory(directory)){
            directory_iterator itr(directory);
            for(directory_entry& e: itr){
                if (is_regular_file(e.path())){
                    handler(e.path().string());
                }else if (is_directory(e.path())){
                    iterate_files_in_directory(e.path(),handler);
                }
            }
        }
}

uint64_t get_directory_size(const path& directory){
    uint64_t size = 0;
    iterate_files_in_directory(directory,[&size](const path& file){
        size += file_size(file);
    });
    return size;
}

It works fine when directory contains file with simple file names (i.e, without any Unicode character), but when any file with a Unicode character found it throws an exception:

what():

boost::filesystem::file_size: The filename, directory name, or volume label syntax is incorrect

What should I do?

Aucun commentaire:

Enregistrer un commentaire