Browse code

acc(k): Add time2string() function and clean up code.

- Implement time2string():
* Replace redundant time-to-string conversion logic in various
functions with implementation and call to new, single function.
* Reduce microseconds precision from timeval structure to
milliseconds as it used to be prior to refactoring.
* Store conversion result in static buffer.
- Add variables and macro definitions now needed.
- Remove variables no longer needed.

Timo Reimann authored on 03/10/2011 18:10:34
Showing 1 changed files
... ...
@@ -47,6 +47,7 @@
47 47
 #include <sys/time.h>
48 48
 
49 49
 #define TIME_STR_BUFFER_SIZE 20
50
+#define TIME_BUFFER_LENGTH 256
50 51
 
51 52
 struct dlg_binds dlgb;
52 53
 struct acc_extra* cdr_extra = NULL;
... ...
@@ -57,8 +58,8 @@ static const str end_id = { "et", 2};
57 57
 static const str duration_id = { "d", 1};
58 58
 static const str zero_duration = { "0", 1};
59 59
 static const char time_separator = {'.'};
60
-static const int milliseconds_max = 1000000;
61
-static const unsigned int time_buffer_length = 256;
60
+static const int milliseconds_max = 1000;
61
+static char time_buffer[ TIME_BUFFER_LENGTH];
62 62
 static const str empty_string = { "", 0};
63 63
 
64 64
 // buffers which are used to collect the crd data for writing
... ...
@@ -228,6 +229,35 @@ static int string2time( str* time_str, struct timeval* time_value)
228 228
     return 0;
229 229
 }
230 230
 
231
+/* convert a timeval struct into a string */
232
+static int time2string( struct timeval* time_value, str* time_str)
233
+{
234
+    int buffer_length;
235
+
236
+    if( !time_value)
237
+    {
238
+        LM_ERR( "time_value or any of its fields is empty!\n");
239
+        return -1;
240
+    }
241
+
242
+    buffer_length = snprintf( time_buffer,
243
+                              TIME_BUFFER_LENGTH,
244
+                              "%ld%c%03d",
245
+                              time_value->tv_sec,
246
+                              time_separator,
247
+                              (int)time_value->tv_usec/1000);
248
+
249
+    if( buffer_length < 0)
250
+    {
251
+        LM_ERR( "failed to write to buffer.\n");
252
+        return -1;
253
+    }
254
+
255
+    time_str->s = time_buffer;
256
+    time_str->len = buffer_length;
257
+    return 0;
258
+}
259
+
231 260
 /* set the duration in the dialog struct */
232 261
 static int set_duration( struct dlg_cell* dialog)
233 262
 {
... ...
@@ -235,9 +265,7 @@ static int set_duration( struct dlg_cell* dialog)
235 235
     struct timeval end_time;
236 236
     int milliseconds = -1;
237 237
     int seconds = -1;
238
-    char buffer[ time_buffer_length];
239
-    int buffer_length = -1;
240
-    str duration_time = empty_string;
238
+    str duration_time;
241 239
 
242 240
     if( !dialog)
243 241
     {
... ...
@@ -288,21 +316,11 @@ static int set_duration( struct dlg_cell* dialog)
288 288
         return -1;
289 289
     }
290 290
 
291
-    buffer_length = snprintf( buffer,
292
-                              time_buffer_length,
293
-                              "%d%c%03d",
294
-                              seconds,
295
-                              time_separator,
296
-                              milliseconds);
297
-
298
-    if( buffer_length < 0)
299
-    {
300
-        LM_ERR( "failed to write to buffer.\n");
291
+    if( time2string(&(struct timeval){seconds, milliseconds}, &duration_time) < 0) {
292
+        LM_ERR( "failed to convert current time to string\n");
301 293
         return -1;
302 294
     }
303 295
 
304
-    duration_time = ( str){ buffer, buffer_length};
305
-
306 296
     if( dlgb.set_dlg_var( dialog,
307 297
                           (str*)&duration_id,
308 298
                           (str*)&duration_time) != 0)
... ...
@@ -317,10 +335,8 @@ static int set_duration( struct dlg_cell* dialog)
317 317
 /* set the current time as start-time in the dialog struct */
318 318
 static int set_start_time( struct dlg_cell* dialog)
319 319
 {
320
-    char buffer[ time_buffer_length];
321 320
     struct timeval current_time;
322
-    int buffer_length = -1;
323
-    str start_time = empty_string;
321
+    str start_time;
324 322
 
325 323
     if( !dialog)
326 324
     {
... ...
@@ -334,21 +350,11 @@ static int set_start_time( struct dlg_cell* dialog)
334 334
         return -1;
335 335
     }
336 336
 
337
-    buffer_length = snprintf( buffer,
338
-                              time_buffer_length,
339
-                              "%d%c%03d",
340
-                              (int)current_time.tv_sec,
341
-                              time_separator,
342
-                              (int)current_time.tv_usec);
343
-
344
-    if( buffer_length < 0)
345
-    {
346
-        LM_ERR( "reach buffer size\n");
337
+    if( time2string(&current_time, &start_time) < 0) {
338
+        LM_ERR( "failed to convert current time to string\n");
347 339
         return -1;
348 340
     }
349 341
 
350
-    start_time = (str){ buffer, buffer_length};
351
-
352 342
     if( dlgb.set_dlg_var( dialog,
353 343
                           (str*)&start_id,
354 344
                           (str*)&start_time) != 0)
... ...
@@ -379,10 +385,8 @@ static int set_start_time( struct dlg_cell* dialog)
379 379
 /* set the current time as end-time in the dialog struct */
380 380
 static int set_end_time( struct dlg_cell* dialog)
381 381
 {
382
-    char buffer[ time_buffer_length];
383 382
     struct timeval current_time;
384
-    int buffer_length = -1;
385
-    str end_time = empty_string;
383
+    str end_time;
386 384
 
387 385
     if( !dialog)
388 386
     {
... ...
@@ -396,21 +400,11 @@ static int set_end_time( struct dlg_cell* dialog)
396 396
         return -1;
397 397
     }
398 398
 
399
-    buffer_length = snprintf( buffer,
400
-                              time_buffer_length,
401
-                              "%d%c%03d",
402
-                              (int)current_time.tv_sec,
403
-                              time_separator,
404
-                              (int)current_time.tv_usec);
405
-
406
-    if( buffer_length < 0)
407
-    {
408
-        LM_ERR( "failed to write buffer\n");
399
+    if( time2string(&current_time, &end_time) < 0) {
400
+        LM_ERR( "failed to convert current time to string\n");
409 401
         return -1;
410 402
     }
411 403
 
412
-    end_time = ( str){ buffer, buffer_length};
413
-
414 404
     if( dlgb.set_dlg_var( dialog,
415 405
                           (str*)&end_id,
416 406
                           (str*)&end_time) != 0)