1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
9 #include "base/logging.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
13 namespace chrome_pdf
{
15 template <class StringType
>
16 PDFiumAPIStringBufferAdapter
<StringType
>::PDFiumAPIStringBufferAdapter(
19 bool check_expected_size
)
21 data_(base::WriteInto(str
, expected_size
+ 1)),
22 expected_size_(expected_size
),
23 check_expected_size_(check_expected_size
),
27 template <class StringType
>
28 PDFiumAPIStringBufferAdapter
<StringType
>::~PDFiumAPIStringBufferAdapter() {
32 template <class StringType
>
33 void* PDFiumAPIStringBufferAdapter
<StringType
>::GetData() {
38 template <class StringType
>
39 void PDFiumAPIStringBufferAdapter
<StringType
>::Close(size_t actual_size
) {
43 if (check_expected_size_
)
44 DCHECK_EQ(expected_size_
, actual_size
);
46 if (actual_size
> 0) {
47 DCHECK((*str_
)[actual_size
- 1] == 0);
48 str_
->resize(actual_size
- 1);
54 template <class StringType
>
55 PDFiumAPIStringBufferSizeInBytesAdapter
<StringType
>::
56 PDFiumAPIStringBufferSizeInBytesAdapter(StringType
* str
,
58 bool check_expected_size
)
60 expected_size
/ sizeof(typename
StringType::value_type
),
61 check_expected_size
) {
62 DCHECK(expected_size
% sizeof(typename
StringType::value_type
) == 0);
65 template <class StringType
>
66 PDFiumAPIStringBufferSizeInBytesAdapter
<
67 StringType
>::~PDFiumAPIStringBufferSizeInBytesAdapter() = default;
69 template <class StringType
>
70 void* PDFiumAPIStringBufferSizeInBytesAdapter
<StringType
>::GetData() {
71 return adapter_
.GetData();
74 template <class StringType
>
75 void PDFiumAPIStringBufferSizeInBytesAdapter
<StringType
>::Close(
77 DCHECK(actual_size
% sizeof(typename
StringType::value_type
) == 0);
78 adapter_
.Close(actual_size
/ sizeof(typename
StringType::value_type
));
81 // explicit instantiations
82 template class PDFiumAPIStringBufferAdapter
<std::string
>;
83 template class PDFiumAPIStringBufferAdapter
<base::string16
>;
84 template class PDFiumAPIStringBufferSizeInBytesAdapter
<base::string16
>;
86 } // namespace chrome_pdf