2014-11-14 14:48:12 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AAPT_UTIL_H
|
|
|
|
#define AAPT_UTIL_H
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
#include "androidfw/ResourceTypes.h"
|
2017-01-16 15:07:21 -08:00
|
|
|
#include "androidfw/StringPiece.h"
|
2016-10-21 17:56:45 -07:00
|
|
|
#include "utils/ByteOrder.h"
|
|
|
|
|
|
|
|
#include "util/BigBuffer.h"
|
|
|
|
#include "util/Maybe.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// TODO(adamlesinski): remove once http://b/32447322 is resolved.
|
|
|
|
// utils/ByteOrder.h includes winsock2.h on WIN32,
|
|
|
|
// which will pull in the ERROR definition. This conflicts
|
|
|
|
// with android-base/logging.h, which takes care of undefining
|
|
|
|
// ERROR, but it gets included too early (before winsock2.h).
|
|
|
|
#ifdef ERROR
|
|
|
|
#undef ERROR
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
namespace aapt {
|
|
|
|
namespace util {
|
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
std::vector<std::string> Split(const android::StringPiece& str, char sep);
|
|
|
|
std::vector<std::string> SplitAndLowercase(const android::StringPiece& str, char sep);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-04-09 19:53:22 -07:00
|
|
|
/**
|
|
|
|
* Returns true if the string starts with prefix.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
bool StartsWith(const android::StringPiece& str, const android::StringPiece& prefix);
|
2015-04-09 19:53:22 -07:00
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
/**
|
|
|
|
* Returns true if the string ends with suffix.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
bool EndsWith(const android::StringPiece& str, const android::StringPiece& suffix);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new StringPiece16 that points to a substring
|
|
|
|
* of the original string without leading or trailing whitespace.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
android::StringPiece TrimWhitespace(const android::StringPiece& str);
|
2015-10-30 16:31:42 -07:00
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
/**
|
|
|
|
* UTF-16 isspace(). It basically checks for lower range characters that are
|
|
|
|
* whitespace.
|
|
|
|
*/
|
2016-10-19 12:18:14 -07:00
|
|
|
inline bool isspace16(char16_t c) { return c < 0x0080 && isspace(c); }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator to the first character that is not alpha-numeric and that
|
|
|
|
* is not in the allowedChars set.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
android::StringPiece::const_iterator FindNonAlphaNumericAndNotInSet(
|
|
|
|
const android::StringPiece& str, const android::StringPiece& allowed_chars);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2015-06-08 11:41:09 -07:00
|
|
|
/**
|
|
|
|
* Tests that the string is a valid Java class name.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
bool IsJavaClassName(const android::StringPiece& str);
|
2015-06-08 11:41:09 -07:00
|
|
|
|
2015-08-14 14:26:04 -07:00
|
|
|
/**
|
|
|
|
* Tests that the string is a valid Java package name.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
bool IsJavaPackageName(const android::StringPiece& str);
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2015-06-08 11:41:09 -07:00
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Converts the class name to a fully qualified class name from the given
|
|
|
|
* `package`. Ex:
|
2015-06-08 11:41:09 -07:00
|
|
|
*
|
|
|
|
* asdf --> package.asdf
|
|
|
|
* .asdf --> package.asdf
|
|
|
|
* .a.b --> package.a.b
|
|
|
|
* asdf.adsf --> asdf.adsf
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
Maybe<std::string> GetFullyQualifiedClassName(const android::StringPiece& package,
|
|
|
|
const android::StringPiece& class_name);
|
2015-06-08 11:41:09 -07:00
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Makes a std::unique_ptr<> with the template parameter inferred by the
|
|
|
|
* compiler.
|
2014-11-14 14:48:12 -08:00
|
|
|
* This will be present in C++14 and can be removed then.
|
|
|
|
*/
|
|
|
|
template <typename T, class... Args>
|
|
|
|
std::unique_ptr<T> make_unique(Args&&... args) {
|
2016-10-19 12:18:14 -07:00
|
|
|
return std::unique_ptr<T>(new T{std::forward<Args>(args)...});
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Writes a set of items to the std::ostream, joining the times with the
|
|
|
|
* provided
|
2014-11-14 14:48:12 -08:00
|
|
|
* separator.
|
|
|
|
*/
|
2016-08-11 13:39:24 -07:00
|
|
|
template <typename Container>
|
2016-10-21 17:56:45 -07:00
|
|
|
::std::function<::std::ostream&(::std::ostream&)> Joiner(
|
2016-10-19 12:18:14 -07:00
|
|
|
const Container& container, const char* sep) {
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
2016-10-21 17:56:45 -07:00
|
|
|
const auto begin_iter = begin(container);
|
|
|
|
const auto end_iter = end(container);
|
|
|
|
return [begin_iter, end_iter, sep](::std::ostream& out) -> ::std::ostream& {
|
|
|
|
for (auto iter = begin_iter; iter != end_iter; ++iter) {
|
|
|
|
if (iter != begin_iter) {
|
2016-10-19 12:18:14 -07:00
|
|
|
out << sep;
|
|
|
|
}
|
|
|
|
out << *iter;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Helper method to extract a UTF-16 string from a StringPool. If the string is
|
|
|
|
* stored as UTF-8,
|
2016-07-08 15:00:32 -07:00
|
|
|
* the conversion to UTF-16 happens within ResStringPool.
|
2014-11-14 14:48:12 -08:00
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-07-08 15:00:32 -07:00
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Helper method to extract a UTF-8 string from a StringPool. If the string is
|
|
|
|
* stored as UTF-16,
|
|
|
|
* the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is
|
|
|
|
* done by this method,
|
|
|
|
* which maintains no state or cache. This means we must return an std::string
|
|
|
|
* copy.
|
2016-07-08 15:00:32 -07:00
|
|
|
*/
|
2016-10-21 17:56:45 -07:00
|
|
|
std::string GetString(const android::ResStringPool& pool, size_t idx);
|
2015-11-23 14:22:47 -08:00
|
|
|
|
2015-11-03 12:24:17 -08:00
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Checks that the Java string format contains no non-positional arguments
|
|
|
|
* (arguments without
|
|
|
|
* explicitly specifying an index) when there are more than one argument. This
|
|
|
|
* is an error
|
|
|
|
* because translations may rearrange the order of the arguments in the string,
|
|
|
|
* which will
|
2015-11-03 12:24:17 -08:00
|
|
|
* break the string interpolation.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
bool VerifyJavaStringFormat(const android::StringPiece& str);
|
2015-11-03 12:24:17 -08:00
|
|
|
|
2014-11-14 14:48:12 -08:00
|
|
|
class StringBuilder {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
2017-01-16 15:07:21 -08:00
|
|
|
StringBuilder& Append(const android::StringPiece& str);
|
2016-10-21 17:56:45 -07:00
|
|
|
const std::string& ToString() const;
|
|
|
|
const std::string& Error() const;
|
2017-03-02 19:31:28 -08:00
|
|
|
bool IsEmpty() const;
|
2016-10-19 12:18:14 -07:00
|
|
|
|
|
|
|
// When building StyledStrings, we need UTF-16 indices into the string,
|
|
|
|
// which is what the Java layer expects when dealing with java
|
|
|
|
// String.charAt().
|
2016-10-21 17:56:45 -07:00
|
|
|
size_t Utf16Len() const;
|
2016-10-19 12:18:14 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
explicit operator bool() const;
|
2016-10-19 12:18:14 -07:00
|
|
|
|
|
|
|
private:
|
2016-10-21 17:56:45 -07:00
|
|
|
std::string str_;
|
|
|
|
size_t utf16_len_ = 0;
|
|
|
|
bool quote_ = false;
|
|
|
|
bool trailing_space_ = false;
|
|
|
|
bool last_char_was_escape_ = false;
|
|
|
|
std::string error_;
|
2014-11-14 14:48:12 -08:00
|
|
|
};
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline const std::string& StringBuilder::ToString() const { return str_; }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline const std::string& StringBuilder::Error() const { return error_; }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2017-03-02 19:31:28 -08:00
|
|
|
inline bool StringBuilder::IsEmpty() const { return str_.empty(); }
|
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline size_t StringBuilder::Utf16Len() const { return utf16_len_; }
|
2016-09-07 13:45:13 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline StringBuilder::operator bool() const { return error_.empty(); }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a UTF8 string to a UTF16 string.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
|
|
|
|
std::string Utf16ToUtf8(const android::StringPiece16& utf16);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the entire BigBuffer to the output stream.
|
|
|
|
*/
|
2016-10-21 17:56:45 -07:00
|
|
|
bool WriteAll(std::ostream& out, const BigBuffer& buffer);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies the entire BigBuffer into a single buffer.
|
|
|
|
*/
|
2016-10-21 17:56:45 -07:00
|
|
|
std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A Tokenizer implemented as an iterable collection. It does not allocate
|
|
|
|
* any memory on the heap nor use standard containers.
|
|
|
|
*/
|
|
|
|
class Tokenizer {
|
2016-10-19 12:18:14 -07:00
|
|
|
public:
|
|
|
|
class iterator {
|
|
|
|
public:
|
|
|
|
iterator(const iterator&) = default;
|
|
|
|
iterator& operator=(const iterator&) = default;
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
iterator& operator++();
|
2016-07-08 15:00:32 -07:00
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
android::StringPiece operator*() { return token_; }
|
2016-10-19 12:18:14 -07:00
|
|
|
bool operator==(const iterator& rhs) const;
|
|
|
|
bool operator!=(const iterator& rhs) const;
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
private:
|
|
|
|
friend class Tokenizer;
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
iterator(android::StringPiece s, char sep, android::StringPiece tok, bool end);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
android::StringPiece str_;
|
2016-10-21 17:56:45 -07:00
|
|
|
char separator_;
|
2017-01-16 15:07:21 -08:00
|
|
|
android::StringPiece token_;
|
2016-10-21 17:56:45 -07:00
|
|
|
bool end_;
|
2016-10-19 12:18:14 -07:00
|
|
|
};
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
Tokenizer(android::StringPiece str, char sep);
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
iterator begin() { return begin_; }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
iterator end() { return end_; }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
private:
|
2016-10-21 17:56:45 -07:00
|
|
|
const iterator begin_;
|
|
|
|
const iterator end_;
|
2016-07-08 15:00:32 -07:00
|
|
|
};
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2017-01-16 15:07:21 -08:00
|
|
|
inline Tokenizer Tokenize(const android::StringPiece& str, char sep) { return Tokenizer(str, sep); }
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline uint16_t HostToDevice16(uint16_t value) { return htods(value); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline uint32_t HostToDevice32(uint32_t value) { return htodl(value); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline uint16_t DeviceToHost16(uint16_t value) { return dtohs(value); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-21 17:56:45 -07:00
|
|
|
inline uint32_t DeviceToHost32(uint32_t value) { return dtohl(value); }
|
2015-08-14 14:26:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a path like: res/xml-sw600dp/foo.xml
|
|
|
|
*
|
|
|
|
* Extracts "res/xml-sw600dp/" into outPrefix.
|
|
|
|
* Extracts "foo" into outEntry.
|
|
|
|
* Extracts ".xml" into outSuffix.
|
|
|
|
*
|
|
|
|
* Returns true if successful.
|
|
|
|
*/
|
2017-01-16 15:07:21 -08:00
|
|
|
bool ExtractResFilePathParts(const android::StringPiece& path, android::StringPiece* out_prefix,
|
|
|
|
android::StringPiece* out_entry, android::StringPiece* out_suffix);
|
2015-08-14 14:26:04 -07:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
} // namespace util
|
2014-11-14 14:48:12 -08:00
|
|
|
|
|
|
|
/**
|
2016-10-19 12:18:14 -07:00
|
|
|
* Stream operator for functions. Calls the function with the stream as an
|
|
|
|
* argument.
|
2014-11-14 14:48:12 -08:00
|
|
|
* In the aapt namespace for lookup.
|
|
|
|
*/
|
2016-10-19 12:18:14 -07:00
|
|
|
inline ::std::ostream& operator<<(
|
|
|
|
::std::ostream& out,
|
|
|
|
const ::std::function<::std::ostream&(::std::ostream&)>& f) {
|
|
|
|
return f(out);
|
2014-11-14 14:48:12 -08:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
} // namespace aapt
|
2014-11-14 14:48:12 -08:00
|
|
|
|
2016-10-19 12:18:14 -07:00
|
|
|
#endif // AAPT_UTIL_H
|