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

buf.c File Reference

General-purpose dynamic buffer. More...

#include <stdlib.h>
#include <string.h>
#include "buf.h"

Include dependency graph for buf.c:

Include dependency graph

Go to the source code of this file.

Defines

#define MINSIZE   10
 Minimum size buffer allocated.


Functions

int allocsize (int n)
 Returns the size of buffer to allocate, given the requested size.

ABufbuf_new (unsigned int len)
 Allocate a new buffer.

ABufbuf_free (ABuf *buf)
 Free a buffer_handle_type.

ABufbuf_check (ABuf *buf, unsigned int len)
 Check that the buffer is at least 'len' bytes.

ABufbuf_strcat (ABuf *buf, char *s)
 append the string 's' to the buffer.

ABufbuf_strcpy (ABuf *buf, char *s)
 Copy the string 's' into the buffer, replacing whatever's there.

ABufbuf_str_shrinkbyone (ABuf *buf)
 Shrinks the char string in the buffer by one character.

ABufbuf_terminate (ABuf *buf)
 Ensure that the last char in the buffer contains '\0'.


Detailed Description

General-purpose dynamic buffer.

Author:
Darryl Luff
A buffer pointer is passed as the first argument to every function call. All functions return a pointer to the buffer. Note that this may NOT be the same pointer that went in. For example:

 ABuf *buffer = buf_new(4);
 buffer = buf_cat(buffer, "123456");

Here, 'buffer's value after the call will be different to the 'buffer's value before the call, because the buffer is reallocated to fit in the new contents. So for any function call that can cause the buffer to grow, always save the return value into the buffer.

Functions are all NULL-safe. If a null buffer is passed in, a new buffer will be allocated and returned. So instead of the above, could just say:

 ABuf *buffer = buf_cpy(NULL, "123456");
To minimise reallocating memory, it is a good idea to use buf_new to allocate a buffer with a reasonable size though.

When finished with the buffer, call buf_free on it. buf_free always returns a NULL buffer pointer. This is to let you do:

 buffer = buf_free(buffer);
So the buffer pointer is set to NULL at the same time the memory is freed.

Definition in file buf.c.


Define Documentation

#define MINSIZE   10
 

Minimum size buffer allocated.

Any buffer allocation will return a buffer that is at least this size, even buf_new(0).

Definition at line 58 of file buf.c.

Referenced by allocsize().


Function Documentation

int allocsize int    n
 

Returns the size of buffer to allocate, given the requested size.

Parameters:
n  The requested size.
Returns:
Either the requested size or MINSIZE, whichever is smaller.

Definition at line 63 of file buf.c.

References MINSIZE.

Referenced by buf_new().

ABuf* buf_check ABuf   buf,
unsigned int    len
 

Check that the buffer is at least 'len' bytes.

If it is not at least 'len' bytes long, a new buffer is allocated. Returns a pointer to either the original or the new buffer. Always use:

 buf = buf_check(buf, len);
with this function, and never rely on the returned pointer being the same as the passed pointer.
Parameters:
buf  The original pointer. If NULL, a new buffer will be allocated.
len  The requested buffer size.
Returns:
Either the original buffer if it was large enough, or a new larger buffer.

Definition at line 112 of file buf.c.

References _a_buf::b_dat, _a_buf::b_len, buf_free(), and buf_new().

Referenced by buf_strcat(), buf_strcpy(), export_html_dialog_show(), my_DBform_to_url(), pg_DBform_to_url(), send_callback(), xp_DBform_to_url(), and xp_DBget_field_value_attrib().

ABuf* buf_free ABuf   buf
 

Free a buffer_handle_type.

Returns:
NULL pointer so you can use
 'buf = buf_free(buf);
to free the buffer and clear the variable in one line.

Definition at line 90 of file buf.c.

References _a_buf::b_dat.

Referenced by buf_check(), db_connect_dialog_reconnect(), disconnect_first(), export_as_html(), export_as_text(), export_text_dialog_create(), frm_save_file(), gui_load_query_from_file(), gui_send_query(), on_databaseDisconnect_activate(), on_queryImportDlgOkBtn_clicked(), query_add(), query_import_dialog_create(), query_save_dialog_create(), result_export(), send_callback(), and ws_save_file().

ABuf* buf_new unsigned int    len
 

Allocate a new buffer.

allocsize() is used to calculate the buffer size, so the returned buffer may be larger than the requested size.

Returns:
A pointer to the new buffer.
See also:
MINSIZE

Definition at line 74 of file buf.c.

References allocsize(), _a_buf::b_dat, and _a_buf::b_len.

Referenced by buf_check(), buf_strcat(), buf_strcpy(), db_connect_dialog_reconnect(), disconnect_first(), export_as_html(), export_as_text(), frm_save_file(), gui_load_query_from_file(), gui_send_query(), on_databaseDisconnect_activate(), on_queryImportDlgOkBtn_clicked(), query_add(), query_import_dialog_create(), result_export(), send_callback(), and ws_save_file().

ABuf* buf_str_shrinkbyone ABuf   buf
 

Shrinks the char string in the buffer by one character.

Parameters:
buf  The original buffer.
Returns:
The buffer.

Definition at line 187 of file buf.c.

References _a_buf::b_dat.

Referenced by frm_check_fields().

ABuf* buf_strcat ABuf   buf,
char *    s
 

append the string 's' to the buffer.

The buffer is reallocated if necessary.

 buf = buf_strcat(buf, "Hello");
Parameters:
buf  The original buffer
s  The string to append
Returns:
The new buffer

Definition at line 134 of file buf.c.

References _a_buf::b_dat, _a_buf::b_len, buf_check(), buf_new(), and buf_terminate().

Referenced by cfg_get_default_wsname(), db_connect_dialog_reconnect(), frm_check_fields(), frm_save_file(), get_rcname(), my_DBform_to_url(), pg_DBform_to_url(), query_import_dialog_create(), query_save_dialog_create(), send_callback(), ws_save_file(), xp_DBform_to_url(), xp_DBget_field_value_attrib(), and xp_DBget_field_value_content().

ABuf* buf_strcpy ABuf   buf,
char *    s
 

Copy the string 's' into the buffer, replacing whatever's there.

The buffer is reallocated if required.

 buf = buf_strcpy(buf, "Hello");
Parameters:
buf  The original buffer
s  The string to copy
Returns:
The new buffer.

Definition at line 158 of file buf.c.

References _a_buf::b_dat, _a_buf::b_len, buf_check(), buf_new(), and buf_terminate().

Referenced by cfg_get_default_wsname(), db_connect_dialog_reconnect(), frm_save_file(), my_DBform_to_url(), pg_DBform_to_url(), query_import_dialog_create(), query_save_dialog_create(), ws_save_file(), xp_DBget_field_value_attrib(), and xp_DBget_field_value_content().

ABuf* buf_terminate ABuf   buf
 

Ensure that the last char in the buffer contains '\0'.

Parameters:
buf  The original buffer.
Returns:
The buffer.

Definition at line 199 of file buf.c.

References _a_buf::b_dat, and _a_buf::b_len.

Referenced by buf_strcat(), and buf_strcpy().


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