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