1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include "stdafx.h"
18 #include "JavaPath.h"
19 #include "utils.h"
20 
21 #define  _CRT_SECURE_NO_WARNINGS
22 
23 // --------------
24 
25 const CJavaPath CJavaPath::sEmpty = CJavaPath();
26 
CJavaPath(int version,CPath path)27 CJavaPath::CJavaPath(int version, CPath path) : mVersion(version), mPath(path) {
28     mPath.Canonicalize();
29 }
30 
isEmpty() const31 bool CJavaPath::isEmpty() const {
32     return mVersion <= 0;
33 }
34 
clear()35 void CJavaPath::clear() {
36     mVersion = 0;
37     mPath = CPath();
38 }
39 
set(int version,CPath path)40 void CJavaPath::set(int version, CPath path) {
41     mVersion = version;
42     mPath = path;
43     mPath.Canonicalize();
44 }
45 
getVersion() const46 CString CJavaPath::getVersion() const {
47     CString s;
48     s.Format(_T("%d.%d"), JAVA_MAJOR(mVersion), JAVA_MINOR(mVersion));
49     return s;
50 }
51 
52 
toShortPath()53 bool CJavaPath::toShortPath() {
54     const TCHAR *longPath = mPath;
55     if (longPath == nullptr) {
56         return false;
57     }
58 
59     DWORD lenShort = _tcslen(longPath) + 1;
60     TCHAR *shortPath = (TCHAR *)malloc(lenShort * sizeof(TCHAR));
61 
62     DWORD length = GetShortPathName(longPath, shortPath, lenShort);
63     if (length > lenShort) {
64         // The buffer wasn't big enough, this is the size to use.
65         free(shortPath);
66         lenShort = length;
67         shortPath = (TCHAR *)malloc(length);
68         length = GetShortPathName(longPath, shortPath, lenShort);
69     }
70 
71     if (length != 0) {
72         mPath = CPath(shortPath);
73     }
74 
75     free(shortPath);
76     return length != 0;
77 }
78 
operator <(const CJavaPath & rhs) const79 bool CJavaPath::operator< (const CJavaPath& rhs) const {
80     if (mVersion != rhs.mVersion) {
81         // sort in reverse order on the version
82         return rhs.mVersion > mVersion;
83     }
84     // sort in normal order on the path
85     const CString &pl = mPath;
86     const CString &pr = rhs.mPath;
87     return pl.Compare(pr) < 0;
88 }
89 
operator ==(const CJavaPath & rhs) const90 bool CJavaPath::operator== (const CJavaPath& rhs) const {
91     if (mVersion == rhs.mVersion) {
92         const CString &pl = mPath;
93         const CString &pr = rhs.mPath;
94         return pl.Compare(pr) == 0;
95     }
96     return false;
97 }
98