Build doom on clipv2 and clip+
[kugel-rb.git] / apps / plugins / zxbox / zxmisc.c
blob324fb484d2cd5e9cdf05e5472d7e646ae37efed6
1 /*
2 * Copyright (C) 1996-1998 Szeredi Miklos
3 * Email: mszeredi@inf.bme.hu
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.
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.
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.
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 #define DIR_SEP_CHAR '/'
30 char *get_base_name(char *fname)
32 char *p;
34 p = fname;
35 for(; *p; p++);
36 for(; p >= fname && *p != DIR_SEP_CHAR; p--);
37 return ++p;
41 int check_ext(const char *filename, const char *ext)
43 int flen, elen;
44 int i;
46 flen = (int) rb->strlen(filename);
47 elen = (int) rb->strlen(ext);
49 if(flen <= elen + 1) return 0;
51 if(filename[flen-elen-1] != '.') return 0;
52 for(i = 0; i < elen; i++) if(filename[flen-elen+i] != toupper(ext[i])) break;
53 if(i == elen) return 1;
54 for(i = 0; i < elen; i++) if(filename[flen-elen+i] != tolower(ext[i])) break;
55 if(i == elen) return 1;
56 return 0;
59 void add_extension(char *filename, const char *ext)
61 int i;
62 int upper;
64 i = (int) rb->strlen(filename);
65 if(filename[i] > 64 && filename[i] < 96) upper = 1;
66 else upper = 0;
68 filename[i++] = '.';
69 if(upper)
70 for(; *ext; i++, ext++) filename[i] = toupper(*ext);
71 else
72 for(; *ext; i++, ext++) filename[i] = tolower(*ext);
75 int file_exist(const char *filename)
77 /*FILE *fp;*/
78 int fd;
80 fd = rb->open(filename, O_RDONLY);
81 if(fd >= 0) {
82 rb->close(fd);
83 return 1;
85 else return 0;
86 /* if(errno == ENOENT) return 0;
87 return 1;*/
90 int try_extension(char *filename, const char *ext)
92 int tend;
94 tend = (int) rb->strlen(filename);
95 add_extension(filename, ext);
96 if(file_exist(filename)) return 1;
98 filename[tend] = '\0';
99 return 0;
102 void *malloc_err(size_t size)
104 char *p;
106 p = (char *) my_malloc(size);
107 if(p == NULL) {
108 // fprintf(stderr, "Out of memory!\n");
109 /*exit(1);*/
111 return (void *) p;
114 char *make_string(char *ostr, const char *nstr)
116 if(ostr != NULL) /*free(ostr)*/ostr=0;
117 ostr = malloc_err(rb->strlen(nstr) + 1);
118 rb->strcpy(ostr, nstr);
119 return ostr;
122 void free_string(char *ostr)
124 if(ostr != NULL) /*free(ostr)*/ostr=0;
127 int mis_strcasecmp(const char *s1, const char *s2)
129 int c1, c2;
131 for(;; s1++, s2++) {
132 c1 = tolower(*s1);
133 c2 = tolower(*s2);
135 if(!c1 || c1 != c2) break;
137 return c1-c2;