OSDN Git Service

modified: src/lib/16_ca.c
[proj16/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 /* local function */\r
26 void wait(clock_t wait);
27 void* AllocateLargestFreeBlock(size_t* Size);
28 size_t GetFreeSize(void);
29 long int filesize(FILE *fp);\r
30 \r
31 /* Function: Wait **********************************************************\r
32 *\r
33 *     Parameters:    wait - time in microseconds\r
34 *\r
35 *     Description:    pauses for a specified number of microseconds.\r
36 *\r
37 */\r
38 void wait(clock_t wait){\r
39         clock_t goal;\r
40 \r
41         if(!wait) return;\r
42 \r
43         goal = wait + clock();\r
44         while((goal > clock()) && !kbhit()) ;\r
45 } /* End of wait */
46
47 void* AllocateLargestFreeBlock(size_t* Size)
48 {
49   size_t s0, s1;
50   void* p;
51
52   s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
53
54   while (s0 && (p = malloc(s0)) == NULL)
55     s0 >>= 1;
56
57   if (p)
58     free(p);
59
60   s1 = s0 >> 1;
61
62   while (s1)
63   {
64     if ((p = malloc(s0 + s1)) != NULL)
65     {
66       s0 += s1;
67       free(p);
68     }
69     s1 >>= 1;
70   }
71
72   while (s0 && (p = malloc(s0)) == NULL)
73     s0 ^= s0 & -s0;
74
75   *Size = s0;
76   return p;
77 }
78
79 size_t GetFreeSize(void)
80 {
81   size_t total = 0;
82   void* pFirst = NULL;
83   void* pLast = NULL;
84
85   for (;;)
86   {
87     size_t largest;
88     void* p = AllocateLargestFreeBlock(&largest);
89
90     if (largest < sizeof(void*))
91     {
92       if (p != NULL)
93         free(p);
94       break;
95     }
96
97     *(void**)p = NULL;
98
99     total += largest;
100
101     if (pFirst == NULL)
102       pFirst = p;
103
104     if (pLast != NULL)
105       *(void**)pLast = p;
106
107     pLast = p;
108   }
109
110   while (pFirst != NULL)
111   {
112     void* p = *(void**)pFirst;
113     free(pFirst);
114     pFirst = p;
115   }
116
117   return total;
118 }
119
120 long int
121 filesize(FILE *fp)\r
122 {\r
123         long int save_pos, size_of_file;\r
124 \r
125         save_pos = ftell(fp);\r
126         fseek(fp, 0L, SEEK_END);\r
127         size_of_file = ftell(fp);\r
128         fseek(fp, save_pos, SEEK_SET);\r
129         return(size_of_file);\r
130 }
131
132 ///////////////////////////////////////////////////////////////////////////\r
133 //\r
134 //      US_CheckParm() - checks to see if a string matches one of a set of\r
135 //              strings. The check is case insensitive. The routine returns the\r
136 //              index of the string that matched, or -1 if no matches were found\r
137 //\r
138 ///////////////////////////////////////////////////////////////////////////\r
139 int\r
140 US_CheckParm(char *parm,char **strings)\r
141 {\r
142         char    cp,cs,\r
143                         *p,*s;\r
144         int             i;\r
145 \r
146         while (!isalpha(*parm)) // Skip non-alphas\r
147                 parm++;\r
148 \r
149         for (i = 0;*strings && **strings;i++)\r
150         {\r
151                 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)\r
152                 {\r
153                         cs = *s++;\r
154                         if (!cs)\r
155                                 return(i);\r
156                         cp = *p++;\r
157 \r
158                         if (isupper(cs))\r
159                                 cs = tolower(cs);\r
160                         if (isupper(cp))\r
161                                 cp = tolower(cp);\r
162                 }\r
163         }\r
164         return(-1);\r
165 }
166
167 /*\r
168 ==========================\r
169 =\r
170 = Quit\r
171 =\r
172 ==========================\r
173 */\r
174 \r
175 /*void Quit(char *error, ...)\r
176 {\r
177         short exit_code=0;\r
178         unsigned        finscreen;\r
179 \r
180         va_list ap;\r
181 \r
182         va_start(ap,error);\r
183 \r
184 #ifndef CATALOG\r
185         if (!error)\r
186         {\r
187                 CA_SetAllPurge ();\r
188                 CA_CacheGrChunk (PIRACY);\r
189                 finscreen = (unsigned)grsegs[PIRACY];\r
190         }\r
191 #endif\r
192 \r
193         //ShutdownId ();\r
194 \r
195         if (error && *error)\r
196         {\r
197                 vprintf(error,ap);\r
198                 exit_code = 1;\r
199         }\r
200 #ifndef CATALOG\r
201         else\r
202         if (!NoWait)\r
203         {\r
204                 movedata (finscreen,0,0xb800,0,4000);\r
205                 bioskey (0);\r
206         }\r
207 #endif\r
208 \r
209         va_end(ap);\r
210 \r
211 #ifndef CATALOG\r
212         if (!error)\r
213         {\r
214                 _argc = 2;\r
215                 _argv[1] = "LAST.SHL";\r
216                 _argv[2] = "ENDSCN.SCN";\r
217                 _argv[3] = NULL;\r
218                 if (execv("LOADSCN.EXE", _argv) == -1)\r
219                 {\r
220                         clrscr();\r
221                         puts("Couldn't find executable LOADSCN.EXE.\n");\r
222                         exit(1);\r
223                 }\r
224         }\r
225 #endif\r
226 \r
227         exit(exit_code);\r
228 }*/