diff options
Diffstat (limited to 'src/java/org/anarres')
-rw-r--r-- | src/java/org/anarres/cpp/FileLexerSource.java | 21 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Preprocessor.java | 40 | ||||
-rw-r--r-- | src/java/org/anarres/cpp/Source.java | 8 |
3 files changed, 39 insertions, 30 deletions
diff --git a/src/java/org/anarres/cpp/FileLexerSource.java b/src/java/org/anarres/cpp/FileLexerSource.java index 3cf8604..4edf4fb 100644 --- a/src/java/org/anarres/cpp/FileLexerSource.java +++ b/src/java/org/anarres/cpp/FileLexerSource.java @@ -37,14 +37,15 @@ import static org.anarres.cpp.Token.*; * @see Source */ public class FileLexerSource extends LexerSource { - private File file; + // private File file; + private String path; /** * Creates a new Source for lexing the given File. * * Preprocessor directives are honoured within the file. */ - public FileLexerSource(File file) + public FileLexerSource(File file, String path) throws IOException { super( new BufferedReader( @@ -55,7 +56,13 @@ public class FileLexerSource extends LexerSource { true ); - this.file = file; + // this.file = file; + this.path = path; + } + + public FileLexerSource(File file) + throws IOException { + this(file, file.getPath()); } public FileLexerSource(String path) @@ -64,16 +71,16 @@ public class FileLexerSource extends LexerSource { } @Override - /* pp */ File getFile() { - return file; + /* pp */ String getPath() { + return path; } @Override /* pp */ String getName() { - return String.valueOf(file); + return getPath(); } public String toString() { - return "file " + file; + return "file " + path; } } diff --git a/src/java/org/anarres/cpp/Preprocessor.java b/src/java/org/anarres/cpp/Preprocessor.java index c6e98cb..0547399 100644 --- a/src/java/org/anarres/cpp/Preprocessor.java +++ b/src/java/org/anarres/cpp/Preprocessor.java @@ -939,38 +939,38 @@ public class Preprocessor { } /** - * Includes the given file. + * Attempts to include the given file. * * User code may override this method to implement a virtual * file system. - * - * XXX This API subject to change, using File makes no sense - * here. */ - protected boolean include(File file) + protected boolean include(String path) throws IOException, LexerException { + File file = new File(path); // System.out.println("Try to include " + file); - if (!file.exists()) - return false; if (!file.isFile()) return false; push_source(new FileLexerSource(file), true); return true; } + protected String include_dirname(String path) { + return (new File(path)).getParentFile().getPath(); + } + + protected String include_filename(String dir, String name) { + return dir + File.separator + name; + } + /** * Includes a file from an include path, by name. - * - * XXX This API subject to change, using File makes no sense - * here. */ - protected boolean include(Iterable<String> path, String name) + private boolean include(Iterable<String> path, String name) throws IOException, LexerException { for (String dir : path) { - File file = new File(dir + File.separator + name); - if (include(file)) + if (include(include_filename(dir, name))) return true; } return false; @@ -983,16 +983,18 @@ public class Preprocessor { * for the include directive, for example, creating a Source * based on a virtual file system. */ - protected void include(File parent, int line, + protected void include(String parent, int line, String name, boolean quoted) throws IOException, LexerException { if (quoted) { - File dir = parent.getAbsoluteFile().getParentFile(); + String dir = include_dirname(parent); + String path; if (dir == null) - dir = new File("/"); // XXX bleh. - File file = new File(dir, name); - if (include(file)) + path = name; + else + path = include_filename(dir, name); + if (include(path)) return; if (include(quoteincludepath, name)) return; @@ -1064,7 +1066,7 @@ public class Preprocessor { } /* Do the inclusion. */ - include(source.getFile(), tok.getLine(), name, quoted); + include(source.getPath(), tok.getLine(), name, quoted); /* 'tok' is the 'nl' after the include. We use it after the * #line directive. */ diff --git a/src/java/org/anarres/cpp/Source.java b/src/java/org/anarres/cpp/Source.java index f89b1f8..0f9efa4 100644 --- a/src/java/org/anarres/cpp/Source.java +++ b/src/java/org/anarres/cpp/Source.java @@ -133,12 +133,12 @@ public abstract class Source implements Iterable<Token> { * it will ask the parent Source, and so forth recursively. * If no Source on the stack is a FileLexerSource, returns null. */ - /* pp */ File getFile() { + /* pp */ String getPath() { Source parent = getParent(); while (parent != null) { - File file = parent.getFile(); - if (file != null) - return file; + String path = parent.getPath(); + if (path != null) + return path; parent = parent.getParent(); } return null; |