1 21 22 package org.armedbear.j; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.util.HashSet ; 28 import java.util.Stack ; 29 30 public final class CheckPath implements Constants 31 { 32 private final Editor editor; 33 private final boolean showAll; 34 private final Buffer buffer; 35 private FastStringBuffer sb = new FastStringBuffer(16384); 36 private String path; 37 private File currentDirectory; 38 private HashSet checkedFiles = new HashSet (256); 39 private Stack stack = new Stack (); 40 private int depthDisplayed; 41 42 private CheckPath(Editor editor, boolean showAll) 43 { 44 this.editor = editor; 45 this.showAll = showAll; 46 buffer = editor.getBuffer(); 47 path = buffer.getStringProperty(Property.INCLUDE_PATH); 48 currentDirectory = editor.getCurrentDirectory(); 49 } 50 51 private String getOutput() 52 { 53 return sb.toString(); 54 } 55 56 private void run() 57 { 58 sb.append("File: "); 59 sb.append(buffer.getFile().netPath()); 60 sb.append('\n'); 61 sb.append("Include path: "); 62 sb.append(path); 63 sb.append('\n'); 64 if (showAll) 65 sb.append("Included files:\n"); 66 else 67 sb.append("The following included files were not found:\n"); 68 checkBuffer(buffer, 0); 69 } 70 71 private void checkBuffer(Buffer b, int depth) 72 { 73 for (Line line = b.getFirstLine(); line != null; line = line.next()) { 74 String s = Utilities.extractInclude(line.getText()); 75 if (s != null) { 76 int result = checkFile(s, depth); 77 if (showAll) { 78 if (result == NOT_FOUND) 79 sb.append(" NOT FOUND"); 80 else if (result == ALREADY_LISTED) 81 sb.append(" (Already listed)"); 82 if (sb.length() == 0 || sb.charAt(sb.length()-1) != '\n') 83 sb.append('\n'); 84 } else if (result == NOT_FOUND) { 85 sb.append(s); 86 sb.append(" NOT FOUND\n"); 87 } 88 } 89 } 90 } 91 92 private static final int NOT_FOUND = 0; 93 private static final int FOUND = 1; 94 private static final int ALREADY_LISTED = 2; 95 96 private int checkFile(final String s, final int depth) 97 { 98 if (showAll) { 99 sb.append(spaces(depth)); 100 sb.append(s); 101 } else if (depth < depthDisplayed) 102 depthDisplayed = depth; 103 File file = Utilities.findInclude(s, path, currentDirectory); 104 if (file == null) 105 return NOT_FOUND; 106 if (checkedFiles.contains(file)) 107 return ALREADY_LISTED; 108 checkedFiles.add(file); 109 int count = 0; 110 try { 111 BufferedReader reader = 112 new BufferedReader (new InputStreamReader (file.getInputStream())); 113 String line; 114 while ((line = reader.readLine()) != null) { 115 String name = Utilities.extractInclude(line); 116 if (name != null) { 117 if (showAll && count == 0) { 118 sb.append('\n'); 119 sb.append(spaces(depth)); 120 sb.append(getDisplayName(file)); 121 sb.append(" -->\n"); 122 } 123 stack.push(file); 125 int result = checkFile(name, depth+1); 126 if (showAll) { 127 if (result == NOT_FOUND) 128 sb.append(" NOT FOUND"); 129 else if (result == ALREADY_LISTED) 130 sb.append(" (Already listed)"); 131 if (sb.length() == 0 || sb.charAt(sb.length()-1) != '\n') 132 sb.append('\n'); 133 } else if (result == NOT_FOUND) { 134 while (depthDisplayed < stack.size()) { 135 sb.append(spaces(depthDisplayed)); 136 sb.append(getDisplayName((File)stack.get(depthDisplayed))); 137 sb.append(" -->\n"); 138 ++depthDisplayed; 139 } 140 sb.append(spaces(depth+1)); 141 sb.append(name); 142 sb.append(" NOT FOUND\n"); 143 } 144 stack.pop(); 145 ++count; 146 } 147 } 148 reader.close(); 149 } 150 catch (IOException e) { 151 Log.error(e); 152 } 153 return FOUND; 154 } 155 156 private String getDisplayName(File file) 157 { 158 File dir = file.getParentFile(); 159 if (dir.equals(currentDirectory)) 160 return file.getName(); 161 return file.canonicalPath(); 162 } 163 164 private static String spaces(int depth) 165 { 166 return Utilities.spaces(depth*2); 167 } 168 169 public static void checkPath() 170 { 171 checkPathInternal(false); 172 } 173 174 public static void listIncludes() 175 { 176 checkPathInternal(true); 177 } 178 179 private static void checkPathInternal(boolean showAll) 180 { 181 final Editor editor = Editor.currentEditor(); 182 final int modeId = editor.getModeId(); 183 if (modeId != C_MODE && modeId != CPP_MODE) 184 return; 185 editor.setWaitCursor(); 186 CheckPath cp = new CheckPath(editor, showAll); 187 cp.run(); 188 editor.setDefaultCursor(); 189 Buffer buf = OutputBuffer.getOutputBuffer(cp.getOutput()); 190 buf.setFormatter(new CheckPathFormatter(buf)); 191 FastStringBuffer sb = new FastStringBuffer(); 192 if (showAll) 193 sb.append("listIncludes "); 194 else 195 sb.append("checkPath"); 196 sb.append(editor.getBuffer().getFile().getName()); 197 buf.setTitle(sb.toString()); 198 editor.makeNext(buf); 199 editor.activateInOtherWindow(buf); 200 } 201 } 202 | Popular Tags |