Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members
CGIParser.cpp
00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: CGIParser.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 /*********************************************************************** 00045 * Video Session Migration System, 2004 * 00046 * Author: Klaus Schoeffmann * 00047 ************************************************************************/ 00048 00049 00050 #include "CGIParser.hpp" 00051 #ifdef ENABLE_QT 00052 #include <qstring.h> 00053 #endif 00054 00055 #ifdef WIN32 00056 #include <algorithm> 00057 typedef unsigned int uint; 00058 #endif 00059 00060 /***********************************************************************/ 00061 CGIParser::CGIParser(string data) 00062 { 00063 parseData(data, '&', '='); 00064 } 00065 00066 /***********************************************************************/ 00067 CGIParser::~CGIParser() 00068 { 00069 formFields.clear(); 00070 } 00071 00072 /***********************************************************************/ 00073 void CGIParser::parseData(string data, const char sep1, const char sep2) 00074 { 00075 list<string> fields = splitData(data, sep1); 00076 list<string>::const_iterator iter = fields.begin(); 00077 for (;iter != fields.end(); iter++) 00078 { 00079 list<string> fieldData = splitData(*iter, sep2); 00080 if (fieldData.size() == 2) 00081 { 00082 //success 00083 formFields[fieldData.front()] = fieldData.back(); 00084 } 00085 else 00086 { 00087 cerr << "\nCGIParser::parseData(): cannot read field: " << *iter << "(size=" << fieldData.size() << ")"; 00088 } 00089 } 00090 } 00091 00092 /***********************************************************************/ 00093 list<string> CGIParser::splitData(string data, const char sep) 00094 { 00095 //list of elements 00096 list<string> elems; 00097 00098 #ifndef WIN32 //back_inserters cause C2039 00099 00100 string::iterator pos1 = data.begin(); 00101 //find returns an iterator to the end, if nothing is found 00102 string::iterator pos2 = find(pos1, data.end(), sep); 00103 while (pos2 != data.end()) 00104 { 00105 string field; 00106 copy(pos1, pos2, back_inserter(field)); 00107 elems.push_back(field); 00108 pos1 = pos2+1; 00109 pos2 = find(pos1, data.end(), sep); 00110 } 00111 00112 string field; 00113 copy(pos1, pos2, back_inserter(field)); 00114 elems.push_back(field); 00115 00116 #endif 00117 00118 return elems; 00119 } 00120 00121 /***********************************************************************/ 00122 int CGIParser::hex2dec(char c) 00123 { 00124 if (c >= 'A' && c <= 'F') 00125 { 00126 c -= 'A'; 00127 c += 10; 00128 } 00129 else if (c >= 'a' && c <= 'f') 00130 { 00131 c -= 'a'; 00132 c += 10; 00133 } 00134 else 00135 { 00136 c -= '0'; 00137 } 00138 00139 return (int)c; 00140 } 00141 00142 #ifndef WIN32 00143 #define STRING_PUSH_BACK(str,ch) str.push_back(ch) 00144 #else 00145 //#define STRING_PUSH_BACK(str,ch) str.append(string(ch)) 00146 #define STRING_PUSH_BACK(str,ch) str+=ch; 00147 #endif 00148 /***********************************************************************/ 00149 string CGIParser::decode(string data) 00150 { 00151 string decoded; 00152 00153 string::iterator iter = data.begin(); 00154 for (; iter != data.end(); iter++) 00155 { 00156 if (*iter == '+') 00157 { //space 00158 //decoded.push_back(' '); 00159 STRING_PUSH_BACK(decoded,' '); 00160 } 00161 else if (*iter == '%') 00162 { //special character starts 00163 uint special = hex2dec(*++iter) * 16 + hex2dec(*++iter); 00164 #ifdef ENABLE_QT 00165 //convert from unicode to latin1 00166 QChar qchar (special); 00167 //decoded.push_back(qchar.latin1()); 00168 STRING_PUSH_BACK(decoded,qchar.latin1()); 00169 #else 00170 //FIXME: this _will_ result in wrong coding!!! 00171 //decoded.push_back(special); 00172 STRING_PUSH_BACK(decoded,special); 00173 #endif 00174 } 00175 else 00176 { 00177 //decoded.push_back(*iter); 00178 STRING_PUSH_BACK(decoded,*iter); 00179 } 00180 } 00181 00182 return decoded; 00183 } 00184 00185 /***********************************************************************/ 00186 string CGIParser::getFieldValue(string name) 00187 { 00188 string decoded = decode(formFields[name]); 00189 return decoded; 00190 } 00191 00192 /***********************************************************************/ 00193 string CGIParser::createRefreshForm(string action, string method) 00194 { 00195 ostringstream str; 00196 00197 str << createOpenForm("refreshForm", action, method, ""); 00198 str << "</form>" << endl; 00199 00200 return str.str(); 00201 } 00202 00203 /***********************************************************************/ 00204 string CGIParser::createOpenForm(string name, string action, string method, string ignoreFields) 00205 { 00206 ostringstream str; 00207 00208 str << "\n<form name=\"" << name 00209 << "\" action=\"" << action 00210 << "\" method=\"" << method << "\">" << endl; 00211 00212 map<string, string>::iterator iter = formFields.begin(); 00213 for (; iter != formFields.end(); iter++) 00214 { 00215 //output every received parameters (but no the submit parameter) 00216 if (iter->first != "submit") 00217 { 00218 00219 //do only output field, if name is not in ignore-string 00220 if (ignoreFields.find(iter->first) == string::npos) 00221 { 00222 str << "<input type=\"hidden\" name=\"" 00223 << iter->first << "\" value=\"" 00224 << iter->second << "\">" << endl; 00225 } 00226 00227 } 00228 } 00229 00230 return str.str(); 00231 } 00232 00233 00234