HAL
utils.cpp
Go to the documentation of this file.
2 
4 
5 #include <algorithm>
6 #include <dirent.h>
7 #include <fstream>
8 #include <random>
9 #include <sstream>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <iomanip>
13 
14 #ifdef _WIN32
15 #include <tchar.h>
16 #include <windows.h>
17 #elif __APPLE__ && __MACH__
18 
19 #include <mach-o/dyld.h>
20 #include <unistd.h>
21 
22 #elif __linux__
23 #include <stdlib.h>
24 #include <unistd.h>
25 #endif
26 
27 namespace hal
28 {
29  namespace utils
30  {
31  bool file_exists(const std::string& filename)
32  {
33  std::ifstream ifile(filename.c_str());
34  return (bool)ifile;
35  }
36 
37  bool folder_exists_and_is_accessible(const std::filesystem::path& folder)
38  {
39 #ifdef _WIN32
40  DWORD ftyp = GetFileAttributesA(folder.c_str());
41  if (ftyp == INVALID_FILE_ATTRIBUTES)
42  {
43  return false; //something is wrong with your path!
44  }
45 
46  if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
47  {
48  if (0 == access(folder.c_str(), R_OK))
49  {
50  return true;
51  }
52  }
53 
54  return false; // this is not a directory!
55 #else
56 
57  struct stat sb;
58 
59  if (stat(folder.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
60  {
61  if (0 == access(folder.c_str(), R_OK))
62  {
63  return true;
64  }
65  }
66  return false;
67 #endif
68  }
69 
70  std::filesystem::path get_binary_directory()
71  {
72  char buf[1024] = {0};
73 #ifdef _WIN32
74  DWORD ret = GetModuleFileNameA(NULL, buf, sizeof(buf));
75  if (ret == 0 || ret == sizeof(buf))
76  {
77  return std::string();
78  }
79  return std::filesystem::path(buf);
80 #elif __APPLE__ && __MACH__
81  uint32_t size = sizeof(buf);
82  int ret = _NSGetExecutablePath(buf, &size);
83  if (0 != ret)
84  {
85  return std::string();
86  }
87 
88  hal::error_code ec;
89  std::filesystem::path p(std::filesystem::canonical(buf, ec));
90  return p.parent_path().make_preferred();
91 #elif __linux__
92  ssize_t size = readlink("/proc/self/exe", buf, sizeof(buf));
93  std::string path(buf, size);
94  hal::error_code ec;
95  auto p = std::filesystem::canonical(path, ec);
96  return p.make_preferred().parent_path();
97 #endif
98  }
99 
100  std::filesystem::path get_base_directory()
101  {
102  auto environ_path = std::getenv("HAL_BASE_PATH");
103  if (environ_path)
104  {
105  return std::filesystem::path(environ_path).make_preferred();
106  }
107  hal::error_code ec;
108  auto bin_dir = get_binary_directory();
109  while (std::filesystem::exists(bin_dir))
110  {
111  if (std::filesystem::exists(bin_dir / "hal", ec))
112  {
113  return bin_dir.parent_path();
114  }
115  if (!bin_dir.has_relative_path())
116  {
117  break;
118  }
119  bin_dir = bin_dir.parent_path();
120  }
121  std::filesystem::path which_result = which("hal");
122  if (!which_result.empty())
123  {
124  return which_result.parent_path().parent_path();
125  }
126  die("core", "Cannot determine base path of hal installation. Please set the environment variable HAL_BASE_PATH. Giving up!");
127  }
128 
129  std::filesystem::path get_library_directory()
130  {
131  std::vector<std::filesystem::path> path_hints = {
132  get_base_directory() / "lib/x86_64-linux-gnu",
133  get_base_directory() / "lib64/",
134  get_base_directory() / "lib/",
135  };
136 
137  for (const auto& path : path_hints)
138  {
139  hal::error_code ec;
140  if (std::filesystem::exists(path / "hal_plugins", ec))
141  {
142  return path;
143  }
144  }
145 
146  return std::filesystem::path();
147  }
148 
149  std::filesystem::path get_share_directory()
150  {
151  std::vector<std::filesystem::path> path_hints = {
152  get_base_directory() / "share/hal",
153  };
154  return get_first_directory_exists(path_hints);
155  }
156 
157  std::filesystem::path get_user_share_directory()
158  {
159  std::filesystem::path dir = std::filesystem::path(getenv("HOME")) / ".local/share/hal";
160  std::filesystem::create_directories(dir);
161  return dir;
162  }
163 
164  std::filesystem::path get_config_directory()
165  {
166  std::vector<std::filesystem::path> path_hints = {
167  get_base_directory() / "share/hal/defaults",
168  };
169  return get_first_directory_exists(path_hints);
170  }
171 
172  std::filesystem::path get_user_config_directory()
173  {
174  std::filesystem::path dir = std::filesystem::path(getenv("HOME")) / ".config/hal";
175  std::filesystem::create_directories(dir);
176  return dir;
177  }
178 
179  std::filesystem::path get_default_log_directory(std::filesystem::path source_file)
180  {
181  std::filesystem::path dir = (source_file.empty()) ? get_user_share_directory() / "log" : source_file.parent_path();
182  std::filesystem::create_directories(dir);
183  return dir;
184  }
185 
186  std::vector<std::filesystem::path> get_gate_library_directories()
187  {
188  std::vector<std::filesystem::path> path_hints = {
189  get_share_directory() / "gate_libraries",
190  get_user_share_directory() / "gate_libraries",
191  };
192  return path_hints;
193  }
194 
195  std::vector<std::filesystem::path> get_plugin_directories()
196  {
197  std::vector<std::filesystem::path> path_hints = {
198 
199  get_library_directory() / "hal_plugins/",
200  get_library_directory() / "plugins/",
201  get_user_share_directory() / "plugins/",
202  std::filesystem::path(getenv("HOME")) / "plugins/",
203  };
204  return path_hints;
205  }
206 
207  std::filesystem::path get_first_directory_exists(std::vector<std::filesystem::path> path_hints)
208  {
209  for (const auto& path : path_hints)
210  {
211  hal::error_code ec;
212  if (std::filesystem::exists(path, ec))
213  {
214  return path;
215  }
216  }
217  return std::filesystem::path();
218  }
219 
220  std::filesystem::path get_file(std::string file_name, std::vector<std::filesystem::path> path_hints)
221  {
222  if (file_name.empty())
223  {
224  return std::filesystem::path();
225  }
226 
227  for (const auto& path : path_hints)
228  {
229  hal::error_code ec1;
230  if (std::filesystem::exists(std::filesystem::path(path), ec1))
231  {
232  std::filesystem::path file_path = std::filesystem::path(path) / std::filesystem::path(file_name).string();
233  hal::error_code ec2;
234  if (std::filesystem::exists(file_path, ec2))
235  {
236  return file_path;
237  }
238  }
239  }
240  return std::filesystem::path();
241  }
242 
243  std::filesystem::path which(const std::string& name, const std::string& path)
244  {
245  if (name.empty())
246  {
247  return std::filesystem::path();
248  }
249  std::string internal_path = path;
250  if (internal_path.empty())
251  {
252  internal_path = std::getenv("PATH");
253  }
254 #ifdef _WIN32
255  const char which_delimiter = ';';
256  return std::filesystem::path(); // Curerently no support for windows. Sorry ...
257 #else
258  const char which_delimiter = ':';
259 #endif
260  auto folders = split(internal_path, which_delimiter, false);
261  for (const auto& folder : folders)
262  {
263  hal::error_code ec;
264  UNUSED(ec);
265  std::filesystem::path p = std::filesystem::path(folder) / name;
266  struct stat sb;
267 
268  if (stat(p.c_str(), &sb) == 0 && sb.st_mode & S_IXUSR)
269  {
270  return p;
271  }
272  }
273  return std::filesystem::path();
274  }
275 
276  Result<std::filesystem::path> get_unique_temp_directory(const std::string& prefix, const u32 max_attmeps)
277  {
278  const auto tmp_dir = std::filesystem::temp_directory_path();
279 
280  std::random_device dev;
281  std::mt19937 prng(dev());
282  std::uniform_int_distribution<uint64_t> rand(0);
283 
284  for (u32 i = 0; i < max_attmeps; i++)
285  {
286  std::stringstream ss;
287  ss << std::setw(16) << std::setfill('0') << std::hex << rand(prng);
288  std::filesystem::path tmp_path = tmp_dir / (prefix + ss.str());
289 
290  if (std::filesystem::create_directories(tmp_path))
291  {
292  return OK(tmp_path);
293  }
294  }
295 
296  return ERR("failed to create unique temporary directory path");
297  }
298 
300  {
301  return R"(pybind11 (https://github.com/pybind/pybind11):
302 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>, All rights reserved.
303 
304 Redistribution and use in source and binary forms, with or without
305 modification, are permitted provided that the following conditions are met:
306 
307 1. Redistributions of source code must retain the above copyright notice, this
308  list of conditions and the following disclaimer.
309 
310 2. Redistributions in binary form must reproduce the above copyright notice,
311  this list of conditions and the following disclaimer in the documentation
312  and/or other materials provided with the distribution.
313 
314 3. Neither the name of the copyright holder nor the names of its contributors
315  may be used to endorse or promote products derived from this software
316  without specific prior written permission.
317 
318 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
319 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
320 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
321 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
322 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
323 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
324 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
325 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
326 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
327 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328 
329 Please also refer to the file CONTRIBUTING.md, which clarifies licensing of
330 external contributions to this project including patches, pull requests, etc.
331 
332 =================================================================================
333 
334 rapidjson (https://github.com/Tencent/rapidjson):
335 
336 Tencent is pleased to support the open source community by making RapidJSON
337 available.
338 
339 Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
340 rights reserved.
341 
342 If you have downloaded a copy of the RapidJSON binary from Tencent, please
343 note that the RapidJSON binary is licensed under the MIT License. If you have
344 downloaded a copy of the RapidJSON source code from Tencent, please note that
345 RapidJSON source code is licensed under the MIT License, except for the
346 third-party components listed below which are subject to different license
347 terms. Your integration of RapidJSON into your own projects may require
348 compliance with the MIT License, as well as the other licenses applicable to
349 the third-party components included within RapidJSON. To avoid the problematic
350 JSON license in your own projects, it's sufficient to exclude the
351 bin/jsonchecker/ directory, as it's the only code under the JSON license. A
352 copy of the MIT License is included in this file.
353 
354 Other dependencies and licenses:
355 
356 Open Source Software Licensed Under the BSD License:
357 --------------------------------------------------------------------
358 
359 The msinttypes r29 Copyright (c) 2006-2013 Alexander Chemeris All rights
360 reserved.
361 
362 Redistribution and use in source and binary forms, with or without
363 modification, are permitted provided that the following conditions are met:
364 
365 * Redistributions of source code must retain the above copyright notice, this
366  list of conditions and the following disclaimer.
367 * Redistributions in binary form must reproduce the above copyright notice,
368  this list of conditions and the following disclaimer in the documentation
369  and/or other materials provided with the distribution.
370 * Neither the name of copyright holder nor the names of its contributors may
371  be used to endorse or promote products derived from this software without
372  specific prior written permission.
373 
374 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
375 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
376 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
377 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
378 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
379 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
380 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
381 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
383 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
384 
385 Open Source Software Licensed Under the JSON License:
386 --------------------------------------------------------------------
387 
388 json.org Copyright (c) 2002 JSON.org All Rights Reserved.
389 
390 JSON_checker Copyright (c) 2002 JSON.org All Rights Reserved.
391 
392 
393 Terms of the JSON License:
394 ---------------------------------------------------
395 
396 Permission is hereby granted, free of charge, to any person obtaining a copy
397 of this software and associated documentation files (the "Software"), to deal
398 in the Software without restriction, including without limitation the rights
399 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
400 copies of the Software, and to permit persons to whom the Software is
401 furnished to do so, subject to the following conditions:
402 
403 The above copyright notice and this permission notice shall be included in all
404 copies or substantial portions of the Software.
405 
406 The Software shall be used for Good, not Evil.
407 
408 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
409 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
410 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
411 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
412 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
413 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
414 SOFTWARE.
415 
416 
417 Terms of the MIT License:
418 --------------------------------------------------------------------
419 
420 Permission is hereby granted, free of charge, to any person obtaining a copy
421 of this software and associated documentation files (the "Software"), to deal
422 in the Software without restriction, including without limitation the rights
423 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
424 copies of the Software, and to permit persons to whom the Software is
425 furnished to do so, subject to the following conditions:
426 
427 The above copyright notice and this permission notice shall be included in all
428 copies or substantial portions of the Software.
429 
430 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
431 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
432 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
433 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
434 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
435 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
436 SOFTWARE.
437 
438 =================================================================================
439 
440 sanitizers-cmake (https://github.com/arsenm/sanitizers-cmake):
441 The MIT License (MIT)
442 
443 Copyright (c)
444  2013 Matthew Arsenault
445  2015-2016 RWTH Aachen University, Federal Republic of Germany
446 
447 Permission is hereby granted, free of charge, to any person obtaining a copy of
448 this software and associated documentation files (the "Software"), to deal in
449 the Software without restriction, including without limitation the rights to
450 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
451 the Software, and to permit persons to whom the Software is furnished to do so,
452 subject to the following conditions:
453 
454 The above copyright notice and this permission notice shall be included in all
455 copies or substantial portions of the Software.
456 
457 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
458 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
459 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
460 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
461 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
462 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
463 
464 =================================================================================
465 
466 spdlog (https://github.com/gabime/spdlog):
467 The MIT License (MIT)
468 
469 Copyright (c) 2016 Gabi Melman.
470 
471 Permission is hereby granted, free of charge, to any person obtaining a copy
472 of this software and associated documentation files (the "Software"), to deal
473 in the Software without restriction, including without limitation the rights
474 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
475 copies of the Software, and to permit persons to whom the Software is
476 furnished to do so, subject to the following conditions:
477 
478 The above copyright notice and this permission notice shall be included in
479 all copies or substantial portions of the Software.
480 
481 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
482 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
483 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
484 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
485 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
486 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
487 THE SOFTWARE.
488 
489 =================================================================================
490 
491 Boost (https://www.boost.org/)
492 Boost Software License - Version 1.0 - August 17th, 2003
493 
494 Permission is hereby granted, free of charge, to any person or organization
495 obtaining a copy of the software and accompanying documentation covered by
496 this license (the "Software") to use, reproduce, display, distribute,
497 execute, and transmit the Software, and to prepare derivative works of the
498 Software, and to permit third-parties to whom the Software is furnished to
499 do so, all subject to the following:
500 
501 The copyright notices in the Software and this entire statement, including
502 the above license grant, this restriction and the following disclaimer,
503 must be included in all copies of the Software, in whole or in part, and
504 all derivative works of the Software, unless such copies or derivative
505 works are solely in the form of machine-executable object code generated by
506 a source language processor.
507 
508 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
509 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
510 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
511 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
512 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
513 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
514 DEALINGS IN THE SOFTWARE.
515 
516 =================================================================================
517 
518 Qt (https://www.qt.io/)
519  GNU LESSER GENERAL PUBLIC LICENSE
520 
521  The Qt Toolkit is Copyright (C) 2015 The Qt Company Ltd.
522  Contact: http://www.qt.io/licensing/
523 
524  You may use, distribute and copy the Qt Toolkit under the terms of
525  GNU Lesser General Public License version 3, which is displayed below.
526  This license makes reference to the version 3 of the GNU General
527  Public License, which you can find in the LICENSE.GPLv3 file.
528 
529 -------------------------------------------------------------------------
530 
531  GNU LESSER GENERAL PUBLIC LICENSE
532  Version 3, 29 June 2007
533 
534  Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/>
535 Everyone is permitted to copy and distribute verbatim copies of this
536 licensedocument, but changing it is not allowed.
537 
538 This version of the GNU Lesser General Public License incorporates
539 the terms and conditions of version 3 of the GNU General Public
540 License, supplemented by the additional permissions listed below.
541 
542 0. Additional Definitions.
543 
544  As used herein, “this License” refers to version 3 of the GNU Lesser
545 General Public License, and the “GNU GPL” refers to version 3 of the
546 GNU General Public License.
547 
548  “The Library” refers to a covered work governed by this License,
549 other than an Application or a Combined Work as defined below.
550 
551  An “Application” is any work that makes use of an interface provided
552 by the Library, but which is not otherwise based on the Library.
553 Defining a subclass of a class defined by the Library is deemed a mode
554 of using an interface provided by the Library.
555 
556  A “Combined Work” is a work produced by combining or linking an
557 Application with the Library. The particular version of the Library
558 with which the Combined Work was made is also called the “Linked
559 Version”.
560 
561  The “Minimal Corresponding Source” for a Combined Work means the
562 Corresponding Source for the Combined Work, excluding any source code
563 for portions of the Combined Work that, considered in isolation, are
564 based on the Application, and not on the Linked Version.
565 
566  The “Corresponding Application Code” for a Combined Work means the
567 object code and/or source code for the Application, including any data
568 and utility programs needed for reproducing the Combined Work from the
569 Application, but excluding the System Libraries of the Combined Work.
570 
571 1. Exception to Section 3 of the GNU GPL.
572 
573  You may convey a covered work under sections 3 and 4 of this License
574 without being bound by section 3 of the GNU GPL.
575 
576 2. Conveying Modified Versions.
577 
578  If you modify a copy of the Library, and, in your modifications, a
579 facility refers to a function or data to be supplied by an Application
580 that uses the facility (other than as an argument passed when the
581 facility is invoked), then you may convey a copy of the modified
582 version:
583 
584  a) under this License, provided that you make a good faith effort
585  to ensure that, in the event an Application does not supply the
586  function or data, the facility still operates, and performs
587  whatever part of its purpose remains meaningful, or
588 
589  b) under the GNU GPL, with none of the additional permissions of
590  this License applicable to that copy.
591 
592 3. Object Code Incorporating Material from Library Header Files.
593 
594  The object code form of an Application may incorporate material from
595 a header file that is part of the Library. You may convey such object
596 code under terms of your choice, provided that, if the incorporated
597 material is not limited to numerical parameters, data structure
598 layouts and accessors, or small macros, inline functions and templates
599 (ten or fewer lines in length), you do both of the following:
600 
601  a) Give prominent notice with each copy of the object code that
602  the Library is used in it and that the Library and its use are
603  covered by this License.
604 
605  b) Accompany the object code with a copy of the GNU GPL and this
606  license document.
607 
608 4. Combined Works.
609 
610  You may convey a Combined Work under terms of your choice that, taken
611 together, effectively do not restrict modification of the portions of
612 the Library contained in the Combined Work and reverse engineering for
613 debugging such modifications, if you also do each of the following:
614 
615  a) Give prominent notice with each copy of the Combined Work that
616  the Library is used in it and that the Library and its use are
617  covered by this License.
618 
619  b) Accompany the Combined Work with a copy of the GNU GPL and this
620  license document.
621 
622  c) For a Combined Work that displays copyright notices during
623  execution, include the copyright notice for the Library among
624  these notices, as well as a reference directing the user to the
625  copies of the GNU GPL and this license document.
626 
627  d) Do one of the following:
628 
629  0) Convey the Minimal Corresponding Source under the terms of
630  this License, and the Corresponding Application Code in a form
631  suitable for, and under terms that permit, the user to
632  recombine or relink the Application with a modified version of
633  the Linked Version to produce a modified Combined Work, in the
634  manner specified by section 6 of the GNU GPL for conveying
635  Corresponding Source.
636 
637  1) Use a suitable shared library mechanism for linking with
638  the Library. A suitable mechanism is one that (a) uses at run
639  time a copy of the Library already present on the user's
640  computer system, and (b) will operate properly with a modified
641  version of the Library that is interface-compatible with the
642  Linked Version.
643 
644  e) Provide Installation Information, but only if you would
645  otherwise be required to provide such information under section 6
646  of the GNU GPL, and only to the extent that such information is
647  necessary to install and execute a modified version of the
648  Combined Work produced by recombining or relinking the Application
649  with a modified version of the Linked Version. (If you use option
650  4d0, the Installation Information must accompany the Minimal
651  Corresponding Source and Corresponding Application Code. If you
652  use option 4d1, you must provide the Installation Information in
653  the manner specified by section 6 of the GNU GPL for conveying
654  Corresponding Source.)
655 
656 5. Combined Libraries.
657 
658  You may place library facilities that are a work based on the Library
659 side by side in a single library together with other library
660 facilities that are not Applications and are not covered by this
661 License, and convey such a combined library under terms of your
662 choice, if you do both of the following:
663 
664  a) Accompany the combined library with a copy of the same work
665  based on the Library, uncombined with any other library
666  facilities, conveyed under the terms of this License.
667 
668  b) Give prominent notice with the combined library that part of
669  it is a work based on the Library, and explaining where to find
670  the accompanying uncombined form of the same work.
671 
672 6. Revised Versions of the GNU Lesser General Public License.
673 
674  The Free Software Foundation may publish revised and/or new versions
675 of the GNU Lesser General Public License from time to time. Such new
676 versions will be similar in spirit to the present version, but may
677 differ in detail to address new problems or concerns.
678 
679 Each version is given a distinguishing version number. If the Library
680 as you received it specifies that a certain numbered version of the
681 GNU Lesser General Public License “or any later version” applies to
682 it, you have the option of following the terms and conditions either
683 of that published version or of any later version published by the
684 Free Software Foundation. If the Library as you received it does not
685 specify a version number of the GNU Lesser General Public License,
686 you may choose any version of the GNU Lesser General Public License
687 ever published by the Free Software Foundation.
688 
689 If the Library as you received it specifies that a proxy can decide
690 whether future versions of the GNU Lesser General Public License shall
691 apply, that proxy's public statement of acceptance of any version is
692 permanent authorization for you to choose that version for the Library.
693 )";
694  }
695 
696  Result<u64> wrapped_stoull(const std::string& s, const u32 base)
697  {
698  try
699  {
700  return OK(std::stoull(s, nullptr, base));
701  }
702  catch (const std::invalid_argument& e)
703  {
704  return ERR("could not get u64 from string '" + s + "': " + e.what());
705  }
706  catch (const std::out_of_range& e)
707  {
708  return ERR("could not get u64 from string '" + s + "': " + e.what());
709  }
710 
711  return ERR("encountered unknown error");
712  }
713 
714  Result<u32> wrapped_stoul(const std::string& s, const u32 base)
715  {
716  try
717  {
718  return OK((u32)std::stoul(s, nullptr, base));
719  }
720  catch (const std::invalid_argument& e)
721  {
722  return ERR("could not get u32 from string '" + s + "': " + e.what());
723  }
724  catch (const std::out_of_range& e)
725  {
726  return ERR("could not get u32 from string '" + s + "': " + e.what());
727  }
728 
729  return ERR("encountered unknown error");
730  }
731 
732  } // namespace utils
733 } // namespace hal
#define UNUSED(expr)
Definition: defines.h:49
#define die(channel,...)
Definition: log.h:94
#define ERR(message)
Definition: result.h:53
#define OK(...)
Definition: result.h:49
std::filesystem::path get_config_directory()
Definition: utils.cpp:164
std::filesystem::path get_user_share_directory()
Definition: utils.cpp:157
std::filesystem::path get_first_directory_exists(std::vector< std::filesystem::path > path_hints)
Definition: utils.cpp:207
CORE_API std::vector< T > split(const T &s, const char delim, bool obey_brackets=false)
Definition: utils.h:237
std::filesystem::path get_share_directory()
Definition: utils.cpp:149
Result< std::filesystem::path > get_unique_temp_directory(const std::string &prefix, const u32 max_attmeps)
Definition: utils.cpp:276
std::vector< std::filesystem::path > get_gate_library_directories()
Definition: utils.cpp:186
std::filesystem::path get_base_directory()
Definition: utils.cpp:100
std::string get_open_source_licenses()
Definition: utils.cpp:299
std::filesystem::path get_library_directory()
Definition: utils.cpp:129
Result< u64 > wrapped_stoull(const std::string &s, const u32 base)
Definition: utils.cpp:696
std::filesystem::path get_user_config_directory()
Definition: utils.cpp:172
Result< u32 > wrapped_stoul(const std::string &s, const u32 base)
Definition: utils.cpp:714
std::vector< std::filesystem::path > get_plugin_directories()
Definition: utils.cpp:195
std::filesystem::path which(const std::string &name, const std::string &path)
Definition: utils.cpp:243
std::filesystem::path get_default_log_directory(std::filesystem::path source_file)
Definition: utils.cpp:179
bool folder_exists_and_is_accessible(const std::filesystem::path &folder)
Definition: utils.cpp:37
bool file_exists(const std::string &filename)
Definition: utils.cpp:31
std::filesystem::path get_binary_directory()
Definition: utils.cpp:70
std::filesystem::path get_file(std::string file_name, std::vector< std::filesystem::path > path_hints)
Definition: utils.cpp:220
std::error_code error_code
Definition: defines.h:46
Definition: utils.py:1
quint32 u32
std::string name