]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/applet/prepinclude.cpp
fix for stupid segfault
[wxWidgets.git] / contrib / src / applet / prepinclude.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 <!--#include
27 *
28 ****************************************************************************/
29
30 // For compilers that support precompilation
31 #include "wx/wxprec.h"
32 //#include "wx/file.h"
33 #include "wx/filesys.h"
34 // Include private headers
35 #include "wx/applet/prepinclude.h"
36
37 #define RECURSE_LIMIT 50
38 /*---------------------------- Global variables ---------------------------*/
39
40
41 /*----------------------------- Implementation ----------------------------*/
42
43 /****************************************************************************
44 PARAMETERS:
45 text - HTML to process for include directives
46
47 RETURNS:
48 The string containing the processed HTML
49
50 REMARKS:
51 This is the only implemented method of the Preprocessor class. It is a constant
52 function that parses a string. Basically we load the string search for include
53 statements then replace them with the correct file. Wash rinse and repeat until
54 all recursive cases are handled.
55 ****************************************************************************/
56 wxString wxIncludePrep::Process(
57 const wxString& text) const
58 {
59 int i;
60 char ft[] = "<!--#include virtual=";
61
62
63 int openedcount = 0;
64
65 // make a copy so we can replace text as we go without affecting the original
66 wxString output = text;
67 while ((i = (output.Lower()).Find(ft)) != -1) {
68 // This loop makes more recursion unnecessary since each iteration adds
69 // the new include files to output.
70 int n, c;
71 wxString fname;
72
73 n = (output.Mid(i+21)).Find("-->");
74
75 if (n == -1) {
76 #ifdef CHECKED
77 wxMessageBox("wxHTML #include error: Could not read filename. Premature end of file.","Error",wxICON_ERROR);
78 #endif
79 break;
80 }
81
82 fname = output.Mid(i+21, n);
83
84 // Clip off any quotation marks if they exist. (don't die if there arn't any)
85 c = fname.Find("\"");
86 if (c != -1) fname = fname.Mid(c+1);
87 c = fname.Find("\"");
88 if (c != -1) fname = fname.Mid(0, c);
89
90 // remove the #include tag
91 output.Remove(i, n+21+3);
92
93 wxFSFile * file;
94 file = m_FS->OpenFile(fname);
95
96 if (!file) {
97 #ifdef CHECKED
98 wxMessageBox(wxString("wxHTML #include error: File not Found ") + fname + wxString("."),"Error",wxICON_ERROR);
99 #endif
100 delete file;
101 continue;
102 }
103
104 wxString tmp;
105
106 do {
107 char tmp2[257];
108 (file->GetStream())->Read(tmp2, 256);
109 c = (file->GetStream())->LastRead();
110 tmp2[c] = 0;
111 tmp += wxString(tmp2);
112 } while (c == 256);
113
114 output = (output.Mid(0,i) + tmp + output.Mid(i));
115
116 #ifdef CHECKED
117 if (openedcount > RECURSE_LIMIT) {
118 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);
119 break;
120 }
121 #endif
122
123 openedcount++;
124 delete file;
125 }
126
127 return output;
128 }
129
130 /****************************************************************************
131 PARAMETERS:
132 dir - Default directory to get included HTML files from
133
134 REMARKS:
135 This function sets the directory to get included HTML files from. The default
136 value is the current directory. Directorys may be given as a relative path.
137 ****************************************************************************/
138 void wxIncludePrep::ChangeDirectory(
139 wxFileSystem *fs)
140 {
141 m_FS = fs;
142 }
143