Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [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 | #ifndef STRING_UTILS_H |
| 17 | #define STRING_UTILS_H |
| 18 | |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 19 | #include <iomanip> |
| 20 | #include <iostream> |
| 21 | #include <ostream> |
| 22 | #include <sstream> |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <unordered_set> |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 25 | |
| 26 | #include <utils/Log.h> |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 31 | class unordered_string_set : public std::unordered_set<std::string> { |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 32 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 33 | bool has(const char* str) { return find(std::string(str)) != end(); } |
John Reck | 704bed0 | 2015-11-05 09:22:17 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | class StringUtils { |
| 37 | public: |
| 38 | static unordered_string_set split(const char* spacedList); |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 41 | struct SizePrinter { |
| 42 | int bytes; |
| 43 | friend std::ostream& operator<<(std::ostream& stream, const SizePrinter& d) { |
| 44 | static const char* SUFFIXES[] = {"B", "KiB", "MiB"}; |
| 45 | size_t suffix = 0; |
| 46 | double temp = d.bytes; |
Chris Craik | 91eff22 | 2016-02-22 13:39:33 -0800 | [diff] [blame] | 47 | while (temp > 1024 && suffix < 2) { |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 48 | temp /= 1024.0; |
| 49 | suffix++; |
| 50 | } |
| 51 | stream << std::fixed << std::setprecision(2) << temp << SUFFIXES[suffix]; |
| 52 | return stream; |
| 53 | } |
| 54 | }; |
| 55 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 56 | class LogcatStream : public std::ostream { |
| 57 | class LogcatStreamBuf : public std::stringbuf { |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 58 | virtual int sync() { |
| 59 | ALOGD("%s", str().c_str()); |
| 60 | str(""); |
| 61 | return 0; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | LogcatStreamBuf buffer; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 66 | |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 67 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 68 | LogcatStream() : std::ostream(&buffer) {} |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Chris Craik | 6e6646c | 2015-09-14 15:54:12 -0700 | [diff] [blame] | 71 | } /* namespace uirenderer */ |
| 72 | } /* namespace android */ |
| 73 | |
| 74 | #endif /* GLUTILS_H */ |