[asterisk-commits] ctooley: branch ctooley/excel-sip-changes r118051 - /team/ctooley/excel-sip-c...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 23 07:57:56 CDT 2008


Author: ctooley
Date: Fri May 23 07:57:55 2008
New Revision: 118051

URL: http://svn.digium.com/view/asterisk?view=rev&rev=118051
Log:
working on string functions

Modified:
    team/ctooley/excel-sip-changes/include/asterisk/strings.h

Modified: team/ctooley/excel-sip-changes/include/asterisk/strings.h
URL: http://svn.digium.com/view/asterisk/team/ctooley/excel-sip-changes/include/asterisk/strings.h?view=diff&rev=118051&r1=118050&r2=118051
==============================================================================
--- team/ctooley/excel-sip-changes/include/asterisk/strings.h (original)
+++ team/ctooley/excel-sip-changes/include/asterisk/strings.h Fri May 23 07:57:55 2008
@@ -684,6 +684,132 @@
 }
 )
 
+/*! \brief Replace a specific character with another character
+ *
+ * \arg buf This is the address pointer to a struct ast_str.
+ *
+ * \arg replace The char to be matched within the string contained
+ *              in the ast_str struct that is be replaced.
+ *
+ * \arg with The char to replace occurrences of "replace" with.  If
+ *           it is an empty string the matched char will be removed
+ *           and the string shortened.
+ *
+ * \arg occurrences The number of occurrences of "replace" to find
+ *                  and replace with the char in "with".
+ *
+ * \arg end Which end of the string to start searching at.  If end
+ *          0 searching starts at the beginning, if 1 it starts at
+ *          the end and works backwards.
+ *
+ * \return The return value of this function is -1 if it does not
+ * 	   work or 0 on success.
+ */
+AST_INLINE_API(
+int ast_str_replace_char(struct ast_str **buf, const char replace, 
+	const char *with, const int occurrences, const int end),
+{
+
+	int replace_ptr;
+	char *str_buf;
+	char *remove_buf;
+	int counter = 0;
+	struct ast_str *remove;
+
+	if(!buf || !replace) {
+		return -1;
+	}
+
+	str_buf = ast_strdupa(buf->str);
+	if (!ast_strlen_zero(with)) {
+		if (!end) {
+			while((replace_ptr = strchr(str_buf, replace)) != NULL) {
+		                replace_ptr[0] = with[0];
+				if(occurrences > 0 && counter == occurrences) {
+					break;
+				} else {
+					counter++;
+				}
+			}
+		} else {
+			while((replace_ptr = strrchr(str_buf, replace)) != NULL) {
+		                replace_ptr[0] = with[0];
+				if(occurrences > 0 && counter == occurrences) {
+					break;
+				} else {
+					counter++;
+				}
+			}
+		}	
+		buf->str = str_buf;
+		return 0;
+	} else {
+		while (str_buf) {
+			remove = ast_str_alloca(buf->len);
+			remove_buf = strsep(str_buf, replace);
+			ast_str_append(remove, remove_buf);
+		}
+		buf->str = remove->str;
+		buf->used = remove->used;
+		return 0;
+	}
+}
+)
+
+/*! \brief Get the current string contained in the ast_str type struct
+ *
+ * \arg buf This is the address pointer to a struct ast_str
+ *
+ * \return The return value of this function is the current string
+ * 	   contained by the struct.
+ */
+AST_INLINE_API(
+char * ast_str_to_string(struct ast_str **buf),
+{
+	if (buf) {
+		return buf->str;
+	} else {
+		return NULL;
+	}
+}
+)
+
+/*! \brief Get the current maximum length of a dynamic string.
+ *
+ * \arg buf This is the address pointer to a struct ast_str
+ *
+ * \return The return value of this function is the current maximum
+ * 	   length (capicity) of the string contained by the struct.
+ */
+AST_INLINE_API(
+size_t ast_str_capacity(struct ast_str **buf),
+{
+	if (buf) {
+		return buf->len;
+	} else {
+		return 0;
+	}
+}
+)
+
+/*! \brief Get the current length of a dynamic string.
+ *
+ * \arg buf This is the address pointer to a struct ast_str
+ *
+ * \return The return value of this function is the current length
+ * 	   of the string contained by the struct.
+ */
+AST_INLINE_API(
+size_t ast_str_length(struct ast_str **buf),
+{
+	if (buf) {
+		return buf->used;
+	} else {
+		return 0;
+	}
+}
+)
+
 /*!
  * \brief Compute a hash value on a string
  *




More information about the asterisk-commits mailing list