cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h" #include "dynamic_buffer.h" #include "stralloc.h" #include "error.h"
9aa6fa1997-05-19Fredrik Hübinette (Hubbe) #include "pike_memory.h"
5267b71995-08-09Fredrik Hübinette (Hubbe)  static dynamic_buffer buff;
1b3a7f1997-09-11Henrik Grubbström (Grubba) char *low_make_buf_space(INT32 space, dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) { char *ret;
6ff3e91996-04-13Fredrik Hübinette (Hubbe) #ifdef DEBUG if(!buf->s.str) fatal("ARRRRGH! Deadly Trap!\n"); #endif
1b3a7f1997-09-11Henrik Grubbström (Grubba)  if(buf->s.len+space >= buf->bufsize)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
38145a1996-02-18Fredrik Hübinette (Hubbe)  if(!buf->bufsize) buf->bufsize=1;
5267b71995-08-09Fredrik Hübinette (Hubbe)  do{ buf->bufsize*=2;
1b3a7f1997-09-11Henrik Grubbström (Grubba)  }while(buf->s.len+space >= buf->bufsize);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
1b3a7f1997-09-11Henrik Grubbström (Grubba)  buf->s.str=(char *)realloc(buf->s.str, buf->bufsize);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!buf->s.str) error("Out of memory.\n"); }
1b3a7f1997-09-11Henrik Grubbström (Grubba)  ret = buf->s.str + buf->s.len; buf->s.len += space;
5267b71995-08-09Fredrik Hübinette (Hubbe)  return ret; } void low_my_putchar(char b,dynamic_buffer *buf) { #ifdef DEBUG if(!buf->s.str) fatal("Error in internal buffering.\n"); #endif low_make_buf_space(1,buf)[0]=b; } void low_my_binary_strcat(const char *b,INT32 l,dynamic_buffer *buf) { #ifdef DEBUG if(!buf->s.str) fatal("Error in internal buffering.\n"); #endif MEMCPY(low_make_buf_space(l,buf),b,l); }
fe13331998-03-03Fredrik Hübinette (Hubbe) void debug_initialize_buf(dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
e43ca21996-11-15Fredrik Hübinette (Hubbe)  buf->s.str=(char *)xalloc((buf->bufsize=BUFFER_BEGIN_SIZE));
5267b71995-08-09Fredrik Hübinette (Hubbe)  *(buf->s.str)=0; buf->s.len=0; }
e43ca21996-11-15Fredrik Hübinette (Hubbe) void low_reinit_buf(dynamic_buffer *buf) { if(!buf->s.str) { initialize_buf(buf); }else{ *(buf->s.str)=0; buf->s.len=0; } }
1b3a7f1997-09-11Henrik Grubbström (Grubba) void low_init_buf_with_string(string s, dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) { if(buf->s.str) { free(buf->s.str); buf->s.str=NULL; } buf->s=s;
82ac181996-11-26Fredrik Hübinette (Hubbe)  if(!buf->s.str) initialize_buf(buf);
5267b71995-08-09Fredrik Hübinette (Hubbe)  /* if the string is an old buffer, this realloc will set the old the bufsize back */ for(buf->bufsize=BUFFER_BEGIN_SIZE;buf->bufsize<buf->s.len;buf->bufsize*=2); buf->s.str=realloc(buf->s.str,buf->bufsize);
82ac181996-11-26Fredrik Hübinette (Hubbe) #ifdef DEBUG if(!buf->s.str) fatal("Realloc failed.\n"); #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) } string complex_free_buf(void) { string tmp; if(!buff.s.str) return buff.s; my_putchar(0); buff.s.len--; tmp=buff.s; buff.s.str=0; return tmp; } void toss_buffer(dynamic_buffer *buf) { if(buf->s.str) free(buf->s.str); buf->s.str=0; } char *simple_free_buf(void) { if(!buff.s.str) return 0; return complex_free_buf().str; }
fe13331998-03-03Fredrik Hübinette (Hubbe) struct pike_string *debug_low_free_buf(dynamic_buffer *buf)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *q;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!buf->s.str) return 0; q=make_shared_binary_string(buf->s.str,buf->s.len); free(buf->s.str); buf->s.str=0; buf->s.len=0; return q; }
fe13331998-03-03Fredrik Hübinette (Hubbe) struct pike_string *debug_free_buf(void) { return low_free_buf(&buff); }
5267b71995-08-09Fredrik Hübinette (Hubbe) char *make_buf_space(INT32 space) { return low_make_buf_space(space,&buff); } void my_putchar(char b) { low_my_putchar(b,&buff); } void my_binary_strcat(const char *b,INT32 l) { low_my_binary_strcat(b,l,&buff); } void my_strcat(const char *b) { my_binary_strcat(b,strlen(b)); }
e43ca21996-11-15Fredrik Hübinette (Hubbe) void init_buf(void) { low_reinit_buf(&buff); }
5267b71995-08-09Fredrik Hübinette (Hubbe) void init_buf_with_string(string s) { low_init_buf_with_string(s,&buff); }
fe13331998-03-03Fredrik Hübinette (Hubbe) char *debug_return_buf(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) { my_putchar(0); return buff.s.str; } /* int my_get_buf_size() { return buff->s.len; } */