Skip to content

Commit 11878e6

Browse files
authored
PR #12417 from Eran: move special_folder into rsutils
2 parents 5105b4d + 7e74b88 commit 11878e6

File tree

10 files changed

+203
-120
lines changed

10 files changed

+203
-120
lines changed

common/device-model.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "imgui-fonts-fontawesome.hpp"
1414
#include "imgui-fonts-monofont.hpp"
1515

16+
#include <rsutils/os/special-folder.h>
1617
#include "os.h"
1718
#include <rsutils/os/os.h>
1819
#include "viewer.h"
@@ -416,7 +417,7 @@ namespace rs2
416417

417418
refresh_notifications(viewer);
418419

419-
auto path = get_folder_path( special_folder::user_documents );
420+
auto path = rsutils::os::get_special_folder( rsutils::os::special_folder::user_documents );
420421
path += "librealsense2/presets/";
421422
try
422423
{

common/fw-update-helper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "viewer.h"
77
#include "ux-window.h"
88

9+
#include <rsutils/os/special-folder.h>
910
#include "os.h"
1011

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

240241
{

common/os.cpp

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -255,105 +255,6 @@ Some auxillary functionalities might be affected. Please report this message if
255255
return buffer;
256256
}
257257
258-
std::string get_folder_path(special_folder f)
259-
{
260-
std::string res;
261-
#ifdef _WIN32
262-
263-
if (f == temp_folder)
264-
{
265-
TCHAR buf[MAX_PATH];
266-
if (GetTempPath(MAX_PATH, buf) != 0)
267-
{
268-
char str[1024];
269-
wcstombs(str, buf, 1023);
270-
res = str;
271-
}
272-
}
273-
else
274-
{
275-
GUID folder;
276-
HRESULT hr;
277-
switch (f)
278-
{
279-
case user_desktop: folder = FOLDERID_Desktop;
280-
break;
281-
case user_documents: folder = FOLDERID_Documents;
282-
// The user's Documents folder location may get overridden, as we know OneDrive does in certain circumstances.
283-
// In such cases, the new function, SHGetKnownFolderPath, does not always return the new path, while the deprecated
284-
// function does.
285-
CHAR path[MAX_PATH];
286-
CHECK_HR(SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, 0, path));
287-
288-
res = path;
289-
res += "\\";
290-
return res;
291-
case user_pictures: folder = FOLDERID_Pictures;
292-
break;
293-
case user_videos: folder = FOLDERID_Videos;
294-
break;
295-
case app_data: folder = FOLDERID_RoamingAppData;
296-
break;
297-
default:
298-
throw std::invalid_argument(
299-
std::string("Value of f (") + std::to_string(f) + std::string(") is not supported"));
300-
}
301-
302-
PWSTR folder_path = NULL;
303-
hr = SHGetKnownFolderPath(folder, KF_FLAG_DEFAULT_PATH, NULL, &folder_path);
304-
if (SUCCEEDED(hr))
305-
{
306-
char str[1024];
307-
wcstombs(str, folder_path, 1023);
308-
CoTaskMemFree(folder_path);
309-
res = str;
310-
res += "\\";
311-
}
312-
else
313-
{
314-
throw std::runtime_error("Failed to get requested special folder");
315-
}
316-
}
317-
#endif //_WIN32
318-
#if defined __linux__ || defined __APPLE__
319-
if (f == special_folder::temp_folder)
320-
{
321-
const char* tmp_dir = getenv("TMPDIR");
322-
res = tmp_dir ? tmp_dir : "/tmp/";
323-
}
324-
else
325-
{
326-
const char* home_dir = getenv("HOME");
327-
if (!home_dir)
328-
{
329-
struct passwd* pw = getpwuid(getuid());
330-
home_dir = (pw && pw->pw_dir) ? pw->pw_dir : "";
331-
}
332-
if (home_dir)
333-
{
334-
res = home_dir;
335-
switch (f)
336-
{
337-
case user_desktop: res += "/Desktop/";
338-
break;
339-
case user_documents: res += "/Documents/";
340-
break;
341-
case user_pictures: res += "/Pictures/";
342-
break;
343-
case user_videos: res += "/Videos/";
344-
break;
345-
case app_data: res += "/.";
346-
break;
347-
default:
348-
throw std::invalid_argument(
349-
std::string("Value of f (") + std::to_string(f) + std::string(") is not supported"));
350-
}
351-
}
352-
}
353-
#endif // defined __linux__ || defined __APPLE__
354-
return res;
355-
}
356-
357258
bool ends_with(const std::string& s, const std::string& suffix)
358259
{
359260
auto i = s.rbegin(), j = suffix.rbegin();

common/os.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,7 @@ namespace rs2
5050
size_t pixel_width, size_t pixels_height, size_t bytes_per_pixel,
5151
const void* raster_data, size_t stride_bytes);
5252

