Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 83 lines (63 sloc) 2.954 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// 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;
}
Something went wrong with that request. Please try again.