1 /* This file is part of the Amalthea library.
2  *
3  * Copyright (C) 2018-2023 Eugene 'Vindex' Stulin
4  *
5  * Distributed under the Boost Software License 1.0 or (at your option)
6  * the GNU Lesser General Public License 3.0 or later.
7  */
8 
9 module amalthea.libcore;
10 
11 package import std.file;
12 package import std.conv;
13 package import std.exception;
14 package import std.stdio;
15 package import std.typecons;
16 
17 import core.stdc.string : strerror;
18 import std.string : fromStringz;
19 import std.compiler;
20 
21 /// Signed size_t.
22 alias ssize_t = ptrdiff_t;
23 
24 
25 /*******************************************************************************
26  * Template for exceptions.
27  */
28 mixin template RealizeException() {
29     this(string msg, string file = __FILE__, size_t line = __LINE__) {
30         super(msg, file, line);
31     }
32 }
33 
34 
35 /*******************************************************************************
36  * Template for exceptions using errno.
37  */
38 mixin template RealizeErrnoException() {
39     this(
40         string msg,
41         int err = .errno,
42         string file = __FILE__,
43         size_t line = __LINE__
44     ) {
45         auto strError = (err == 0) ? msg : msg ~ ": " ~ getInfoAboutError(err);
46         super(strError, file, line);
47     }
48 }
49 
50 
51 /*******************************************************************************
52  * Returns string containing text view of an errno value.
53  */
54 string getInfoAboutError(int err) {
55     return strerror(err).fromStringz.idup;
56 }
57 
58 
59 /*******************************************************************************
60  * Returns current compiler name.
61  */
62 string getCompilerName() {
63     switch (std.compiler.vendor) {
64         case Vendor.digitalMars: return "dmd";
65         case Vendor.gnu:         return "gdc";
66         case Vendor.llvm:        return "ldc2";
67         case Vendor.sdc:         return "sdc";
68         default: break;
69     }
70     return "unknown";
71 }
72 
73 
74 enum amaltheaCompiler = getCompilerName();