Remove dynamic exceptions
[anna.git] / source / http / internal / Token.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <string.h>
10 #include <stdlib.h>
11
12 #include <anna/config/defines.hpp>
13 #include <anna/core/functions.hpp>
14
15 #include <anna/http/internal/Token.hpp>
16
17 using namespace std;
18 using namespace anna;
19
20 const string& http::Token::getStringValue() const
21 {
22   if(contains(0) == false)
23     const_cast <Token*>(this)->a_aux.assign(DataBlock::getData(), DataBlock::getSize());
24   else
25     const_cast <Token*>(this)->a_aux = DataBlock::getData();
26
27   return a_aux;
28 }
29
30 int http::Token::getIntegerValue() const
31 {
32   if(contains(0) == false) {
33     const_cast <Token*>(this)->a_aux.assign(DataBlock::getData(), DataBlock::getSize());
34     return atoi(a_aux.c_str());
35   }
36
37   return atoi(DataBlock::getData());
38 }
39
40 bool http::Token::contains(const char byte) const
41 {
42   const char* p;
43   const char* maxp;
44
45   for(p = DataBlock::getData(), maxp = p + DataBlock::getSize(); p < maxp; p ++) {
46     if(*p == byte)
47       return true;
48   }
49
50   return false;
51 }
52
53 int http::Token::calculeOffset(const DataBlock& base) const
54 noexcept(false) {
55   const int result = getData() - base.getData();
56
57   if(result < 0) {
58     string msg("anna::http::Token::calculeOffset | Invalid address for the token: ");
59     msg += functions::asString(*this);
60     throw RuntimeException(msg, ANNA_FILE_LOCATION);
61   }
62
63   return result;
64 }
65
66 bool http::Token::match(const char* str) const
67 {
68   const char* pp = DataBlock::getData();
69   int size = DataBlock::getSize();
70
71   while(size > 0) {
72     if(*pp != ' ')
73       break;
74
75     size --;
76     pp ++;
77   }
78
79   return (anna_strlen(str) != size) ? false : (strncasecmp(pp, str, size) == 0);
80 }