Skip to content

Commit

Permalink
change tools to use special-folder in rsutils::os
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Nov 20, 2023
1 parent 5b37950 commit 7e74b88
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 120 deletions.
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

0 comments on commit 7e74b88

Please sign in to comment.