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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLOutputStream.h" |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLString.h" |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 13 | |
| 14 | #ifdef SKSL_STANDALONE |
| 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | class StringStream : public OutputStream { |
| 19 | public: |
| 20 | void write8(uint8_t b) override { |
| 21 | fBuffer += (char) b; |
| 22 | } |
| 23 | |
| 24 | void writeText(const char* s) override { |
| 25 | fBuffer += s; |
| 26 | } |
| 27 | |
| 28 | void write(const void* s, size_t size) override { |
| 29 | fBuffer.append((const char*) s, size); |
| 30 | } |
| 31 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 32 | const String& str() const { |
| 33 | return fBuffer; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void reset() { |
| 37 | fBuffer = ""; |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | String fBuffer; |
| 42 | }; |
| 43 | |
| 44 | #else |
| 45 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "include/core/SkData.h" |
| 47 | #include "include/core/SkStream.h" |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 48 | |
| 49 | namespace SkSL { |
| 50 | |
| 51 | class StringStream : public OutputStream { |
| 52 | public: |
| 53 | void write8(uint8_t b) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 54 | fStream.write8(b); |
| 55 | } |
| 56 | |
| 57 | void writeText(const char* s) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 58 | fStream.writeText(s); |
| 59 | } |
| 60 | |
| 61 | void write(const void* s, size_t size) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 62 | fStream.write(s, size); |
| 63 | } |
| 64 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 65 | const String& str() const { |
| 66 | if (!fString.size()) { |
| 67 | sk_sp<SkData> data = fStream.detachAsData(); |
| 68 | fString = String((const char*) data->data(), data->size()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 69 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 70 | return fString; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void reset() { |
| 74 | fStream.reset(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 75 | fString = ""; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
| 79 | mutable SkDynamicMemoryWStream fStream; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 80 | mutable String fString; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | #endif // SKSL_STANDALONE |
| 84 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame^] | 85 | } // namespace SkSL |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 86 | |
| 87 | #endif |