blob: 92934c34396069198a858fae7f53ba1f606a0687 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski93190b72017-11-03 15:20:17 -070023#include "android-base/stringprintf.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "util/Maybe.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
29
Adam Lesinski93190b72017-11-03 15:20:17 -070030// Represents a file on disk. Used for logging and showing errors.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031struct Source {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 std::string path;
33 Maybe<size_t> line;
Ryan Mitchellf3649d62018-08-02 16:16:45 -070034 Maybe<std::string> archive;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 Source() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskid5083f62017-01-16 15:07:21 -080038 inline Source(const android::StringPiece& path) : path(path.to_string()) { // NOLINT(implicit)
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Ryan Mitchellf3649d62018-08-02 16:16:45 -070041 inline Source(const android::StringPiece& path, const android::StringPiece& archive)
42 : path(path.to_string()), archive(archive.to_string()) {}
43
Adam Lesinskid5083f62017-01-16 15:07:21 -080044 inline Source(const android::StringPiece& path, size_t line)
45 : path(path.to_string()), line(line) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046
Adam Lesinski93190b72017-11-03 15:20:17 -070047 inline Source WithLine(size_t line) const {
48 return Source(path, line);
49 }
50
51 std::string to_string() const {
Ryan Mitchellf3649d62018-08-02 16:16:45 -070052 std::string s = path;
53 if (archive) {
54 s = ::android::base::StringPrintf("%s@%s", archive.value().c_str(), s.c_str());
Adam Lesinski93190b72017-11-03 15:20:17 -070055 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -070056 if (line) {
57 s = ::android::base::StringPrintf("%s:%zd", s.c_str(), line.value());
58 }
59 return s;
Adam Lesinski93190b72017-11-03 15:20:17 -070060 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061};
62
63//
64// Implementations
65//
66
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067inline ::std::ostream& operator<<(::std::ostream& out, const Source& source) {
Adam Lesinski93190b72017-11-03 15:20:17 -070068 return out << source.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069}
70
Adam Lesinskia5870652015-11-20 15:32:30 -080071inline bool operator==(const Source& lhs, const Source& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return lhs.path == rhs.path && lhs.line == rhs.line;
Adam Lesinskia5870652015-11-20 15:32:30 -080073}
74
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075inline bool operator<(const Source& lhs, const Source& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 int cmp = lhs.path.compare(rhs.path);
77 if (cmp < 0) return true;
78 if (cmp > 0) return false;
79 if (lhs.line) {
80 if (rhs.line) {
81 return lhs.line.value() < rhs.line.value();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 return false;
84 }
85 return bool(rhs.line);
Adam Lesinskia1ad4a82015-06-08 11:41:09 -070086}
87
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090#endif // AAPT_SOURCE_H