]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/missing.h
a9288d5c0dc49aaaf01d6efdb90a89215d0c5875
[xonotic/netradiant.git] / libs / missing.h
1 /*\r
2 Copyright (c) 2001, Loki software, inc.\r
3 All rights reserved.\r
4 \r
5 Redistribution and use in source and binary forms, with or without modification, \r
6 are permitted provided that the following conditions are met:\r
7 \r
8 Redistributions of source code must retain the above copyright notice, this list \r
9 of conditions and the following disclaimer.\r
10 \r
11 Redistributions in binary form must reproduce the above copyright notice, this\r
12 list of conditions and the following disclaimer in the documentation and/or\r
13 other materials provided with the distribution.\r
14 \r
15 Neither the name of Loki software nor the names of its contributors may be used \r
16 to endorse or promote products derived from this software without specific prior \r
17 written permission. \r
18 \r
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' \r
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE \r
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE \r
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY \r
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \r
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; \r
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS \r
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \r
29 */\r
30 \r
31 #ifndef _MISSING_H_\r
32 #define _MISSING_H_\r
33 \r
34 // NOTE TTimo\r
35 //   this goes along with str.h and provides various utility classes\r
36 //   and portability defines\r
37 //   the filename is a legecy issue, it would be better to clean that up\r
38 //   in a central 'portability' lib\r
39 \r
40 #include <glib.h>\r
41 #include <string.h>\r
42 \r
43 #ifdef _WIN32\r
44 #include <direct.h>\r
45 #include <io.h>\r
46 \r
47 #define MyCopyFile(a,b) CopyFile(a,b,FALSE)\r
48 \r
49 #define S_ISDIR(mode) (mode & _S_IFDIR)\r
50 #define R_OK 04\r
51 #define mymkdir(a,b) _mkdir(a)\r
52 \r
53 #else\r
54 \r
55 #define MyCopyFile CopyFile\r
56 #define mymkdir(a,b) mkdir(a,b)\r
57 \r
58 #endif\r
59 \r
60 #ifndef _WIN32\r
61 \r
62 // LZ: very ugly hacks\r
63 inline int GetLastError () { return 0; };\r
64 \r
65 // temp stuff\r
66 inline int GetPrivateProfileInt(char* a, char* b, int i, char* c) { return i; };\r
67 #define VERIFY(a) a;\r
68 int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart);\r
69 bool CopyFile(const char *lpExistingFileName, const char *lpNewFileName);\r
70 \r
71 #ifndef APIENTRY\r
72 #define APIENTRY\r
73 #endif\r
74 \r
75 int MemorySize(void *ptr);\r
76 #define _msize MemorySize\r
77 \r
78 #define MK_LBUTTON          0x0001\r
79 #define MK_RBUTTON          0x0002\r
80 #define MK_SHIFT            0x0004\r
81 #define MK_CONTROL          0x0008\r
82 #define MK_MBUTTON          0x0010\r
83 \r
84 #endif\r
85 \r
86 #define CString Str\r
87 #include "str.h"\r
88 \r
89 class CPtrArray\r
90 {\r
91 public:\r
92   CPtrArray ()\r
93   { m_ptrs = g_ptr_array_new (); };\r
94   virtual ~CPtrArray ()\r
95   { g_ptr_array_free (m_ptrs, TRUE); };\r
96   \r
97   void* operator[](int i) const\r
98   { return g_ptr_array_index (m_ptrs,i); };\r
99   void* GetAt(int i) const\r
100   { return g_ptr_array_index (m_ptrs,i); };\r
101   int GetSize () const\r
102   { return m_ptrs->len; };\r
103   void Add (void* ptr)\r
104   { g_ptr_array_add (m_ptrs, ptr); };\r
105   void RemoveAll ()\r
106   { g_ptr_array_set_size (m_ptrs, 0); };\r
107   void RemoveAt(int index, int count = 1)\r
108   {\r
109     if ((index < 0) || (count < 0) || (count + index > (int)m_ptrs->len))\r
110       return;\r
111     for (; count > 0; count--)\r
112       g_ptr_array_remove_index (m_ptrs, index);\r
113   }\r
114   void InsertAt(int nStartIndex, CPtrArray* pNewArray)\r
115   {\r
116     for (int i = 0; i < pNewArray->GetSize(); i++)\r
117       InsertAt(nStartIndex+i, pNewArray->GetAt(i));\r
118   }\r
119   void InsertAt(int nIndex, void* newElement, int nCount = 1)\r
120   {\r
121     if ((guint32)nIndex >= m_ptrs->len)\r
122     {\r
123       g_ptr_array_set_size (m_ptrs, nIndex + nCount);  // grow so nIndex is valid\r
124     }\r
125     else\r
126     {\r
127       // inserting in the middle of the array\r
128       int nOldSize = m_ptrs->len;\r
129       g_ptr_array_set_size (m_ptrs, m_ptrs->len + nCount);\r
130       // shift old data up to fill gap\r
131       memmove(&m_ptrs->pdata[nIndex+nCount], &m_ptrs->pdata[nIndex],\r
132         (nOldSize-nIndex) * sizeof(gpointer));\r
133       \r
134       memset(&m_ptrs->pdata[nIndex], 0, nCount * sizeof(gpointer));\r
135     }\r
136     \r
137     // insert new value in the gap\r
138     while (nCount--)\r
139       m_ptrs->pdata[nIndex++] = newElement;\r
140   }\r
141   void Copy(const CPtrArray& src)\r
142   {\r
143     g_ptr_array_set_size (m_ptrs, src.m_ptrs->len);\r
144     memcpy (m_ptrs->pdata, src.m_ptrs->pdata, m_ptrs->len*sizeof(gpointer));\r
145   }\r
146   \r
147 protected:\r
148   GPtrArray* m_ptrs;\r
149 };\r
150 \r
151 typedef struct stringmap_s\r
152 {\r
153   char* key;\r
154   char* value;\r
155 } stringmap_t;\r
156 \r
157 class CMapStringToString\r
158 {\r
159 public:\r
160   CMapStringToString ()\r
161   { m_map = g_ptr_array_new (); };\r
162   ~CMapStringToString ()\r
163   {\r
164     for (guint32 i = 0; i < m_map->len; i++)\r
165       FreeElement ((stringmap_t*)g_ptr_array_index (m_map,i));\r
166     g_ptr_array_set_size (m_map, 0);\r
167     g_ptr_array_free (m_map, TRUE);\r
168   };\r
169   void SetAt(char* key, char* newValue)\r
170   {\r
171     for (guint32 i = 0; i < m_map->len; i++)\r
172     {\r
173       stringmap_t* entry = (stringmap_t*)g_ptr_array_index (m_map,i);\r
174       if (strcmp (entry->key, key) == 0)\r
175       {\r
176         g_free (entry->value);\r
177         entry->value = g_strdup (newValue);\r
178         return;\r
179       }\r
180     }\r
181     stringmap_t* entry = (stringmap_t*)g_malloc (sizeof (stringmap_t));\r
182     entry->key = g_strdup (key);\r
183     entry->value = g_strdup (newValue);\r
184     g_ptr_array_add (m_map, entry);\r
185   }\r
186   \r
187   bool Lookup(const char* key, CString& rValue) const\r
188   {\r
189     for (guint32 i = 0; i < m_map->len; i++)\r
190     {\r
191       stringmap_t* entry = (stringmap_t*)g_ptr_array_index (m_map,i);\r
192       if (strcmp (entry->key, key) == 0)\r
193       {\r
194         rValue = entry->value;\r
195         return true;\r
196       }\r
197     }\r
198     return false;\r
199   }\r
200   \r
201 protected:\r
202   GPtrArray* m_map;\r
203   \r
204   void FreeElement(stringmap_t* elem)\r
205   { \r
206     g_free (elem->key);\r
207     g_free (elem->value);\r
208     g_free (elem);\r
209   };\r
210 };\r
211 \r
212 #endif // _MISSING_H_\r