OpenMS
Loading...
Searching...
No Matches
PathUtils.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Chris Bielow $
6// $Authors: Chris Bielow $
7// --------------------------------------------------------------------------
8
9#pragma once
10
11#include <filesystem>
12#include <string>
13
14namespace OpenMS
15{
16 namespace PathUtils
17 {
23 inline std::string basename(const std::string& file)
24 {
25 // Preserve File::basename's unsigned npos + 1 wraparound to zero.
26 return file.substr(file.find_last_of("\\/") + 1);
27 }
28 }
29
38 inline std::filesystem::path to_path(const std::string& s)
39 {
40#ifdef _WIN32
41 try
42 {
43 return std::filesystem::path(std::u8string(reinterpret_cast<const char8_t*>(s.data()), s.size()));
44 }
45 catch (const std::system_error&)
46 {
47 // Not valid UTF-8 (e.g., ANSI-encoded filename from argv); let Windows use the current code page.
48 return std::filesystem::path(s);
49 }
50#else
51 return std::filesystem::path(std::u8string(reinterpret_cast<const char8_t*>(s.data()), s.size()));
52#endif
53 }
54} // namespace OpenMS
std::string basename(const std::string &file)
Return the basename using either slash style on every platform.
Definition PathUtils.h:23
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::filesystem::path to_path(const std::string &s)
Definition PathUtils.h:38