[Refdb-cvs] CVS: refdb/src strfncs.c,1.15.2.3,1.15.2.4 strfncs.h,1.8.2.2,1.8.2.3
Status: Beta
Brought to you by:
mhoenicka
From: Markus H. <mho...@us...> - 2005-10-11 20:35:16
|
Update of /cvsroot/refdb/refdb/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8637/src Modified Files: Tag: Release_0_9_5_stable strfncs.c strfncs.h Log Message: added escape_chars(), unescape_chars() Index: strfncs.c =================================================================== RCS file: /cvsroot/refdb/refdb/src/strfncs.c,v retrieving revision 1.15.2.3 retrieving revision 1.15.2.4 diff -u -U2 -r1.15.2.3 -r1.15.2.4 --- strfncs.c 9 Sep 2004 21:32:50 -0000 1.15.2.3 +++ strfncs.c 11 Oct 2005 20:35:02 -0000 1.15.2.4 @@ -954,4 +954,91 @@ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + escape_chars(): backslash-escapes characters in a string + + size_t escape_chars returns the length of the escaped string + + char *dest pointer to a buffer that will receive the escaped string + must hold at least twice the size of orig + + char *orig pointer to the buffer with the string to be escaped + + size_t orig_size length of original string + + const char *toescape array of characters that must be escaped + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +size_t escape_chars(char *dest, const char *orig, size_t orig_size, const char *toescape) { + char *curdest = dest; + const char *curorig = orig; + const char *curescaped; + size_t len = 0; + + while (curorig && curorig < orig+orig_size) { + curescaped = toescape; + while (curescaped && *curescaped) { + if (*curorig == *curescaped) { + *curdest = '\\'; + curdest++; + len++; + break; + } + curescaped++; + } + /* Copy char to destination */ + *curdest = *curorig; + + curorig++; + curdest++; + len++; + } + + /* append a NULL byte. This is required if orig was a + zero-terminated string. It does not hurt if orig was a + binary string as the calling function is not supposed to + read past len bytes */ + *curdest = '\0'; + return len; +} + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + unescape_chars(): unescapes a backslash-escaped string + + size_t unescape_chars returns the length of the unescaped string + + char *dest pointer to a buffer that will receive the unescaped string + must hold at least the size of orig + + char *orig pointer to the buffer with the string to be unescaped + + size_t orig_size length of original string + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +size_t unescape_chars(char *dest, const char *orig, size_t orig_size) { + char *curdest = dest; + const char *curorig = orig; + const char *curescaped; + size_t len = 0; + + while (curorig && curorig < orig+orig_size) { + if (*curorig == '\\') { + curorig++; + } + /* Copy char to destination */ + *curdest = *curorig; + + curorig++; + curdest++; + len++; + } + + /* append a NULL byte. This is required if orig was a + zero-terminated string. It does not hurt if orig was a + binary string as the calling function is not supposed to + read past len bytes */ + *curdest = '\0'; + return len; +} + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ increment_suffix(): calling this function repeatedly generates a sequence of suffices. The sequence Index: strfncs.h =================================================================== RCS file: /cvsroot/refdb/refdb/src/strfncs.h,v retrieving revision 1.8.2.2 retrieving revision 1.8.2.3 diff -u -U2 -r1.8.2.2 -r1.8.2.3 --- strfncs.h 9 Sep 2004 21:32:50 -0000 1.8.2.2 +++ strfncs.h 11 Oct 2005 20:35:02 -0000 1.8.2.3 @@ -67,4 +67,6 @@ char *canonicalize_path(char *the_path); char *strip_quote(char *the_string); +size_t escape_chars(char *dest, const char *orig, size_t orig_size, const char *toescape); +size_t unescape_chars(char *dest, const char *orig, size_t orig_size); int increment_suffix(char* suffix, int max_depth, int upper); int parse_versioninfo(const char* version, struct VERSIONINFO* ver); |