1 /*
2  *  zlib License
3  *  
4  *  (C) 2016 jython234
5  *  
6  *  This software is provided 'as-is', without any express or implied
7  *  warranty.  In no event will the authors be held liable for any damages
8  *  arising from the use of this software.
9  *  
10  *  Permission is granted to anyone to use this software for any purpose,
11  *  including commercial applications, and to alter it and redistribute it
12  *  freely, subject to the following restrictions:
13  *  
14  *  1. The origin of this software must not be misrepresented; you must not
15  *     claim that you wrote the original software. If you use this software
16  *     in a product, an acknowledgment in the product documentation would be
17  *     appreciated but is not required.
18  *  2. Altered source versions must be plainly marked as such, and must not be
19  *     misrepresented as being the original software.
20  *  3. This notice may not be removed or altered from any source distribution.
21 */
22 module blocksound.util;
23 
24 /// Vector 3 struct with floats.
25 struct Vec3 {
26     /// X coordinate
27     float x;
28     /// Y coordinate
29     float y;
30     /// Z coordinate
31     float z;
32 }
33 
34 /// Represents an exception when an End-Of-File is reached.
35 class EOFException : Exception {
36     /// Default constructor
37     this(in string msg) {
38         super(msg);
39     }
40 }
41 
42 /++
43     Helper class which emulates an ArrayList due to
44     dynamic arrays not having a remove function for
45     elements.
46 
47     Uses an associative array to emulate.
48 +/
49 synchronized class ArrayList(T) {
50     private shared size_t counter = 0;
51     private shared T[size_t] list;
52 
53     /// Representation of the ArrayList as an Array. Returns a copy.
54     @property T[] array() @trusted {
55         return cast(T[]) list.values();
56     }
57 
58     /++
59         Adds the element to the array at the next
60         position.
61 
62         Params:
63                 element =   The element to be added.
64     +/
65     void add(T element) @trusted {
66         import core.atomic;
67         atomicOp!"+="(counter, 1);
68 
69         list[counter] = cast(shared) element;
70     }
71 
72     /++
73         Removes the element from the array.
74         
75         Params:
76                 element =   The element to be removed.
77     +/
78     void remove(T element) @trusted {
79         size_t posToRemove;
80         foreach(key, val; list) {
81             if((cast(T) val) == element) {
82                 posToRemove = key;
83                 break;
84             }
85         }
86         list.remove(posToRemove);
87     }
88 }
89 
90 /++
91     Converts a D string (immutable(char[])) to a C string
92     (char*).
93 
94     Params:
95             dString =   The D string to be converted.
96 
97     Returns: A C string (char array).
98 +/
99 char* toCString(in string dString) @trusted {
100     import std.string : toStringz;
101     return cast(char*) toStringz(dString);
102 }
103 
104 /++
105     Converts a C string (char array) to a D string
106     (immutable(char[]))
107 
108     Params:
109             cString =   The C string to be converted.
110 
111     Returns: A D string (immutable(char[]))
112 +/
113 string toDString(in char* cString) @trusted {
114     import std.string : fromStringz;
115     return cast(string) fromStringz(cString);
116 }