FormEntry.cpp

00001 /* -*-mode:c++; c-file-style: "gnu";-*- */
00002 /*
00003  *  $Id: FormEntry_8cpp-source.html,v 1.1 2007/07/03 21:34:42 sebdiaz Exp $
00004  *
00005  *  Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
00006  *                       2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
00007  *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License as published by the Free Software Foundation; either
00012  *  version 3 of the License, or (at your option) any later version.
00013  *
00014  *  This library is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *  Lesser General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU Lesser General Public
00020  *  License along with this library; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
00022  */
00023 
00024 #ifdef __GNUG__
00025 #  pragma implementation
00026 #endif
00027 
00028 #include <new>
00029 #include <cstdlib>
00030 
00031 #include "cgicc/FormEntry.h"
00032 
00033 // local macro for integer maximum
00034 #define MAX(a, b) ((a) > (b) ? (a) : (b))
00035 
00036 cgicc::FormEntry& 
00037 cgicc::FormEntry::operator= (const FormEntry& entry)
00038 {
00039   fName  = entry.fName;
00040   fValue = entry.fValue;
00041 
00042   return *this;
00043 }
00044 
00045 long
00046 cgicc::FormEntry::getIntegerValue(long min, 
00047                                   long max)             const
00048 {
00049   long value = std::atol(fValue.c_str());
00050 
00051   if(value > max)
00052     value = max;
00053   else if(value < min)
00054     value = min;
00055   
00056   return value;
00057 }
00058 
00059 long
00060 cgicc::FormEntry::getIntegerValue(long min, 
00061                                   long max,
00062                                   bool& bounded)        const
00063 {
00064   long value = std::atol(fValue.c_str());
00065 
00066   bounded = false;
00067 
00068   if(value > max) {
00069     value = max;
00070     bounded = true;
00071   }
00072   else if(value < min) {
00073     value = min;
00074     bounded = true;
00075   }
00076   
00077   return value;
00078 }
00079 
00080 double
00081 cgicc::FormEntry::getDoubleValue(double min, 
00082                                  double max)            const
00083 {
00084   double value = std::atof(fValue.c_str());
00085   if(value > max)
00086     value = max;
00087   else if(value < min)
00088     value = min;
00089   
00090   return value;
00091 }
00092 
00093 double
00094 cgicc::FormEntry::getDoubleValue(double min, 
00095                                  double max,
00096                                  bool& bounded)         const
00097 {
00098   double value = std::atof(fValue.c_str());
00099 
00100   bounded = false;
00101 
00102   if(value > max) {
00103     value = max;
00104     bounded = true;
00105   }
00106   else if(value < min) {
00107     value = min;
00108     bounded = true;
00109   }
00110   
00111   return value;
00112 }
00113 
00114 std::string
00115 cgicc::FormEntry::makeString(std::string::size_type maxLen, 
00116                              bool allowNewlines)        const
00117 {
00118   std::string::size_type        len             = 0;
00119   std::string::size_type        avail           = maxLen;
00120   std::string::size_type        crCount         = 0;
00121   std::string::size_type        lfCount         = 0;    
00122   std::string::const_iterator   src             = fValue.begin();
00123   std::string::const_iterator   lim             = fValue.end();
00124   std::string                   dst;
00125 
00126 
00127   while(src != lim && len < avail) {
00128 
00129     // handle newlines
00130     if('\r' == *src || '\n' == *src) {
00131       crCount = 0;
00132       lfCount = 0;
00133       
00134       // Count the number of each kind of line break ('\r' and '\n')
00135       while( ('\r' == *src || '\n' == *src) && (src != lim)) {
00136         if('\r' == *src) 
00137           crCount++;
00138         else 
00139           lfCount++;
00140         ++src;
00141       }
00142       
00143       // if newlines are allowed, add them
00144       if(allowNewlines) {
00145         // output the larger value
00146         int lfsAdd = MAX(crCount, lfCount);
00147         dst.append(lfsAdd, '\n');
00148         len += lfsAdd;          
00149       }
00150     }
00151     // just append all other characters
00152     else {
00153       dst.append(1, *src);
00154       ++len;
00155       ++src;
00156     }
00157   }
00158   
00159   return dst;
00160 }

Generated on Tue Jul 3 15:44:28 2007 for GNUCgicc by  doxygen 1.5.1