1
/*******************************************************************************
2
* Copyright (c) 2005, 2008 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Rational Software - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.parser.scanner;
19
public class ScannerUtility {
21
static final char DOT = '.';
22
static final char SLASH = '/';
23
static final char BSLASH = '\\';
24
static final char QUOTE = '\"';
27
* This method is quick 1-pass path reconciler.
29
* - replace "/" or "\" by system's separator
30
* - replace multiple separators by single one
34
* Note: "/../" is left untouched on purpose in order to work properly under
35
* circumstances such as this:
37
* header file at include_1/vector:
38
* // Is supposed to find the STL vector header:
39
* #include <ext/../vector>
45
* (ls include_1/ext/../vector does not work either).
47
* @param originalPath - path to process
48
* @return - reconciled path
50
public static String reconcilePath(String originalPath ) {
51
int len = originalPath.length();
52
int len1 = len - 1; // to avoid multiple calculations
53
int j = 0; // index for output array
54
boolean noSepBefore = true; // to avoid duplicate separators
56
char[] ein = new char[len];
57
char[] aus = new char[len + 1];
59
originalPath.getChars(0, len, ein, 0);
61
// allow double backslash at beginning for windows UNC paths, bug 233511
62
if(ein.length >= 2 && ein[0] == BSLASH && ein[1] == BSLASH &&
63
File.separatorChar == BSLASH) {
68
for (int i=0; i<len; i++) {
71
case QUOTE: // quotes are removed
74
case SLASH: // both separators are processed
75
case BSLASH: // in the same way
78
aus[j++] = File.separatorChar;
82
// no separator before, not a 1st string symbol.
83
if (noSepBefore && j>0)
85
else { // separator before "." !
87
c = ein[i+1]; // check for next symbol
88
// check for "/./" case
89
if (c == SLASH || c == BSLASH) {
90
// write nothing to output
91
// skip the next symbol
95
// symbol other than "." - write it also
102
// Processed as usual
110
{} // do nothing when "." is last symbol
118
return new String(aus, 0, j);
122
* @param path - include path
123
* @param fileName - include file name
124
* @return - reconsiled path
126
public static String createReconciledPath(String path, String fileName) {
127
boolean pathEmpty = (path == null || path.length() == 0);
128
return (pathEmpty ? fileName : reconcilePath(path + File.separatorChar + fileName));