1 | /* $Id: timedelta.rl 1452 2008-10-02 12:09:09Z lukasz $ */ |
---|
2 | /* |
---|
3 | * FedStage DRMAA utilities library |
---|
4 | * Copyright (C) 2006-2008 FedStage Systems |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | #include <drmaa_utils/common.h> |
---|
27 | |
---|
28 | #ifndef lint |
---|
29 | static char rcsid[] |
---|
30 | # ifdef __GNUC__ |
---|
31 | __attribute__ ((unused)) |
---|
32 | # endif |
---|
33 | = "$Id: timedelta.rl 1452 2008-10-02 12:09:09Z lukasz $"; |
---|
34 | #endif |
---|
35 | |
---|
36 | int |
---|
37 | fsd_parse_timedelta( const char *string ) |
---|
38 | { |
---|
39 | %%{ |
---|
40 | machine fsd_timedelta; |
---|
41 | action error { |
---|
42 | fsd_exc_raise_fmt( |
---|
43 | FSD_ERRNO_INVALID_VALUE_FORMAT, |
---|
44 | "time delta syntax error: %s", string ); |
---|
45 | } |
---|
46 | action next_field { |
---|
47 | fields[ n_fields++ ] = v; |
---|
48 | } |
---|
49 | integer = [0-9]+ >{ v = 0; } ${ v *= 10; v += fc - '0'; }; |
---|
50 | timedelta = integer %next_field (':' integer %next_field){1,2}; |
---|
51 | main := timedelta @eof(error) $err(error); |
---|
52 | }%% |
---|
53 | |
---|
54 | %%write data; |
---|
55 | int v = 0; |
---|
56 | int fields[3]; |
---|
57 | int n_fields = 0; |
---|
58 | int timedelta; |
---|
59 | int i; |
---|
60 | |
---|
61 | const char *p = string; |
---|
62 | const char *pe = p + strlen(p); |
---|
63 | const char *eof = pe; |
---|
64 | int cs; |
---|
65 | |
---|
66 | fsd_log_enter(( "(%s)", string )); |
---|
67 | %%write init; |
---|
68 | %%write exec; |
---|
69 | timedelta = 0; |
---|
70 | for( i = 0; i < n_fields; i++ ) |
---|
71 | { |
---|
72 | timedelta *= 60; |
---|
73 | timedelta += fields[i]; |
---|
74 | } |
---|
75 | fsd_log_return(( " =%d", timedelta )); |
---|
76 | return timedelta; |
---|
77 | } |
---|
78 | |
---|
79 | /* vim: set ft=ragel: */ |
---|