~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal_format.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 * Implementation of the server's date and time string matching utility.
29
29
 */
30
30
 
31
 
#include "config.h"
 
31
#include "drizzled/global.h"
32
32
 
33
33
#include "drizzled/temporal_format.h"
34
34
#include "drizzled/temporal.h"
35
35
 
 
36
#include <string> /** C++ string class used */
36
37
#include <string.h>
 
38
#include <vector>
37
39
#include PCRE_HEADER
38
40
 
39
 
#include <string>
40
 
#include <vector>
41
 
 
42
 
using namespace std;
43
 
 
44
41
namespace drizzled
45
42
{
46
43
 
47
 
TemporalFormat::TemporalFormat(const char *pattern) :
 
44
  TemporalFormat::TemporalFormat(const char *pattern)
 
45
  :
48
46
  _pattern(pattern)
49
47
, _error_offset(0)
50
48
, _error(NULL)
57
55
, _usecond_part_index(0)
58
56
, _nsecond_part_index(0)
59
57
{
 
58
  /* Make sure we've got no junk in the match_vector. */
 
59
  memset(_match_vector, 0, sizeof(_match_vector));
 
60
 
60
61
  /* Compile our regular expression */
61
62
  _re= pcre_compile(pattern
62
63
                    , 0 /* Default options */
70
71
{
71
72
  if (! is_valid()) 
72
73
    return false;
73
 
 
74
 
  int32_t match_vector[OUT_VECTOR_SIZE]; /**< Stores match substring indexes */
75
74
  
76
 
  /* Make sure we've got no junk in the match_vector. */
77
 
  memset(match_vector, 0, sizeof(match_vector));
78
 
 
79
75
  /* Simply check the subject against the compiled regular expression */
80
76
  int32_t result= pcre_exec(_re
81
77
                            , NULL /* No extra data */
83
79
                            , data_len
84
80
                            , 0 /* Start at offset 0 of subject...*/
85
81
                            , 0 /* Default options */
86
 
                            , match_vector
 
82
                            , _match_vector
87
83
                            , OUT_VECTOR_SIZE
88
84
                            );
89
85
  if (result < 0)
111
107
    return false;
112
108
 
113
109
  /* C++ string class easy to use substr() method is very useful here */
114
 
  string copy_data(data, data_len);
 
110
  std::string copy_data(data, data_len);
115
111
  /* 
116
112
   * OK, we have the expected substring matches, so grab
117
113
   * the various temporal parts from the subject string
123
119
   */
124
120
  if (_year_part_index > 1)
125
121
  {
126
 
    size_t year_start= match_vector[_year_part_index];
127
 
    size_t year_len= match_vector[_year_part_index + 1] - match_vector[_year_part_index];
 
122
    size_t year_start= _match_vector[_year_part_index];
 
123
    size_t year_len= _match_vector[_year_part_index + 1] - _match_vector[_year_part_index];
128
124
    to->_years= atoi(copy_data.substr(year_start, year_len).c_str());
129
125
    if (year_len == 2)
130
126
      to->_years+= (to->_years >= DRIZZLE_YY_PART_YEAR ? 1900 : 2000);
131
127
  }
132
128
  if (_month_part_index > 1)
133
129
  {
134
 
    size_t month_start= match_vector[_month_part_index];
135
 
    size_t month_len= match_vector[_month_part_index + 1] - match_vector[_month_part_index];
 
130
    size_t month_start= _match_vector[_month_part_index];
 
131
    size_t month_len= _match_vector[_month_part_index + 1] - _match_vector[_month_part_index];
136
132
    to->_months= atoi(copy_data.substr(month_start, month_len).c_str());
137
133
  }
138
134
  if (_day_part_index > 1)
139
135
  {
140
 
    size_t day_start= match_vector[_day_part_index];
141
 
    size_t day_len= match_vector[_day_part_index + 1] - match_vector[_day_part_index];
 
136
    size_t day_start= _match_vector[_day_part_index];
 
137
    size_t day_len= _match_vector[_day_part_index + 1] - _match_vector[_day_part_index];
142
138
    to->_days= atoi(copy_data.substr(day_start, day_len).c_str());
143
139
  }
144
140
  if (_hour_part_index > 1)
145
141
  {
146
 
    size_t hour_start= match_vector[_hour_part_index];
147
 
    size_t hour_len= match_vector[_hour_part_index + 1] - match_vector[_hour_part_index];
 
142
    size_t hour_start= _match_vector[_hour_part_index];
 
143
    size_t hour_len= _match_vector[_hour_part_index + 1] - _match_vector[_hour_part_index];
148
144
    to->_hours= atoi(copy_data.substr(hour_start, hour_len).c_str());
149
145
  }
150
146
  if (_minute_part_index > 1)
151
147
  {
152
 
    size_t minute_start= match_vector[_minute_part_index];
153
 
    size_t minute_len= match_vector[_minute_part_index + 1] - match_vector[_minute_part_index];
 
148
    size_t minute_start= _match_vector[_minute_part_index];
 
149
    size_t minute_len= _match_vector[_minute_part_index + 1] - _match_vector[_minute_part_index];
154
150
    to->_minutes= atoi(copy_data.substr(minute_start, minute_len).c_str());
155
151
  }
156
152
  if (_second_part_index > 1)
157
153
  {
158
 
    size_t second_start= match_vector[_second_part_index];
159
 
    size_t second_len= match_vector[_second_part_index + 1] - match_vector[_second_part_index];
 
154
    size_t second_start= _match_vector[_second_part_index];
 
155
    size_t second_len= _match_vector[_second_part_index + 1] - _match_vector[_second_part_index];
160
156
    to->_seconds= atoi(copy_data.substr(second_start, second_len).c_str());
161
157
  }
162
158
  if (_usecond_part_index > 1)
163
159
  {
164
 
    size_t usecond_start= match_vector[_usecond_part_index];
165
 
    size_t usecond_len= match_vector[_usecond_part_index + 1] - match_vector[_usecond_part_index];
 
160
    size_t usecond_start= _match_vector[_usecond_part_index];
 
161
    size_t usecond_len= _match_vector[_usecond_part_index + 1] - _match_vector[_usecond_part_index];
166
162
    /* 
167
163
     * For microseconds, which are millionth of 1 second, 
168
164
     * we must ensure that we produce a correct result, 
180
176
  }
181
177
  if (_nsecond_part_index > 1)
182
178
  {
183
 
    size_t nsecond_start= match_vector[_nsecond_part_index];
184
 
    size_t nsecond_len= match_vector[_nsecond_part_index + 1] - match_vector[_nsecond_part_index];
 
179
    size_t nsecond_start= _match_vector[_nsecond_part_index];
 
180
    size_t nsecond_len= _match_vector[_nsecond_part_index + 1] - _match_vector[_nsecond_part_index];
185
181
    /* 
186
182
     * For nanoseconds, which are 1 billionth of a second, 
187
183
     * we must ensure that we produce a correct result, 
200
196
  return true;
201
197
}
202
198
 
 
199
} /* end namespace drizzled */
203
200
 
204
201
#define COUNT_KNOWN_FORMATS 19
205
202
 
251
248
, {"^(\\d{1,2})\\.(\\d{1,6})$", 0, 0, 0, 0, 0, 1, 2, 0} /* [S]S.uuuuuu */
252
249
};
253
250
 
254
 
vector<TemporalFormat *> known_datetime_formats;
255
 
vector<TemporalFormat *> known_date_formats;
256
 
vector<TemporalFormat *> known_time_formats;
257
 
vector<TemporalFormat *> all_temporal_formats;
 
251
std::vector<drizzled::TemporalFormat *> known_datetime_formats;
 
252
std::vector<drizzled::TemporalFormat *> known_date_formats;
 
253
std::vector<drizzled::TemporalFormat *> known_time_formats;
 
254
std::vector<drizzled::TemporalFormat *> all_temporal_formats;
258
255
 
259
256
/**
260
257
 * We allocate and initialize all known date/time formats.
264
261
bool init_temporal_formats()
265
262
{
266
263
  /* Compile all the regular expressions for the datetime formats */
267
 
  TemporalFormat *tmp;
 
264
  drizzled::TemporalFormat *tmp;
268
265
  struct temporal_format_args current_format_args;
269
266
  int32_t x;
270
267
  
271
268
  for (x= 0; x<COUNT_KNOWN_FORMATS; ++x)
272
269
  {
273
270
    current_format_args= __format_args[x];
274
 
    tmp= new TemporalFormat(current_format_args.pattern);
 
271
    tmp= new drizzled::TemporalFormat(current_format_args.pattern);
275
272
    tmp->set_year_part_index(current_format_args.year_part_index);
276
273
    tmp->set_month_part_index(current_format_args.month_part_index);
277
274
    tmp->set_day_part_index(current_format_args.day_part_index);
303
300
/** Free all allocated temporal formats */
304
301
void deinit_temporal_formats()
305
302
{
306
 
  vector<TemporalFormat *>::iterator p= all_temporal_formats.begin();
 
303
  std::vector<drizzled::TemporalFormat *>::iterator p= all_temporal_formats.begin();
307
304
  while (p != all_temporal_formats.end())
308
305
  {
309
306
    delete *p;
314
311
  known_time_formats.clear();
315
312
  all_temporal_formats.clear();
316
313
}
317
 
 
318
 
} /* end namespace drizzled */