[PATCH] Octstr additions
Nisan Bloch <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi All
Some additions to octstr.[ch]
/*
* Strip all occurence of char ch from start of Octstr
*/
void octstr_strip_char(Octstr *text, char ch);
/*
* Check if ostr is numeric
*/
int octstr_isnum(Octstr *ostr1);
/*
* Replace all occurences of needle with repl within haystack
*/
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl);
/*
* Symbolise hexstr - 78797a becomes %78%79%7a
*/
int octstr_symbolize(Octstr *ostr);
Nisan
--- ../cvs/gateway/gwlib/octstr.h Thu Sep 5 21:11:59 2002
+++ ../gateway-click/gwlib/octstr.h Sat Mar 1 00:07:32 2003
@@ -582,4 +582,25 @@
*/
int octstr_recode (Octstr *tocode, Octstr *fromcode, Octstr *orig);
+/*
+ * Strip all occurence of char ch from start of Octstr
+ */
+void octstr_strip_char(Octstr *text, char ch);
+
+/*
+ * Check if ostr is numeric
+ */
+int octstr_isnum(Octstr *ostr1);
+
+/*
+ * Replace all occurences of needle with repl within haystack
+ */
+
+void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl);
+
+/*
+ * Symbolise hexstr - 78797a becomes %78%79%7a
+*/
+int octstr_symbolize(Octstr *ostr);
+
#endif
--- ../cvs/gateway/gwlib/octstr.c Wed Sep 25 16:05:51 2002
+++ ../gateway-click/gwlib/octstr.c Sat Mar 1 00:07:59 2003
@@ -2181,3 +2181,81 @@
return resultcode;
}
+
+void octstr_strip_char(Octstr *text, char ch)
+{
+ int start = 0, end, len = 0;
+
+ seems_valid(text);
+ gw_assert(!text->immutable);
+
+ /* Remove char from the beginning of the text */
+ while ((ch == octstr_get_char(text, start)) &&
+ start <= octstr_len(text))
+ start ++;
+
+ if (start > 0)
+ octstr_delete(text, 0, start);
+
+
+ seems_valid(text);
+}
+
+int octstr_isnum(Octstr *ostr1)
+{
+
+
+ int start = 0;
+ char c;
+
+ seems_valid(ostr1);
+ while (start < octstr_len(ostr1))
+ {
+ c = octstr_get_char(ostr1, start);
+ if (!isdigit(c) && (c!='+'))
+ return 0;
+ start++;
+ }
+ return 1;
+
+}
+
+
+void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
+{
+ int p=-1;
+ long len;
+
+ len = octstr_len(needle);
+
+ while ((p = octstr_search(haystack,needle,p+1)) != -1)
+ {
+ octstr_delete(haystack, p, len);
+ octstr_insert(haystack, repl, p);
+ }
+}
+
+// symbolise string, return 0 if empty, -1 on error or 1 on success
+int octstr_symbolize(Octstr *ostr)
+{
+ long len, i;
+
+ seems_valid(ostr);
+ gw_assert(!ostr->immutable);
+
+ if (ostr->len == 0)
+ return 0;
+
+ /* Check if it's in the right format */
+ if (!octstr_check_range(ostr, 0, ostr->len, gw_isxdigit))
+ return -1;
+
+ len = ostr->len + (ostr->len/2);
+ octstr_grow(ostr, ostr->len * 2);
+
+ for (i=0;i<len;i+=3)
+ octstr_insert_data(ostr, i, "%", 1);
+
+ return (1);
+}
+