diff options
Diffstat (limited to 'apps/plugins/zxbox/misc.c')
-rw-r--r-- | apps/plugins/zxbox/misc.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/apps/plugins/zxbox/misc.c b/apps/plugins/zxbox/misc.c new file mode 100644 index 0000000000..a5a3c6bbb4 --- /dev/null +++ b/apps/plugins/zxbox/misc.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* | ||
2 | * Copyright (C) 1996-1998 Szeredi Miklos | ||
3 | * Email: mszeredi@inf.bme.hu | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. See the file COPYING. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | #include "misc.h" | ||
22 | #include "zxconfig.h" | ||
23 | #include <stdio.h> | ||
24 | #include <string.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <ctype.h> | ||
27 | #include "helpers.h" | ||
28 | /*#include <errno.h>*/ | ||
29 | #include "zxconfig.h" | ||
30 | #define DIR_SEP_CHAR '/' | ||
31 | |||
32 | char *get_base_name(char *fname) | ||
33 | { | ||
34 | char *p; | ||
35 | |||
36 | p = fname; | ||
37 | for(; *p; p++); | ||
38 | for(; p >= fname && *p != DIR_SEP_CHAR; p--); | ||
39 | return ++p; | ||
40 | } | ||
41 | |||
42 | |||
43 | int check_ext(const char *filename, const char *ext) | ||
44 | { | ||
45 | int flen, elen; | ||
46 | int i; | ||
47 | |||
48 | flen = (int) rb->strlen(filename); | ||
49 | elen = (int) rb->strlen(ext); | ||
50 | |||
51 | if(flen <= elen + 1) return 0; | ||
52 | |||
53 | if(filename[flen-elen-1] != '.') return 0; | ||
54 | for(i = 0; i < elen; i++) if(filename[flen-elen+i] != toupper(ext[i])) break; | ||
55 | if(i == elen) return 1; | ||
56 | for(i = 0; i < elen; i++) if(filename[flen-elen+i] != tolower(ext[i])) break; | ||
57 | if(i == elen) return 1; | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | void add_extension(char *filename, const char *ext) | ||
62 | { | ||
63 | int i; | ||
64 | int upper; | ||
65 | |||
66 | i = (int) rb->strlen(filename); | ||
67 | if(filename[i] > 64 && filename[i] < 96) upper = 1; | ||
68 | else upper = 0; | ||
69 | |||
70 | filename[i++] = '.'; | ||
71 | if(upper) | ||
72 | for(; *ext; i++, ext++) filename[i] = toupper(*ext); | ||
73 | else | ||
74 | for(; *ext; i++, ext++) filename[i] = tolower(*ext); | ||
75 | } | ||
76 | |||
77 | int file_exist(const char *filename) | ||
78 | { | ||
79 | /*FILE *fp;*/ | ||
80 | int fd; | ||
81 | |||
82 | fd = rb->open(filename, O_RDONLY); | ||
83 | if(fd != NULL) { | ||
84 | rb->close(fd); | ||
85 | return 1; | ||
86 | } | ||
87 | else return 0; | ||
88 | /* if(errno == ENOENT) return 0; | ||
89 | return 1;*/ | ||
90 | } | ||
91 | |||
92 | int try_extension(char *filename, const char *ext) | ||
93 | { | ||
94 | int tend; | ||
95 | |||
96 | tend = (int) rb->strlen(filename); | ||
97 | add_extension(filename, ext); | ||
98 | if(file_exist(filename)) return 1; | ||
99 | |||
100 | filename[tend] = '\0'; | ||
101 | return 0; | ||
102 | } | ||
103 | |||
104 | void *malloc_err(size_t size) | ||
105 | { | ||
106 | char *p; | ||
107 | |||
108 | p = (char *) my_malloc(size); | ||
109 | if(p == NULL) { | ||
110 | // fprintf(stderr, "Out of memory!\n"); | ||
111 | /*exit(1);*/ | ||
112 | } | ||
113 | return (void *) p; | ||
114 | } | ||
115 | |||
116 | char *make_string(char *ostr, const char *nstr) | ||
117 | { | ||
118 | if(ostr != NULL) /*free(ostr)*/ostr=0; | ||
119 | ostr = malloc_err(rb->strlen(nstr) + 1); | ||
120 | rb->strcpy(ostr, nstr); | ||
121 | return ostr; | ||
122 | } | ||
123 | |||
124 | void free_string(char *ostr) | ||
125 | { | ||
126 | if(ostr != NULL) /*free(ostr)*/ostr=0; | ||
127 | } | ||
128 | |||
129 | int mis_strcasecmp(const char *s1, const char *s2) | ||
130 | { | ||
131 | int c1, c2; | ||
132 | |||
133 | for(;; s1++, s2++) { | ||
134 | c1 = tolower(*s1); | ||
135 | c2 = tolower(*s2); | ||
136 | |||
137 | if(!c1 || c1 != c2) break; | ||
138 | } | ||
139 | return c1-c2; | ||
140 | } | ||