00001 // 00002 // ParsedString.cc 00003 // 00004 // ParsedString: Contains a string. The string my contain $var, ${var}, $(var) 00005 // `filename`. The get method will expand those using the 00006 // dictionary given in argument. 00007 // 00008 // Part of the ht://Dig package <http://www.htdig.org/> 00009 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group 00010 // For copyright details, see the file COPYING in your distribution 00011 // or the GNU General Public License version 2 or later 00012 // <http://www.gnu.org/copyleft/gpl.html> 00013 // 00014 // $Id: ParsedString_8cc-source.html,v 1.1 2008年06月08日 10:12:55 sebdiaz Exp $ 00015 // 00016 00017 #ifdef HAVE_CONFIG_H 00018 #include "config.h" 00019 #endif /* HAVE_CONFIG_H */ 00020 00021 #include "ParsedString.h" 00022 00023 #include <ctype.h> 00024 #include <stdio.h> 00025 00026 00027 //***************************************************************************** 00028 // ParsedString::ParsedString() 00029 // 00030 ParsedString::ParsedString() 00031 { 00032 } 00033 00034 00035 //***************************************************************************** 00036 // 00037 ParsedString::ParsedString(const String& s) 00038 { 00039 value = s; 00040 } 00041 00042 00043 //***************************************************************************** 00044 // ParsedString::~ParsedString() 00045 // 00046 ParsedString::~ParsedString() 00047 { 00048 } 00049 00050 00051 //***************************************************************************** 00052 // 00053 void 00054 ParsedString::set(const String& str) 00055 { 00056 value = str; 00057 } 00058 00059 00060 //***************************************************************************** 00061 // Return a fully parsed string. 00062 // 00063 // Allowed syntax: 00064 // $var 00065 // ${var} 00066 // $(var) 00067 // `filename` 00068 // 00069 // The filename can also contain variables 00070 // 00071 const String 00072 ParsedString::get(const Dictionary &dict) const 00073 { 00074 String variable; 00075 String parsed; 00076 ParsedString *temp; 00077 const char *str = value.get(); 00078 char delim = ' '; 00079 int need_delim = 0; 00080 00081 while (*str) 00082 { 00083 if (*str == '$') 00084 { 00085 // 00086 // A dollar sign starts a variable. 00087 // 00088 str++; 00089 need_delim = 1; 00090 if (*str == '{') 00091 delim = '}'; 00092 else if (*str == '(') 00093 delim = ')'; 00094 else 00095 need_delim = 0; 00096 if (need_delim) 00097 str++; 00098 variable.trunc(); 00099 while (isalpha(*str) || *str == '_' || *str == '-') 00100 { 00101 variable << *str++; 00102 } 00103 if (*str) 00104 { 00105 if (need_delim && *str == delim) 00106 { 00107 // 00108 // Found end of variable 00109 // 00110 temp = (ParsedString *) dict[variable]; 00111 if (temp) 00112 parsed << temp->get(dict); 00113 str++; 00114 } 00115 else if (need_delim) 00116 { 00117 // 00118 // Error. Probably an illegal value in the name We'll 00119 // assume the variable ended here. 00120 // 00121 temp = (ParsedString *) dict[variable]; 00122 if (temp) 00123 parsed << temp->get(dict); 00124 } 00125 else 00126 { 00127 // 00128 // This variable didn't have a delimiter. 00129 // 00130 temp = (ParsedString *) dict[variable]; 00131 if (temp) 00132 parsed << temp->get(dict); 00133 } 00134 } 00135 else 00136 { 00137 // 00138 // End of string reached. We'll assume that this is also 00139 // the end of the variable 00140 // 00141 temp = (ParsedString *) dict[variable]; 00142 if (temp) 00143 parsed << temp->get(dict); 00144 } 00145 } 00146 else if (*str == '`') 00147 { 00148 // 00149 // Back-quote delimits a filename which we need to insert 00150 // 00151 str++; 00152 variable.trunc(); 00153 while (*str && *str != '`') 00154 { 00155 variable << *str++; 00156 } 00157 if (*str == '`') 00158 str++; 00159 ParsedString filename(variable); 00160 variable.trunc(); 00161 getFileContents(variable, filename.get(dict)); 00162 parsed << variable; 00163 } 00164 else if (*str == '\\') 00165 { 00166 // 00167 // Backslash escapes the next character 00168 // 00169 str++; 00170 if (*str) 00171 parsed << *str++; 00172 } 00173 else 00174 { 00175 // 00176 // Normal character 00177 // 00178 parsed << *str++; 00179 } 00180 } 00181 return parsed; 00182 } 00183 00184 00185 void 00186 ParsedString::getFileContents(String &str, const String& filename) const 00187 { 00188 FILE *fl = fopen(filename, "r"); 00189 char buffer[1000]; 00190 00191 if (!fl) 00192 return; 00193 while (fgets(buffer, sizeof(buffer), fl)) 00194 { 00195 String s(buffer); 00196 s.chop("\r\n\t "); 00197 str << s << ' '; 00198 } 00199 str.chop(1); 00200 fclose(fl); 00201 } 00202