]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_head.c
FICK!!!!! i need super legit help!
[16.git] / src / lib / 16_head.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
3  *
4  * This file is part of Project 16.
5  *
6  * Project 16 is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Project 16 is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,
19  * Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include "src/lib/16_head.h"
24
25 /* Function: Wait **********************************************************\r
26 *\r
27 *     Parameters:    wait - time in microseconds\r
28 *\r
29 *     Description:    pauses for a specified number of microseconds.\r
30 *\r
31 */\r
32 void wait(clock_t wait){\r
33         clock_t goal;\r
34 \r
35         if(!wait) return;\r
36 \r
37         goal = wait + clock();\r
38         while((goal > clock()) && !kbhit()) ;\r
39 } /* End of wait */
40
41 void* AllocateLargestFreeBlock(size_t* Size)
42 {
43   size_t s0, s1;
44   void* p;
45
46   s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
47
48   while (s0 && (p = malloc(s0)) == NULL)
49     s0 >>= 1;
50
51   if (p)
52     free(p);
53
54   s1 = s0 >> 1;
55
56   while (s1)
57   {
58     if ((p = malloc(s0 + s1)) != NULL)
59     {
60       s0 += s1;
61       free(p);
62     }
63     s1 >>= 1;
64   }
65
66   while (s0 && (p = malloc(s0)) == NULL)
67     s0 ^= s0 & -s0;
68
69   *Size = s0;
70   return p;
71 }
72
73 size_t GetFreeSize(void)
74 {
75   size_t total = 0;
76   void* pFirst = NULL;
77   void* pLast = NULL;
78
79   for (;;)
80   {
81     size_t largest;
82     void* p = AllocateLargestFreeBlock(&largest);
83
84     if (largest < sizeof(void*))
85     {
86       if (p != NULL)
87         free(p);
88       break;
89     }
90
91     *(void**)p = NULL;
92
93     total += largest;
94
95     if (pFirst == NULL)
96       pFirst = p;
97
98     if (pLast != NULL)
99       *(void**)pLast = p;
100
101     pLast = p;
102   }
103
104   while (pFirst != NULL)
105   {
106     void* p = *(void**)pFirst;
107     free(pFirst);
108     pFirst = p;
109   }
110
111   return total;
112 }
113
114 void far* AllocateLargestFarFreeBlock(size_t far* Size)
115 {
116         size_t s0, s1;
117         void far* p;
118
119         s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
120         while (s0 && (p = _fmalloc(s0)) == NULL)
121                 s0 >>= 1;
122
123         if (p)
124                 _ffree(p);
125
126         s1 = s0 >> 1;
127         while (s1)
128         {
129                 if ((p = _fmalloc(s0 + s1)) != NULL)
130                 {
131                         s0 += s1;
132                         _ffree(p);
133                 }
134         s1 >>= 1;
135         }
136         while (s0 && (p = _fmalloc(s0)) == NULL)
137                 s0 ^= s0 & -s0;
138
139         *Size = s0;
140         return p;
141 }
142
143 size_t GetFarFreeSize(void)
144 {
145         size_t total = 0;
146         void far* pFirst = NULL;
147         void far* pLast = NULL;
148         for(;;)
149         {
150                 size_t largest;
151                 void far* p = AllocateLargestFarFreeBlock(&largest);
152                 if (largest < sizeof(void far*))
153                 {
154                         if (p != NULL)
155                         _ffree(p);
156                         break;
157                 }
158                 *(void far* far*)p = NULL;
159                 total += largest;
160                 if (pFirst == NULL)
161                         pFirst = p;
162
163                 if (pLast != NULL)
164                         *(void far* far*)pLast = p;
165                 pLast = p;
166         }
167
168         while (pFirst != NULL)
169         {
170                 void far* p = *(void far* far*)pFirst;
171                 _ffree(pFirst);
172                 pFirst = p;
173         }
174         return total;
175 }
176
177 long int
178 filesize(FILE *fp)\r
179 {\r
180         long int save_pos, size_of_file;\r
181 \r
182         save_pos = ftell(fp);\r
183         fseek(fp, 0L, SEEK_END);\r
184         size_of_file = ftell(fp);\r
185         fseek(fp, save_pos, SEEK_SET);\r
186         return(size_of_file);\r
187 }
188
189 ///////////////////////////////////////////////////////////////////////////\r
190 //\r
191 //      US_CheckParm() - checks to see if a string matches one of a set of\r
192 //              strings. The check is case insensitive. The routine returns the\r
193 //              index of the string that matched, or -1 if no matches were found\r
194 //\r
195 ///////////////////////////////////////////////////////////////////////////\r
196 int\r
197 US_CheckParm(char *parm,char **strings)\r
198 {\r
199         char    cp,cs,\r
200                         *p,*s;\r
201         int             i;\r
202 \r
203         while (!isalpha(*parm)) // Skip non-alphas\r
204                 parm++;\r
205 \r
206         for (i = 0;*strings && **strings;i++)\r
207         {\r
208                 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)\r
209                 {\r
210                         cs = *s++;\r
211                         if (!cs)\r
212                                 return(i);\r
213                         cp = *p++;\r
214 \r
215                         if (isupper(cs))\r
216                                 cs = tolower(cs);\r
217                         if (isupper(cp))\r
218                                 cp = tolower(cp);\r
219                 }\r
220         }\r
221         return(-1);\r
222 }
223
224 /*\r
225 ==========================\r
226 =\r
227 = Quit\r
228 =\r
229 ==========================\r
230 */\r
231 \r
232 /*void Quit(char *error, ...)\r
233 {\r
234         short exit_code=0;\r
235         unsigned        finscreen;\r
236 \r
237         va_list ap;\r
238 \r
239         va_start(ap,error);\r
240 \r
241 #ifndef CATALOG\r
242         if (!error)\r
243         {\r
244                 CA_SetAllPurge ();\r
245                 CA_CacheGrChunk (PIRACY);\r
246                 finscreen = (unsigned)grsegs[PIRACY];\r
247         }\r
248 #endif\r
249 \r
250         //ShutdownId ();\r
251 \r
252         if (error && *error)\r
253         {\r
254                 vprintf(error,ap);\r
255                 exit_code = 1;\r
256         }\r
257 #ifndef CATALOG\r
258         else\r
259         if (!NoWait)\r
260         {\r
261                 movedata (finscreen,0,0xb800,0,4000);\r
262                 bioskey (0);\r
263         }\r
264 #endif\r
265 \r
266         va_end(ap);\r
267 \r
268 #ifndef CATALOG\r
269         if (!error)\r
270         {\r
271                 _argc = 2;\r
272                 _argv[1] = "LAST.SHL";\r
273                 _argv[2] = "ENDSCN.SCN";\r
274                 _argv[3] = NULL;\r
275                 if (execv("LOADSCN.EXE", _argv) == -1)\r
276                 {\r
277                         clrscr();\r
278                         puts("Couldn't find executable LOADSCN.EXE.\n");\r
279                         exit(1);\r
280                 }\r
281         }\r
282 #endif\r
283 \r
284         exit(exit_code);\r
285 }*/