summaryrefslogtreecommitdiffstats
path: root/src/jake2/sys/Sys.java
blob: aeb30fc4dc20074e2bc44dbccfa0ace16eb345f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Sys.java
 * Copyright (C) 2003
 * 
 * $Id: Sys.java,v 1.10 2004-12-14 00:11:01 hzi Exp $
 */
/*
 Copyright (C) 1997-2001 Id Software, Inc.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 */
package jake2.sys;

import jake2.Defines;
import jake2.Globals;
import jake2.client.CL;
import jake2.qcommon.Com;

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Sys
 */
public final class Sys extends Defines {

    public static void Error(String error) {

        CL.Shutdown();
        //StackTrace();
        new Exception(error).printStackTrace();
        System.exit(1);
    }

    public static void Quit() {
        CL.Shutdown();

        System.exit(0);
    }

    //ok!
    public static File[] FindAll(String path, int musthave, int canthave) {

        int index = path.lastIndexOf('/');

        if (index != -1) {
            findbase = path.substring(0, index);
            findpattern = path.substring(index + 1, path.length());
        } else {
            findbase = path;
            findpattern = "*";
        }

        if (findpattern.equals("*.*")) {
            findpattern = "*";
        }

        File fdir = new File(findbase);

        if (!fdir.exists())
            return null;

        FilenameFilter filter = new FileFilter(findpattern, musthave, canthave);

        return fdir.listFiles(filter);
    }

    /**
     * Match the pattern findpattern against the filename.
     * 
     * In the pattern string, `*' matches any sequence of characters, `?'
     * matches any character, [SET] matches any character in the specified set,
     * [!SET] matches any character not in the specified set. A set is composed
     * of characters or ranges; a range looks like character hyphen character
     * (as in 0-9 or A-Z). [0-9a-zA-Z_] is the set of characters allowed in C
     * identifiers. Any other character in the pattern must be matched exactly.
     * To suppress the special syntactic significance of any of `[]*?!-\', and
     * match the character exactly, precede it with a `\'.
     */
    static class FileFilter implements FilenameFilter {

        String regexpr;

        int musthave, canthave;

        FileFilter(String findpattern, int musthave, int canthave) {
            this.regexpr = convert2regexpr(findpattern);
            this.musthave = musthave;
            this.canthave = canthave;

        }

        /*
         * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
         */
        public boolean accept(File dir, String name) {
            if (name.matches(regexpr)) {
                return CompareAttributes(dir, musthave, canthave);
            }
            return false;
        }

        String convert2regexpr(String pattern) {

            StringBuffer sb = new StringBuffer();

            char c;
            boolean escape = false;

            String subst;

            // convert pattern
            for (int i = 0; i < pattern.length(); i++) {
                c = pattern.charAt(i);
                subst = null;
                switch (c) {
                case '*':
                    subst = (!escape) ? ".*" : "*";
                    break;
                case '.':
                    subst = (!escape) ? "\\." : ".";
                    break;
                case '!':
                    subst = (!escape) ? "^" : "!";
                    break;
                case '?':
                    subst = (!escape) ? "." : "?";
                    break;
                case '\\':
                    escape = !escape;
                    break;
                default:
                    escape = false;
                }
                if (subst != null) {
                    sb.append(subst);
                    escape = false;
                } else
                    sb.append(c);
            }

            // the converted pattern
            String regexpr = sb.toString();

            //Com.DPrintf("pattern: " + pattern + " regexpr: " + regexpr +
            // '\n');
            try {
                Pattern.compile(regexpr);
            } catch (PatternSyntaxException e) {
                Com.Printf("invalid file pattern ( *.* is used instead )\n");
                return ".*"; // the default
            }
            return regexpr;
        }

        boolean CompareAttributes(File dir, int musthave, int canthave) {
            // . and .. never match
            String name = dir.getName();

            if (name.equals(".") || name.equals(".."))
                return false;

            return true;
        }

    }

    private static long secbase = System.currentTimeMillis();

    public static int Milliseconds() {
        return Globals.curtime = (int) (System.currentTimeMillis() - secbase);
    }

    //============================================

    static File[] fdir;

    static int fileindex;

    static String findbase;

    static String findpattern;

    // ok.
    public static File FindFirst(String path, int musthave, int canthave) {

        if (fdir != null)
            Sys.Error("Sys_BeginFind without close");

        //	COM_FilePath (path, findbase);

        fdir = FindAll(path, canthave, musthave);
        fileindex = 0;

        if (fdir == null)
            return null;

        return FindNext();
    }

    public static File FindNext() {

        if (fileindex >= fdir.length)
            return null;

        return fdir[fileindex++];
    }

    public static void FindClose() {
        fdir = null;
    }

    public static void SendKeyEvents() {
		Globals.re.getKeyboardHandler().Update();

        // grab frame time
        Globals.sys_frame_time = Sys.Milliseconds();
    }

    public static String GetClipboardData() {
        // TODO: implement GetClipboardData
        return null;
    }

    public static void ConsoleOutput(String msg) {
        if (Globals.nostdout != null && Globals.nostdout.value != 0)
            return;

        System.out.print(msg);
    }

}