2 * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3 * Copyright (C) 2005 QSDN,Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 #include <apr_pools.h>
20 #include "chxj_buffered_write.h"
24 chxj_buffered_write_init(apr_pool_t *parent_pool, buf_object *buf)
27 if ((rv = apr_pool_create(&buf->pool, parent_pool)) != APR_SUCCESS) {
30 buf->buffer = apr_palloc(buf->pool, CHXJ_BUFFERED_OBJECT_SIZE);
34 memset(buf->buffer, 0, CHXJ_BUFFERED_OBJECT_SIZE);
41 chxj_buffered_write(const char *src, buf_object *buf, const char *add, int add_len)
45 if (buf->use_len + add_len < CHXJ_BUFFERED_OBJECT_SIZE - 1) {
46 memcpy(&buf->buffer[buf->use_len], add, add_len);
47 buf->use_len += add_len;
48 buf->buffer[buf->use_len] = 0;
51 else if (buf->use_len < CHXJ_BUFFERED_OBJECT_SIZE - 1) {
52 int new_add_len = CHXJ_BUFFERED_OBJECT_SIZE - 1 - buf->use_len;
53 memcpy(&buf->buffer[buf->use_len], add, new_add_len);
54 buf->use_len += new_add_len;
55 buf->buffer[buf->use_len] = 0;
56 result = chxj_buffered_write_flush(src, buf);
57 result = chxj_buffered_write(result, buf, add + new_add_len, add_len - new_add_len);
60 result = chxj_buffered_write_flush(src, buf);
61 result = chxj_buffered_write(result, buf, add, add_len);
67 chxj_buffered_write_flush(const char *src, buf_object *buf)
71 buf->buffer[buf->use_len] = 0;
72 result = apr_pstrcat(buf->pool, src, buf->buffer, NULL);
81 chxj_buffered_write_terminate(buf_object *buf)
83 apr_pool_destroy(buf->pool);