~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ScannerUtility.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 *    IBM Rational Software - Initial API and implementation
10
 
 *******************************************************************************/
11
 
package org.eclipse.cdt.internal.core.parser.scanner;
12
 
 
13
 
import java.io.File;
14
 
 
15
 
/**
16
 
 * @author jcamelon
17
 
 *
18
 
 */
19
 
public class ScannerUtility {
20
 
 
21
 
        static final char DOT    = '.';  
22
 
        static final char SLASH  = '/';
23
 
        static final char BSLASH = '\\'; 
24
 
        static final char QUOTE  = '\"'; 
25
 
 
26
 
        /**
27
 
         * This method is quick 1-pass path reconciler.
28
 
         * Functions:
29
 
         *   - replace "/" or "\" by system's separator
30
 
         *   - replace multiple separators by single one
31
 
         *   - skip "/./" 
32
 
         *   - skip quotes
33
 
         * 
34
 
         * Note: "/../" is left untouched on purpose in order to work properly under
35
 
         * circumstances such as this:
36
 
         * 
37
 
         * header file at include_1/vector:
38
 
         *   // Is supposed to find the STL vector header:
39
 
         *   #include <ext/../vector>
40
 
         *   
41
 
         * GCC include tree 
42
 
         *   include_gcc/ext/...
43
 
         *              /vector
44
 
         * 
45
 
         * (ls include_1/ext/../vector does not work either).
46
 
         * 
47
 
         * @param originalPath - path to process
48
 
         * @return             - reconciled path   
49
 
         */
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
55
 
                
56
 
                char[] ein = new char[len];
57
 
                char[] aus = new char[len + 1];
58
 
                
59
 
                originalPath.getChars(0, len, ein, 0);
60
 
                
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) {
64
 
                    aus[j++] = BSLASH;
65
 
                }
66
 
 
67
 
                
68
 
                for (int i=0; i<len; i++) {
69
 
                        char c = ein[i]; 
70
 
                        switch (c) {
71
 
                        case QUOTE:       // quotes are removed
72
 
                                noSepBefore = true;
73
 
                                break;
74
 
                        case SLASH:     // both separators are processed  
75
 
                        case BSLASH:    // in the same way
76
 
                                if (noSepBefore) {
77
 
                                        noSepBefore = false;
78
 
                                        aus[j++] = File.separatorChar;
79
 
                                }
80
 
                                break;
81
 
                        case DOT:
82
 
                                // no separator before, not a 1st string symbol. 
83
 
                                if (noSepBefore && j>0) 
84
 
                                        aus[j++] = c;
85
 
                                else { // separator before "." ! 
86
 
                                        if (i < len1) {
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
92
 
                                                        i++;
93
 
                                                        noSepBefore = false;
94
 
                                                } 
95
 
                                                // symbol other than "." - write it also 
96
 
                                                else if (c != DOT) {
97
 
                                                        i++;
98
 
                                                        noSepBefore = true;
99
 
                                                        aus[j++] = DOT;
100
 
                                                        aus[j++] = c;
101
 
                                                }
102
 
                                                // Processed as usual
103
 
                                                else {
104
 
                                                        i++;
105
 
                                                        noSepBefore = true;
106
 
                                                        aus[j++] = DOT;
107
 
                                                        aus[j++] = DOT;
108
 
                                                }
109
 
                                        } else 
110
 
                                        {} // do nothing when "." is last symbol
111
 
                                }
112
 
                                break;
113
 
                        default: 
114
 
                                noSepBefore = true;
115
 
                                aus[j++] = c;
116
 
                        }
117
 
                }
118
 
                return new String(aus, 0, j);
119
 
        }
120
 
 
121
 
        /**
122
 
         * @param path     - include path
123
 
         * @param fileName - include file name
124
 
         * @return         - reconsiled path
125
 
         */
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));
129
 
        }
130
 
}