Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame^] | 23 | #include "androidfw/StringPiece.h" |
| 24 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "util/Maybe.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Represents a file on disk. Used for logging and |
| 31 | * showing errors. |
| 32 | */ |
| 33 | struct Source { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | std::string path; |
| 35 | Maybe<size_t> line; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | Source() = default; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame^] | 39 | inline Source(const android::StringPiece& path) : path(path.to_string()) { // NOLINT(implicit) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame^] | 42 | inline Source(const android::StringPiece& path, size_t line) |
| 43 | : path(path.to_string()), line(line) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | inline Source WithLine(size_t line) const { return Source(path, line); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | // |
| 49 | // Implementations |
| 50 | // |
| 51 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | out << source.path; |
| 54 | if (source.line) { |
| 55 | out << ":" << source.line.value(); |
| 56 | } |
| 57 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 60 | inline bool operator==(const Source& lhs, const Source& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | return lhs.path == rhs.path && lhs.line == rhs.line; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | inline bool operator<(const Source& lhs, const Source& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | int cmp = lhs.path.compare(rhs.path); |
| 66 | if (cmp < 0) return true; |
| 67 | if (cmp > 0) return false; |
| 68 | if (lhs.line) { |
| 69 | if (rhs.line) { |
| 70 | return lhs.line.value() < rhs.line.value(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | return bool(rhs.line); |
Adam Lesinski | a1ad4a8 | 2015-06-08 11:41:09 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | #endif // AAPT_SOURCE_H |