001: /*
002: * CheckPath.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: CheckPath.java,v 1.1.1.1 2002/09/24 16:08:20 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedReader;
025: import java.io.IOException;
026: import java.io.InputStreamReader;
027: import java.util.HashSet;
028: import java.util.Stack;
029:
030: public final class CheckPath implements Constants {
031: private final Editor editor;
032: private final boolean showAll;
033: private final Buffer buffer;
034: private FastStringBuffer sb = new FastStringBuffer(16384);
035: private String path;
036: private File currentDirectory;
037: private HashSet checkedFiles = new HashSet(256);
038: private Stack stack = new Stack();
039: private int depthDisplayed;
040:
041: private CheckPath(Editor editor, boolean showAll) {
042: this .editor = editor;
043: this .showAll = showAll;
044: buffer = editor.getBuffer();
045: path = buffer.getStringProperty(Property.INCLUDE_PATH);
046: currentDirectory = editor.getCurrentDirectory();
047: }
048:
049: private String getOutput() {
050: return sb.toString();
051: }
052:
053: private void run() {
054: sb.append("File: ");
055: sb.append(buffer.getFile().netPath());
056: sb.append('\n');
057: sb.append("Include path: ");
058: sb.append(path);
059: sb.append('\n');
060: if (showAll)
061: sb.append("Included files:\n");
062: else
063: sb.append("The following included files were not found:\n");
064: checkBuffer(buffer, 0);
065: }
066:
067: private void checkBuffer(Buffer b, int depth) {
068: for (Line line = b.getFirstLine(); line != null; line = line
069: .next()) {
070: String s = Utilities.extractInclude(line.getText());
071: if (s != null) {
072: int result = checkFile(s, depth);
073: if (showAll) {
074: if (result == NOT_FOUND)
075: sb.append(" NOT FOUND");
076: else if (result == ALREADY_LISTED)
077: sb.append(" (Already listed)");
078: if (sb.length() == 0
079: || sb.charAt(sb.length() - 1) != '\n')
080: sb.append('\n');
081: } else if (result == NOT_FOUND) {
082: sb.append(s);
083: sb.append(" NOT FOUND\n");
084: }
085: }
086: }
087: }
088:
089: private static final int NOT_FOUND = 0;
090: private static final int FOUND = 1;
091: private static final int ALREADY_LISTED = 2;
092:
093: private int checkFile(final String s, final int depth) {
094: if (showAll) {
095: sb.append(spaces(depth));
096: sb.append(s);
097: } else if (depth < depthDisplayed)
098: depthDisplayed = depth;
099: File file = Utilities.findInclude(s, path, currentDirectory);
100: if (file == null)
101: return NOT_FOUND;
102: if (checkedFiles.contains(file))
103: return ALREADY_LISTED;
104: checkedFiles.add(file);
105: int count = 0;
106: try {
107: BufferedReader reader = new BufferedReader(
108: new InputStreamReader(file.getInputStream()));
109: String line;
110: while ((line = reader.readLine()) != null) {
111: String name = Utilities.extractInclude(line);
112: if (name != null) {
113: if (showAll && count == 0) {
114: sb.append('\n');
115: sb.append(spaces(depth));
116: sb.append(getDisplayName(file));
117: sb.append(" -->\n");
118: }
119: // Recurse!
120: stack.push(file);
121: int result = checkFile(name, depth + 1);
122: if (showAll) {
123: if (result == NOT_FOUND)
124: sb.append(" NOT FOUND");
125: else if (result == ALREADY_LISTED)
126: sb.append(" (Already listed)");
127: if (sb.length() == 0
128: || sb.charAt(sb.length() - 1) != '\n')
129: sb.append('\n');
130: } else if (result == NOT_FOUND) {
131: while (depthDisplayed < stack.size()) {
132: sb.append(spaces(depthDisplayed));
133: sb.append(getDisplayName((File) stack
134: .get(depthDisplayed)));
135: sb.append(" -->\n");
136: ++depthDisplayed;
137: }
138: sb.append(spaces(depth + 1));
139: sb.append(name);
140: sb.append(" NOT FOUND\n");
141: }
142: stack.pop();
143: ++count;
144: }
145: }
146: reader.close();
147: } catch (IOException e) {
148: Log.error(e);
149: }
150: return FOUND;
151: }
152:
153: private String getDisplayName(File file) {
154: File dir = file.getParentFile();
155: if (dir.equals(currentDirectory))
156: return file.getName();
157: return file.canonicalPath();
158: }
159:
160: private static String spaces(int depth) {
161: return Utilities.spaces(depth * 2);
162: }
163:
164: public static void checkPath() {
165: checkPathInternal(false);
166: }
167:
168: public static void listIncludes() {
169: checkPathInternal(true);
170: }
171:
172: private static void checkPathInternal(boolean showAll) {
173: final Editor editor = Editor.currentEditor();
174: final int modeId = editor.getModeId();
175: if (modeId != C_MODE && modeId != CPP_MODE)
176: return;
177: editor.setWaitCursor();
178: CheckPath cp = new CheckPath(editor, showAll);
179: cp.run();
180: editor.setDefaultCursor();
181: Buffer buf = OutputBuffer.getOutputBuffer(cp.getOutput());
182: buf.setFormatter(new CheckPathFormatter(buf));
183: FastStringBuffer sb = new FastStringBuffer();
184: if (showAll)
185: sb.append("listIncludes ");
186: else
187: sb.append("checkPath");
188: sb.append(editor.getBuffer().getFile().getName());
189: buf.setTitle(sb.toString());
190: editor.makeNext(buf);
191: editor.activateInOtherWindow(buf);
192: }
193: }
|