Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.esotericsoftware.utils;

import com.esotericsoftware.scar.*;

public class FileSupport {
    public static final String WINDOWS_UNC_PATH_PREFIX = "\\\\";

    public static String getWindowsDriveIndicator( String path ) {
        return ((path.length() > 1) && (path.charAt( 1 ) == ':')) ? path.substring( 0, 2 ).toUpperCase() : "";
    }

    public static String removeFromFront( String pToRemove, String path ) {
        return (pToRemove.length() == 0) ? path : path.substring( pToRemove.length() );
    }

    public static String normalizePath( IFileSystem pFileSystem, String path ) {
        path = path.trim();
        String zPrefix = "";
        if ( pFileSystem.isWindows() ) {
            if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) {
                path = removeFromFront( zPrefix = WINDOWS_UNC_PATH_PREFIX, path ).trim();
            } else {
                path = removeFromFront( zPrefix = getWindowsDriveIndicator( path ), path ).trim(); // Handle Drive Letter
            }
        }
        path = path.trim();
        if ( '/' != pFileSystem.separatorChar() ) {
            path = path.replace( '/', pFileSystem.separatorChar() );
        }
        int at = path.indexOf( pFileSystem.separatorChar() );
        if ( at != -1 ) {
            String zFileSeparator = "" + pFileSystem.separatorChar();

            // remove white space around file Parts
            StringBuilder sb = new StringBuilder( path.length() );
            int from = 0;
            do {
                sb.append( path.substring( from, at ).trim() ).append( pFileSystem.separatorChar() );
                from = at + 1;
            }
            while ( -1 != (at = path.indexOf( pFileSystem.separatorChar(), from )) );
            path = sb.append( path.substring( from ).trim() ).toString();

            // Clean up silly middle nothings
            path = Utils.replace( path, zFileSeparator + "." + zFileSeparator, zFileSeparator ); // "/./"
            path = Utils.replace( path, zFileSeparator + zFileSeparator, zFileSeparator ); // "//"

            // Remove ending "/."
            while ( path.endsWith( zFileSeparator + "." ) ) {
                path = path.substring( 0, path.length() - 2 );
            }
            // Remove leading "./"
            while ( path.startsWith( "." + zFileSeparator ) ) {
                path = path.substring( 2 );
            }

            // Process Funky ..
            String zPrefixDotDotSlash = "";
            String zDotDotSlash = ".." + zFileSeparator;
            while ( path.startsWith( zDotDotSlash ) ) {
                zPrefixDotDotSlash += zDotDotSlash;
                path = path.substring( 3 );
            }
            String zUpLevel = zFileSeparator + "..";
            if ( path.endsWith( zUpLevel ) ) {
                path += zFileSeparator;
            }
            zUpLevel += zFileSeparator;
            for ( at = path.indexOf( zUpLevel ); at > 0; at = path.indexOf( zUpLevel ) ) {
                path = removeDotDot( path, at, pFileSystem.separatorChar() );
            }
            path = zPrefixDotDotSlash + path;
            if ( (path.length() > 1) && path.endsWith( zFileSeparator ) ) {
                path = path.substring( 0, path.length() - 1 );
            }
        }
        if ( path.length() == 0 ) {
            path = ".";
        }
        path = zPrefix + path;
        return path;
    }

    private static String removeDotDot( String path, int pAt, char pSeparatorChar ) {
        int zEnd = pAt + 4;
        while ( path.charAt( --pAt ) != pSeparatorChar ) {
            if ( pAt == 0 ) {
                return path.substring( zEnd );
            }
        }
        return path.substring( 0, pAt + 1 ) + path.substring( zEnd );
    }

    public static boolean isAbsoluteNormalizedPath( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelativeForWindowsDriveLetter, String path ) {
        if ( pFileSystem.isWindows() ) {
            if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) {
                return true;
            }
            String zDriveIndicator = getWindowsDriveIndicator( path );
            if ( zDriveIndicator.length() != 0 ) // Handle Drive Letter
            {
                if ( !pCanonicalParentDirIfPathRelativeForWindowsDriveLetter.startsWith( zDriveIndicator ) || !pFileSystem.canonicalCurrentPath().startsWith( zDriveIndicator ) ) {
                    return true; // Has Drive Letter and it is NOT the same both the 'CanonicalDirForWindowDriveLetterSourceRelativeness' && pFileSystem.canonicalCurrentPath()
                }
                path = removeFromFront( zDriveIndicator, path );
            }
        }
        return (path.length() > 0) && (path.charAt( 0 ) == pFileSystem.separatorChar());
    }

    public static String canonicalizeNormalizedPath( IFileSystem pFileSystem, String pCanonicalParentDirIfPathRelative, String path ) {
        if ( !pFileSystem.isWindows() ) {
            if ( !isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, path ) ) {
                path = normalizePath( pFileSystem, pCanonicalParentDirIfPathRelative + pFileSystem.separatorChar() + path );
            }
            return canonicalizeAbsoluteNormalizedPath( pFileSystem, path );
        }
        // Windows!
        if ( path.startsWith( WINDOWS_UNC_PATH_PREFIX ) ) {
            return canonicalizeAbsoluteNormalizedPath( pFileSystem, path );
        }
        String zDriveIndicator = getWindowsDriveIndicator( path );
        if ( !isAbsoluteNormalizedPath( pFileSystem, pCanonicalParentDirIfPathRelative, path ) ) // Relative!
        {
            path = normalizePath( pFileSystem, pCanonicalParentDirIfPathRelative + pFileSystem.separatorChar() + removeFromFront( zDriveIndicator, path ) );
            return canonicalizeAbsoluteNormalizedPath( pFileSystem, path );
        }
        // Absolute
        if ( zDriveIndicator.length() == 0 ) // to "default" Drive
        {
            return canonicalizeAbsoluteNormalizedPath( pFileSystem, path );
        }
        // Windows path w/ DriveIndicator which 'might' actually be relative to the given DriveIndicator
        if ( (path = removeFromFront( zDriveIndicator, path )).length() == 0 ) // Should NOT be possible!
        {
            path = ".";
        }
        if ( (path.charAt( 0 ) != pFileSystem.separatorChar()) && !path.startsWith( "." ) ) {
            path = "." + pFileSystem.separatorChar() + path;
        }
        return canonicalizeAbsoluteNormalizedPath( pFileSystem, zDriveIndicator + path );
    }

    private static String canonicalizeAbsoluteNormalizedPath( IFileSystem pFileSystem, String path ) {
        String origPath = path;
        if ( !pFileSystem.exists( origPath ) ) {
            String zEnd = "";
            for ( int at; -1 != (at = path.lastIndexOf( pFileSystem.separatorChar() )); ) {
                zEnd = path.substring( at ) + zEnd;
                if ( pFileSystem.exists( path = path.substring( 0, at ) ) ) {
                    return pFileSystem.canonicalizeNormalizedExisting( path ) + zEnd;
                }
            }
        }
        return pFileSystem.canonicalizeNormalizedExisting( origPath );
    }
}

Commits for litesoft/trunk/Java/ScarPlus/src/com/esotericsoftware/utils/FileSupport.java

Diff revisions: vs.
Revision Author Commited Message
959 Diff Diff GeorgeS picture GeorgeS Sat 19 Jul, 2014 15:27:50 +0000

Scar update

315 Diff Diff GeorgeS picture GeorgeS Sun 17 Jul, 2011 15:48:36 +0000
309 Diff Diff GeorgeS picture GeorgeS Mon 11 Jul, 2011 03:16:47 +0000
308 Diff Diff GeorgeS picture GeorgeS Sun 10 Jul, 2011 23:55:06 +0000
307 GeorgeS picture GeorgeS Sun 10 Jul, 2011 01:43:51 +0000