Re: xmalloc string functions



Malcolm McLean wrote:

Here are six functions implemented on top of xmalloc(). No C
programmer should have any triouble providing the implemetations,
though replace and getquote are non-trivial.

char *dup(const char *str);
char *cat(const char *str1, const char *str2);
char *catandkill(char *str1, const char *str2);
char *tok(const char *str, const char *delims, char **end);
char *midstr(const char *str, int idx, int len);
char *replace(const char *str, const char *pattern, const char *rep);
char *getquote(const char *str, char quote, char escape, char **end);
char *getline(FILE *fp);

All return strings allocated with xmalloc().

dup() is strdup(), and duplicates a string. cat() concatenates two
strings. catandkill concatenagtes two strings, and frees the first. It
also returns dup(str2) is the first pointer is null. It is designed
for building strings in conveneint loops.
tok() is strtok() that takes a string argument instead of using a
global. midstr() takes out a central portion of the string. replace()
is a find and replace, getquote() retrives the next quoted string,
allowing for the quotes themselves to be escaped. You will have to
escape non-quote escaped characters manually, of course. getline()
will be a familiar friend.

I've think we've got something quite powerful here, purely because
none of these functions can ever return null for out of memory
conditions. It massively simplifies string handling.
The library is not terribly efficient and doesn't pretend to be. If
you are doing heavy string processing you might want to look at Paul
Hsieh's better string library, that stores a length parameter with
strings. It is meant to be a few simple functions for easy
manipulation.

Just one suggestion. Usually it's not typical for a /library/ function
to take the liberty of terminating your program. I suggest that
xmalloc() calls a user supplied call-back function if malloc() fails.
The address of the call back need not be passed with every function in
the library. Instead the user could call a call-back register function
before using the other library routines.

The call-back allows the library client to decide the strategy to adopt
on malloc() failure. The client might decide to just call exit() or it
might print a few messages before doing so, or it might decide to
continue with some other part of the program, or whatever...

.



Relevant Pages

  • Re: xmalloc string functions
    ... char *dup; ... char *midstr(const char *str, int idx, int len); ... tokis strtokthat takes a string argument instead of using a global. ... replaceis a find and replace, getquote() retrives the next quoted string, allowing for the quotes themselves to be escaped. ...
    (comp.lang.c)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)