OpenCores
URL https://opencores.org/ocsvn/uart2bus_testbench/uart2bus_testbench/trunk

Subversion Repositories uart2bus_testbench

[/] [uart2bus_testbench/] [trunk/] [tb/] [uvm_src/] [dpi/] [uvm_regex.cc] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 HanySalah
//----------------------------------------------------------------------
2
//   Copyright 2007-2011 Mentor Graphics Corporation
3
//   Copyright 2011 Cadence Design Systems, Inc.
4
//   All Rights Reserved Worldwide
5
//
6
//   Licensed under the Apache License, Version 2.0 (the
7
//   "License"); you may not use this file except in
8
//   compliance with the License.  You may obtain a copy of
9
//   the License at
10
//
11
//       http://www.apache.org/licenses/LICENSE-2.0
12
//
13
//   Unless required by applicable law or agreed to in
14
//   writing, software distributed under the License is
15
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
16
//   CONDITIONS OF ANY KIND, either express or implied.  See
17
//   the License for the specific language governing
18
//   permissions and limitations under the License.
19
//----------------------------------------------------------------------
20
 
21
 
22
#include "uvm_dpi.h"
23
#include <sys/types.h>
24
 
25
 
26
const char uvm_re_bracket_char = '/';
27
#define UVM_REGEX_MAX_LENGTH 2048
28
static char uvm_re[UVM_REGEX_MAX_LENGTH+4];
29
 
30
 
31
//--------------------------------------------------------------------
32
// uvm_re_match
33
//
34
// Match a string to a regular expression.  The regex is first lookup
35
// up in the regex cache to see if it has already been compiled.  If
36
// so, the compile version is retrieved from the cache.  Otherwise, it
37
// is compiled and cached for future use.  After compilation the
38
// matching is done using regexec().
39
//--------------------------------------------------------------------
40
int uvm_re_match(const char * re, const char *str)
41
{
42
  regex_t *rexp;
43
  int err;
44
 
45
  // safety check.  Args should never be ~null~ since this is called
46
  // from DPI.  But we'll check anyway.
47
  if(re == NULL)
48
    return 1;
49
  if(str == NULL)
50
    return 1;
51
 
52
  int len = strlen(re);
53
  char * rex = &uvm_re[0];
54
 
55
  if (len > UVM_REGEX_MAX_LENGTH) {
56
      const char* err_str = "uvm_re_match : regular expression greater than max %0d: |%s|";
57
      char buffer[strlen(err_str) + int_str_max(10) + strlen(re)];
58
      sprintf(buffer, err_str, UVM_REGEX_MAX_LENGTH, re);
59
      m_uvm_report_dpi(M_UVM_ERROR,
60
                       (char*) "UVM/DPI/REGEX_MAX",
61
                       &buffer[0],
62
                       M_UVM_NONE,
63
                       (char*)__FILE__,
64
                       __LINE__);
65
    return 1;
66
  }
67
 
68
  // we copy the regexp because we need to remove any brackets around it
69
  strncpy(&uvm_re[0],re,UVM_REGEX_MAX_LENGTH);
70
  if (len>1 && (re[0] == uvm_re_bracket_char) && re[len-1] == uvm_re_bracket_char) {
71
    uvm_re[len-1] = '\0';
72
    rex++;
73
  }
74
 
75
  rexp = (regex_t*)malloc(sizeof(regex_t));
76
 
77
  if (rexp == NULL) {
78
      m_uvm_report_dpi(M_UVM_ERROR,
79
                       (char*) "UVM/DPI/REGEX_ALLOC",
80
                       (char*) "uvm_re_match: internal memory allocation error",
81
                       M_UVM_NONE,
82
                       (char*)__FILE__,
83
                       __LINE__);
84
    return 1;
85
  }
86
 
87
  err = regcomp(rexp, rex, REG_EXTENDED);
88
 
89
  if (err != 0) {
90
      regerror(err,rexp,uvm_re,UVM_REGEX_MAX_LENGTH-1);
91
      const char * err_str = "uvm_re_match : invalid glob or regular expression: |%s||%s|";
92
      char buffer[strlen(err_str) + strlen(re) + strlen(uvm_re)];
93
      sprintf(buffer, err_str, re, uvm_re);
94
      m_uvm_report_dpi(M_UVM_ERROR,
95
                       (char*) "UVM/DPI/REGEX_INV",
96
                       &buffer[0],
97
                       M_UVM_NONE,
98
                       (char*)__FILE__,
99
                       __LINE__);
100
    regfree(rexp);
101
    free(rexp);
102
    return err;
103
  }
104
 
105
  err = regexec(rexp, str, 0, NULL, 0);
106
 
107
  //vpi_printf((PLI_BYTE8*)  "UVM_INFO: uvm_re_match: re=%s str=%s ERR=%0d\n",rex,str,err);
108
  regfree(rexp);
109
  free(rexp);
110
 
111
  return err;
112
}
113
 