53-
enum special_folder
54-
{
55-
user_desktop,
56-
user_documents,
57-
user_pictures,
58-
user_videos,
59-
temp_folder,
60-
app_data
61-
};
62-
6353
std::string get_timestamped_file_name();
64-
std::string get_folder_path(special_folder f);
6554

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

common/rs-config.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "rs-config.h"
55

6+
#include <rsutils/os/special-folder.h>
67
#include <nlohmann/json.hpp>
78

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

8990
config_file& config_file::instance()
9091
{
91-
static config_file inst(get_folder_path(rs2::special_folder::app_data)
92-
+ std::string("realsense-config.json"));
92+
static config_file inst( rsutils::os::get_special_folder( rsutils::os::special_folder::app_data )
93+
+ "realsense-config.json" );
9394
return inst;
9495
}
9596

common/ux-window.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <imgui_impl_glfw.h>
1313

1414
#include "device-model.h"
15+
16+
#include <rsutils/os/special-folder.h>
1517
#include "os.h"
1618

1719
// We use STB image to load the splash-screen from memory
@@ -86,7 +88,7 @@ namespace rs2
8688
std::string path;
8789
try
8890
{
89-
path = get_folder_path(special_folder::user_documents);
91+
path = rsutils::os::get_special_folder( rsutils::os::special_folder::user_documents );
9092
}
9193
catch (const std::exception&)
9294
{

common/viewer.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#endif
88
#endif
99

10-
#include <regex>
11-
1210
#include "viewer.h"
1311
#include "os.h"
1412

@@ -19,9 +17,13 @@
1917
#include <imgui_internal.h>
2018

2119
#define ARCBALL_CAMERA_IMPLEMENTATION
22-
#include <arcball_camera.h>
20+
#include <third-party/arcball_camera.h>
21+
22+
#include <rsutils/os/special-folder.h>
2323
#include <rsutils/string/trim-newlines.h>
24-
#include "../common/utilities/imgui/wrap.h"
24+
#include <common/utilities/imgui/wrap.h>
25+
26+
#include <regex>
2527

2628
namespace rs2
2729
{
@@ -764,7 +766,9 @@ namespace rs2
764766

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

769773
std::ofstream out(tmp_filename.c_str());
770774
out << realsense_udev_rules;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// License: Apache 2.0. See LICENSE file in root directory.
2+
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
3+
#pragma once
4+
5+
#include <string>
6+
7+
8+
namespace rsutils {
9+
namespace os {
10+
11+
12+
enum class special_folder
13+
{
14+
user_desktop,
15+
user_documents,
16+
user_pictures,
17+
user_videos,
18+
temp_folder,
19+
app_data
20+
};
21+
22+
23+
// Get the path to one of the special folders listed above. These differ based on operating system.
24+
// Meant to be used to append a filename to it: may or may not end with a directory separator slash!
25+
// Throws invalid_argument if an invalid folder was passed in.
26+
// Throws runtime_error on failure.
27+
//
28+
std::string get_special_folder( special_folder );
29+
30+
31+
} // namespace os
32+
} // namespace rsutils

third-party/rsutils/py/pyrsutils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <rsutils/number/running-average.h>
1111
#include <rsutils/number/stabilized-value.h>
1212
#include <rsutils/os/executable-name.h>
13+
#include <rsutils/os/special-folder.h>
1314

1415

1516
#define NAME pyrsutils
@@ -141,4 +142,13 @@ PYBIND11_MODULE(NAME, m) {
141142
.def( "clear", &stabilized_value::clear )
142143
.def( "to_string", to_string )
143144
.def( "__str__", to_string );
145+
146+
py::enum_< rsutils::os::special_folder >( m, "special_folder" )
147+
.value( "app_data", rsutils::os::special_folder::app_data )
148+
.value( "temp_folder", rsutils::os::special_folder::temp_folder )
149+
.value( "user_desktop", rsutils::os::special_folder::user_desktop )
150+
.value( "user_documents", rsutils::os::special_folder::user_documents )
151+
.value( "user_pictures", rsutils::os::special_folder::user_pictures )
152+
.value( "user_videos", rsutils::os::special_folder::user_videos );
153+
m.def( "get_special_folder", rsutils::os::get_special_folder );
144154
}

0 commit comments

Comments
 (0)