home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / contrib / src / applet / prepinclude.cpp < prev   
C/C++ Source or Header  |  2002-02-07  |  6KB  |  174 lines

  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. // Include private headers
  31. #include "wx/applet/prepinclude.h"
  32. #include "wx/applet/echovar.h"
  33.  
  34. // wxWindows
  35. #include "wx/filesys.h"
  36. #include "wx/msgdlg.h"
  37.  
  38. /*----------------------------- Implementation ----------------------------*/
  39.  
  40. #define RECURSE_LIMIT 50
  41.  
  42. /****************************************************************************
  43. PARAMETERS:
  44. text        - text to process for echo variables
  45.  
  46. RETURNS:
  47. The string containing the processed filename
  48.  
  49. REMARKS:
  50. This routine searches through the text of the filename for variables contained
  51. in % percent signs
  52. ****************************************************************************/
  53. wxString 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. }
  76.  
  77. /****************************************************************************
  78. PARAMETERS:
  79. text        - HTML to process for include directives
  80.  
  81. RETURNS:
  82. The string containing the processed HTML
  83.  
  84. REMARKS:
  85. This is the only implemented method of the Preprocessor class. It is a constant
  86. function that parses a string. Basically we load the string search for include
  87. statements then replace them with the correct file. Wash rinse and repeat until
  88. all recursive cases are handled.
  89. ****************************************************************************/
  90. wxString wxIncludePrep::Process(
  91.     const wxString& text) const
  92. {
  93.     int i;
  94.     char ft[] = "<!--#include virtual=";
  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.  
  125.         wxFSFile * file;
  126.         file = m_FS->OpenFile(ParseFilename(fname));
  127.     
  128.         if (!file) {
  129. #ifdef CHECKED        
  130.             wxMessageBox(wxString("wxHTML #include error: File not Found ") + fname + wxString("."),"Error",wxICON_ERROR);
  131. #endif
  132.             delete file;
  133.             continue;
  134.             }            
  135.                             
  136.         wxString tmp;
  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));
  147. #ifdef CHECKED    
  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.             }
  152. #endif            
  153.         openedcount++;
  154.         delete file;
  155.         }
  156.     
  157.     return output;
  158. }
  159.  
  160. /****************************************************************************
  161. PARAMETERS:
  162. dir     - Default directory to get included HTML files from
  163.  
  164. REMARKS:
  165. This function sets the directory to get included HTML files from. The default
  166. value is the current directory. Directorys may be given as a relative path.
  167. ****************************************************************************/
  168. void wxIncludePrep::ChangeDirectory(
  169.     wxFileSystem *fs)
  170. {
  171.      m_FS = fs;
  172. }
  173.  
  174.