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