xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
environment.h
Go to the documentation of this file.
1 #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 
26 namespace xtd {
33  class core_export_ environment final static_ {
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 
226  static xtd::ustring current_directory();
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 
264  static xtd::collections::specialized::string_vector get_command_line_args();
265 
273 
282  static xtd::ustring get_environment_variable(const xtd::ustring& variable, environment_variable_target target);
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 
302 
309  static xtd::ustring get_folder_path(environment::special_folder folder, environment::special_folder_option option);
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 
358  static xtd::ustring machine_name();
359 
362  static xtd::ustring new_line();
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 
376  static xtd::operating_system os_version();
377 
380  static uint32_t processor_count() {return processor_information().core_count();}
381 
384  static xtd::processor processor_information();
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 
416 
419  static size_t system_page_size();
420 
423  static std::chrono::milliseconds tick_count();
424 
427  static xtd::ustring user_domain_name();
428 
434  static bool user_interactive() {return true;}
435 
438  static xtd::ustring user_name();
439 
442  static xtd::version version();
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::diagnostics::stack_trace class.
The file system directory that contains the programs that appear in the Startup folder for all users...
The file system directory that serves as a repository for videos that belong to a user...
Contains xtd::static_object class.
ustring remove(size_t start_index) const noexcept
Deletes all the characters from this string beginning at a specified position and continuing through ...
A folder for components that are shared across applications. This special folder is valid only for Wi...
static xtd::ustring stack_trace()
Gets current stack trace information.
Definition: environment.h:408
Represents information about c++ libraries, such as the version and standard identifier. This class cannot be inherited.
Definition: compiler.h:23
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
Contains xtd::operating_system class.
The file system directory that contains resource data.
The environment variable is stored or retrieved from the environment block associated with the curren...
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
The directory that contains the user&#39;s program groups.
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
The directory that serves as a common repository for documents. This member is equivalent to MyDocume...
special_folder
Specifies enumerated constants used to retrieve directory paths to system special folders...
Definition: environment.h:92
size_t index_of(value_type value) const noexcept
Reports the index of the first occurrence of the specified character in this string.
static bool is_64_bit_operating_system()
Determines whether the current operating system is a 64-bit operating system.
Definition: environment.h:349
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:38
The file system directory that contains the link objects that can exist in the Printers virtual folde...
The path to the folder is verified. If the folder exists, the path is returned. If the folder does no...
A file system directory that contains the link objects that may exist in the My Network Places virtua...
static xtd::ustring command_line()
Gets the command line for this process.
Definition: environment.h:207
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
A virtual folder that contains fonts.
Contains xtd::guid class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Represents a stack trace, which is an ordered collection of one or more stack frames.
Definition: stack_trace.h:32
The file system directory that acts as a staging area for files waiting to be written to a CD...
The directory used to physically store file objects on the desktop. Do not confuse this directory wit...
Contains xtd::environment_variable_target enum class.
The file system directory that serves as a repository for image files common to all users...
special_folder_option
Specifies options to use for getting the path to a special folder.
Definition: environment.h:193
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Represents information about c++ libraries, such as the version and language identifier. This class cannot be inherited.
Definition: cpp_language.h:24
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition: version.h:93
Represents information about an operating system, such as the version and platform identifier...
Definition: operating_system.h:23
The file system directory that serves as a repository for video files common to all users...
The file system directory that contains documents that are common to all users. This special folder i...
Contains xtd::compiler class.
ustring substring(size_t start_index) const noexcept
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Indicates that no style elements, such as leading or trailing white space, thousands separators...
static bool user_interactive()
Gets a value indicating whether the current process is running in user interactive mode...
Definition: environment.h:434
The file system directory that contains home folder.
static uint32_t processor_count()
Gets the number of processors on the current machine.
Definition: environment.h:380
The directory that serves as a common repository for document templates.
The file system directory that contains the programs and folders that appear on the Start menu for al...
The file system directory that is used to store administrative tools for an individual user...
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
The directory that contains the Start menu items.
Contains xtd::collections::specialized::string_vector typedef.
Contains xtd::cpp_language class.
xtd::ustring to_string() const noexcept
Builds a readable representation of the stack trace.
std::vector< xtd::ustring > string_vector
Represents a collection of strings.
Definition: string_vector.h:20
The My Computer folder. The MyComputer constant always yields the empty string ("") because no path i...
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 void exit(int exit_code)
Terminates this process and returns an exit code to the operating system.
Definition: environment.h:321
The Windows directory or SYSROOT. This corresponds to the windir% or SYSTEMROOT% environment variable...
The file system directory that contains administrative tools for all users of the computer...
The path to the folder is returned without verifying whether the path exists. If the folder is locate...
static xtd::ustring system_directory()
Gets the fully qualified path of the system directory.
Definition: environment.h:415
The file system directory that serves as a repository for music files common to all users...
The file system directory that contains files and folders that appear on the desktop for all users...
Contains xtd::processor class.
Contains xtd::ustring class.
The directory that serves as a common repository for the user&#39;s favorite items.
Contains xtd::version class.
The directory that serves as a common repository for Internet cookies.
The program files directory.On a non-x86 system, passing ProgramFiles to the GetFolderPath method ret...
static std::thread::id current_thread_id()
Gets a unique identifier for the current thread.
Definition: environment.h:234
The directory for components that are shared across applications.To get the x86 common program files ...
The directory that contains the Send To menu items.
The directory that corresponds to the user&#39;s Startup program group. The system starts these programs ...
The Program Files folder On an x86 system, passing the ProgramFilesX86 member to the Environment...
The logical Desktop rather than the physical file system location.
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
static bool is_64_bit_process()
Determines whether the current process is a 64-bit process.
Definition: environment.h:353
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
The directory that serves as a common repository for application-specific data for the current roamin...
Represents information about a processor, such as the architecture. This class cannot be inherited...
Definition: processor.h:19
The directory that serves as a common repository for application-specific data that is used by all us...
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
Contains xtd::argument_exception exception.
The path to the folder is created if it does not already exist.
The file system directory that contains localized resource data.
static xtd::compiler compiler_version()
Gets an cpp_standard object that contains the current c++ standard identifier and version number...
Definition: environment.h:211
The user&#39;s profile folder. Applications should not create files or folders at this level; they should...
The file system directory that contains the templates that are available to all users. This special folder is valid only for Windows NT systems.
The environment class.
Definition: environment.h:33
The directory that contains the user&#39;s most recently used documents.
This value is recognized in Windows Vista for backward compatibility, but the special folder itself i...
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:849
The directory that serves as a common repository for application-specific data that is used by the cu...
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
The directory that serves as a common repository for temporary Internet files.
The My Documents folder. This member is equivalent to Personal.
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
Contains xtd::platform_id enum class.
The directory that serves as a common repository for Internet history items.