OSDN Git Service

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