[asterisk-commits] eliel: branch group/appdocsxml r151899 - /team/group/appdocsxml/main/pbx.c
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sat Oct 25 00:02:26 CDT 2008
Author: eliel
Date: Sat Oct 25 00:02:26 2008
New Revision: 151899
URL: http://svn.digium.com/view/asterisk?view=rev&rev=151899
Log:
Simplify the xmldoc_string_wrap() function.
Modified:
team/group/appdocsxml/main/pbx.c
Modified: team/group/appdocsxml/main/pbx.c
URL: http://svn.digium.com/view/asterisk/team/group/appdocsxml/main/pbx.c?view=diff&rev=151899&r1=151898&r2=151899
==============================================================================
--- team/group/appdocsxml/main/pbx.c (original)
+++ team/group/appdocsxml/main/pbx.c Sat Oct 25 00:02:26 2008
@@ -3215,6 +3215,70 @@
}
/*! \internal
+ * \brief Try to find a space or a break in text starting at currentpost
+ * and moving at most maxdiff positions.
+ * Helper for xmldoc_string_wrap().
+ * \param text Input string where it will search.
+ * \param currentpos Current position within text.
+ * \param maxdiff Not move more than maxdiff inside text.
+ * \retval 1 if a space or break is found inside text while moving.
+ * \retval 0 if no space or break is found.
+ */
+static int xmldoc_wait_nextspace(const char *text, int currentpos, int maxdiff)
+{
+ int i, textlen;
+
+ if (!text) {
+ return 0;
+ }
+ textlen = strlen(text);
+
+ for (i = currentpos; i < textlen; i++) {
+ if (text[i] == ESC) {
+ while (i < textlen && text[i] != 'm') {
+ i++;
+ }
+ } else if (text[i] == ' ' || text[i] == '\n') {
+ return 1;
+ }
+ if (i - currentpos > maxdiff) {
+ return 0;
+ }
+ }
+ return 0;
+}
+
+/*! \internal
+ * \brief Helper function for xmldoc_string_wrap().
+ * Try to found a space or a break inside text moving backward
+ * not more than maxdiff positions.
+ * \param text The input string where to search for a space.
+ * \param currentpos The current cursor position.
+ * \param maxdiff The max number of positions to move within text.
+ * \retval 0 If no space is found (Notice that text[currentpos] is not a space or a break)
+ * \retval > 0 If a space or a break is found, and the result is the position relative to
+ * currentpos.
+ */
+static int xmldoc_foundspace_backward(const char *text, int currentpos, int maxdiff)
+{
+ int i;
+
+ for (i = currentpos; i > 0; i--) {
+ if (text[i] == ' ' || text[i] == '\n') {
+ return (currentpos - i);
+ } else if (text[i] == 'm' && (text[i-1] >= '0' || text[i-1] <= '9')) {
+ /* give up, we found the end of a possible ESC sequence. */
+ return 0;
+ } else if (currentpos - i > maxdiff) {
+ /* give up, we can't move anymore. */
+ return 0;
+ }
+ }
+ /* we found the beginning of the text */
+ return 0;
+}
+
+/*! \internal
* \brief Justify a text to a number of columns.
* \param text Input text to be justified.
* \param columns Number of columns to preserve in the text.
@@ -3222,13 +3286,11 @@
* \retval NULL on error.
* \retval The wrapped text.
*/
-static char *xmldoc_string_wrap(char *text, int columns, int maxdiff)
-{
- char *tmp, *ret, *t;
- char *in = (char *) text;
- int count = 1, tmplen = 0, i, postbrreallen = 0, postbrlen;
- char postbr[160];
- int sep, needtobreak = 0, colmax;
+static char *xmldoc_string_wrap(const char *text, int columns, int maxdiff)
+{
+ struct ast_str *tmp;
+ char *ret, postbr[160];
+ int count = 1, i, backspace, needtobreak = 0, colmax, textlen;
/* sanity check */
if (!text || columns <= 0 || maxdiff < 0) {
@@ -3236,8 +3298,7 @@
return NULL;
}
- /* XXX: We could calculate better how much space do we need. */
- tmp = ast_calloc(1, strlen(text) * 3);
+ tmp = ast_str_create(strlen(text) * 3);
if (!tmp) {
return NULL;
@@ -3245,95 +3306,60 @@
/* Check for blanks and tabs and put them in postbr. */
xmldoc_setpostbr(postbr, sizeof(postbr), text);
- postbrreallen = xmldoc_postbrlen(postbr);
- colmax = columns - postbrreallen;
-
- while (*in) {
+ colmax = columns - xmldoc_postbrlen(postbr);
+
+ textlen = strlen(text);
+
+ for (i = 0; i < textlen; i++) {
if (needtobreak || !(count % colmax)) {
- if (*in == ' ') {
- tmp[tmplen++] = '\n';
- postbrlen = strlen(postbr);
- for (i = 0; i < postbrlen; i++) {
- tmp[tmplen++] = postbr[i];
- }
+ if (text[i] == ' ') {
+ ast_str_append(&tmp, 0, "\n%s", postbr);
needtobreak = 0;
count = 1;
- } else if (*in != '\n') {
+ } else if (text[i] != '\n') {
needtobreak = 1;
- t = in;
- sep = maxdiff;
- while (*t && sep > 0) {
- t++;
- /* Wait for the next space? */
- if (*t == ' ' || *t == '\n') {
- break;
- } else if (*t == ESC) {
- /* bypass escape sequences. */
- while (*t && *t != 'm') {
- t++;
- }
- }
- sep--;
+ if (xmldoc_wait_nextspace(text, i, maxdiff)) {
+ /* wait for the next space */
+ ast_str_append(&tmp, 0, "%c", text[i]);
+ continue;
}
- if (!sep) {
- /* Try to look backwards */
- t = in;
- sep = (maxdiff % tmplen);
- while (sep > 0) {
- t--;
- if (*t == ' ' || *t == '\n') {
- break;
- } else if (*t == 'm') { /* XXX: Possible ESC don't continue */
- sep = 0;
- break;
- }
- sep--;
- }
- if (sep) {
- needtobreak = 1;
- tmplen -= (maxdiff % tmplen) - sep;
- in = t;
- continue;
- }
- /* If we add this, we can have an extra - in colored output
- Sometimes that looks really bad. If needed, we can enable it again
- tmp[tmplen++] = '-'; */
- tmp[tmplen++] = '\n';
- postbrlen = strlen(postbr);
- for (i = 0; i < postbrlen; i++) {
- tmp[tmplen++] = postbr[i];
- }
- needtobreak = 0;
- count = 1;
+ /* Try to look backwards */
+ backspace = xmldoc_foundspace_backward(text, i, maxdiff);
+ if (backspace) {
+ needtobreak = 1;
+ tmp->used -= backspace;
+ tmp->str[tmp->used] = '\0';
+ i -= backspace + 1;
+ continue;
}
+ ast_str_append(&tmp, 0, "\n%s", postbr);
+ needtobreak = 0;
+ count = 1;
}
/* skip blanks after a \n */
- while (*in == ' ') {
- in++;
- }
- }
- if (*in == '\n') {
- xmldoc_setpostbr(postbr, sizeof(postbr), in + 1);
- postbrreallen = xmldoc_postbrlen(postbr);
- colmax = columns - postbrreallen;
- needtobreak = 0, count = 1;
- }
- if (*in == ESC) {
+ while (text[i] == ' ') {
+ i++;
+ }
+ }
+ if (text[i] == '\n') {
+ xmldoc_setpostbr(postbr, sizeof(postbr), &text[i] + 1);
+ colmax = columns - xmldoc_postbrlen(postbr);
+ needtobreak = 0;
+ count = 1;
+ }
+ if (text[i] == ESC) {
/* Ignore Escape sequences. */
do {
- tmp[tmplen++] = *in;
- in++;
- } while (*in && *in != 'm');
- tmp[tmplen++] = *in;
- in++;
+ ast_str_append(&tmp, 0, "%c", text[i]);
+ i++;
+ } while (i < textlen && text[i] != 'm');
} else {
- tmp[tmplen++] = *in;
count++;
- in++;
- }
- }
-
- ret = ast_strdup(tmp);
+ }
+ ast_str_append(&tmp, 0, "%c", text[i]);
+ }
+
+ ret = ast_strdup(tmp->str);
ast_free(tmp);
return ret;
More information about the asterisk-commits
mailing list