summaryrefslogtreecommitdiff
path: root/libguile/strings.c
diff options
context:
space:
mode:
authorStefan Jahn <stefan@lkcc.org>2001-12-08 12:50:37 +0000
committerStefan Jahn <stefan@lkcc.org>2001-12-08 12:50:37 +0000
commit4d4528e79a13ac73e4278fe877a8a37bdc260240 (patch)
tree6cd3f7ba2b4261679a2037028ff4072a9aefa57e /libguile/strings.c
parenta7a7bb95eb2d62ceeac14b236c5b6c9f80b002af (diff)
downloadguile-4d4528e79a13ac73e4278fe877a8a37bdc260240.tar.gz
2001-12-08 Stefan Jahn <stefan@lkcc.org>
* strings.c (scm_c_string2str): New function. Converts a given Scheme string into a C string. Also put in two THINKME's regarding the malloc policy for the missing converter routines.
Diffstat (limited to 'libguile/strings.c')
-rw-r--r--libguile/strings.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/libguile/strings.c b/libguile/strings.c
index 680707261..dab7a18f8 100644
--- a/libguile/strings.c
+++ b/libguile/strings.c
@@ -336,6 +336,42 @@ SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
}
#undef FUNC_NAME
+/* Converts the given Scheme string OBJ into a C string, containing a copy
+ of OBJ's content with a trailing null byte. If LENP is non-NULL, set
+ *LENP to the string's length.
+
+ When STR is non-NULL it receives the copy and is returned by the function,
+ otherwise new memory is allocated and the caller is responsible for
+ freeing it via free(). If out of memory, NULL is returned.
+
+ Note that Scheme strings may contain arbitrary data, including null
+ characters. This means that null termination is not a reliable way to
+ determine the length of the returned value. However, the function always
+ copies the complete contents of OBJ, and sets *LENP to the true length
+ of the string (if LENP is non-null). */
+char *
+scm_c_string2str (SCM obj, char *str, size_t *lenp)
+{
+ size_t len;
+
+ SCM_ASSERT (SCM_STRINGP (obj), obj, SCM_ARG1, "scm_c_string2str");
+ len = SCM_STRING_LENGTH (obj);
+
+ /* THINKME: What malloc policy? */
+ if (str == NULL)
+ str = (char *) malloc ((len + 1) * sizeof (char));
+ if (str == NULL)
+ return NULL;
+
+ memcpy (str, SCM_STRING_CHARS (obj), len);
+ /* THINKME: Is this necessary for arguments? I do not think so... */
+ scm_remember_upto_here_1 (obj);
+ str[len] = '\0';
+
+ if (lenp != NULL)
+ *lenp = len;
+ return str;
+}
void
scm_init_strings ()