switchinit.cpp

The following code example is taken from the book
C++17 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2017
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License


#include <string>
#include <iostream>
#include <filesystem>
namespace std {
    using namespace std::experimental;
}

void checkFilename(const std::string& name)
{
  using namespace std::filesystem;

  switch (path p(name); status(p).type()) {

   case file_type::not_found:
     std::cout << p << " not found\n";
     break;

   case file_type::directory:
     std::cout << p << ":\n";
     for (const auto& entry : std::filesystem::directory_iterator(p)) {
        std::cout << "- " << entry.path() << '\n'; 
     }
     break;

   default:
     std::cout << p << " exists\n";
     break;
  }
}

int main()
{
  for (auto name : {".", "switchinit.cpp", "nofile"} ) {
    checkFilename(name);
    std::cout << '\n';
  }
}