File: variables.cpp

package info (click to toggle)
sip-tester 2.0.1-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,164 kB
  • ctags: 1,187
  • sloc: cpp: 12,331; ansic: 1,984; sh: 229; makefile: 184
file content (266 lines) | stat: -rw-r--r-- 7,034 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Authors : Benjamin GAUTHIER - 24 Mar 2004
 *            Joseph BANINO
 *            Olivier JACQUES
 *            Richard GAYRAUD
 *            From Hewlett Packard Company.
 *           
 */

#include "sipp.hpp"

/*
__________________________________________________________________________

              C L A S S    C C a l l V a r i a b l e
__________________________________________________________________________
*/

bool CCallVariable::isSet()
{
  if(M_nbOfMatchingValue >= 1)
    return(true);
  else
    return(false);
}

// WARNING : setMatchingValue does't allocate the memory for the matching value
// but the destructor free the memory
void CCallVariable::setMatchingValue(char* P_matchingVal)
{
  if(M_matchingValue != NULL) {
    delete [] M_matchingValue;    
  }
  M_matchingValue     = P_matchingVal;
  M_nbOfMatchingValue++;
}

char* CCallVariable::getMatchingValue()
{
  return(M_matchingValue);
}


// Constuctor and destructor
CCallVariable::CCallVariable()
{
  M_matchingValue     = NULL;
  M_nbOfMatchingValue = 0;
}

CCallVariable::~CCallVariable()
{
  if(M_matchingValue != NULL) {
    delete [] M_matchingValue;
  }
  M_matchingValue = NULL;
  M_nbOfMatchingValue = 0;
}

/*
__________________________________________________________________________

              C L A S S    C V a r i a b l e
__________________________________________________________________________
*/

bool CVariable::matchRegularExpression(char* P_string)
{
  if(M_regExpWellFormed) {
    if(regexec(&(M_internalRegExp), P_string, 0, NULL, 0) == 0) {
      return(true);
    } else {
      return(false);
    }
  } else {
    return(false);
  }
}

void CVariable::setSubString(char** P_target, char* P_source, int P_start, int P_stop)
{
  int sizeOf;
  int sourceLength;
  size_t L_size = 0;

  if(P_source != NULL) {
    sizeOf = P_stop - P_start;
    if(sizeOf > 0) {
      L_size = (size_t) sizeOf;
      L_size += 1;
      (*P_target) = new char[L_size];
      sourceLength = strlen(P_source);
     
      memcpy((*P_target), &(P_source[P_start]), sizeOf);

      (*P_target)[sizeOf] = '\0';
	 }
  } else {
    *P_target = NULL ;
  }
}


int CVariable::executeRegExp(char* P_string, 
                             CCallVariable** P_callVarTable,
                	     int  P_varId,
			     int  P_nbSubVar,
                             int  * P_subVarIdTable)
{
   regmatch_t pmatch[10];
  int error;
   int nbOfMatch = 0;
   int L_i ;
   CCallVariable* L_callVar ;
   char* result = NULL ;
   int   L_currentSubIdx = 0 ;


   memset((void*)pmatch, 0, sizeof(regmatch_t)*10);

  if(M_regExpWellFormed) {
     error = regexec(&(M_internalRegExp), P_string, 10, pmatch, REGEXP_PARAMS);
     if ( error == 0) {
        L_callVar = P_callVarTable[P_varId] ;
        for(L_i=0; L_i < 10; L_i++) {
          if(pmatch[L_i].rm_eo == -1) break ;
 	  setSubString(&result, P_string, 
                       pmatch[L_i].rm_so, pmatch[L_i].rm_eo);
          L_callVar->setMatchingValue(result);
          if (L_currentSubIdx == P_nbSubVar) break ;
          L_callVar = P_callVarTable[P_subVarIdTable[L_currentSubIdx]] ;
	  L_currentSubIdx ++ ;

	  /* 
            printf(" the pmatch %d %d  \n", L_i, pmatch[L_i].rm_eo);
            printf(" the pmatch %d %d  \n", L_i, pmatch[L_i].rm_so);
            int L_k ;

            for(L_k = pmatch[L_i].rm_so; L_k <= pmatch[L_i].rm_eo; L_k++) {
                    printf("%c", P_string[L_k]);
            }
            printf("\n");
          */

		}
	 }
  }
  return(nbOfMatch);

}

bool CVariable::extractAllMatchedExpression(char* P_string, 
                                            char *** P_result, 
                                            int* P_number)
{
  regmatch_t pmatch;
  int error;
  char tmpTab[MAX_MATCHING_EXPR][BUFFER_SIZE];
  char* strBuff;
  int currentStop;
  int maxLength;
  
  if(M_regExpWellFormed) {

    currentStop = 0;
    maxLength = strlen(P_string);
    error = regexec(&(M_internalRegExp), P_string, 1, &pmatch, REGEXP_PARAMS);
    (*P_number) = 0;

    while(error == 0) {
      setSubString(&strBuff, P_string+currentStop, 
                   pmatch.rm_so, pmatch.rm_eo);
      if (strlen(strBuff) > BUFFER_SIZE) {
        ERROR_P2("Regular expression match size (%zu) is bigger than buffer size (%d). Change BUFFER_SIZE in call.hpp and recompile SIPp.", strlen(strBuff), BUFFER_SIZE);
      }
      strcpy(tmpTab[(*P_number)], strBuff);
      delete(strBuff);
      (*P_number)++;
      currentStop += pmatch.rm_eo;
      if((currentStop >= maxLength) || ((*P_number) >= MAX_MATCHING_EXPR))
        break;
      error = regexec(&(M_internalRegExp), 
                      P_string+currentStop, 1, 
                      &pmatch, REGEXP_PARAMS);
      if(pmatch.rm_eo == pmatch.rm_so)
        break;
    }
    if((*P_number) > 0) {
      (*P_result) = (char**) malloc(sizeof(char*)*(*P_number));
      for(int i=0; i<(*P_number); i++)
        {
          (*P_result)[i] = (char*) malloc(sizeof(char)*(maxLength+1));
          strcpy((*P_result)[i], tmpTab[i]);
        }
      return(true);
    } else {
      return(false);
    }
  } else {
    return(false);
  }
}

// selecteur et accesseur
bool CVariable::isRegExpWellFormed()
{
  return(M_regExpWellFormed);
}

char* CVariable::getRegularExpression()
{
  return(M_regularExpression);
}


// Constuctor and destructor
CVariable::CVariable(char* P_regularExpression)
{
  int sizeOf;
  int errorCode;

  if(P_regularExpression != NULL)
  {
    sizeOf = strlen(P_regularExpression);
    M_regularExpression = new char[sizeOf+1];
    strcpy(M_regularExpression, P_regularExpression);
  }

  // we must call regcomp to avoid a coredump on the regfree. Even if the char* P_regularExpression is null
  errorCode = regcomp(&(M_internalRegExp), M_regularExpression, REGEXP_PARAMS);
  if(errorCode != 0)
  {
    /* regerror(errorCode, &M_internalRegExp, buffer, sizeof(buffer));
       printf("recomp error : regular expression '%s' - error '%s'\n", 
                  M_regularExpression, 
                  buffer); */
    M_regExpWellFormed = false;
   }
   else
   {
     M_regExpWellFormed = true;
   }
}

CVariable::~CVariable()
{
  if(M_regularExpression != NULL)
    delete [] M_regularExpression;
  M_regularExpression = NULL;

  regfree(&(M_internalRegExp)); 
}