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