Add to git.
[monolith.git] / src / text.c
1 /* Monolith text processing functions.
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: text.c,v 1.1 2002/12/08 17:29:17 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #include <pool.h>
31 #include <pthr_iolib.h>
32
33 #include "monolith.h"
34 #include "ml_smarttext.h"
35
36 void
37 ml_plaintext_print (io_handle io, const char *text)
38 {
39   while (*text)
40     {
41       switch (*text)
42         {
43         default: io_fputc (*text, io); break;
44         case '<': io_fputs ("&lt;", io); break;
45         case '>': io_fputs ("&gt;", io); break;
46         case '\n': io_fputs ("<br>", io); break;
47         case '&': io_fputs ("&amp;", io); break;
48         case '"': io_fputs ("&quot;", io); break;
49         }
50       text++;
51     }
52 }
53
54 const char *
55 ml_plaintext_to_html (pool pool, const char *text)
56 {
57   char *new_text;
58   int len = strlen (text);
59   int new_len = 0;
60   int i, j;
61
62   /* Work out how long the escaped string will be. Escaped strings get
63    * bigger.
64    */
65   for (i = 0; i < len; ++i)
66     if (text[i] == '<' || text[i] == '>' || text[i] == '\n')
67       new_len += 4;
68     else if (text[i] == '&')
69       new_len += 5;
70     else if (text[i] == '"')
71       new_len += 6;
72     else
73       new_len++;
74
75   new_text = pmalloc (pool, sizeof (char) * (new_len+1));
76
77   /* Now copy and escape the string. */
78   for (i = j = 0; i < len; ++i)
79     switch (text[i])
80       {
81       default:
82         new_text[j++] = text[i];
83         break;
84       case '<':
85         new_text[j++] = '&';
86         new_text[j++] = 'l';
87         new_text[j++] = 't';
88         new_text[j++] = ';';
89         break;
90       case '>':
91         new_text[j++] = '&';
92         new_text[j++] = 'g';
93         new_text[j++] = 't';
94         new_text[j++] = ';';
95         break;
96       case '\n':
97         new_text[j++] = '<';
98         new_text[j++] = 'b';
99         new_text[j++] = 'r';
100         new_text[j++] = '>';
101         break;
102       case '&':
103         new_text[j++] = '&';
104         new_text[j++] = 'a';
105         new_text[j++] = 'm';
106         new_text[j++] = 'p';
107         new_text[j++] = ';';
108         break;
109       case '"':
110         new_text[j++] = '&';
111         new_text[j++] = 'q';
112         new_text[j++] = 'u';
113         new_text[j++] = 'o';
114         new_text[j++] = 't';
115         new_text[j++] = ';';
116         break;
117       }
118
119   new_text[j++] = '\0';
120
121   return new_text;
122 }
123
124 void
125 ml_smarttext_print (io_handle io, const char *text)
126 {
127   pool tmp = new_subpool (global_pool);
128   const char *s;
129
130   s = ml_smarttext_to_html (tmp, text);
131   io_fputs (s, io);
132   delete_pool (tmp);
133 }
134
135 void
136 ml_filterhtml_print (io_handle io, const char *text)
137 {
138   /* XXX NOT IMPLEMENTED XXX */
139   io_fputs (text, io);
140 }
141
142 const char *
143 ml_filterhtml_to_html (pool pool, const char *text)
144 {
145   /* XXX NOT IMPLEMENTED XXX */
146   return text;
147 }
148
149 void
150 ml_anytext_print (io_handle io, const char *text, char type)
151 {
152   switch (type)
153     {
154     case 'p':
155       ml_plaintext_print (io, text);
156       break;
157     case 's':
158       ml_smarttext_print (io, text);
159       break;
160     case 'h':
161       ml_filterhtml_print (io, text);
162       break;
163     }
164 }
165
166 const char *
167 ml_anytext_to_html (pool pool, const char *text, char type)
168 {
169   switch (type)
170     {
171     case 'p':
172       text = ml_plaintext_to_html (pool, text);
173       break;
174     case 's':
175       text = ml_smarttext_to_html (pool, text);
176       break;
177     case 'h':
178       text = ml_filterhtml_to_html (pool, text);
179       break;
180     }
181
182   return text;
183 }