]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/applet/prepinclude.cpp
formatting changes
[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 wxFileSystem *fs = new wxFileSystem;
63 fs->ChangePathTo(DOC_ROOT, true);
64
65 int openedcount = 0;
66
67 // make a copy so we can replace text as we go without affecting the original
68 wxString output = text;
69 while ((i = (output.Lower()).Find(ft)) != -1) {
70 // This loop makes more recursion unnecessary since each iteration adds
71 // the new include files to output.
72 int n, c;
73 wxString fname;
74
75 n = (output.Mid(i+21)).Find("-->");
76
77 if (n == -1) {
78 #ifdef CHECKED
79 wxMessageBox("wxHTML #include error: Could not read filename. Premature end of file.","Error",wxICON_ERROR);
80 #endif
81 break;
82 }
83
84 fname = output.Mid(i+21, n);
85
86 // Clip off any quotation marks if they exist. (don't die if there arn't any)
87 c = fname.Find("\"");
88 if (c != -1) fname = fname.Mid(c+1);
89 c = fname.Find("\"");
90 if (c != -1) fname = fname.Mid(0, c);
91
92 // remove the #include tag
93 output.Remove(i, n+21+3);
94
95 wxFSFile * file = fs->OpenFile(DOC_ROOT + fname);
96
97 if (!file) {
98 #ifdef CHECKED
99 wxMessageBox(wxString("wxHTML #include error: File not Found ") + DOC_ROOT + fname + wxString("."),"Error",wxICON_ERROR);
100 #endif
101 delete file;
102 continue;
103 }
104
105 wxString tmp;
106
107 do {
108 char tmp2[257];
109 (file->GetStream())->Read(tmp2, 256);
110 c = (file->GetStream())->LastRead();
111 tmp2[c] = 0;
112 tmp += wxString(tmp2);
113 } while (c == 256);
114
115 output = (output.Mid(0,i) + tmp + output.Mid(i));
116
117 #ifdef CHECKED
118 if (openedcount > RECURSE_LIMIT) {
119 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);
120 break;
121 }
122 #endif
123
124 openedcount++;
125 delete file;
126 }
127
128 delete fs;
129 return output;
130 }
131
132 /****************************************************************************
133 PARAMETERS:
134 dir - Default directory to get included HTML files from
135
136 REMARKS:
137 This function sets the directory to get included HTML files from. The default
138 value is the current directory. Directorys may be given as a relative path.
139 ****************************************************************************/
140 void wxIncludePrep::ChangeDirectory(
141 const wxString &dir)
142 {
143
144 DOC_ROOT = dir;
145 }
146