Coding Guidelines/String Functions: Difference between revisions

From wiki.zmanda.com
Jump to navigation Jump to search
No edit summary
 
(get strappend in there too)
Line 1: Line 1:
Amanda provides a library of string functions with names that parse like this:
Amanda provides a library of string functions with names that parse like this:
  new|v|stralloc|f
  new|v|stralloc|f
as well as a few others (described below).   
as well as a few others (described below).  The entire set of functions appears in <tt>amanda.h</tt>.  The functions are all implemented as macros to debug functions, to allow tracing of file/line numbers.


The base function is, of course, ''stralloc'', which simply allocates enough space, and then copies the string it is given as an argument.  Adding ''new'' means that the first argument is a dynamically allocated string which this function will be '''replacing''': if it is not NULL, it will be freed after the new string is allocated and constructed.  This allows code like
The base function is, of course, ''stralloc'', which simply allocates enough space, and then copies the string it is given as an argument.  Adding ''new'' means that the first argument is a dynamically allocated string which this function will be '''replacing''': if it is not NULL, it will be freed after the new string is allocated and constructed.  This allows code like
Line 18: Line 18:
  *errmsg = vstralloc(TMPDIR, "/foo/", filename, NULL);
  *errmsg = vstralloc(TMPDIR, "/foo/", filename, NULL);


= Misc Functions =
The function ''stralloc2'' is a simple concatenation option; ''stralloc2(x, y)'' is equivalent to ''vstralloc(x, y, NULL)''.
The function ''stralloc2'' is a simple concatenation option; ''stralloc2(x, y)'' is equivalent to ''vstralloc(x, y, NULL)''.


The entire set of functions appears in <tt>amanda.h</tt>The functions are all implemented as macros to debug functions, to allow tracing of file/line numbers.
The function ''strappend'' is a macro which appends its second argument to its first''strappend(x, y)'' is equivalent to ''newvstralloc(x, x, y, NULL)''.

Revision as of 18:14, 30 May 2007

Amanda provides a library of string functions with names that parse like this:

new|v|stralloc|f

as well as a few others (described below). The entire set of functions appears in amanda.h. The functions are all implemented as macros to debug functions, to allow tracing of file/line numbers.

The base function is, of course, stralloc, which simply allocates enough space, and then copies the string it is given as an argument. Adding new means that the first argument is a dynamically allocated string which this function will be replacing: if it is not NULL, it will be freed after the new string is allocated and constructed. This allows code like

{
  char *tmp = NULL;
  tmp = newstralloc(tmp, "first string");
  /* ... */
  tmp = newstralloc(tmp, "second string");
  /* ... */
  amfree(tmp);
}

without worrying about memory leaks (since each newstralloc invocation will free(tmp) if it is non-NULL).

Adding v to a function name indicates that it is variadic - that is, that it takes a variable number of arguments. Variadic functions which end in f act like printf, with the format as the first (or second, in the case of new* functions) argument. Variadic functions without f must be given a NULL as the final argument. Examples:

tmp = newvstrallocf(tmp, "could not open '%s': %s", filename, strerror(errno));
*errmsg = vstralloc(TMPDIR, "/foo/", filename, NULL);

Misc Functions

The function stralloc2 is a simple concatenation option; stralloc2(x, y) is equivalent to vstralloc(x, y, NULL).

The function strappend is a macro which appends its second argument to its first. strappend(x, y) is equivalent to newvstralloc(x, x, y, NULL).