Line data Source code
1 : // © 2016 and later: Unicode, Inc. and others.
2 : // License & terms of use: http://www.unicode.org/copyright.html
3 : // Copyright (C) 2009-2011, International Business Machines
4 : // Corporation and others. All Rights Reserved.
5 : //
6 : // Copyright 2007 Google Inc. All Rights Reserved.
7 : // Author: sanjay@google.com (Sanjay Ghemawat)
8 :
9 : #include "unicode/utypes.h"
10 : #include "unicode/bytestream.h"
11 : #include "cmemory.h"
12 :
13 : U_NAMESPACE_BEGIN
14 :
15 0 : ByteSink::~ByteSink() {}
16 :
17 0 : char* ByteSink::GetAppendBuffer(int32_t min_capacity,
18 : int32_t /*desired_capacity_hint*/,
19 : char* scratch, int32_t scratch_capacity,
20 : int32_t* result_capacity) {
21 0 : if (min_capacity < 1 || scratch_capacity < min_capacity) {
22 0 : *result_capacity = 0;
23 0 : return NULL;
24 : }
25 0 : *result_capacity = scratch_capacity;
26 0 : return scratch;
27 : }
28 :
29 0 : void ByteSink::Flush() {}
30 :
31 0 : CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity)
32 0 : : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity),
33 0 : size_(0), appended_(0), overflowed_(FALSE) {
34 0 : }
35 :
36 0 : CheckedArrayByteSink::~CheckedArrayByteSink() {}
37 :
38 0 : CheckedArrayByteSink& CheckedArrayByteSink::Reset() {
39 0 : size_ = appended_ = 0;
40 0 : overflowed_ = FALSE;
41 0 : return *this;
42 : }
43 :
44 0 : void CheckedArrayByteSink::Append(const char* bytes, int32_t n) {
45 0 : if (n <= 0) {
46 0 : return;
47 : }
48 0 : appended_ += n;
49 0 : int32_t available = capacity_ - size_;
50 0 : if (n > available) {
51 0 : n = available;
52 0 : overflowed_ = TRUE;
53 : }
54 0 : if (n > 0 && bytes != (outbuf_ + size_)) {
55 0 : uprv_memcpy(outbuf_ + size_, bytes, n);
56 : }
57 0 : size_ += n;
58 : }
59 :
60 0 : char* CheckedArrayByteSink::GetAppendBuffer(int32_t min_capacity,
61 : int32_t /*desired_capacity_hint*/,
62 : char* scratch,
63 : int32_t scratch_capacity,
64 : int32_t* result_capacity) {
65 0 : if (min_capacity < 1 || scratch_capacity < min_capacity) {
66 0 : *result_capacity = 0;
67 0 : return NULL;
68 : }
69 0 : int32_t available = capacity_ - size_;
70 0 : if (available >= min_capacity) {
71 0 : *result_capacity = available;
72 0 : return outbuf_ + size_;
73 : } else {
74 0 : *result_capacity = scratch_capacity;
75 0 : return scratch;
76 : }
77 : }
78 :
79 : U_NAMESPACE_END
|