/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <b.h>

b_error_t
b_string_new (b_string_t ** new)
{
  B_LOG1 ("b_string_new: %p", new);

  if (!new)
    return B_ERROR_DATA;

  if (!(*new = (b_string_t *) calloc (1, sizeof (b_string_t))))
    return B_ERROR_MEMORY;

  return B_OK;
}

b_error_t
b_string_new_callback (b_string_t ** new, b_write_func callback,
		       b_uint64 size, void *data)
{
  B_LOG4 ("b_string_new_callback: %p %p %d %p", new, callback, (int) size,
	  data);

  if (!new || !callback || !size)
    return B_ERROR_DATA;

  if (!(*new = (b_string_t *) calloc (1, sizeof (b_string_t))))
    return B_ERROR_MEMORY;

  (*new)->callback = callback;
  (*new)->callback_data = data;
  (*new)->callback_size = size;

  return B_OK;
}

b_error_t
b_string_destroy (b_string_t * str, b_int64 * ret)
{
  B_LOG2 ("b_string_destroy: %p %p", str, ret);

  if (!str)
    return B_ERROR_DATA;

  if (str->size && str->callback && str->callback_ret >= 0)
    str->callback_ret =
      str->callback (str->string, str->size, str->callback_data);

  if (str->string)
    free (str->string);

  if (ret)
    *ret = str->callback_ret;

  free (str);
  return B_OK;
}

b_error_t
b_string_add (b_string_t * str, unsigned char *what, b_uint64 size)
{
  B_LOG3 ("b_string_add: %p %p %d", str, what, (int) size);

  if (!str || !what)
    return B_ERROR_DATA;

  size = size > 0 ? size : strlen ((char *) what);

  if (!str->string)
    str->string = malloc (sizeof (char) * (size_t) (size + 1));
  else
    str->string =
      realloc (str->string, sizeof (char) * (size_t) (size + 1 + str->size));

  if (!str->string)
    return B_ERROR_MEMORY;

  memcpy (str->string + str->size, what, (size_t) size);
  str->size += size;
  str->string[str->size] = 0;

  if (str->callback_ret >= 0 && str->callback
      && str->size >= str->callback_size)
    {
      str->callback_ret =
	str->callback (str->string, str->size, str->callback_data);
      free (str->string);

      str->string = NULL;
      str->size = 0;
    }

  return B_OK;
}

unsigned char *
b_string_alloc (unsigned char *what, b_uint64 len)
{
  unsigned char *str;

  B_LOG2 ("b_string_alloc: %p %d", what, (int) len);

  if (!
      (str =
       (unsigned char *) malloc (sizeof (unsigned char) *
				 (size_t) (len + 1))))
    return NULL;

  memcpy (str, what, (size_t) len);
  str[len] = 0;

  return str;
}

/* EOF */
