void btm_inc_day(struct btm *const d) { assert(d != NULL); d->day ++; if (d->day > max_monthday(d->year,d->month)) { d->day = 1; btm_inc_month(d); } }
void btm_dec_day(struct btm *const d) { assert(d != NULL); d->day--; if (d->day == 0) { btm_dec_month(d); d->day = max_monthday(d->year,d->month); } }
bool tumbler_new(tumbler__s *const tum,const char *text) { /*---------------------------------------------------------------------- ; all variables are defined here, even though u2, u3, u4, segments and ; part aren't used until tumbler_new_range. It appears that defining them ; there causes a compiler error. It's either a bug in the version of GCC ; I'm using, or you can't declare variables after a goto target label. ; ; I'm not sure what the case is here, so I'm moving them up here. ;----------------------------------------------------------------------*/ struct value u1; struct value u2; struct value u3; struct value u4; bool part = false; assert(tum != NULL); assert(text != NULL); memset(tum,0,sizeof(tumbler__s)); /*------------------------------------------------- ; parse year ;-------------------------------------------------*/ if (!parse_num(&u1,&text,g_blog->first.year,g_blog->last.year)) return false; if (u1.val == g_blog->first.year) { tum->start.month = g_blog->first.month; tum->start.day = g_blog->first.day; } else { tum->start.month = 1; tum->start.day = 1; } tum->ustart = tum->ustop = UNIT_YEAR; tum->start.year = tum->stop.year = u1.val; tum->start.part = 1; tum->stop.month = 12; tum->stop.day = 31; tum->stop.part = ENTRY_MAX; if (*text == '\0') return check_dates(tum); if (*text == '-') goto tumbler_new_range; if (*text != '/') return false; text++; if (*text == '\0') return tum->redirect |= true; /*------------------------------------------------- ; parse month ;-------------------------------------------------*/ if (!parse_num(&u1,&text,1,12)) return false; assert(tum->start.part == 1); assert(tum->stop.part == ENTRY_MAX); tum->start.month = tum->stop.month = u1.val; tum->ustart = tum->ustop = UNIT_MONTH; tum->stop.day = max_monthday(tum->start.year,tum->start.month); if (u1.len == 1) tum->redirect |= true; if (*text == '\0') return check_dates(tum); if (*text == '-') goto tumbler_new_range; if (*text != '/') return false; text++; if (*text == '\0') return tum->redirect |= true; /*-------------------- ; parse day ;---------------------*/ if (!parse_num(&u1,&text,1,max_monthday(tum->start.year,tum->start.month))) return false; tum->start.day = tum->stop.day = u1.val; tum->ustart = tum->ustop = UNIT_DAY; if (u1.len == 1) tum->redirect |= true; if (*text == '\0') return check_dates(tum); if (*text == '-') goto tumbler_new_range; if (*text == '/') goto tumbler_new_file; if (*text != '.') return false; text++; if (*text == '\0') return false; /*----------------------------- ; parse part ;-----------------------------*/ if (!parse_num(&u1,&text,1,ENTRY_MAX)) return false; tum->start.part = tum->stop.part = u1.val; tum->ustart = tum->ustop = UNIT_PART; if ((u1.len > 1) && (*u1.txt == '0')) tum->redirect |= true; if (*text == '\0') return check_dates(tum); if (*text != '-') return false; assert(*text == '-'); goto tumbler_new_range; /*----------------------------- ; parse file ;-----------------------------*/ tumbler_new_file: assert(*text == '/'); tum->file = true; text++; for (size_t i = 0 ; i < FILENAME_MAX ; i++) { tum->filename[i] = *text++; if (tum->filename[i] == '\0') return check_dates(tum); if (tum->filename[i] == '/') return false; } return false; /*-------------------------------------- ; parse the range portion of the tumbler. Since the number of segments in ; this portion is variable, we parse first without reguard to the actual ; ranges, keep track of the number of segments found, then after we're ; done with the parsing, we figure everything out. ;---------------------------------------*/ tumbler_new_range: assert(*text == '-'); tum->range = true; text++; /*-------------------- ; first unit ;---------------------*/ if (!parse_num(&u1,&text,1,INT_MAX)) return false; tum->segments++; if (*text == '\0') goto tumbler_new_calculate; if (*text == '.') goto tumbler_new_range_part; if (*text != '/') return false; text++; if (*text == '\0') { tum->redirect |= true; goto tumbler_new_calculate; } /*------------------------------ ; second unit ;------------------------------*/ if (!parse_num(&u2,&text,1,INT_MAX)) return false; tum->segments++; if (*text == '\0') goto tumbler_new_calculate; if (*text == '.') goto tumbler_new_range_part; if (*text != '/') return false; text++; if (*text == '\0') { tum->redirect |= true; goto tumbler_new_calculate; } /*---------------------------------- ; third unit ;-----------------------------------*/ if (!parse_num(&u3,&text,1,INT_MAX)) return false; tum->segments++; if (*text == '\0') goto tumbler_new_calculate; if (*text != '.') return false; /*----------------------------------------------------------------------- ; the fourth unit, OR the part. The part will always be stuffed into u4. ;------------------------------------------------------------------------*/ tumbler_new_range_part: assert(part == false); assert(*text == '.'); assert(tum->segments >= 1); text++; if (!parse_num(&u4,&text,1,ENTRY_MAX)) return false; tum->segments++; part = true; if ((u4.len > 1) && (*u4.txt == '0')) tum->redirect |= true; if (*text != '\0') return false; /*---------------------------------------------------------------------- ; Now figure everything out. If we have all four segments, then this part ; is easy---we fill out every part of the range portion with our values. ; All values have already been checked against the lower bound of 1, so we ; only have to check against the upper limit for the most part. ; ; Here is also were we check for leading or missing zeros, and set the ; redirect flag appropriately. The month and day require a leading zero, ; while the part does not. ;------------------------------------------------------------------------*/ tumbler_new_calculate: if (tum->segments == 4) { tum->redirect |= (u2.len == 1) || (u3.len == 1) || ((u4.len > 1) && (*u4.txt == '0')) ; tum->stop.year = u1.val; tum->stop.month = u2.val; tum->stop.day = u3.val; tum->stop.part = u4.val; tum->ustop = UNIT_PART; return check_dates(tum); } /*--------------------------------------------------------------- ; If we have three segments, there are two cases to consider: ; ; 2000/02/03-2004/05/06 ; ; or ; ; 2000/02/03-04/05.6 ; ; The first is when part is false. This is a fairly easy case to handle. ;-----------------------------------------------------------------------*/ else if (tum->segments == 3) { if (part) { tum->redirect |= (u1.len == 1) || (u2.len == 1) || ((u4.len > 1) && (*u4.txt == '0')) ; tum->stop.month = u1.val; tum->stop.day = u2.val; tum->stop.part = u4.val; tum->ustop = UNIT_PART; return check_dates(tum); } else { tum->stop.year = u1.val; tum->stop.month = u2.val; tum->stop.day = u3.val; tum->stop.part = ENTRY_MAX; return check_dates(tum); } } /*------------------------------------------------------------------ ; We have two segments. If there is a part, then it's easy: ; ; 2000/02/03-04.1 ; ; The next few cases not so: ; ; 2000/02/03-2004/05 ; 2000/02/03-04/05 ; ; We use some huristics to distinquish between the two. This means we ; can't specify years before 13 AD, but I doubt that will be an issue any ; time soon. ;------------------------------------------------------------------------*/ else if (tum->segments == 2) { if (part) { tum->redirect |= (u1.len == 1) || ((u4.len > 1) && (*u4.txt == '0')) ; tum->stop.day = u1.val; tum->stop.part = u4.val; tum->ustop = UNIT_PART; return check_dates(tum); } else { /*--------------------------------------- ; check for year/month vs. month/day ;--------------------------------------*/ if (u1.val >= g_blog->first.year) { tum->redirect |= (u2.len == 1); tum->stop.year = u1.val; tum->stop.month = u2.val; tum->stop.day = max_monthday(tum->stop.year,tum->stop.month); tum->stop.part = ENTRY_MAX; tum->ustop = UNIT_MONTH; return check_dates(tum); } else { tum->redirect |= (u1.len == 1) || (u2.len == 1) ; tum->stop.month = u1.val; tum->stop.day = u2.val; tum->stop.part = ENTRY_MAX; tum->ustop = UNIT_DAY; return check_dates(tum); } } } /*------------------------------------------------------------------ ; Final case---one segment. We use the unit defined in the starting ; portion to determine what type of segment this is. ;-------------------------------------------------------------------*/ else { switch(tum->ustart) { case UNIT_YEAR: tum->stop.year = u1.val; tum->stop.month = 12; tum->stop.day = 31; /* there are always 31 days in Debtember */ tum->stop.part = ENTRY_MAX; tum->ustop = UNIT_YEAR; return check_dates(tum); case UNIT_MONTH: tum->redirect |= (u1.len == 1); tum->stop.month = u1.val; tum->stop.day = max_monthday(tum->stop.year,tum->stop.month); tum->stop.part = ENTRY_MAX; tum->ustop = UNIT_MONTH; return check_dates(tum); case UNIT_DAY: tum->redirect |= (u1.len == 1); tum->stop.day = u1.val; tum->stop.part = ENTRY_MAX; tum->ustop = UNIT_DAY; return check_dates(tum); case UNIT_PART: tum->redirect |= ((u1.len > 1) && (*u1.txt == '0')); tum->stop.part = u1.val; tum->ustop = UNIT_PART; return check_dates(tum); case UNIT_INDEX: assert(0); return false; } } return false; }