1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include "sal/config.h" 25 #include "osl/diagnose.h" 26 #include "rtl/string.h" 27 #include "sal/types.h" 28 #include "xmlreader/pad.hxx" 29 #include "xmlreader/span.hxx" 30 31 namespace xmlreader { 32 33 void Pad::add(char const * begin, sal_Int32 length) { 34 OSL_ASSERT( 35 begin != 0 && length >= 0 && !(span_.is() && buffer_.getLength() != 0)); 36 if (length != 0) { 37 flushSpan(); 38 if (buffer_.getLength() == 0) { 39 span_ = Span(begin, length); 40 } else { 41 buffer_.append(begin, length); 42 } 43 } 44 } 45 46 void Pad::addEphemeral(char const * begin, sal_Int32 length) { 47 OSL_ASSERT( 48 begin != 0 && length >= 0 && !(span_.is() && buffer_.getLength() != 0)); 49 if (length != 0) { 50 flushSpan(); 51 buffer_.append(begin, length); 52 } 53 } 54 55 void Pad::clear() { 56 OSL_ASSERT(!(span_.is() && buffer_.getLength() != 0)); 57 span_.clear(); 58 buffer_.setLength(0); 59 } 60 61 Span Pad::get() const { 62 OSL_ASSERT(!(span_.is() && buffer_.getLength() != 0)); 63 if (span_.is()) { 64 return span_; 65 } else if (buffer_.getLength() == 0) { 66 return Span(RTL_CONSTASCII_STRINGPARAM("")); 67 } else { 68 return Span(buffer_.getStr(), buffer_.getLength()); 69 } 70 } 71 72 void Pad::flushSpan() { 73 if (span_.is()) { 74 buffer_.append(span_.begin, span_.length); 75 span_.clear(); 76 } 77 } 78 79 } 80