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