Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

buf.c

Go to the documentation of this file.
00001 
00033 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00034  * Copyright (C) 2002-2003 Darryl Luff
00035  *
00036  * This program is free software; you can redistribute it and/or modify
00037  * it under the terms of the GNU General Public License as published by
00038  * the Free Software Foundation; either version 2 of the License, or
00039  * (at your option) any later version.
00040  *
00041  * This program is distributed in the hope that it will be useful,
00042  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00043  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00044  * GNU General Public License for more details.
00045  *
00046  * You should have received a copy of the GNU General Public License
00047  * along with this program; if not, write to the Free Software
00048  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00049  */
00052 #include <stdlib.h>
00053 #include <string.h>
00054 #include "buf.h"
00055 
00058 #define MINSIZE 10
00059 
00063 int allocsize(int n) {
00064     if (n < MINSIZE)
00065         return MINSIZE;
00066     else
00067         return n;
00068 }
00069 
00074 ABuf *buf_new(unsigned int len)
00075 {
00076     ABuf *b = calloc(1, sizeof(ABuf));
00077     if (b) {
00078         b->b_len = allocsize(len);
00079         b->b_dat = calloc(1, b->b_len);
00080     }
00081     return b;
00082 }
00083 
00090 ABuf *buf_free(ABuf * buf)
00091 {
00092     if (buf) {
00093         if (buf->b_dat)
00094             free(buf->b_dat);
00095 
00096         free(buf);
00097     }
00098     return NULL;
00099 }
00100 
00112 ABuf *buf_check(ABuf * buf, unsigned int len)
00113 {
00114     if (!buf)
00115         return buf_new(len);
00116 
00117     if (buf->b_len < len) {
00118         ABuf *b = buf_new(len);
00119         memmove(b->b_dat, buf->b_dat, buf->b_len);
00120         buf_free(buf);
00121         return b;
00122     }
00123     return buf;
00124 }
00125 
00134 ABuf *buf_strcat(ABuf * buf, char *s)
00135 {
00136     ABuf *b = NULL;
00137     if (! buf)
00138         b = buf_new(strlen(s)+1);
00139     else
00140         b = buf_check(buf, strlen(buf->b_dat) + strlen(s) + 1);
00141 
00142     if (b) {
00143         strncat(b->b_dat, s, b->b_len);
00144         b = buf_terminate(b);
00145     }
00146 
00147     return b;
00148 }
00149 
00158 ABuf *buf_strcpy(ABuf * buf, char *s)
00159 {
00160     ABuf *b = NULL;
00161     int len;
00162 
00163     if (! s)
00164         return buf;
00165 
00166     len = strlen(s) + 1;
00167     if (! buf)
00168         b = buf_new(len);
00169     else
00170         b = buf_check(buf, len);
00171 
00172     if ((b) && (b->b_dat)) {
00173         if ((s) && (strlen(s) > 0)) {
00174             strncpy(b->b_dat, s, b->b_len);
00175             buf_terminate(b);
00176         }
00177         else
00178             *(b->b_dat) = '\0';
00179     }
00180     return b;
00181 }
00182 
00187 ABuf *buf_str_shrinkbyone(ABuf * buf) {
00188     if ((buf == NULL) || (buf->b_dat == NULL) || (strlen(buf->b_dat) < 1))
00189         return buf;
00190 
00191     buf->b_dat[strlen(buf->b_dat)-1] = '\0';
00192     return buf;
00193 }
00194 
00199 ABuf *buf_terminate(ABuf * buf)
00200 {
00201     if (buf) {
00202         if ((buf->b_dat) && (buf->b_len)) {
00203             buf->b_dat[buf->b_len - 1] = '\0';
00204         }
00205     }
00206     return buf;
00207 }

Generated on Sun May 9 19:19:15 2004 for GtkSQL by doxygen1.2.18