Google
Web alhem.net

Header.cpp

Go to the documentation of this file.
00001 // Header.cpp
00002 /*
00003 Copyright (C) 2003  Anders Hedstrom
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 */
00019 
00020 #ifdef _WIN32
00021 #pragma warning(disable:4786)
00022 #define strcasecmp stricmp
00023 #endif
00024 #include <string.h>
00025 #include <ctype.h>
00026 
00027 #include "Header.h"
00028 #include "Parse.h"
00029 //#include "list.h"
00030 
00031 
00032 namespace Cgi {
00033 
00034 Header::Header()
00035 {
00036 //      headerbase = NULL;
00037 //      current = NULL;
00038         m_current = m_headers.end();
00039         dataindex = 0;
00040 }
00041 
00042 Header::Header(char *s,int l)
00043 {
00044         HEADER *c = NULL; //,*tmp;
00045         Cgi::Parse *pa = new Cgi::Parse(s,";");
00046         Cgi::Parse *pa2;
00047         char *slask = new char[l + 1];
00048         char *name = new char[l + 1];
00049         char *value = new char[l + 1];
00050         int i,ok = 1;
00051 
00052 //      headerbase = NULL;
00053 //      current = NULL;
00054         m_current = m_headers.end();
00055         dataindex = 0;
00056 
00057         pa -> getline(slask);
00058         while (*slask)
00059         {
00060                 for (i = 0; i < (int)strlen(slask); i++)
00061                         if (!isprint(slask[i]) && slask[i] != 13 && slask[i] != 10 && slask[i] != 9)
00062                         {
00063 //printf("Don't like char %d\n",slask[i]);
00064                                 ok = 0;
00065                         }
00066 
00067                 pa2 = new Cgi::Parse(slask);
00068                 pa2 -> getword(name);
00069                 pa2 -> getrest(value);
00070                 delete pa2;
00071                 c = new HEADER;
00072                 c -> name = new char[strlen(name) + 1];
00073                 strcpy(c -> name,name);
00074                 c -> value = new char[strlen(value) + 1];
00075                 strcpy(c -> value,value);
00076 //              addlistl(&headerbase,c);
00077                 m_headers.push_back(c);
00078                 //
00079                 pa -> getline(slask);
00080         }
00081         dataindex = pa -> getptr(); // 1st char after last empty header line
00082         delete pa;
00083         delete[] slask;
00084         delete[] name;
00085         delete[] value;
00086 
00087         if (!ok)
00088         {
00089 //              for (c = headerbase; c; c = tmp)
00090                 for (header_v::iterator it = m_headers.begin(); it != m_headers.end(); it++)
00091                 {
00092                         c = *it;
00093                         delete[] c -> name;
00094                         delete[] c -> value;
00095 //                      tmp = c -> next;
00096                         delete c;
00097                 }
00098                 while (m_headers.size())
00099                         m_headers.erase(m_headers.begin());
00100 //              headerbase = NULL;
00101         }
00102 }
00103 
00104 Header::~Header()
00105 {
00106         HEADER *c = NULL; //,*tmp;
00107 
00108 //      for (c = headerbase; c; c = tmp)
00109         for (header_v::iterator it = m_headers.begin(); it != m_headers.end(); it++)
00110         {
00111                 c = *it;
00112                 delete[] c -> name;
00113                 delete[] c -> value;
00114 //              tmp = c -> next;
00115                 delete c;
00116         }
00117 }
00118 
00119 int Header::getvalue(char *name,char *buffer,short length)
00120 {
00121         HEADER *c = NULL;
00122 
00123 //      for (c = headerbase; c; c = c -> next)
00124         for (header_v::iterator it = m_headers.begin(); it != m_headers.end(); it++)
00125         {
00126                 c = *it;
00127                 if (!strcasecmp(c -> name,name))
00128                         break;
00129         }
00130         if (c)
00131         {
00132                 if ((short)strlen(c -> value) >= length)
00133                 {
00134                         strncpy(buffer,c -> value,length - 1);
00135                         buffer[length - 1] = 0;
00136                 } else
00137                         strcpy(buffer,c -> value);
00138                 return 1;
00139         } else
00140         {
00141                 *buffer = 0;
00142                 return 0;
00143         }
00144 }
00145 
00146 void Header::replacevalue(char *name,char *value)
00147 {
00148         HEADER *c = NULL;
00149         
00150 //      for (c = headerbase; c; c = c -> next)
00151         for (header_v::iterator it = m_headers.begin(); it != m_headers.end(); it++)
00152         {
00153                 c = *it;
00154                 if (!strcasecmp(c -> name,name))
00155                         break;
00156         }
00157 
00158         if (c)
00159         {
00160                 if (strlen(value) > strlen(c -> value))
00161                 {
00162                         delete[] c -> value;
00163                         c -> value = new char[strlen(value) + 1];
00164                 }
00165                 strcpy(c -> value,value);
00166         } else
00167         {
00168                 c = new HEADER;
00169                 c -> name = new char[strlen(name) + 1];
00170                 strcpy(c -> name,name);
00171                 c -> value = new char[strlen(value) + 1];
00172                 strcpy(c -> value,value);
00173 //              addlistl(&headerbase,c);
00174                 m_headers.push_back(c);
00175         }
00176 }
00177 
00178 int Header::getlength(char *name)
00179 {
00180         HEADER *c = NULL;
00181 
00182 //      for (c = headerbase; c; c = c -> next)
00183         for (header_v::iterator it = m_headers.begin(); it != m_headers.end(); it++)
00184         {
00185                 c = *it;
00186                 if (!strcasecmp(c -> name,name))
00187                         break;
00188         }
00189         return c ? strlen(c -> value) : 0;
00190 }
00191 
00192 /* get names */
00193 
00194 void Header::getfirst(char *n,short len)
00195 {
00196         m_current = m_headers.begin(); //current = headerbase;
00197         getnext(n,len);
00198 }
00199 
00200 void Header::getnext(char *n,short len)
00201 {
00202         if (m_current != m_headers.end())
00203         {
00204                 HEADER *current = *m_current;
00205                 strncpy(n,current -> name,len - 1);
00206                 n[len - 1] = 0;
00207                 m_current++; //current = current -> next;
00208         } else
00209                 *n = 0;
00210 }
00211 
00212 Header::Header(const Header& ) 
00213 {
00214 }
00215 
00216 Header& Header::operator=(const Header& ) 
00217 { 
00218   return *this; 
00219 }
00220 
00221 } // namespace

Generated for cgi++ by doxygen 1.3.7

Page, code, and content Copyright (C) 2004 by Anders Hedström