|
17 | 17 | import org.eclipse.core.runtime.NullProgressMonitor;
|
18 | 18 |
|
19 | 19 | public class IndexHelper {
|
20 |
| - /** |
21 |
| - * given a list of names from the indexer. Find a function call and return |
22 |
| - * the parameter I assume there is only one parameter and it is a string so |
23 |
| - * there are quotes |
24 |
| - * |
25 |
| - * @param names |
26 |
| - * the names provided by the indexer |
27 |
| - * @param function |
28 |
| - * the name of the function for which we are looking for a |
29 |
| - * parameter |
30 |
| - * @return the string or defaultValue if no string is found |
31 |
| - */ |
32 |
| - private static String findParameterInFunction(IIndexName[] names, String function, String defaultValue) { |
33 |
| - for (IIndexName name : names) { |
34 |
| - String SetupFileName = name.getFileLocation().getFileName(); |
35 |
| - String SetupFileContent; |
36 |
| - try { |
37 |
| - SetupFileContent = FileUtils.readFileToString(new File(SetupFileName)); |
38 |
| - } catch (IOException e) { |
39 |
| - return defaultValue; |
40 |
| - } |
41 |
| - SetupFileContent = SetupFileContent.replaceAll("//.*|/\\*((.|\\n)(?!=*/))+\\*/", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
42 |
| - int serialBeginStart = SetupFileContent.indexOf(function); |
43 |
| - if (serialBeginStart != -1) { |
44 |
| - int serialBeginStartbraket = SetupFileContent.indexOf("(", serialBeginStart); //$NON-NLS-1$ |
45 |
| - if (serialBeginStartbraket != -1) { |
46 |
| - int serialBeginCloseBraket = SetupFileContent.indexOf(")", serialBeginStartbraket); //$NON-NLS-1$ |
47 |
| - if (serialBeginCloseBraket != -1) { |
48 |
| - return SetupFileContent.substring(serialBeginStartbraket + 1, serialBeginCloseBraket).trim(); |
| 20 | + /** |
| 21 | + * given a list of names from the indexer. Find a function call and return |
| 22 | + * the parameter I assume there is only one parameter The parameter is |
| 23 | + * considered everything between the () |
| 24 | + * |
| 25 | + * @param names |
| 26 | + * the names provided by the indexer |
| 27 | + * @param function |
| 28 | + * the name of the function for which we are looking for a |
| 29 | + * parameter |
| 30 | + * @return the string or defaultValue if no string is found |
| 31 | + */ |
| 32 | + private static String findParameterInFunction(IIndexName[] names, String function, String defaultValue) { |
| 33 | + for (IIndexName name : names) { |
| 34 | + String codeFileName = name.getFileLocation().getFileName(); |
| 35 | + String rawCodeFileContent; |
| 36 | + try { |
| 37 | + rawCodeFileContent = FileUtils.readFileToString(new File(codeFileName)); |
| 38 | + } catch (IOException e) { |
| 39 | + return defaultValue; |
| 40 | + } |
| 41 | + String codeFileContent = rawCodeFileContent.replaceAll("//.*|/\\*((.|\\n)(?!=*/))+\\*/", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 42 | + int functionStart = codeFileContent.indexOf(function); |
| 43 | + if (functionStart != -1) { |
| 44 | + int parameterStartQuote = codeFileContent.indexOf("(", functionStart); //$NON-NLS-1$ |
| 45 | + if (parameterStartQuote != -1) { |
| 46 | + char[] functionParams = codeFileContent.substring(parameterStartQuote, parameterStartQuote + 30) |
| 47 | + .toCharArray(); |
| 48 | + int curbrackets = 1; |
| 49 | + int curchar = 1; |
| 50 | + while ((curbrackets != 0) && (curchar < functionParams.length)) { |
| 51 | + if (functionParams[curchar] == ')') { |
| 52 | + curbrackets--; |
| 53 | + } else { |
| 54 | + if (functionParams[curchar] == '(') { |
| 55 | + curbrackets++; |
| 56 | + } |
| 57 | + } |
| 58 | + curchar++; |
| 59 | + } |
| 60 | + if (curbrackets == 0) { |
| 61 | + return codeFileContent.substring(parameterStartQuote + 1, parameterStartQuote + curchar - 1); |
| 62 | + } |
49 | 63 |
|
50 |
| - } |
| 64 | + } |
| 65 | + } |
51 | 66 | }
|
52 |
| - } |
| 67 | + return defaultValue; |
| 68 | + |
53 | 69 | }
|
54 |
| - return defaultValue; |
55 | 70 |
|
56 |
| - } |
| 71 | + /** |
| 72 | + * given a project look in the source code for the line of code that sets |
| 73 | + * the password; |
| 74 | + * |
| 75 | + * |
| 76 | + * |
| 77 | + * return the password string of no_pwd_found_in_code |
| 78 | + * |
| 79 | + * @param iProject |
| 80 | + * @return |
| 81 | + */ |
| 82 | + public static String findParameterInFunction(IProject project, String parentFunctionName, String childFunctionName, |
| 83 | + String defaultValue) { |
57 | 84 |
|
58 |
| - /** |
59 |
| - * given a project look in the source code for the line of code that sets |
60 |
| - * the password; |
61 |
| - * |
62 |
| - * |
63 |
| - * |
64 |
| - * return the password string of no_pwd_found_in_code |
65 |
| - * |
66 |
| - * @param iProject |
67 |
| - * @return |
68 |
| - */ |
69 |
| - public static String findParameterInFunction(IProject project, String parentFunctionName, String childFunctionName, |
70 |
| - String defaultValue) { |
| 85 | + ICProject curProject = CoreModel.getDefault().getCModel().getCProject(project.getName()); |
71 | 86 |
|
72 |
| - ICProject curProject = CoreModel.getDefault().getCModel().getCProject(project.getName()); |
| 87 | + IIndex index = null; |
| 88 | + try { |
| 89 | + index = CCorePlugin.getIndexManager().getIndex(curProject); |
| 90 | + index.acquireReadLock(); |
| 91 | + IIndexBinding[] bindings = index.findBindings(parentFunctionName.toCharArray(), IndexFilter.ALL_DECLARED, |
| 92 | + new NullProgressMonitor()); |
| 93 | + ICPPFunction parentFunction = null; |
| 94 | + for (IIndexBinding curbinding : bindings) { |
| 95 | + if (curbinding instanceof ICPPFunction) { |
| 96 | + parentFunction = (ICPPFunction) curbinding; |
| 97 | + } |
| 98 | + } |
73 | 99 |
|
74 |
| - IIndex index = null; |
75 |
| - try { |
76 |
| - index = CCorePlugin.getIndexManager().getIndex(curProject); |
77 |
| - index.acquireReadLock(); |
78 |
| - IIndexBinding[] bindings = index.findBindings(parentFunctionName.toCharArray(), IndexFilter.ALL_DECLARED, |
79 |
| - new NullProgressMonitor()); |
80 |
| - ICPPFunction parentFunction = null; |
81 |
| - for (IIndexBinding curbinding : bindings) { |
82 |
| - if (curbinding instanceof ICPPFunction) { |
83 |
| - parentFunction = (ICPPFunction) curbinding; |
84 |
| - } |
85 |
| - } |
| 100 | + if (parentFunction == null) { |
| 101 | + return defaultValue;// that on found binding must be a function |
| 102 | + } |
86 | 103 |
|
87 |
| - if (parentFunction == null) { |
88 |
| - return defaultValue;// that on found binding must be a function |
89 |
| - } |
| 104 | + IIndexName[] names = index.findNames(parentFunction, org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS); |
90 | 105 |
|
91 |
| - IIndexName[] names = index.findNames(parentFunction, org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS); |
| 106 | + return findParameterInFunction(names, childFunctionName, defaultValue); |
92 | 107 |
|
93 |
| - return findParameterInFunction(names, childFunctionName, defaultValue); |
| 108 | + } catch (CoreException | InterruptedException e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } finally { |
| 111 | + if (index != null) { |
| 112 | + index.releaseReadLock(); |
| 113 | + } |
| 114 | + } |
94 | 115 |
|
95 |
| - } catch (CoreException | InterruptedException e) { |
96 |
| - e.printStackTrace(); |
97 |
| - } finally { |
98 |
| - if (index != null) { |
99 |
| - index.releaseReadLock(); |
100 |
| - } |
| 116 | + return defaultValue; |
101 | 117 | }
|
102 | 118 |
|
103 |
| - return defaultValue; |
104 |
| - } |
105 |
| - |
106 | 119 | }
|
0 commit comments