]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/applet/prepifelse.cpp
1 /****************************************************************************
3 * wxWindows HTML Applet Package
5 * Copyright (C) 1991-2001 SciTech Software, Inc.
8 * ========================================================================
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
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.
20 * ========================================================================
25 * Description: This file is the implementation of the Preprocessor object
26 * for parsing the <!--#if directive
28 ****************************************************************************/
30 // For compilers that support precompilation
31 #include "wx/wxprec.h"
32 #include "wx/html/forcelnk.h"
34 // Include private headers
35 #include "wx/applet/prepifelse.h"
36 #include "wx/applet/ifelsevar.h"
38 /*---------------------------- Global variables ---------------------------*/
41 /*----------------------------- Implementation ----------------------------*/
44 /****************************************************************************
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 ****************************************************************************/
54 wxASSERT( str
.GetStringData()->IsValid() );
56 // TODO could be made much quicker than that
57 int p
= tstr
.Len()-str
.Len()-1;
59 if ( wxStrncmp(tstr
.c_str() + p
, str
.c_str(), str
.Len()) == 0 )
67 /****************************************************************************
69 text - HTML to process for if/else blocks
72 The string containing the processed HTML
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.
83 ****************************************************************************/
84 wxString
wxIfElsePrep::Process(
85 const wxString
& text
) const
88 char ft
[] = "<!--#if ";
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-->
97 wxString usecode
, code
;
104 // grab the tag and get the name of the variable
105 end
= (output
.Mid(b
)).Find("-->");
108 wxMessageBox("wxHTML #if error: Premature end of file while parsing #if.","Error",wxICON_ERROR
);
114 tag
= output
.Mid(b
, end
);
115 output
.Remove(b
, end
);
120 // find the classname
121 c
= (tag
.Mid(8, n
-8)).Find(" ");
124 cname
= tag
.Mid(8, n
);
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
);
132 // Grab the value from the variable class identified by cname
133 value
= wxIfElseVariable::FindValue(cname
);
135 // Find the end of the tag (<!--#endif-->) and copy it all into the variable code
136 end
= ((output
.Mid(b
)).Lower()).Find("<!--#endif-->");
139 wxMessageBox("wxHTML #if error: Premature end of file while searching for matching #endif.","Error",wxICON_ERROR
);
144 code
= output
.Mid(b
, end
);
145 output
.Remove(b
, end
+13); // remove the entire #if block from original document
147 // Find out if there is an else statement
148 end
= (code
.Lower()).Find("<!--#else-->");
151 // Use the else statement
152 usecode
= code
.Mid(end
+12);
155 // Use statement before #else
156 usecode
= code
.Mid(0, end
);
160 // There is no #else statement
164 if (usecode
!= wxString(""))
165 output
= (output
.Mid(0,b
) + usecode
+ output
.Mid(b
));
171 FORCE_LINK(ifelsevar
)