xtd - Reference Guide  0.1.2
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
environment.h
Go to the documentation of this file.
1
4#pragma once
5#include <map>
6#include <cstdlib>
7#include <thread>
8#include <vector>
9
10#include "argument_exception.h"
11#include "compiler.h"
12#include "core_export.h"
13#include "cpp_language.h"
15#include "guid.h"
16#include "operating_system.h"
17#include "platform_id.h"
18#include "processor.h"
19#include "static.h"
20#include "ustring.h"
21#include "version.h"
24
26namespace xtd {
34 public:
92 enum class special_folder {
94 desktop = 0,
96 programs = 2,
98 my_documents = 5,
100 personal = 5,
102 favorites = 6,
104 startup = 7,
106 recent = 8,
108 send_to = 9,
110 start_menu = 11,
112 my_music = 13,
114 my_videos = 14,
116 desktop_directory = 16,
118 my_computer = 17,
120 network_shortcuts = 19,
122 fonts = 20,
124 templates = 21,
126 common_start_menu = 22,
128 common_programs = 23,
130 common_startup = 24,
132 common_desktop_directory = 25,
134 application_data = 26,
136 printer_shortcuts = 27,
138 local_application_data = 28,
140 internet_cache = 32,
142 cookies = 33,
144 history = 34,
146 common_application_data = 35,
148 windows = 36,
150 system = 37,
152 program_files = 38,
154 my_pictures = 39,
156 user_profile = 40,
158 system_x86 = 41,
160 program_files_x86 = 42,
162 common_program_files = 43,
164 common_program_files_x86 = 44,
166 common_templates = 45,
168 common_documents = 46,
170 common_admin_tools = 47,
172 admin_tools = 48,
174 common_music = 53,
176 common_pictures = 54,
178 common_videos = 55,
180 resources = 56,
182 localized_resources = 57,
184 common_oem_links = 58,
186 cd_burning = 59,
188 home = 64
189 };
190
195 none = 0,
197 do_not_verify = 16384,
199 create = 32768
200 };
201
207 static xtd::ustring command_line() {return xtd::ustring::join(" ", get_command_line_args());}
208
212 static xtd::compiler compiler;
213 return compiler;
214 }
215
220 return cpp_language;
221 }
222
227
230 static void current_directory(const xtd::ustring& directory_name);
231
234 static std::thread::id current_thread_id() {return std::this_thread::get_id();}
235
241 static int exit_code();
247 static void exit_code(int value);
248
265
272 static xtd::ustring get_environment_variable(const xtd::ustring& variable) {return get_environment_variable(variable, environment_variable_target::process);}
273
283
287 static std::map<std::string, std::string>& get_environment_variables() {return get_environment_variables(environment_variable_target::process);}
288
295 static std::map<std::string, std::string>& get_environment_variables(environment_variable_target target);
296
301 static xtd::ustring get_folder_path(environment::special_folder folder) {return get_folder_path(folder, environment::special_folder_option::none);}
302
310
315 return {}; //__opaque_io::get_drives();
316 }
317
321 static void exit(int exit_code) {::_Exit(exit_code);}
322
328 xtd::ustring buffer = name;
329 xtd::ustring result;
330
331 size_t index = buffer.index_of('%');
332 while (index != xtd::ustring::npos && buffer.index_of('%', index + 1) != xtd::ustring::npos) {
333 result += buffer.substring(0, index);
334 buffer = buffer.remove(0, index + 1);
335 index = buffer.index_of('%');
336 if (get_environment_variable(buffer.substring(0, index)) != "")
337 result += get_environment_variable(buffer.substring(0, index));
338 else
339 result += xtd::ustring::format("%{0}%", buffer.substring(0, index));
340 buffer = buffer.remove(0, index + 1);
341 index = buffer.index_of('%');
342 }
343 result += buffer;
344 return result;
345 }
346
349 static bool is_64_bit_operating_system() {return os_version().is_64_bit();}
350
353 static bool is_64_bit_process() {return sizeof(size_t) == 8;}
354
359
363
367 template <class Char, class Traits>
368 static std::basic_ostream<Char, Traits>& new_line(std::basic_ostream<Char, Traits>& os) {
369 os.put(os.widen('\n'));
370 os.flush();
371 return os;
372 }
373
377
380 static uint32_t processor_count() {return processor_information().core_count();}
381
385
392 static void set_environment_variable(const xtd::ustring& variable, const xtd::ustring& value) {
393 set_environment_variable(variable, value, environment_variable_target::process);
394 }
395
404 static void set_environment_variable(const xtd::ustring& variable, const xtd::ustring& value, environment_variable_target target);
405
410 }
411
415 static xtd::ustring system_directory() {return get_folder_path(environment::special_folder::system);}
416
419 static size_t system_page_size();
420
423 static std::chrono::milliseconds tick_count();
424
428
434 static bool user_interactive() {return true;}
435
439
443 };
444
446 inline std::ostream& operator<<(std::ostream& os, environment::special_folder value) {return os << to_string(value, {{environment::special_folder::desktop, "desktop"}, {environment::special_folder::programs, "programs"}, {environment::special_folder::my_documents, "my_documents"}, {environment::special_folder::personal, "personal"}, {environment::special_folder::favorites, "favorites"}, {environment::special_folder::startup, "startup"}, {environment::special_folder::recent, "recent"}, {environment::special_folder::send_to, "send_to"}, {environment::special_folder::start_menu, "start_menu"}, {environment::special_folder::personal, "my_music"}, {environment::special_folder::my_videos, "my_videos"}, {environment::special_folder::desktop_directory, "desktop_directory"}, {environment::special_folder::my_computer, "my_computer"}, {environment::special_folder::network_shortcuts, "network_shortcuts"}, {environment::special_folder::fonts, "fonts"}, {environment::special_folder::templates, "templates"}, {environment::special_folder::common_start_menu, "common_start_menu"}, {environment::special_folder::common_programs, "common_programs"}, {environment::special_folder::common_startup, "common_startup"}, {environment::special_folder::common_desktop_directory, "common_desktop_directory"}, {environment::special_folder::application_data, "application_data"}, {environment::special_folder::printer_shortcuts, "printer_shortcuts"}, {environment::special_folder::local_application_data, "local_application_data"}, {environment::special_folder::internet_cache, "internet_cache"}, {environment::special_folder::cookies, "cookies"}, {environment::special_folder::history, "history"}, {environment::special_folder::common_application_data, "common_application_data"}, {environment::special_folder::windows, "windows"}, {environment::special_folder::system, "system"}, {environment::special_folder::program_files, "program_files"}, {environment::special_folder::my_pictures, "my_pictures"}, {environment::special_folder::user_profile, "user_profile"}, {environment::special_folder::system_x86, "system_x86"}, {environment::special_folder::program_files_x86, "program_files_x86"}, {environment::special_folder::common_program_files, "common_program_files"}, {environment::special_folder::common_program_files_x86, "common_program_files_x86"}, {environment::special_folder::common_templates, "common_templates"}, {environment::special_folder::common_documents, "common_documents"}, {environment::special_folder::common_admin_tools, "common_admin_tools"}, {environment::special_folder::admin_tools, "admin_tools"}, {environment::special_folder::common_music, "common_music"}, {environment::special_folder::common_pictures, "common_pictures"}, {environment::special_folder::common_videos, "common_videos"}, {environment::special_folder::resources, "resources"}, {environment::special_folder::localized_resources, "localized_resources"}, {environment::special_folder::common_oem_links, "common_oem_links"}, {environment::special_folder::cd_burning, "cd_burning"}, {environment::special_folder::home, "home"}});}
447 inline std::wostream& operator<<(std::wostream& os, environment::special_folder value) {return os << to_string(value, {{environment::special_folder::desktop, L"desktop"}, {environment::special_folder::programs, L"programs"}, {environment::special_folder::my_documents, L"my_documents"}, {environment::special_folder::personal, L"personal"}, {environment::special_folder::favorites, L"favorites"}, {environment::special_folder::startup, L"startup"}, {environment::special_folder::recent, L"recent"}, {environment::special_folder::send_to, L"send_to"}, {environment::special_folder::start_menu, L"start_menu"}, {environment::special_folder::personal, L"my_music"}, {environment::special_folder::my_videos, L"my_videos"}, {environment::special_folder::desktop_directory, L"desktop_directory"}, {environment::special_folder::my_computer, L"my_computer"}, {environment::special_folder::network_shortcuts, L"network_shortcuts"}, {environment::special_folder::fonts, L"fonts"}, {environment::special_folder::templates, L"templates"}, {environment::special_folder::common_start_menu, L"common_start_menu"}, {environment::special_folder::common_programs, L"common_programs"}, {environment::special_folder::common_startup, L"common_startup"}, {environment::special_folder::common_desktop_directory, L"common_desktop_directory"}, {environment::special_folder::application_data, L"application_data"}, {environment::special_folder::printer_shortcuts, L"printer_shortcuts"}, {environment::special_folder::local_application_data, L"local_application_data"}, {environment::special_folder::internet_cache, L"internet_cache"}, {environment::special_folder::cookies, L"cookies"}, {environment::special_folder::history, L"history"}, {environment::special_folder::common_application_data, L"common_application_data"}, {environment::special_folder::windows, L"windows"}, {environment::special_folder::system, L"system"}, {environment::special_folder::program_files, L"program_files"}, {environment::special_folder::my_pictures, L"my_pictures"}, {environment::special_folder::user_profile, L"user_profile"}, {environment::special_folder::system_x86, L"system_x86"}, {environment::special_folder::program_files_x86, L"program_files_x86"}, {environment::special_folder::common_program_files, L"common_program_files"}, {environment::special_folder::common_program_files_x86, L"common_program_files_x86"}, {environment::special_folder::common_templates, L"common_templates"}, {environment::special_folder::common_documents, L"common_documents"}, {environment::special_folder::common_admin_tools, L"common_admin_tools"}, {environment::special_folder::admin_tools, L"admin_tools"}, {environment::special_folder::common_music, L"common_music"}, {environment::special_folder::common_pictures, L"common_pictures"}, {environment::special_folder::common_videos, L"common_videos"}, {environment::special_folder::resources, L"resources"}, {environment::special_folder::localized_resources, L"localized_resources"}, {environment::special_folder::common_oem_links, L"common_oem_links"}, {environment::special_folder::cd_burning, L"cd_burning"}, {environment::special_folder::home, L"home"}});}
448 inline std::ostream& operator<<(std::ostream& os, environment::special_folder_option value) {return os << to_string(value, {{environment::special_folder_option::none, "none"}, {environment::special_folder_option::do_not_verify, "do_not_verify"}, {environment::special_folder_option::create, "create"}});}
449 inline std::wostream& operator<<(std::wostream& os, environment::special_folder_option value) {return os << to_string(value, {{environment::special_folder_option::none, L"none"}, {environment::special_folder_option::do_not_verify, L"do_not_verify"}, {environment::special_folder_option::create, L"create"}});}
451}
Contains xtd::argument_exception exception.
Represents information about c++ libraries, such as the version and standard identifier....
Definition compiler.h:23
Represents information about c++ libraries, such as the version and language identifier....
Definition cpp_language.h:24
Represents a stack trace, which is an ordered collection of one or more stack frames.
Definition stack_trace.h:32
xtd::ustring to_string() const noexcept
Builds a readable representation of the stack trace.
The environment class.
Definition environment.h:33
static std::thread::id current_thread_id()
Gets a unique identifier for the current thread.
Definition environment.h:234
static std::chrono::milliseconds tick_count()
Gets the number of milliseconds elapsed since the system started.
static xtd::cpp_language cpp_version()
Gets an cpp_standard object that contains the current c++ standard identifier and version number.
Definition environment.h:218
static xtd::ustring get_folder_path(environment::special_folder folder)
Gets the path to the system special folder that is identified by the specified enumeration.
Definition environment.h:301
static void exit_code(int value)
Sets the exit code of the process.
static xtd::ustring user_domain_name()
Gets the network domain name associated with the current user.
static void set_environment_variable(const xtd::ustring &variable, const xtd::ustring &value, environment_variable_target target)
Creates, modifies, or deletes an environment variable stored in the current process or in the Windows...
static bool is_64_bit_operating_system()
Determines whether the current operating system is a 64-bit operating system.
Definition environment.h:349
static xtd::ustring machine_name()
Gets the NetBIOS name of this local computer.
special_folder
Specifies enumerated constants used to retrieve directory paths to system special folders.
Definition environment.h:92
@ common_startup
The file system directory that contains the programs that appear in the Startup folder for all users....
@ personal
The directory that serves as a common repository for documents. This member is equivalent to MyDocume...
@ printer_shortcuts
The file system directory that contains the link objects that can exist in the Printers virtual folde...
@ windows
The Windows directory or SYSROOT. This corresponds to the windir% or SYSTEMROOT% environment variable...
@ home
The file system directory that contains home folder.
@ local_application_data
The directory that serves as a common repository for application-specific data that is used by the cu...
@ my_computer
The My Computer folder. The MyComputer constant always yields the empty string ("") because no path i...
@ user_profile
The user's profile folder. Applications should not create files or folders at this level; they should...
@ common_application_data
The directory that serves as a common repository for application-specific data that is used by all us...
@ localized_resources
The file system directory that contains localized resource data.
@ common_program_files
The directory for components that are shared across applications.To get the x86 common program files ...
@ program_files
The program files directory.On a non-x86 system, passing ProgramFiles to the GetFolderPath method ret...
@ history
The directory that serves as a common repository for Internet history items.
@ common_videos
The file system directory that serves as a repository for video files common to all users.
@ common_admin_tools
The file system directory that contains administrative tools for all users of the computer.
@ programs
The directory that contains the user's program groups.
@ system
The System directory.
@ resources
The file system directory that contains resource data.
@ cookies
The directory that serves as a common repository for Internet cookies.
@ admin_tools
The file system directory that is used to store administrative tools for an individual user....
@ common_desktop_directory
The file system directory that contains files and folders that appear on the desktop for all users....
@ common_start_menu
The file system directory that contains the programs and folders that appear on the Start menu for al...
@ common_pictures
The file system directory that serves as a repository for image files common to all users.
@ desktop_directory
The directory used to physically store file objects on the desktop. Do not confuse this directory wit...
@ common_programs
A folder for components that are shared across applications. This special folder is valid only for Wi...
@ recent
The directory that contains the user's most recently used documents.
@ common_music
The file system directory that serves as a repository for music files common to all users.
@ my_pictures
The My Pictures folder.
@ fonts
A virtual folder that contains fonts.
@ system_x86
The Windows System folder.
@ start_menu
The directory that contains the Start menu items.
@ my_documents
The My Documents folder. This member is equivalent to Personal.
@ common_program_files_x86
The Program Files folder.
@ favorites
The directory that serves as a common repository for the user's favorite items.
@ desktop
The logical Desktop rather than the physical file system location.
@ network_shortcuts
A file system directory that contains the link objects that may exist in the My Network Places virtua...
@ internet_cache
The directory that serves as a common repository for temporary Internet files.
@ common_templates
The file system directory that contains the templates that are available to all users....
@ send_to
The directory that contains the Send To menu items.
@ application_data
The directory that serves as a common repository for application-specific data for the current roamin...
@ program_files_x86
The Program Files folder On an x86 system, passing the ProgramFilesX86 member to the Environment....
@ cd_burning
The file system directory that acts as a staging area for files waiting to be written to a CD.
@ startup
The directory that corresponds to the user's Startup program group. The system starts these programs ...
@ common_oem_links
This value is recognized in Windows Vista for backward compatibility, but the special folder itself i...
@ my_videos
The file system directory that serves as a repository for videos that belong to a user.
@ common_documents
The file system directory that contains documents that are common to all users. This special folder i...
@ templates
The directory that serves as a common repository for document templates.
static xtd::version version()
Gets a version consisting of the major, minor, build, and revision numbers of the xtd framework.
static xtd::ustring user_name()
Gets the user name of the person who is currently logged on to the operating system.
static xtd::ustring get_folder_path(environment::special_folder folder, environment::special_folder_option option)
Gets the path to the system special folder that is identified by the specified enumeration,...
static uint32_t processor_count()
Gets the number of processors on the current machine.
Definition environment.h:380
static std::map< std::string, std::string > & get_environment_variables()
Retrieves all environment variable names and their values from the current process.
Definition environment.h:287
static xtd::collections::specialized::string_vector get_command_line_args()
Returns a string array containing the command-line arguments for the current process.
static xtd::ustring system_directory()
Gets the fully qualified path of the system directory.
Definition environment.h:415
static std::basic_ostream< Char, Traits > & new_line(std::basic_ostream< Char, Traits > &os)
Inserts a new-line character and flushes the stream.
Definition environment.h:368
static xtd::compiler compiler_version()
Gets an cpp_standard object that contains the current c++ standard identifier and version number.
Definition environment.h:211
static xtd::ustring current_directory()
Gets the fully qualified path of the current working directory.
static std::map< std::string, std::string > & get_environment_variables(environment_variable_target target)
Retrieves all environment variable names and their values from the current process,...
special_folder_option
Specifies options to use for getting the path to a special folder.
Definition environment.h:193
@ none
The path to the folder is verified. If the folder exists, the path is returned. If the folder does no...
@ create
The path to the folder is created if it does not already exist.
@ do_not_verify
The path to the folder is returned without verifying whether the path exists. If the folder is locate...
static xtd::ustring get_environment_variable(const xtd::ustring &variable, environment_variable_target target)
Retrieves the value of an environment variable from the current process or from the Windows operating...
static bool is_64_bit_process()
Determines whether the current process is a 64-bit process.
Definition environment.h:353
static xtd::ustring expand_environment_variables(const xtd::ustring &name)
Replaces the name of each environment variable embedded in the specified string with the string equiv...
Definition environment.h:327
static xtd::collections::specialized::string_vector get_logical_drives()
Returns an array of string containing the names of the logical drives on the current computer.
Definition environment.h:314
static void set_environment_variable(const xtd::ustring &variable, const xtd::ustring &value)
Creates, modifies, or deletes an environment variable stored in the current process.
Definition environment.h:392
static xtd::operating_system os_version()
Gets an operating_system object that contains the current platform identifier and version number.
static xtd::ustring get_environment_variable(const xtd::ustring &variable)
Retrieves the value of an environment variable from the current process.
Definition environment.h:272
static xtd::ustring command_line()
Gets the command line for this process.
Definition environment.h:207
static void current_directory(const xtd::ustring &directory_name)
Sets the fully qualified path of the current working directory.
static size_t system_page_size()
Gets the number of bytes in the operating system's memory page.
static xtd::ustring new_line()
Gets the newline string defined for this environment.
static bool user_interactive()
Gets a value indicating whether the current process is running in user interactive mode.
Definition environment.h:434
static xtd::processor processor_information()
Gets an operating_system object that contains the current platform identifier and version number.
static void exit(int exit_code)
Terminates this process and returns an exit code to the operating system.
Definition environment.h:321
static xtd::ustring stack_trace()
Gets current stack trace information.
Definition environment.h:408
static int exit_code()
Gets the exit code of the process.
Represents information about an operating system, such as the version and platform identifier....
Definition operating_system.h:23
Represents information about a processor, such as the architecture. This class cannot be inherited.
Definition processor.h:19
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:48
size_t index_of(value_type value) const noexcept
Reports the index of the first occurrence of the specified character in this string.
static ustring join(const ustring separator, const collection_t &values) noexcept
Concatenates a specified separator string between each element of a specified object array,...
Definition ustring.h:842
ustring remove(size_t start_index) const noexcept
Deletes all the characters from this string beginning at a specified position and continuing through ...
ustring substring(size_t start_index) const noexcept
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.h:93
Contains xtd::compiler class.
Contains core_export_ keyword.
Contains xtd::cpp_language class.
Contains xtd::environment_variable_target enum class.
std::vector< xtd::ustring > string_vector
Represents a collection of strings.
Definition string_vector.h:20
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition ustring.h:689
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.h:38
#define core_export_
Define shared library export.
Definition core_export.h:13
environment_variable_target
Specifies the location where an environment variable is stored or retrieved in a set or get operation...
Definition environment_variable_target.h:15
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:37
@ home
The HOME key.
Contains xtd::guid class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition system_report.h:17
Contains xtd::operating_system class.
Contains xtd::platform_id enum class.
Contains xtd::processor class.
Contains xtd::diagnostics::stack_trace class.
Contains xtd::static_object class.
Contains xtd::collections::specialized::string_vector typedef.
Contains xtd::ustring class.
Contains xtd::version class.