diff options
Diffstat (limited to 'apps/plugins/zxbox/zxmisc.c')
-rw-r--r-- | apps/plugins/zxbox/zxmisc.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/apps/plugins/zxbox/zxmisc.c b/apps/plugins/zxbox/zxmisc.c new file mode 100644 index 0000000000..df147be7bf --- /dev/null +++ b/apps/plugins/zxbox/zxmisc.c | |||
@@ -0,0 +1,139 @@ | |||
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 "zxmisc.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 "zxconfig.h" | ||
29 | #define DIR_SEP_CHAR '/' | ||
30 | |||
31 | char *get_base_name(char *fname) | ||
32 | { | ||
33 | char *p; | ||
34 | |||
35 | p = fname; | ||
36 | for(; *p; p++); | ||
37 | for(; p >= fname && *p != DIR_SEP_CHAR; p--); | ||
38 | return ++p; | ||
39 | } | ||
40 | |||
41 | |||
42 | int check_ext(const char *filename, const char *ext) | ||
43 | { | ||
44 | int flen, elen; | ||
45 | int i; | ||
46 | |||
47 | flen = (int) rb->strlen(filename); | ||
48 | elen = (int) rb->strlen(ext); | ||
49 | |||
50 | if(flen <= elen + 1) return 0; | ||
51 | |||
52 | if(filename[flen-elen-1] != '.') return 0; | ||
53 | for(i = 0; i < elen; i++) if(filename[flen-elen+i] != toupper(ext[i])) break; | ||
54 | if(i == elen) return 1; | ||
55 | for(i = 0; i < elen; i++) if(filename[flen-elen+i] != tolower(ext[i])) break; | ||
56 | if(i == elen) return 1; | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | void add_extension(char *filename, const char *ext) | ||
61 | { | ||
62 | int i; | ||
63 | int upper; | ||
64 | |||
65 | i = (int) rb->strlen(filename); | ||
66 | if(filename[i] > 64 && filename[i] < 96) upper = 1; | ||
67 | else upper = 0; | ||
68 | |||
69 | filename[i++] = '.'; | ||
70 | if(upper) | ||
71 | for(; *ext; i++, ext++) filename[i] = toupper(*ext); | ||
72 | else | ||
73 | for(; *ext; i++, ext++) filename[i] = tolower(*ext); | ||
74 | } | ||
75 | |||
76 | int file_exist(const char *filename) | ||
77 | { | ||
78 | /*FILE *fp;*/ | ||
79 | int fd; | ||
80 | |||
81 | fd = rb->open(filename, O_RDONLY); | ||
82 | if(fd != NULL) { | ||
83 | rb->close(fd); | ||
84 | return 1; | ||
85 | } | ||
86 | else return 0; | ||
87 | /* if(errno == ENOENT) return 0; | ||
88 | return 1;*/ | ||
89 | } | ||
90 | |||
91 | int try_extension(char *filename, const char *ext) | ||
92 | { | ||
93 | int tend; | ||
94 | |||
95 | tend = (int) rb->strlen(filename); | ||
96 | add_extension(filename, ext); | ||
97 | if(file_exist(filename)) return 1; | ||
98 | |||
99 | filename[tend] = '\0'; | ||
100 | return 0; | ||
101 | } | ||
102 | |||
103 | void *malloc_err(size_t size) | ||
104 | { | ||
105 | char *p; | ||
106 | |||
107 | p = (char *) my_malloc(size); | ||
108 | if(p == NULL) { | ||
109 | // fprintf(stderr, "Out of memory!\n"); | ||
110 | /*exit(1);*/ | ||
111 | } | ||
112 | return (void *) p; | ||
113 | } | ||
114 | |||
115 | char *make_string(char *ostr, const char *nstr) | ||
116 | { | ||
117 | if(ostr != NULL) /*free(ostr)*/ostr=0; | ||
118 | ostr = malloc_err(rb->strlen(nstr) + 1); | ||
119 | rb->strcpy(ostr, nstr); | ||
120 | return ostr; | ||
121 | } | ||
122 | |||
123 | void free_string(char *ostr) | ||
124 | { | ||
125 | if(ostr != NULL) /*free(ostr)*/ostr=0; | ||
126 | } | ||
127 | |||
128 | int mis_strcasecmp(const char *s1, const char *s2) | ||
129 | { | ||
130 | int c1, c2; | ||
131 | |||
132 | for(;; s1++, s2++) { | ||
133 | c1 = tolower(*s1); | ||
134 | c2 = tolower(*s2); | ||
135 | |||
136 | if(!c1 || c1 != c2) break; | ||
137 | } | ||
138 | return c1-c2; | ||
139 | } | ||