Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move special_folder into rsutils #12417

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion common/device-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "imgui-fonts-fontawesome.hpp"
#include "imgui-fonts-monofont.hpp"

#include <rsutils/os/special-folder.h>
#include "os.h"
#include <rsutils/os/os.h>
#include "viewer.h"
Expand Down Expand Up @@ -416,7 +417,7 @@ namespace rs2

refresh_notifications(viewer);

auto path = get_folder_path( special_folder::user_documents );
auto path = rsutils::os::get_special_folder( rsutils::os::special_folder::user_documents );
path += "librealsense2/presets/";
try
{
Expand Down
3 changes: 2 additions & 1 deletion common/fw-update-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "viewer.h"
#include "ux-window.h"

#include <rsutils/os/special-folder.h>
#include "os.h"

#include <rsutils/easylogging/easyloggingpp.h>
Expand Down Expand Up @@ -234,7 +235,7 @@ namespace rs2
// Not all cameras supports this feature
if( !flash.empty() )
{
auto temp = get_folder_path( special_folder::app_data );
auto temp = rsutils::os::get_special_folder( rsutils::os::special_folder::app_data );
temp += serial + "." + get_timestamped_file_name() + ".bin";

{
Expand Down
99 changes: 0 additions & 99 deletions common/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,105 +255,6 @@ Some auxillary functionalities might be affected. Please report this message if
return buffer;
}

std::string get_folder_path(special_folder f)
{
std::string res;
#ifdef _WIN32

if (f == temp_folder)
{
TCHAR buf[MAX_PATH];
if (GetTempPath(MAX_PATH, buf) != 0)
{
char str[1024];
wcstombs(str, buf, 1023);
res = str;
}
}
else
{
GUID folder;
HRESULT hr;
switch (f)
{
case user_desktop: folder = FOLDERID_Desktop;
break;
case user_documents: folder = FOLDERID_Documents;
// The user's Documents folder location may get overridden, as we know OneDrive does in certain circumstances.
// In such cases, the new function, SHGetKnownFolderPath, does not always return the new path, while the deprecated
// function does.
CHAR path[MAX_PATH];
CHECK_HR(SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, 0, path));

res = path;
res += "\\";
return res;
case user_pictures: folder = FOLDERID_Pictures;
break;
case user_videos: folder = FOLDERID_Videos;
break;
case app_data: folder = FOLDERID_RoamingAppData;
break;
default:
throw std::invalid_argument(
std::string("Value of f (") + std::to_string(f) + std::string(") is not supported"));
}

PWSTR folder_path = NULL;
hr = SHGetKnownFolderPath(folder, KF_FLAG_DEFAULT_PATH, NULL, &folder_path);
if (SUCCEEDED(hr))
{
char str[1024];
wcstombs(str, folder_path, 1023);
CoTaskMemFree(folder_path);
res = str;
res += "\\";
}
else
{
throw std::runtime_error("Failed to get requested special folder");
}
}
#endif //_WIN32
#if defined __linux__ || defined __APPLE__
if (f == special_folder::temp_folder)
{
const char* tmp_dir = getenv("TMPDIR");
res = tmp_dir ? tmp_dir : "/tmp/";
}
else
{
const char* home_dir = getenv("HOME");
if (!home_dir)
{
struct passwd* pw = getpwuid(getuid());
home_dir = (pw && pw->pw_dir) ? pw->pw_dir : "";
}
if (home_dir)
{
res = home_dir;
switch (f)
{
case user_desktop: res += "/Desktop/";
break;
case user_documents: res += "/Documents/";
break;
case user_pictures: res += "/Pictures/";
break;
case user_videos: res += "/Videos/";
break;
case app_data: res += "/.";
break;
default:
throw std::invalid_argument(
std::string("Value of f (") + std::to_string(f) + std::string(") is not supported"));
}
}
}
#endif // defined __linux__ || defined __APPLE__
return res;
}

bool ends_with(const std::string& s, const std::string& suffix)
{
auto i = s.rbegin(), j = suffix.rbegin();
Expand Down
11 changes: 0 additions & 11 deletions common/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@ namespace rs2
size_t pixel_width, size_t pixels_height, size_t bytes_per_pixel,
const void* raster_data, size_t stride_bytes);

enum special_folder
{
user_desktop,
user_documents,
user_pictures,
user_videos,
temp_folder,
app_data
};

std::string get_timestamped_file_name();
std::string get_folder_path(special_folder f);

std::string url_encode(const std::string &value);

Expand Down
5 changes: 3 additions & 2 deletions common/rs-config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "rs-config.h"

#include <rsutils/os/special-folder.h>
#include <nlohmann/json.hpp>

#include "model-views.h"
Expand Down Expand Up @@ -88,8 +89,8 @@ void config_file::save(const char* filename)

config_file& config_file::instance()
{
static config_file inst(get_folder_path(rs2::special_folder::app_data)
+ std::string("realsense-config.json"));
static config_file inst( rsutils::os::get_special_folder( rsutils::os::special_folder::app_data )
+ "realsense-config.json" );
return inst;
}

Expand Down
4 changes: 3 additions & 1 deletion common/ux-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <imgui_impl_glfw.h>

#include "device-model.h"

#include <rsutils/os/special-folder.h>
#include "os.h"

// We use STB image to load the splash-screen from memory
Expand Down Expand Up @@ -86,7 +88,7 @@ namespace rs2
std::string path;
try
{
path = get_folder_path(special_folder::user_documents);
path = rsutils::os::get_special_folder( rsutils::os::special_folder::user_documents );
}
catch (const std::exception&)
{
Expand Down
14 changes: 9 additions & 5 deletions common/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#endif
#endif

#include <regex>

#include "viewer.h"
#include "os.h"

Expand All @@ -19,9 +17,13 @@
#include <imgui_internal.h>

#define ARCBALL_CAMERA_IMPLEMENTATION
#include <arcball_camera.h>
#include <third-party/arcball_camera.h>

#include <rsutils/os/special-folder.h>
#include <rsutils/string/trim-newlines.h>
#include "../common/utilities/imgui/wrap.h"
#include <common/utilities/imgui/wrap.h>

#include <regex>

namespace rs2
{
Expand Down Expand Up @@ -764,7 +766,9 @@ namespace rs2

if (create_file)
{
std::string tmp_filename = rsutils::string::from() << get_folder_path(special_folder::app_data) << "/.99-realsense-libusb.rules";
std::string tmp_filename
= rsutils::os::get_special_folder( rsutils::os::special_folder::app_data ) // ~/.
+ "99-realsense-libusb.rules";

std::ofstream out(tmp_filename.c_str());
out << realsense_udev_rules;
Expand Down
32 changes: 32 additions & 0 deletions third-party/rsutils/include/rsutils/os/special-folder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#pragma once

#include <string>


namespace rsutils {
namespace os {


enum class special_folder
{
user_desktop,
user_documents,
user_pictures,
user_videos,
temp_folder,
app_data
};


// Get the path to one of the special folders listed above. These differ based on operating system.
// Meant to be used to append a filename to it: may or may not end with a directory separator slash!
// Throws invalid_argument if an invalid folder was passed in.
// Throws runtime_error on failure.
//
std::string get_special_folder( special_folder );


} // namespace os
} // namespace rsutils
10 changes: 10 additions & 0 deletions third-party/rsutils/py/pyrsutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <rsutils/number/running-average.h>
#include <rsutils/number/stabilized-value.h>
#include <rsutils/os/executable-name.h>
#include <rsutils/os/special-folder.h>


#define NAME pyrsutils
Expand Down Expand Up @@ -141,4 +142,13 @@ PYBIND11_MODULE(NAME, m) {
.def( "clear", &stabilized_value::clear )
.def( "to_string", to_string )
.def( "__str__", to_string );

py::enum_< rsutils::os::special_folder >( m, "special_folder" )
.value( "app_data", rsutils::os::special_folder::app_data )
.value( "temp_folder", rsutils::os::special_folder::temp_folder )
.value( "user_desktop", rsutils::os::special_folder::user_desktop )
.value( "user_documents", rsutils::os::special_folder::user_documents )
.value( "user_pictures", rsutils::os::special_folder::user_pictures )
.value( "user_videos", rsutils::os::special_folder::user_videos );
m.def( "get_special_folder", rsutils::os::get_special_folder );
}
Loading