]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/applet/prepifelse.cpp
Made a couple Number() to GetCount() changes
[wxWidgets.git] / contrib / src / applet / prepifelse.cpp
1 /****************************************************************************
2 *
3 * wxWindows HTML Applet Package
4 *
5 * Copyright (C) 1991-2001 SciTech Software, Inc.
6 * All rights reserved.
7 *
8 * ========================================================================
9 *
10 * The contents of this file are subject to the wxWindows License
11 * Version 3.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.wxwindows.org/licence3.txt
14 *
15 * Software distributed under the License is distributed on an
16 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
19 *
20 * ========================================================================
21 *
22 * Language: ANSI C++
23 * Environment: Any
24 *
25 * Description: This file is the implementation of the Preprocessor object
26 * for parsing the <!--#if directive
27 *
28 ****************************************************************************/
29
30 // For compilers that support precompilation
31 #include "wx/wxprec.h"
32 #include "wx/html/forcelnk.h"
33
34 // Include private headers
35 #include "wx/applet/prepifelse.h"
36 #include "wx/applet/ifelsevar.h"
37
38 /*---------------------------- Global variables ---------------------------*/
39
40
41 /*----------------------------- Implementation ----------------------------*/
42
43 /* {SECRET} */
44 /****************************************************************************
45 REMARKS:
46 None of the Reverse Find functions in wxWindows appear to work in a way that
47 can be used by our code. This includes the libstr rfind implementations which
48 do not correctly pass the given return value.
49 ****************************************************************************/
50 int ReverseFind(
51 const wxString &tstr,
52 const wxString &str)
53 {
54 wxASSERT( str.GetStringData()->IsValid() );
55
56 // TODO could be made much quicker than that
57 int p = tstr.Len()-str.Len()-1;
58 while ( p >= 0 ) {
59 if ( wxStrncmp(tstr.c_str() + p, str.c_str(), str.Len()) == 0 )
60 return p;
61 p--;
62 }
63
64 return -1;
65 }
66
67 /****************************************************************************
68 PARAMETERS:
69 text - HTML to process for if/else blocks
70
71 RETURNS:
72 The string containing the processed HTML
73
74 REMARKS:
75 This function replaces #if, #else, and #endif directives with the text
76 contained within the blocks, dependant on the value of the given boolean
77 variable. The variable is created by making a sub class of wxIfElseVariable.
78 Dynamic class construction is used at run time internally to create an instance
79 of this class and access the value of the variable.
80
81 SEE ALSO:
82 wxIfElseVariable
83 ****************************************************************************/
84 wxString wxIfElsePrep::Process(
85 const wxString& text) const
86 {
87 int b;
88 char ft[] = "<!--#if ";
89
90 // make a copy so we can replace text as we go without affecting the original
91 wxString output = text;
92 while ((b = ReverseFind(output.Lower(), ft)) != -1) {
93 // Loop until every #echo directive is found
94 // We search from the end of the string so that #if statements will properly recurse
95 // and we avoid the hassle of matching statements with the correct <!--#endif-->
96 int end, c, n;
97 wxString usecode, code;
98 wxString cname;
99 wxString tag;
100 bool value;
101
102 code = wxString("");
103
104 // grab the tag and get the name of the variable
105 end = (output.Mid(b)).Find("-->");
106 if (end == -1) {
107 #ifdef CHECKED
108 wxMessageBox("wxHTML #if error: Premature end of file while parsing #if.","Error",wxICON_ERROR);
109 #endif
110 break;
111 }
112
113 end += 3;
114 tag = output.Mid(b, end);
115 output.Remove(b, end);
116
117 c = tag.Find("-->");
118 n = c;
119
120 // find the classname
121 c = (tag.Mid(8, n-8)).Find(" ");
122 if (c == -1) n -= 8;
123 else n = c;
124 cname = tag.Mid(8, n);
125
126 cname.Trim(false);
127 c = cname.Find("\"");
128 if (c != -1) cname = cname.Mid(c+1);
129 c = cname.Find("\"");
130 if (c != -1) cname = cname.Mid(0, c);
131
132 // Grab the value from the variable class identified by cname
133 value = wxIfElseVariable::GetValue(cname);
134
135 // Find the end of the tag (<!--#endif-->) and copy it all into the variable code
136 end = ((output.Mid(b)).Lower()).Find("<!--#endif-->");
137 if (end == -1) {
138 #ifdef CHECKED
139 wxMessageBox("wxHTML #if error: Premature end of file while searching for matching #endif.","Error",wxICON_ERROR);
140 #endif
141 break;
142 }
143
144 code = output.Mid(b, end);
145 output.Remove(b, end+13); // remove the entire #if block from original document
146
147 // Find out if there is an else statement
148 end = (code.Lower()).Find("<!--#else-->");
149 if (end != -1) {
150 if (!value) {
151 // Use the else statement
152 usecode = code.Mid(end+12);
153 }
154 else {
155 // Use statement before #else
156 usecode = code.Mid(0, end);
157 }
158 }
159 else if (value) {
160 // There is no #else statement
161 usecode = code;
162 }
163
164 if (usecode != wxString(""))
165 output = (output.Mid(0,b) + usecode + output.Mid(b));
166 }
167
168 return output;
169 }
170
171 FORCE_LINK(ifelsevar)