]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/missing.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / radiant / missing.cpp
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 //\r
32 // Missing functions\r
33 //\r
34 // Leonardo Zide (leo@lokigames.com)\r
35 //\r
36 \r
37 #if defined (__linux__) || defined (__APPLE__)\r
38 \r
39 #include <stdio.h>\r
40 #include <unistd.h>\r
41 #include <sys/time.h>\r
42 #include <stdlib.h>\r
43 #include "missing.h"\r
44 \r
45 bool CopyFile(const char *lpExistingFileName, const char *lpNewFileName)\r
46 {\r
47   FILE *src, *dst;\r
48   void* buf;\r
49   int l, ret = 0;\r
50   char realsrc[PATH_MAX], realdest[PATH_MAX];\r
51 \r
52   realpath (lpExistingFileName, realsrc);\r
53   realpath (lpNewFileName, realdest);\r
54 \r
55   src = fopen (realsrc, "rb");\r
56   if (!src)\r
57     return 0;\r
58   dst = fopen (realdest, "wb");\r
59   if (!dst)\r
60   {\r
61     fclose (src);\r
62     return 0;\r
63   }\r
64  \r
65   fseek (src, 0, SEEK_END);\r
66   l = ftell (src);\r
67   rewind (src);\r
68   buf = g_malloc (l);\r
69 \r
70   if (buf != NULL)\r
71     if (fread (buf, l, 1, src) == 1)\r
72       if (fwrite (buf, l, 1, dst) == 1)\r
73         ret = 1;\r
74 \r
75   g_free (buf);\r
76   fclose (src);\r
77   fclose (dst);\r
78 \r
79   return ret;\r
80 }\r
81 \r
82 int GetFullPathName(const char *lpFileName, int nBufferLength, char *lpBuffer, char **lpFilePart)\r
83 {\r
84   if (lpFileName[0] == '/')\r
85   {\r
86     strcpy (lpBuffer, lpFileName);\r
87     *lpFilePart = strrchr (lpBuffer, '/');\r
88     return strlen (lpBuffer);\r
89   }\r
90 \r
91   if (getcwd (lpBuffer, nBufferLength) == NULL)\r
92     return 0;\r
93 \r
94   strcat (lpBuffer, "/");\r
95   *lpFilePart = lpBuffer + strlen (lpBuffer);\r
96   strcat (lpBuffer, lpFileName);\r
97 \r
98   char *scr = lpBuffer, *dst = lpBuffer;\r
99   for (int i = 0; (i < nBufferLength) && (*scr != 0); i++)\r
100   {\r
101     if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')\r
102     {\r
103       scr += 4;\r
104       while (dst != lpBuffer && *dst != '/')\r
105       {\r
106         dst--;\r
107         i--;\r
108       }\r
109     }\r
110 \r
111     *dst = *scr;\r
112 \r
113     scr++; dst++;\r
114   }\r
115   *dst = 0;\r
116 \r
117   return strlen (lpBuffer);\r
118 }\r
119 /*\r
120 static void g_string_sprintfa_int (GString *string, const gchar *fmt, va_list args)\r
121 {\r
122   gchar *buffer;\r
123 \r
124   buffer = g_strdup_vprintf (fmt, args);\r
125   g_string_append (string, buffer);\r
126   g_free (buffer);\r
127 }\r
128 \r
129 const CString& CString::operator=(const char* lpsz)\r
130 {\r
131   g_string_assign (m_str, lpsz);\r
132   return *this;\r
133 }\r
134 \r
135 const CString& CString::operator+=(const char* lpsz)\r
136 {\r
137   g_string_append (m_str, lpsz);\r
138   return *this;\r
139 }\r
140 \r
141 CString::operator char*() const\r
142\r
143   return m_str->str;\r
144 }\r
145 \r
146 void CString::Format(const char* fmt, ...)\r
147 {\r
148   va_list args;\r
149  \r
150   g_string_truncate (m_str, 0);\r
151  \r
152   va_start (args, fmt);\r
153   g_string_sprintfa_int (m_str, fmt, args);\r
154   va_end (args);\r
155 }\r
156 \r
157 CString CString::Right(int nCount) const\r
158 {\r
159   if (nCount < 0)\r
160     nCount = 0;\r
161   else if (nCount > m_str->len)\r
162     nCount = m_str->len;\r
163 \r
164   CString dest (&m_str->str[m_str->len-nCount]);\r
165   return dest;\r
166 }\r
167 \r
168 CString CString::Left(int nCount) const\r
169 {\r
170   if (nCount < 0)\r
171     nCount = 0;\r
172   else if (nCount > m_str->len)\r
173     nCount = m_str->len;\r
174 \r
175   CString dest;\r
176   dest.m_str = g_string_sized_new (nCount);\r
177   memcpy (dest.m_str->str, m_str->str, nCount);\r
178   dest.m_str->str[nCount] = 0;\r
179   return dest;\r
180 }\r
181 \r
182 void CString::SetAt(int nIndex, char ch)\r
183 {\r
184   if (nIndex >= 0 && nIndex < m_str->len)\r
185     m_str->str[nIndex] = ch;\r
186 }\r
187 \r
188 char CString::GetAt(int nIndex) const\r
189 {\r
190   if (nIndex >= 0 && nIndex < m_str->len)\r
191     return m_str->str[nIndex];\r
192   return 0;\r
193 }\r
194 \r
195 char CString::operator[](int nIndex) const\r
196 {\r
197   if (nIndex >= 0 && nIndex < m_str->len)\r
198     return m_str->str[nIndex];\r
199   return 0;\r
200 }\r
201 */\r
202 \r
203 #endif\r