Octstr support for converting to and from HTML 4 entities.
"Oded Arbel" <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi list. We needed the ability to encode data to XML/HTML with HTML 4 entities as defined by W3C, so I thought that adding it to the Octstr infrastructure would be nice - so that other people will be able to use it. So here it is for your review - don't spare the ammo. Both conversions (to HTML entities and from) are using a .def file that is included into the code. the 'to' coversion is very straight forward and should be very easy. I'm afraid that the 'from' conversion is rather processor intensive - I'm yet unsure as to how to make it more efficient and still keep it as readable. -- Oded Arbel m-Wise mobile solutions [email protected] +972-9-9581711 (116) +972-67-340014 ::.. Finagle's laws for existentialists - 8.Everything takes longer than you think
octstr.patch
(application/octet-stream, 2 KB)
--- gwlib/octstr.h 2002-09-04 20:42:57.000000000 +0300
+++ gwlib/octstr.h 2002-09-22 20:44:43.000000000 +0300
@@ -582,4 +582,17 @@
*/
int octstr_recode (Octstr *tocode, Octstr *fromcode, Octstr *orig);
+/*
+ * make data HTML safe by converting appropriate characters to HTML entities.
+ * conversion is done in place
+ */
+void octstr_convert_to_html_entities(Octstr* input);
+
+/*
+ * convert HTML safe data back to binary data by replacing HTML entities with their
+ * respective character values.
+ * conversion is done in place
+ */
+void octstr_convert_from_html_entities(Octstr* input);
+
#endif
--- gwlib/octstr.c 2002-09-04 20:42:57.000000000 +0300
+++ gwlib/octstr.c 2002-09-22 21:03:47.000000000 +0300
@@ -2182,3 +2182,48 @@
return resultcode;
}
+
+/*
+ * function octstr_convert_to_html_entities()
+ * make data HTML safe by converting appropriate characters to HTML entities
+ * Input: data to be inserted in HTML
+ **/
+void octstr_convert_to_html_entities(Octstr* input)
+{
+ int i;
+
+ for (i = 0; i < octstr_len(input); ++i) {
+ switch (octstr_get_char(input, i)) {
+#define O_REPLACE(a,b,c) { \
+ octstr_delete(a, b, 1); \
+ octstr_insert(a, octstr_imm("&" c ";"), b); \
+ b += sizeof(c); }
+#define ENTITY(a,b) \
+ case a: O_REPLACE(input, i, b); break;
+#include "gwlib/html-entities.def"
+#undef O_REPLACE
+#undef ENTITY
+ }
+ }
+}
+
+/*
+ * function octstr_convert_from_html_entities()
+ * convert HTML safe data back to binary data by replacing HTML entities with their
+ * respective character values
+ * Input: data to be inserted in HTML
+ **/
+void octstr_convert_from_html_entities(Octstr* input)
+{
+ int pos = 0;
+
+ while ((pos = octstr_search_char(input, '&', pos)) != -1) {
+#define ENTITY(a,b) \
+ if (octstr_search(input, octstr_imm("&" b ";"), pos) == pos) { \
+ octstr_delete(input, pos, sizeof("&" b)); \
+ octstr_insert_char(input, pos, a); \
+ }
+#include "gwlib/html-entities.def"
+#undef ENTITY
+ }
+}
html-entities.def
(application/octet-stream, 1.9 KB) - not displayed