source: trunk/src/core/RpEntityRef.cc @ 1427

Last change on this file since 1427 was 1366, checked in by gah, 16 years ago

Begin reorganizing Rappture C++ API, starting with the use of
Rappture::Outcome.

Instead of returning an Outcome and expecting every routine to
save it, pass it as a reference. Let's strip it down to what's
needed right now and add back functionality as required.

Right now too many return values are ignored in the rappture library.
We need to check all results and return the error messages via
the Outcome. At the topmost level where is touches the developer
API we can drop the error checking or turn it on via an environment
variable.

Example. Not enough checks are made for memory allocation failure.
If the application is running on an over-committed server memory
allocation errors will be silently passed on. It's okay to be
fault tolerant where possible, but if we fail to check in the internal
library functions, it's too late.

--gah

File size: 4.1 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture Library Entity Reference Translation Header
4 *
5 *  Begin Character Entity Translator
6 *
7 *  The next section of code implements routines used to translate
8 *  character entity references into their corresponding strings.
9 *
10 *  Examples:
11 *
12 *        &          "&"
13 *        &lt;           "<"
14 *        &gt;           ">"
15 *        &nbsp;         " "
16 *
17 *
18 * ======================================================================
19 *  AUTHOR:  Derrick Kearney, Purdue University
20 *  Copyright (c) 2004-2006  Purdue Research Foundation
21 *
22 *  See the file "license.terms" for information on usage and
23 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
24 *
25 *  Also see below text for additional information on usage and redistribution
26 *
27 * ======================================================================
28 */
29
30#include "RpEntityRef.h"
31#include <cctype>
32#include <cstring>
33
34#ifdef __cplusplus
35    extern "C" {
36#endif // ifdef __cplusplus
37
38using namespace Rappture;
39
40const char*
41EntityRef::decode (const char* value, unsigned int len)
42{
43    unsigned int pos = 0;
44
45    if (value == NULL) {
46        // empty string, noting to do
47        return NULL;
48    }
49
50    _bout.clear();
51    if (len == 0) {
52        len = strlen(value);
53    }
54
55    while (pos < len) {
56        if (value[pos] == '&') {
57            pos++;
58            if ((pos < len)) {
59                if (value[pos] && isalpha(value[pos])) {
60                    if (        (value[pos] == 'q')
61                            &&  (strncmp("quot;",value+pos,5) == 0) ) {
62                        _bout.append("\"");
63                        pos += 5;
64                    }
65                    else if (   (value[pos] == 'a')
66                            &&  (strncmp("amp;",value+pos,4) == 0) ) {
67                        _bout.append("&");
68                        pos += 4;
69                    }
70                    else if (   (value[pos] == 'l')
71                            &&  (strncmp("lt;",value+pos,3) == 0) ) {
72                        _bout.append("<");
73                        pos += 3;
74                    }
75                    else if (   (value[pos] == 'g')
76                            &&  (strncmp("gt;",value+pos,3) == 0) ) {
77                        _bout.append(">");
78                        pos += 3;
79                    }
80                    else {
81                        // unrecognized
82                        _bout.append(value+pos,1);
83                        pos++;
84                    }
85                }
86                else {
87                    _bout.append(value+pos,1);
88                    pos++;
89                }
90            }
91            else {
92                // last character was really an ampersand
93                // add it to the buffer
94                pos--;
95                _bout.append(value+pos,1);
96                pos++;
97            }
98        }
99        else
100        {
101            // non entity ref character
102            _bout.append(value+pos,1);
103            pos++;
104        }
105    }
106
107    _bout.append("\0",1);
108    return _bout.bytes();
109}
110
111const char*
112EntityRef::encode (
113    const char* value,
114    unsigned int len
115)
116{
117    unsigned int pos = 0;
118
119
120    if (value == NULL) {
121        // empty string, noting to do
122        return NULL;
123    }
124
125    _bout.clear();
126    if (len == 0) {
127        len = strlen(value);
128    }
129
130    while (pos < len) {
131        if (*(value+pos) == '"') {
132            _bout.append("&quot;");
133        }
134        else if (*(value+pos) == '&') {
135            _bout.append("&amp;");
136        }
137        else if (*(value+pos) == '<') {
138            _bout.append("&lt;");
139        }
140        else if (*(value+pos) == '>') {
141            _bout.append("&gt;");
142        }
143        /*
144        else if (*(value+pos) == '\n') {
145            _bout.append("&#xA;");
146        }
147        */
148        else
149        {
150            _bout.append(value+pos,1);
151        }
152        pos++;
153    }
154
155    _bout.append("\0",1);
156    return _bout.bytes();
157}
158
159int
160EntityRef::size () {
161    return _bout.size();
162}
163
164
165#ifdef __cplusplus
166    } // extern c
167#endif // ifdef __cplusplus
Note: See TracBrowser for help on using the repository browser.