0
|
1 |
/*
|
|
2 |
* Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.s60tools.apiquery.shared.util;
|
|
19 |
|
|
20 |
/**
|
|
21 |
* Miscellaneous utilities for parsing source code.
|
|
22 |
*/
|
|
23 |
public class SourceCodeParsingUtilities {
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Parses header name from include directive line.
|
|
27 |
* @param line Line to check for.
|
|
28 |
* @return Name of the header file if found, otherwise <code>null</code>.
|
|
29 |
*/
|
|
30 |
public static String parseIncludeFromLine(String line) {
|
|
31 |
String include = null;
|
|
32 |
//If include is system inculde (#include <someapi.h>)
|
|
33 |
//This can be really a user include, if user has been using
|
|
34 |
//SYSTEMINCLUDE and USERINCLUDE in "wrong" ways
|
|
35 |
if(hasSystemInclude(line)){
|
|
36 |
include = line.substring(line.indexOf("<") +1, line.indexOf(">")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
37 |
include = removeIncludePath(include);
|
|
38 |
|
|
39 |
}
|
|
40 |
//If include is user inculde (#include "myapi.h")
|
|
41 |
//this can be really a system include, if user has been using
|
|
42 |
//SYSTEMINCLUDE and USERINCLUDE in "wrong" ways
|
|
43 |
else if(hasUserInclude(line)){
|
|
44 |
include=line.substring(line.indexOf("\"") +1, line.lastIndexOf("\"")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
45 |
include = removeIncludePath(include);
|
|
46 |
}
|
|
47 |
return include;
|
|
48 |
}
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Removes path from the include, if exists, and returns only header's basename.
|
|
52 |
* @param include Absolute header path name.
|
|
53 |
* @return Header's basename.
|
|
54 |
*/
|
|
55 |
private static String removeIncludePath(String include) {
|
|
56 |
if(include.contains("\\")){ //$NON-NLS-1$
|
|
57 |
include = include.substring(include.lastIndexOf("\\") + 1); //$NON-NLS-1$
|
|
58 |
}
|
|
59 |
if(include.contains("/")){ //$NON-NLS-1$
|
|
60 |
include = include.substring(include.lastIndexOf("/") + 1); //$NON-NLS-1$
|
|
61 |
}
|
|
62 |
return include;
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Checks if line contains include directive.
|
|
67 |
* @param line Line to check for.
|
|
68 |
* @return <code>true</code> if include was found.
|
|
69 |
*/
|
|
70 |
private static boolean hasInclude(String line) {
|
|
71 |
return line.contains("#include"); //$NON-NLS-1$
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Checks if line contains user include directive.
|
|
76 |
* @param line Line to check for.
|
|
77 |
* @return <code>true</code> if include was found.
|
|
78 |
*/
|
|
79 |
public static boolean hasUserInclude(String line) {
|
|
80 |
return hasInclude(line) && line.contains("\""); //$NON-NLS-1$
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Checks if line contains system include directive.
|
|
85 |
* @param line Line to check for.
|
|
86 |
* @return <code>true</code> if include was found.
|
|
87 |
*/
|
|
88 |
public static boolean hasSystemInclude(String line) {
|
|
89 |
return hasInclude(line) && line.contains("<") && line.contains(">"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Checks if line contains system or user include directives.
|
|
94 |
* @param line Line to check for.
|
|
95 |
* @return <code>true</code> if an include was found.
|
|
96 |
*/
|
|
97 |
public static boolean hasIncludes(String line) {
|
|
98 |
return hasUserInclude(line) || hasSystemInclude(line);
|
|
99 |
}
|
|
100 |
|
|
101 |
}
|