16e6646c03788f198a9878763680c05342d7622f3Chris Craik/*
26e6646c03788f198a9878763680c05342d7622f3Chris Craik * Copyright (C) 2015 The Android Open Source Project
36e6646c03788f198a9878763680c05342d7622f3Chris Craik *
46e6646c03788f198a9878763680c05342d7622f3Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
56e6646c03788f198a9878763680c05342d7622f3Chris Craik * you may not use this file except in compliance with the License.
66e6646c03788f198a9878763680c05342d7622f3Chris Craik * You may obtain a copy of the License at
76e6646c03788f198a9878763680c05342d7622f3Chris Craik *
86e6646c03788f198a9878763680c05342d7622f3Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
96e6646c03788f198a9878763680c05342d7622f3Chris Craik *
106e6646c03788f198a9878763680c05342d7622f3Chris Craik * Unless required by applicable law or agreed to in writing, software
116e6646c03788f198a9878763680c05342d7622f3Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
126e6646c03788f198a9878763680c05342d7622f3Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136e6646c03788f198a9878763680c05342d7622f3Chris Craik * See the License for the specific language governing permissions and
146e6646c03788f198a9878763680c05342d7622f3Chris Craik * limitations under the License.
156e6646c03788f198a9878763680c05342d7622f3Chris Craik */
166e6646c03788f198a9878763680c05342d7622f3Chris Craik#ifndef STRING_UTILS_H
176e6646c03788f198a9878763680c05342d7622f3Chris Craik#define STRING_UTILS_H
186e6646c03788f198a9878763680c05342d7622f3Chris Craik
19c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv#include <iomanip>
20c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv#include <iostream>
21c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv#include <ostream>
22c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv#include <sstream>
236e6646c03788f198a9878763680c05342d7622f3Chris Craik#include <string>
246e6646c03788f198a9878763680c05342d7622f3Chris Craik#include <unordered_set>
25c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv
26c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv#include <utils/Log.h>
276e6646c03788f198a9878763680c05342d7622f3Chris Craik
286e6646c03788f198a9878763680c05342d7622f3Chris Craiknamespace android {
296e6646c03788f198a9878763680c05342d7622f3Chris Craiknamespace uirenderer {
306e6646c03788f198a9878763680c05342d7622f3Chris Craik
31704bed0da7cc75d0c517d425445de70ceb58060bJohn Reckclass unordered_string_set : public std::unordered_set<std::string> {
326e6646c03788f198a9878763680c05342d7622f3Chris Craikpublic:
33704bed0da7cc75d0c517d425445de70ceb58060bJohn Reck    bool has(const char* str) {
34704bed0da7cc75d0c517d425445de70ceb58060bJohn Reck        return find(std::string(str)) != end();
35704bed0da7cc75d0c517d425445de70ceb58060bJohn Reck    }
36704bed0da7cc75d0c517d425445de70ceb58060bJohn Reck};
37704bed0da7cc75d0c517d425445de70ceb58060bJohn Reck
38704bed0da7cc75d0c517d425445de70ceb58060bJohn Reckclass StringUtils {
39704bed0da7cc75d0c517d425445de70ceb58060bJohn Reckpublic:
40704bed0da7cc75d0c517d425445de70ceb58060bJohn Reck    static unordered_string_set split(const char* spacedList);
416e6646c03788f198a9878763680c05342d7622f3Chris Craik};
426e6646c03788f198a9878763680c05342d7622f3Chris Craik
4338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reckstruct SizePrinter {
4438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    int bytes;
4538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    friend std::ostream& operator<<(std::ostream& stream, const SizePrinter& d) {
4638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        static const char* SUFFIXES[] = {"B", "KiB", "MiB"};
4738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        size_t suffix = 0;
4838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        double temp = d.bytes;
4991eff22b5d7f8fe551bae01331948858ce932a96Chris Craik        while (temp > 1024 && suffix < 2) {
5038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            temp /= 1024.0;
5138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            suffix++;
5238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        }
5338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        stream << std::fixed << std::setprecision(2) << temp << SUFFIXES[suffix];
5438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck        return stream;
5538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    }
5638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck};
5738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
58c3849aa786db65dbda254b90b7db3b13efd98e65sergeyvclass LogcatStream: public std::ostream {
59c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv    class LogcatStreamBuf: public std::stringbuf {
60c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv        virtual int sync() {
61c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv            ALOGD("%s", str().c_str());
62c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv            str("");
63c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv            return 0;
64c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv        }
65c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv    };
66c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv
67c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv    LogcatStreamBuf buffer;
68c3849aa786db65dbda254b90b7db3b13efd98e65sergeyvpublic:
69c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv    LogcatStream()
70c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv            :std::ostream(&buffer) {
71c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv    }
72c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv};
73c3849aa786db65dbda254b90b7db3b13efd98e65sergeyv
746e6646c03788f198a9878763680c05342d7622f3Chris Craik} /* namespace uirenderer */
756e6646c03788f198a9878763680c05342d7622f3Chris Craik} /* namespace android */
766e6646c03788f198a9878763680c05342d7622f3Chris Craik
776e6646c03788f198a9878763680c05342d7622f3Chris Craik#endif /* GLUTILS_H */
78