string.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:3k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. /*
  18.   Code for handling strings with can grow dynamicly.
  19.   Copyright Monty Program KB.
  20.   By monty.
  21. */
  22. #include "mysys_priv.h"
  23. #include <m_string.h>
  24. my_bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
  25.     uint init_alloc, uint alloc_increment)
  26. {
  27.   uint length;
  28.   DBUG_ENTER("init_dynamic_string");
  29.   if (!alloc_increment)
  30.     alloc_increment=128;
  31.   length=1;
  32.   if (init_str && (length= (uint) strlen(init_str)+1) < init_alloc)
  33.     init_alloc=((length+alloc_increment-1)/alloc_increment)*alloc_increment;
  34.   if (!init_alloc)
  35.     init_alloc=alloc_increment;
  36.   if (!(str->str=(char*) my_malloc(init_alloc,MYF(MY_WME))))
  37.     DBUG_RETURN(TRUE);
  38.   str->length=length-1;
  39.   if (init_str)
  40.     memcpy(str->str,init_str,length);
  41.   str->max_length=init_alloc;
  42.   str->alloc_increment=alloc_increment;
  43.   DBUG_RETURN(FALSE);
  44. }
  45. my_bool dynstr_set(DYNAMIC_STRING *str, const char *init_str)
  46. {
  47.   uint length;
  48.   DBUG_ENTER("dynstr_set");
  49.   if (init_str && (length= (uint) strlen(init_str)+1) > str->max_length)
  50.   {
  51.     str->max_length=((length+str->alloc_increment-1)/str->alloc_increment)*
  52.       str->alloc_increment;
  53.     if (!str->max_length)
  54.       str->max_length=str->alloc_increment;
  55.     if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME))))
  56.       DBUG_RETURN(TRUE);
  57.   }
  58.   if (init_str)
  59.   {
  60.     str->length=length-1;
  61.     memcpy(str->str,init_str,length);
  62.   }
  63.   else
  64.     str->length=0;
  65.   DBUG_RETURN(FALSE);
  66. }
  67. my_bool dynstr_realloc(DYNAMIC_STRING *str, ulong additional_size)
  68. {
  69.   DBUG_ENTER("dynstr_realloc");
  70.   if (!additional_size) DBUG_RETURN(FALSE);
  71.   if (str->length + additional_size > str->max_length)
  72.   {
  73.     str->max_length=((str->length + additional_size+str->alloc_increment-1)/
  74.      str->alloc_increment)*str->alloc_increment;
  75.     if (!(str->str=(char*) my_realloc(str->str,str->max_length,MYF(MY_WME))))
  76.       DBUG_RETURN(TRUE);
  77.   }
  78.   DBUG_RETURN(FALSE);
  79. }
  80. my_bool dynstr_append(DYNAMIC_STRING *str, const char *append)
  81. {
  82.   return dynstr_append_mem(str,append,strlen(append));
  83. }
  84. my_bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append,
  85.   uint length)
  86. {
  87.   char *new_ptr;
  88.   if (str->length+length >= str->max_length)
  89.   {
  90.     uint new_length=(str->length+length+str->alloc_increment)/
  91.       str->alloc_increment;
  92.     new_length*=str->alloc_increment;
  93.     if (!(new_ptr=(char*) my_realloc(str->str,new_length,MYF(MY_WME))))
  94.       return TRUE;
  95.     str->str=new_ptr;
  96.     str->max_length=new_length;
  97.   }
  98.   memcpy(str->str + str->length,append,length);
  99.   str->length+=length;
  100.   str->str[str->length]=0; /* Safety for C programs */
  101.   return FALSE;
  102. }
  103. void dynstr_free(DYNAMIC_STRING *str)
  104. {
  105.   if (str->str)
  106.   {
  107.     my_free(str->str,MYF(MY_WME));
  108.     str->str=0;
  109.   }
  110. }