114
 
115
//--------------------------------------------------------------------
116
// uvm_glob_to_re
117
//
118
// Convert a glob expression to a normal regular expression.
119
//--------------------------------------------------------------------
120
 
121
const char * uvm_glob_to_re(const char *glob)
122
{
123
  const char *p;
124
  int len;
125
 
126
  // safety check.  Glob should never be ~null~ since this is called
127
  // from DPI.  But we'll check anyway.
128
  if(glob == NULL)
129
    return NULL;
130
 
131
  len = strlen(glob);
132
 
133
  if (len > 2040) {
134
      const char * err_str = "uvm_re_match : glob expression greater than max 2040: |%s|";
135
      char buffer[strlen(err_str) + strlen(glob)];
136
      sprintf(buffer, err_str, glob);
137
      m_uvm_report_dpi(M_UVM_ERROR,
138
                       (char*) "UVM/DPI/REGEX_MAX",
139
                       &buffer[0],
140
                       M_UVM_NONE,
141
                       (char*)__FILE__,
142
                       __LINE__);
143
    return glob;
144
  }
145
 
146
  // If either of the following cases appear then return an empty string
147
  //
148
  //  1.  The glob string is empty (it has zero characters)
149
  //  2.  The glob string has a single character that is the
150
  //      uvm_re_bracket_char  (i.e. "/")
151
  if(len == 0 || (len == 1 && *glob == uvm_re_bracket_char))
152
  {
153
    uvm_re[0] = '\0';
154
    return &uvm_re[0];  // return an empty string
155
  }
156
 
157
  // If bracketed with the /glob/, then it's already a regex
158
  if(glob[0] == uvm_re_bracket_char && glob[len-1] == uvm_re_bracket_char)
159
  {
160
    strcpy(uvm_re,glob);
161
    return &uvm_re[0];
162
  }
163
  else
164
  {
165
    // Convert the glob to a true regular expression (Posix syntax)
166
    len = 0;
167
 
168
    uvm_re[len++] = uvm_re_bracket_char;
169
 
170
    // ^ goes at the beginning...
171
    if (*glob != '^')
172
      uvm_re[len++] = '^';
173
 
174
    for(p = glob; *p; p++)
175
    {
176
      // Replace the glob metacharacters with corresponding regular
177
      // expression metacharacters.
178
      switch(*p)
179
      {
180
      case '*':
181
        uvm_re[len++] = '.';
182
        uvm_re[len++] = '*';
183
        break;
184
 
185
      case '+':
186
        uvm_re[len++] = '.';
187
        uvm_re[len++] = '+';
188
        break;
189
 
190
      case '.':
191
        uvm_re[len++] = '\\';
192
        uvm_re[len++] = '.';
193
        break;
194
 
195
      case '?':
196
        uvm_re[len++] = '.';
197
        break;
198
 
199
      case '[':
200
        uvm_re[len++] = '\\';
201
        uvm_re[len++] = '[';
202
        break;
203
 
204
      case ']':
205
        uvm_re[len++] = '\\';
206
        uvm_re[len++] = ']';
207
        break;
208
 
209
      case '(':
210
        uvm_re[len++] = '\\';
211
        uvm_re[len++] = '(';
212
        break;
213
 
214
      case ')':
215
        uvm_re[len++] = '\\';
216
        uvm_re[len++] = ')';
217
        break;
218
 
219
      default:
220
        uvm_re[len++] = *p;
221
        break;
222
      }
223
    }
224
  }
225
 
226
  // Let's check to see if the regular expression is bounded by ^ at
227
  // the beginning and $ at the end.  If not, add those characters in
228
  // the appropriate position.
229
 
230
  if (uvm_re[len-1] != '$')
231
    uvm_re[len++] = '$';
232
 
233
  uvm_re[len++] = uvm_re_bracket_char;
234
 
235
  uvm_re[len++] = '\0';
236
 
237
  return &uvm_re[0];
238
}
239
 
240
 
241
//--------------------------------------------------------------------
242
// uvm_dump_re_cache
243
//
244
// Dumps the set of regular expressions stored in the cache
245
//--------------------------------------------------------------------
246
 
247
void uvm_dump_re_cache()
248
{
249
    m_uvm_report_dpi(M_UVM_INFO,
250
                     (char*) "UVM/DPI/REGEX_MAX",
251
                     (char*)  "uvm_dump_re_cache: cache not implemented",
252
                     M_UVM_LOW,
253
                     (char*)__FILE__,
254
                     __LINE__);
255
}
256
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.