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 "src/sksl/SkSLOutputStream.h"
12#include "src/sksl/SkSLString.h"
13
14#ifdef SKSL_STANDALONE
15
16namespace SkSL {
17
18class StringStream : public OutputStream {
19public:
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
32 const String& str() const {
33 return fBuffer;
34 }
35
36 void reset() {
37 fBuffer = "";
38 }
39
40private:
41 String fBuffer;
42};
43
44#else
45
46#include "include/core/SkData.h"
47#include "include/core/SkStream.h"
48
49namespace SkSL {
50
51class StringStream : public OutputStream {
52public:
53 void write8(uint8_t b) override {
54 fStream.write8(b);
55 }
56
57 void writeText(const char* s) override {
58 fStream.writeText(s);
59 }
60
61 void write(const void* s, size_t size) override {
62 fStream.write(s, size);
63 }
64
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());
69 }
70 return fString;
71 }
72
73 void reset() {
74 fStream.reset();
75 fString = "";
76 }
77
78private:
79 mutable SkDynamicMemoryWStream fStream;
80 mutable String fString;
81};
82
83#endif // SKSL_STANDALONE
84
85} // namespace
86
87#endif
88