Article View: news.software.nntp
Article #92413Re: INN laxmid clarification
From: Stefan Ram
Date: Thu, 04 Sep 2025 18:39
Date: Thu, 04 Sep 2025 18:39
202 lines
7290 bytes
7290 bytes
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
>I am not sure if this request is still relevant right now.
Some lines of the source code have more than 72 characters.
/*
* ctime_parser.c
*
* A descent parser for ctime(3) formatted strings.
*
* Example input:
* "Wed Jun 30 21:49:08 1993\n"
*
* Author: Stefan Ram
* Copyright (c) 2025 Stefan Ram
* Licensed under the Apache License, Version 2.0
*
* Note: This software has just been written and has not been
* extensively tested.
*
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* -------------------- Status codes -------------------- */
typedef enum {
CTIME_OK = 0,
CTIME_ERR_SYNTAX, /* structural problem with the string */
CTIME_ERR_RANGE /* values outside allowed ranges */
} CTimeStatus;
/* -------------------- Parse result -------------------- */
typedef struct {
int weekday; /* 0=Sun .. 6=Sat */
int month; /* 1=Jan .. 12=Dec */
int day; /* 1..31 */
int hour; /* 0..23 */
int minute; /* 0..59 */
int second; /* 0..60 */
int year; /* typically 4-digit */
CTimeStatus status; /* parse result */
} CTimeFields;
/* -------------------- Parser object -------------------- */
typedef struct {
const char *pos; /* current input pointer */
CTimeFields *out; /* where to put results/status */
} Parser;
/* -------------------- Utility functions -------------------- */
static void set_error(Parser *p, CTimeStatus st) {
if (p->out->status == CTIME_OK) {
p->out->status = st;
}
}
static void expect(Parser *p, char c) {
if (*p->pos != c) set_error(p, CTIME_ERR_SYNTAX);
else p->pos++;
}
static int parse_number_digits(Parser *p, int ndigits) {
int v = 0;
for (int i = 0; i < ndigits; i++) {
if (!isdigit((unsigned char)p->pos[i])) {
set_error(p, CTIME_ERR_SYNTAX);
return -1;
}
v = v * 10 + (p->pos[i] - '0');
}
p->pos += ndigits;
return v;
}
/* -------------------- Grammar routines -------------------- */
static int parse_day_of_week(Parser *p) {
static const char *names[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
for (int i = 0; i < 7; i++) {
if (strncmp(p->pos, names[i], 3) == 0) {
p->pos += 3;
return i;
}
}
set_error(p, CTIME_ERR_SYNTAX);
return -1;
}
static int parse_month(Parser *p) {
static const char *names[] = {
"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};
for (int i = 0; i < 12; i++) {
if (strncmp(p->pos, names[i], 3) == 0) {
p->pos += 3;
return i + 1;
}
}
set_error(p, CTIME_ERR_SYNTAX);
return -1;
}
static int parse_day(Parser *p) {
int d;
if (*p->pos == ' ') {
p->pos++;
if (!isdigit((unsigned char)*p->pos)) {
set_error(p, CTIME_ERR_SYNTAX);
return -1;
}
d = *p->pos++ - '0';
} else {
if (!isdigit((unsigned char)p->pos[0]) ||
!isdigit((unsigned char)p->pos[1])) {
set_error(p, CTIME_ERR_SYNTAX);
return -1;
}
d = (p->pos[0]-'0')*10 + (p->pos[1]-'0');
p->pos += 2;
}
if (d < 1 || d > 31) {
set_error(p, CTIME_ERR_RANGE);
return -1;
}
return d;
}
/* -------------------- Top-level parse -------------------- */
CTimeFields parse_ctime(const char *s) {
CTimeFields f = {0};
f.status = CTIME_OK;
Parser P = { s, &f };
f.weekday = parse_day_of_week(&P);
expect(&P, ' ');
f.month = parse_month(&P);
expect(&P, ' ');
f.day = parse_day(&P);
expect(&P, ' ');
f.hour = parse_number_digits(&P, 2);
expect(&P, ':');
f.minute = parse_number_digits(&P, 2);
expect(&P, ':');
f.second = parse_number_digits(&P, 2);
expect(&P, ' ');
f.year = parse_number_digits(&P, 4);
expect(&P, '\n');
/* If there are trailing characters after newline, that's an error */
if (*P.pos != '\0' && f.status == CTIME_OK)
f.status = CTIME_ERR_SYNTAX;
return f;
}
/* -------------------- Demonstration -------------------- */
#define TEST_CTIME_PARSER
#ifdef TEST_CTIME_PARSER
int main(void) {
const char *tests[] = {
"Wed Jun 30 21:49:08 1993\n",
"Sun Dec 5 07:03:12 2021\n",
"Bad Mon 30 00:00:00 1900\n", /* bogus */
NULL
};
for (const char **t = tests; *t; t++) {
printf("Parsing: \"%s\"\n", *t);
CTimeFields f = parse_ctime(*t);
if (f.status == CTIME_OK) {
printf(" -> OK: wd=%d, mon=%d, day=%d, %02d:%02d:%02d, year=%d\n",
f.weekday, f.month, f.day,
f.hour, f.minute, f.second, f.year);
} else {
printf(" -> ERROR, status=%d\n", f.status);
}
}
return 0;
}
#endif
Output:
Parsing: "Wed Jun 30 21:49:08 1993
"
-> OK: wd=3, mon=6, day=30, 21:49:08, year=1993
Parsing: "Sun Dec 5 07:03:12 2021
"
-> OK: wd=0, mon=12, day=5, 07:03:12, year=2021
Parsing: "Bad Mon 30 00:00:00 1900
"
-> ERROR, status=1
(That funny quotation mark is ok, because the ctime(3) format
includes an end-of-line at the end.)
Technical Details
Message-ID:
<reply-20250904193120@ram.dialup.fu-berlin.de>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.pugleaf.net!ygg-srl.newsdeef.eu!srl.newsdeef.eu!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mailReferences:
<u90srm$2mhb$1@nnrp.usenet.blueworldhosting.com> <10906hu$2tvn$1@nnrp.usenet.blueworldhosting.com> <10910cj$12r8f$1@news.trigofacile.com> <87bjnv4ait.fsf@hope.eyrie.org> <10928if$3djku$1@dont-email.me> <1092n83$2c6e$1@nnrp.usenet.blueworldhosting.com> <1094pjt$15nu2$1@news.trigofacile.com> <grammar-20250904190228@ram.dialup.fu-berlin.de>Show Full Headers
Complete Article Headers
Message-ID: <reply-20250904193120@ram.dialup.fu-berlin.de>
Subject: Re: INN laxmid clarification
Date: Thu, 04 Sep 2025 18:39:59 +0000
From: ram@zedat.fu-berlin.de (Stefan Ram)
References: <u90srm$2mhb$1@nnrp.usenet.blueworldhosting.com> <10906hu$2tvn$1@nnrp.usenet.blueworldhosting.com> <10910cj$12r8f$1@news.trigofacile.com> <87bjnv4ait.fsf@hope.eyrie.org> <10928if$3djku$1@dont-email.me> <1092n83$2c6e$1@nnrp.usenet.blueworldhosting.com> <1094pjt$15nu2$1@news.trigofacile.com> <grammar-20250904190228@ram.dialup.fu-berlin.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.pugleaf.net!ygg-srl.newsdeef.eu!srl.newsdeef.eu!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
Newsgroups: news.software.nntp
Organization: Stefan Ram
Lines: 203
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de J1PQBkejDpSm1dYtnincCgleIVDASY7hjl+aRBhJ+NE33k
Cancel-Lock: sha1:MKCOEEBKQayu8GfczBlSV6W3OHU= sha256:dNLVZpmrY71Q0zM5QIJOuBdo7LYS6mErhkoSa/Nj4bY=
X-Copyright: (C) Copyright 2025 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en