1 /* 2 * Copyright (C) 2015 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 #ifndef AAPT_SOURCE_H 18 #define AAPT_SOURCE_H 19 20 #include <ostream> 21 #include <string> 22 23 #include "android-base/stringprintf.h" 24 #include "androidfw/StringPiece.h" 25 26 #include "util/Maybe.h" 27 28 namespace aapt { 29 30 // Represents a file on disk. Used for logging and showing errors. 31 struct Source { 32 std::string path; 33 Maybe<size_t> line; 34 35 Source() = default; 36 SourceSource37 inline Source(const android::StringPiece& path) : path(path.to_string()) { // NOLINT(implicit) 38 } 39 SourceSource40 inline Source(const android::StringPiece& path, size_t line) 41 : path(path.to_string()), line(line) {} 42 WithLineSource43 inline Source WithLine(size_t line) const { 44 return Source(path, line); 45 } 46 to_stringSource47 std::string to_string() const { 48 if (line) { 49 return ::android::base::StringPrintf("%s:%zd", path.c_str(), line.value()); 50 } 51 return path; 52 } 53 }; 54 55 // 56 // Implementations 57 // 58 59 inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) { 60 return out << source.to_string(); 61 } 62 63 inline bool operator==(const Source& lhs, const Source& rhs) { 64 return lhs.path == rhs.path && lhs.line == rhs.line; 65 } 66 67 inline bool operator<(const Source& lhs, const Source& rhs) { 68 int cmp = lhs.path.compare(rhs.path); 69 if (cmp < 0) return true; 70 if (cmp > 0) return false; 71 if (lhs.line) { 72 if (rhs.line) { 73 return lhs.line.value() < rhs.line.value(); 74 } 75 return false; 76 } 77 return bool(rhs.line); 78 } 79 80 } // namespace aapt 81 82 #endif // AAPT_SOURCE_H 83