]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_head.c
16_ca needs huge amounts of work and I should remember what needs to be done soon...
[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 long int\r
26 filesize(FILE *fp)\r
27 {\r
28         long int save_pos, size_of_file;\r
29 \r
30         save_pos = ftell(fp);\r
31         fseek(fp, 0L, SEEK_END);\r
32         size_of_file = ftell(fp);\r
33         fseek(fp, save_pos, SEEK_SET);\r
34         return(size_of_file);\r
35 }\r
36 \r
37 // clrstdin() clear any leftover chars tha may be in stdin stream //\r
38 void clrstdin()\r
39 {\r
40    int ch = 0;\r
41    while( ( ch = getchar() ) != '\n' && ch != EOF );\r
42 }\r
43 \r
44 //from http://stackoverflow.com/questions/2736753/how-to-remove-extension-from-file-name\r
45 // remove_ext: removes the "extension" from a file spec.\r
46 //   mystr is the string to process.\r
47 //   dot is the extension separator.\r
48 //   sep is the path separator (0 means to ignore).\r
49 // Returns an allocated string identical to the original but\r
50 //   with the extension removed. It must be freed when you're\r
51 //   finished with it.\r
52 // If you pass in NULL or the new string can't be allocated,\r
53 //   it returns NULL.\r
54 \r
55 char *remove_ext (char* mystr, char dot, char sep) {\r
56         char *retstr, *lastdot, *lastsep;\r
57 \r
58         // Error checks and allocate string.\r
59         if (mystr == NULL)\r
60                 return NULL;\r
61         if ((retstr = malloc(strlen (mystr) + 1)) == NULL)\r
62                 return NULL;\r
63 \r
64         // Make a copy and find the relevant characters.\r
65 \r
66         strcpy (retstr, mystr);\r
67         lastdot = strrchr (retstr, dot);\r
68         lastsep = (sep == 0) ? NULL : strrchr (retstr, sep);\r
69 \r
70         // If it has an extension separator.\r
71 \r
72         if (lastdot != NULL) {\r
73                 // and it's before the extenstion separator.\r
74 \r
75                 if (lastsep != NULL) {\r
76                         if (lastsep < lastdot) {\r
77                                 // then remove it.\r
78 \r
79                                 *lastdot = '\0';\r
80                         }\r
81                 } else {\r
82                         // Has extension separator with no path separator.\r
83 \r
84                         *lastdot = '\0';\r
85                 }\r
86         }\r
87 \r
88         // Return the modified string.\r
89         free(mystr);\r
90         return retstr;\r
91 }\r
92 \r
93 \r
94 //from http://quiz.geeksforgeeks.org/c-program-cyclically-rotate-array-one/\r
95 void rotateR(byte *arr, byte n)\r
96 {\r
97         byte x = arr[n-1], i;\r
98         for (i = n-1; i > 0; i--)\r
99                 arr[i] = arr[i-1];\r
100         arr[0] = x;\r
101 }\r
102 \r
103 void rotateL(byte *arr, byte n)\r
104 {\r
105         byte x = arr[n+1], i;\r
106         for (i = n+1; i > 0; i++)\r
107                 arr[i] = arr[i+1];\r
108         arr[0] = x;\r
109 }\r
110 \r
111 void printmeminfoline(byte *strc, const byte *pee, size_t h_total, size_t h_used, size_t h_free)\r
112 {\r
113         byte str[64];\r
114         strcat(strc,pee); strcat(strc,"            "); ultoa((dword)h_total,str,10); strcat(strc,str);\r
115         if(strlen(str)<=4) strcat(strc,"        "); //printf("%u\n", strlen(str));\r
116         strcat(strc,"   "); ultoa((dword)h_used,str,10); strcat(strc,str); strcat(strc,"        "); strcat(strc,"  ");\r
117         ultoa((dword)h_free,str,10); strcat(strc,str);\r
118         strcat(strc,"\n");\r
119 }\r
120 \r
121 ///////////////////////////////////////////////////////////////////////////\r
122 //\r
123 //      US_CheckParm() - checks to see if a string matches one of a set of\r
124 //              strings. The check is case insensitive. The routine returns the\r
125 //              index of the string that matched, or -1 if no matches were found\r
126 //\r
127 ///////////////////////////////////////////////////////////////////////////\r
128 int\r
129 US_CheckParm(char *parm,char **strings)\r
130 {\r
131         char    cp,cs,\r
132                         *p,*s;\r
133         int             i;\r
134 \r
135         while (!isalpha(*parm)) // Skip non-alphas\r
136                 parm++;\r
137 \r
138         for (i = 0;*strings && **strings;i++)\r
139         {\r
140                 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)\r
141                 {\r
142                         cs = *s++;\r
143                         if (!cs)\r
144                                 return(i);\r
145                         cp = *p++;\r
146 \r
147                         if (isupper(cs))\r
148                                 cs = tolower(cs);\r
149                         if (isupper(cp))\r
150                                 cp = tolower(cp);\r
151                 }\r
152         }\r
153         return(-1);\r
154 }\r
155 \r
156 // for input test //\r
157 byte dirchar(byte in)\r
158 {\r
159         byte out;\r
160         switch(in)\r
161         {\r
162                 case 0: //up\r
163                         out = 0x1E;\r
164                 break;\r
165                 case 4: //down\r
166                         out = 0x1F;\r
167                 break;\r
168                 case 1: //left\r
169                         out = 0x11;\r
170                 break;\r
171                 case 3: //right\r
172                         out = 0x10;\r
173                 break;\r
174                 default: //null\r
175                         out = 0xB3;\r
176                 break;\r
177         }\r
178         return out;\r
179 }\r
180 \r
181 //from: http://stackoverflow.com/questions/5349896/print-a-struct-in-c\r
182 void print_mem(void const *vp, size_t n)\r
183 {\r
184         size_t i;\r
185         unsigned char const *p = vp;\r
186         for (i=0; i<n; i++)\r
187         {\r
188                 printf("%02x", p[i]);\r
189                 //printf("%c", p[i]);\r
190                 if((!(i%16)) && i) printf("\n");\r
191                 else printf(" ");\r
192                 //printf("%u%%40=%u\n", i, i%40);\r
193         }\r
194         putchar('\n');\r
195         printf("\nstruct size is %zu bytes\n", n);\r
196 };\r