]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/applet/prepinclude.cpp
Patch 559673 and 561053
[wxWidgets.git] / contrib / src / applet / prepinclude.cpp
CommitLineData
716cd410
KB
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 <!--#include
27*
28****************************************************************************/
29
716cd410
KB
30// Include private headers
31#include "wx/applet/prepinclude.h"
574c939e
KB
32#include "wx/applet/echovar.h"
33
34// wxWindows
35#include "wx/filesys.h"
36#include "wx/msgdlg.h"
37
38/*----------------------------- Implementation ----------------------------*/
716cd410
KB
39
40#define RECURSE_LIMIT 50
716cd410 41
574c939e
KB
42/****************************************************************************
43PARAMETERS:
44text - text to process for echo variables
716cd410 45
574c939e
KB
46RETURNS:
47The string containing the processed filename
48
49REMARKS:
50This routine searches through the text of the filename for variables contained
51in % percent signs
52****************************************************************************/
53wxString ParseFilename(
54 wxString &text)
55{
56 int f = 0;
57 int e;
58 while ((f = text.find('%', f)) != wxString::npos) {
59 f++;
60 e = text.find('%', f);
61#ifdef CHECKED
62 if (e == wxString::npos) {
63 wxMessageBox(wxString("wxHTML #include error: % signs should bracket variable names in file attribute. To use a percent sign in a filename write double percents (%%)."), "Error" ,wxICON_ERROR);
64 return text;
65 }
66#endif
67 if (e == f)
68 text.replace(f-1, 2, "%");
69 else {
70 wxString varname = text.Mid(f, (e-f));
71 text.replace(f-1, (e-f)+2, wxEchoVariable::GetValue(varname));
72 }
73 }
74 return text;
75}
716cd410
KB
76
77/****************************************************************************
78PARAMETERS:
79text - HTML to process for include directives
80
81RETURNS:
82The string containing the processed HTML
83
84REMARKS:
85This is the only implemented method of the Preprocessor class. It is a constant
86function that parses a string. Basically we load the string search for include
87statements then replace them with the correct file. Wash rinse and repeat until
88all recursive cases are handled.
89****************************************************************************/
90wxString wxIncludePrep::Process(
91 const wxString& text) const
92{
93 int i;
94 char ft[] = "<!--#include virtual=";
716cd410
KB
95 int openedcount = 0;
96
97 // make a copy so we can replace text as we go without affecting the original
98 wxString output = text;
99 while ((i = (output.Lower()).Find(ft)) != -1) {
100 // This loop makes more recursion unnecessary since each iteration adds
101 // the new include files to output.
102 int n, c;
103 wxString fname;
104
105 n = (output.Mid(i+21)).Find("-->");
106
107 if (n == -1) {
108#ifdef CHECKED
109 wxMessageBox("wxHTML #include error: Could not read filename. Premature end of file.","Error",wxICON_ERROR);
110#endif
111 break;
112 }
113
114 fname = output.Mid(i+21, n);
115
116 // Clip off any quotation marks if they exist. (don't die if there arn't any)
117 c = fname.Find("\"");
118 if (c != -1) fname = fname.Mid(c+1);
119 c = fname.Find("\"");
120 if (c != -1) fname = fname.Mid(0, c);
121
122 // remove the #include tag
123 output.Remove(i, n+21+3);
124
19193a2c 125 wxFSFile * file;
574c939e 126 file = m_FS->OpenFile(ParseFilename(fname));
19193a2c 127
716cd410
KB
128 if (!file) {
129#ifdef CHECKED
19193a2c 130 wxMessageBox(wxString("wxHTML #include error: File not Found ") + fname + wxString("."),"Error",wxICON_ERROR);
716cd410 131#endif
d699f48b 132 delete file;
716cd410
KB
133 continue;
134 }
135
136 wxString tmp;
716cd410
KB
137
138 do {
139 char tmp2[257];
140 (file->GetStream())->Read(tmp2, 256);
141 c = (file->GetStream())->LastRead();
142 tmp2[c] = 0;
143 tmp += wxString(tmp2);
144 } while (c == 256);
145
146 output = (output.Mid(0,i) + tmp + output.Mid(i));
574c939e 147#ifdef CHECKED
716cd410
KB
148 if (openedcount > RECURSE_LIMIT) {
149 wxMessageBox(wxString("wxHTML #include error: More than RECURSE_LIMIT files have been #included you may have a file that is directly or indirectly including itself, causing an endless loop"), "Error" ,wxICON_ERROR);
150 break;
151 }
574c939e 152#endif
716cd410
KB
153 openedcount++;
154 delete file;
155 }
156
716cd410
KB
157 return output;
158}
159
160/****************************************************************************
161PARAMETERS:
162dir - Default directory to get included HTML files from
163
164REMARKS:
165This function sets the directory to get included HTML files from. The default
166value is the current directory. Directorys may be given as a relative path.
167****************************************************************************/
168void wxIncludePrep::ChangeDirectory(
19193a2c 169 wxFileSystem *fs)
716cd410 170{
19193a2c 171 m_FS = fs;
716cd410
KB
172}
173