-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
189 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// Implementation for Yocto/CommonIO | ||
// | ||
|
||
// | ||
// LICENSE: | ||
// | ||
// Copyright (c) 2016 -- 2020 Fabio Pellacini | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
// | ||
|
||
#include "yocto_commonio.h" | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <cstdio> | ||
#include <filesystem> | ||
#include <functional> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <type_traits> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
// ----------------------------------------------------------------------------- | ||
// PATH UTILITIES | ||
// ----------------------------------------------------------------------------- | ||
namespace yocto { | ||
|
||
// Make a path from a utf8 string | ||
static std::filesystem::path make_path(const string& filename) { | ||
return std::filesystem::u8path(filename); | ||
} | ||
|
||
// Normalize path | ||
string normalize_path(const string& filename) { | ||
return make_path(filename).generic_u8string(); | ||
} | ||
|
||
// Get directory name (not including /) | ||
string path_dirname(const string& filename) { | ||
return make_path(filename).parent_path().generic_u8string(); | ||
} | ||
|
||
// Get extension (including .) | ||
string path_extension(const string& filename) { | ||
return make_path(filename).extension().u8string(); | ||
} | ||
|
||
// Get filename without directory. | ||
string path_filename(const string& filename) { | ||
return make_path(filename).filename().u8string(); | ||
} | ||
|
||
// Get filename without directory and extension. | ||
string path_basename(const string& filename) { | ||
return make_path(filename).stem().u8string(); | ||
} | ||
|
||
// Joins paths | ||
string path_join(const string& patha, const string& pathb) { | ||
return (make_path(patha) / make_path(pathb)).generic_u8string(); | ||
} | ||
string path_join( | ||
const string& patha, const string& pathb, const string& pathc) { | ||
return (make_path(patha) / make_path(pathb) / make_path(pathc)) | ||
.generic_u8string(); | ||
} | ||
|
||
// Replaces extensions | ||
string replace_extension(const string& filename, const string& ext) { | ||
return make_path(filename).replace_extension(ext).u8string(); | ||
} | ||
|
||
// Check if a file can be opened for reading. | ||
bool path_exists(const string& filename) { return exists(make_path(filename)); } | ||
|
||
// Check if a file is a directory | ||
bool path_isdir(const string& filename) { | ||
return is_directory(make_path(filename)); | ||
} | ||
|
||
// Check if a file is a file | ||
bool path_isfile(const string& filename) { | ||
return is_regular_file(make_path(filename)); | ||
} | ||
|
||
// List the contents of a directory | ||
vector<string> list_directory(const string& filename) { | ||
auto entries = vector<string>{}; | ||
for (auto entry : std::filesystem::directory_iterator(make_path(filename))) { | ||
entries.push_back(entry.path().generic_u8string()); | ||
} | ||
return entries; | ||
} | ||
|
||
// Create a directory and all missing parent directories if needed | ||
bool make_directory(const string& dirname, string& error) { | ||
if (path_exists(dirname)) return true; | ||
try { | ||
create_directories(make_path(dirname)); | ||
return true; | ||
} catch (...) { | ||
error = dirname + ": cannot create directory"; | ||
return false; | ||
} | ||
} | ||
|
||
} // namespace yocto |
Oops, something went wrong.