]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_head.c
before showmem workings
[16.git] / src / lib / 16_head.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 \r
23 #include "src/lib/16_head.h"\r
24 \r
25 // big global status text buffer\r
26 char global_temp_status_text[512];\r
27 char global_temp_status_text2[512];\r
28 \r
29 long int\r
30 filesize(FILE *fp)\r
31 {\r
32         long int save_pos, size_of_file;\r
33 \r
34         save_pos = ftell(fp);\r
35         fseek(fp, 0L, SEEK_END);\r
36         size_of_file = ftell(fp);\r
37         fseek(fp, save_pos, SEEK_SET);\r
38         return(size_of_file);\r
39 }\r
40 \r
41 // clrstdin() clear any leftover chars tha may be in stdin stream //\r
42 void clrstdin()\r
43 {\r
44    int ch = 0;\r
45    while( ( ch = getchar() ) != '\n' && ch != EOF );\r
46 }\r
47 \r
48 //from http://stackoverflow.com/questions/2736753/how-to-remove-extension-from-file-name\r
49 // remove_ext: removes the "extension" from a file spec.\r
50 //   mystr is the string to process.\r
51 //   dot is the extension separator.\r
52 //   sep is the path separator (0 means to ignore).\r
53 // Returns an allocated string identical to the original but\r
54 //   with the extension removed. It must be freed when you're\r
55 //   finished with it.\r
56 // If you pass in NULL or the new string can't be allocated,\r
57 //   it returns NULL.\r
58 \r
59 char *remove_ext (char* mystr, char dot, char sep) {\r
60         char *retstr, *lastdot, *lastsep;\r
61 \r
62         // Error checks and allocate string.\r
63         if (mystr == NULL)\r
64                 return NULL;\r
65         if ((retstr = malloc(strlen (mystr) + 1)) == NULL)\r
66                 return NULL;\r
67 \r
68         // Make a copy and find the relevant characters.\r
69 \r
70         strcpy (retstr, mystr);\r
71         lastdot = strrchr (retstr, dot);\r
72         lastsep = (sep == 0) ? NULL : strrchr (retstr, sep);\r
73 \r
74         // If it has an extension separator.\r
75 \r
76         if (lastdot != NULL) {\r
77                 // and it's before the extenstion separator.\r
78 \r
79                 if (lastsep != NULL) {\r
80                         if (lastsep < lastdot) {\r
81                                 // then remove it.\r
82 \r
83                                 *lastdot = '\0';\r
84                         }\r
85                 } else {\r
86                         // Has extension separator with no path separator.\r
87 \r
88                         *lastdot = '\0';\r
89                 }\r
90         }\r
91 \r
92         // Return the modified string.\r
93         free(mystr);\r
94         return retstr;\r
95 }\r
96 \r
97 \r
98 //from http://quiz.geeksforgeeks.org/c-program-cyclically-rotate-array-one/\r
99 void rotateR(byte *arr, byte n)\r
100 {\r
101         byte x = arr[n-1], i;\r
102         for (i = n-1; i > 0; i--)\r
103                 arr[i] = arr[i-1];\r
104         arr[0] = x;\r
105 }\r
106 \r
107 void rotateL(byte *arr, byte n)\r
108 {\r
109         byte x = arr[n+1], i;\r
110         for (i = n+1; i > 0; i++)\r
111                 arr[i] = arr[i+1];\r
112         arr[0] = x;\r
113 }\r
114 \r
115 void printmeminfoline(byte *strc, const byte *pee, size_t h_total, size_t h_used, size_t h_free)\r
116 {\r
117         byte str[64];\r
118         strcat(strc,pee); strcat(strc,"            "); ultoa((dword)h_total,str,10); strcat(strc,str);\r
119         if(strlen(str)<=4) strcat(strc,"        "); //printf("%u\n", strlen(str));\r
120         strcat(strc,"   "); ultoa((dword)h_used,str,10); strcat(strc,str); strcat(strc,"        "); strcat(strc,"  ");\r
121         ultoa((dword)h_free,str,10); strcat(strc,str);\r
122         strcat(strc,"\n");\r
123 }\r
124 \r
125 ///////////////////////////////////////////////////////////////////////////\r
126 //\r
127 //      US_CheckParm() - checks to see if a string matches one of a set of\r
128 //              strings. The check is case insensitive. The routine returns the\r
129 //              index of the string that matched, or -1 if no matches were found\r
130 //\r
131 ///////////////////////////////////////////////////////////////////////////\r
132 int\r
133 US_CheckParm(char *parm,char **strings)\r
134 {\r
135         char    cp,cs,\r
136                         *p,*s;\r
137         int             i;\r
138 \r
139         while (!isalpha(*parm)) // Skip non-alphas\r
140                 parm++;\r
141 \r
142         for (i = 0;*strings && **strings;i++)\r
143         {\r
144                 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)\r
145                 {\r
146                         cs = *s++;\r
147                         if (!cs)\r
148                                 return(i);\r
149                         cp = *p++;\r
150 \r
151                         if (isupper(cs))\r
152                                 cs = tolower(cs);\r
153                         if (isupper(cp))\r
154                                 cp = tolower(cp);\r
155                 }\r
156         }\r
157         return(-1);\r
158 }\r
159 \r
160 // for input test //\r
161 byte dirchar(byte in)\r
162 {\r
163         byte out;\r
164         switch(in)\r
165         {\r
166                 case 0: //up\r
167                         out = 0x1E;\r
168                 break;\r
169                 case 4: //down\r
170                         out = 0x1F;\r
171                 break;\r
172                 case 1: //left\r
173                         out = 0x11;\r
174                 break;\r
175                 case 3: //right\r
176                         out = 0x10;\r
177                 break;\r
178                 default: //null\r
179                         out = 0xB3;\r
180                 break;\r
181         }\r
182         return out;\r
183 }\r
184 \r
185 //from: http://stackoverflow.com/questions/5349896/print-a-struct-in-c\r
186 void print_mem(void const *vp, size_t n)\r
187 {\r
188         size_t i;\r
189         unsigned char const *p = vp;\r
190         for (i=0; i<n; i++)\r
191         {\r
192                 printf("%02x", p[i]);\r
193                 //printf("%c", p[i]);\r
194                 if((!(i%16)) && i) printf("\n");\r
195                 else printf(" ");\r
196                 //printf("%u%%40=%u\n", i, i%40);\r
197         }\r
198         putchar('\n');\r
199         printf("\nstruct size is %zu bytes\n", n);\r
200 };\r
201 \r
202 //from: https://groups.google.com/forum/#!topic/comp.lang.asm.x86/QtuVXl43nDo\r
203 void hres (void)\r
204 {\r
205         __asm {\r
206                 mov     ax,3\r
207                 int     10h\r
208                 mov     ax,1112h\r
209                 xor     bx,bx\r
210                 int     10h\r
211         }\r
212 }\r