xmalloc string functions



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.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

.



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: Bug analysis
    ... char *ReadTextFile ... The writer of this code is an experienced C programmer. ... has this bug, that is a classical bug with zero terminated strings, ... in the implementation of the string library in lcc-win you ...
    (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)