Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
84 lines (63 sloc) 2.88 KB
// http-date.rl
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
%%{
machine httpDate;
yearDigit = ( [0-9] @{ gdate.year = gdate.year * 10 + (fc - '0'); });
dayDigit = ( [0-9] @{ gdate.day = gdate.day * 10 + (fc - '0'); });
hourDigit = ( [0-9] @{ gdate.hour = gdate.hour * 10 + (fc - '0'); });
minuteDigit = ( [0-9] @{ gdate.minute = gdate.minute * 10 + (fc - '0'); });
secondDigit = ( [0-9] @{ gdate.second = gdate.second * 10.0 + (fc - '0'); });
wkday = ("Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun");
weekday = ("Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday");
month = (("Jan" @{ gdate.month = 1; }) | ("Feb" @{ gdate.month = 2; }) | ("Mar" @{ gdate.month = 3; }) |
("Apr" @{ gdate.month = 4; }) | ("May" @{ gdate.month = 5; }) | ("Jun" @{ gdate.month = 6; }) |
("Jul" @{ gdate.month = 7; }) | ("Aug" @{ gdate.month = 8; }) | ("Sep" @{ gdate.month = 9; }) |
("Oct" @{ gdate.month = 10; }) | ("Nov" @{ gdate.month = 11; }) | ("Dec" @{ gdate.month = 12; }));
year4Digits = (yearDigit . yearDigit . yearDigit . yearDigit);
year2Digits = ((yearDigit . yearDigit) @{ gdate.year += 1900; });
dayDigits = (dayDigit . dayDigit);
hourDigits = (hourDigit . hourDigit);
minuteDigits = (minuteDigit . minuteDigit);
secondDigits = (secondDigit . secondDigit);
date1 = (dayDigits . " " . month . " " . year4Digits);
date2 = (dayDigits . "-" . month . "-" . year2Digits);
date3 = (month . " " . (dayDigits | " " . dayDigit));
time = (hourDigits . ":" . minuteDigits . ":" . secondDigits);
rfc1123date = (wkday . "," . " " . date1 . " " . time . " " . "GMT");
rfc850date = (weekday . "," . " " . date2 . " " . time . " " . "GMT");
asctimedate = (wkday . " " . date3 . " " . time . " " . year4Digits);
HTTPdate = ((rfc1123date %{ parsed = 1; }) | (rfc850date %{ parsed = 1; }) | (asctimedate %{ parsed = 1; }));
main := HTTPdate;
}%%
%% write data nofinal;
typedef signed char SInt8;
typedef signed int SInt32;
struct __gdate {
SInt32 year;
SInt8 month;
SInt8 day;
SInt8 hour;
SInt8 minute;
double second;
};
typedef struct __gdate __gdate;
void scanner(char *buf) {
int cs;
int parsed = 0;
__gdate gdate;
memset(&gdate, 0, sizeof(__gdate));
%% write init;
{
int len = strlen(buf);
char *p = buf, *pe = p + len, *eof = pe;
%% write exec;
}
printf("parsed: %d, %d/%d/%d %d:%d:%.2f\n", parsed, gdate.month, gdate.day, gdate.year, gdate.hour, gdate.minute, gdate.second);
}
int main(int argc, char *argv[]) {
if(argc > 1) { int x; for(x = 1; x < argc; x++) { scanner(argv[x]); printf("-----\n"); }}
return 0;
}