Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_STRINGSTREAM |
| 9 | #define SKSL_STRINGSTREAM |
| 10 | |
| 11 | #include "SkSLOutputStream.h" |
| 12 | |
| 13 | #ifdef SKSL_STANDALONE |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
| 17 | class StringStream : public OutputStream { |
| 18 | public: |
| 19 | void write8(uint8_t b) override { |
| 20 | fBuffer += (char) b; |
| 21 | } |
| 22 | |
| 23 | void writeText(const char* s) override { |
| 24 | fBuffer += s; |
| 25 | } |
| 26 | |
| 27 | void write(const void* s, size_t size) override { |
| 28 | fBuffer.append((const char*) s, size); |
| 29 | } |
| 30 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 31 | const String& str() const { |
| 32 | return fBuffer; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void reset() { |
| 36 | fBuffer = ""; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | String fBuffer; |
| 41 | }; |
| 42 | |
| 43 | #else |
| 44 | |
| 45 | #include "SkData.h" |
| 46 | #include "SkStream.h" |
| 47 | |
| 48 | namespace SkSL { |
| 49 | |
| 50 | class StringStream : public OutputStream { |
| 51 | public: |
| 52 | void write8(uint8_t b) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 53 | fStream.write8(b); |
| 54 | } |
| 55 | |
| 56 | void writeText(const char* s) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 57 | fStream.writeText(s); |
| 58 | } |
| 59 | |
| 60 | void write(const void* s, size_t size) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 61 | fStream.write(s, size); |
| 62 | } |
| 63 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 64 | const String& str() const { |
| 65 | if (!fString.size()) { |
| 66 | sk_sp<SkData> data = fStream.detachAsData(); |
| 67 | fString = String((const char*) data->data(), data->size()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 68 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 69 | return fString; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void reset() { |
| 73 | fStream.reset(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 74 | fString = ""; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
| 78 | mutable SkDynamicMemoryWStream fStream; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 79 | mutable String fString; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | #endif // SKSL_STANDALONE |
| 83 | |
| 84 | } // namespace |
| 85 | |
| 86 | #endif |