commit 9906eea6ed58d80778218292f5b466da110686ff
parent 45f27a52489dc99b30274d105458fd0a5ff92760
Auteur: Loïc Daignault-Pichette <loic@asteride.xyz>
Date: Sat, 8 Jun 2024 12:42:13 -0400
simplification du mécanisme de césure
Diffstat:
M | aplat.c | | | 28 | ++++++++++++++++++++-------- |
M | tampon.c | | | 56 | ++++++++++++++++++++++++++++---------------------------- |
M | tampon.h | | | 13 | +++++++------ |
3 files changed, 55 insertions(+), 42 deletions(-)
diff --git a/aplat.c b/aplat.c
@@ -37,7 +37,7 @@ static int obt_jeton(FILE *fichier, struct tampon *tp);
static int echapper(FILE *fichier, struct tampon *tp);
static int echapper_guillemets(FILE *fichier, struct tampon *tp);
static int echapper_bloc(FILE *fichier, struct tampon *tp);
-static void afficher_ligne(int *draps, struct tampon *tp_etq,
+static int afficher_ligne(int *draps, struct tampon *tp_etq,
struct tampon *tp_ctn);
char *nom_prog;
@@ -144,12 +144,12 @@ aplatir(FILE *f)
prfd++;
tp = &tp_etq;
if (jtp == JT_OUV || jtp == JT_ATM)
- afficher_ligne(&draps, &tp_etq, &tp_ctn);
+ (void) afficher_ligne(&draps, &tp_etq, &tp_ctn);
draps |= DRAP_OUV;
break;
case JT_FRM:
draps |= DRAP_FRM;
- afficher_ligne(&draps, &tp_etq, &tp_ctn);
+ (void) afficher_ligne(&draps, &tp_etq, &tp_ctn);
if (--prfd == 0)
goto fin;
break;
@@ -164,7 +164,7 @@ aplatir(FILE *f)
nom_prog, TP_ETQ_TLL);
return -1;
}
- afficher_ligne(&draps, &tp_etq, &tp_ctn);
+ (void) afficher_ligne(&draps, &tp_etq, &tp_ctn);
}
jtp = jta;
} while ((jta = obt_jeton(f, tp)) != EOF);
@@ -508,9 +508,11 @@ echapper_bloc(FILE *f, struct tampon *tp)
cr_sortir(0, EOF);
}
-static void
+static int
afficher_ligne(int *draps, struct tampon *tp_etq, struct tampon *tp_ctn)
{
+ size_t lngr;
+
assert(draps);
assert(tp_etq);
assert(tp_ctn);
@@ -520,12 +522,22 @@ afficher_ligne(int *draps, struct tampon *tp_etq, struct tampon *tp_ctn)
if (*draps & DRAP_FRM)
putc(')', stdout);
- tp_terminer(tp_etq);
- tp_terminer(tp_ctn);
+ if (tp_terminer(tp_etq) < 0 || tp_terminer(tp_ctn) < 0)
+ return -1;
- printf("\t%s\t%s\n", tp_etq->tp, tp_ctn->tp);
+ putc('\t', stdout);
+ lngr = tp_longueur(tp_etq);
+ if (fwrite(tp_etq->tp, 1, lngr, stdout) < lngr)
+ return -1;
+ putc('\t', stdout);
+ lngr = tp_longueur(tp_ctn);
+ if (fwrite(tp_ctn->tp, 1, lngr, stdout) < lngr)
+ return -1;
+ putc('\n', stdout);
*draps = 0;
tp_reinit(tp_etq);
tp_reinit(tp_ctn);
+
+ return 0;
}
diff --git a/tampon.c b/tampon.c
@@ -8,9 +8,10 @@
assert((t)->tp), \
assert((t)->po), \
assert((t)->po >= (t)->tp), \
- assert((t)->tll >= (t)->nl))
+ assert((t)->oc >= 0), \
+ assert((t)->tll >= (t)->ol))
-static int utf8_incomplet(char *base, char *fin);
+static int utf8_acouper(char *base, char *fin);
int
tp_init(struct tampon *tp, size_t tll)
@@ -18,11 +19,11 @@ tp_init(struct tampon *tp, size_t tll)
assert(tp);
assert(tll >= 1);
- if ((tp->tp = malloc(tll+1)) == NULL)
+ if ((tp->tp = malloc(tll)) == NULL)
return -1;
tp->po = tp->tp;
- tp->tll = tp->nl = tll;
- tp->nt = 0;
+ tp->tll = tp->ol = tll;
+ tp->oc = 0;
tp_invariants(tp);
@@ -34,8 +35,8 @@ tp_ecr(struct tampon *tp, char c)
{
tp_invariants(tp);
- if (tp->nl > 0) {
- tp->nl--;
+ if (tp->ol > 0) {
+ tp->ol--;
*tp->po++ = c;
tp_invariants(tp);
return (unsigned char) c;
@@ -49,18 +50,15 @@ tp_reinit(struct tampon *tp)
{
tp_invariants(tp);
- tp->nl = tp->tll;
+ tp->ol = tp->tll;
tp->po = tp->tp;
- if (tp->nt > 0) {
- char *src, *dst;
- tp->po += tp->nt;
- tp->nl -= tp->nt;
- *tp->tp = tp->tp[tp->tll];
- dst = tp->tp + 1;
- src = tp->tp + tp->tll - (tp->nt - 1);
- while (--tp->nt > 0) {
- *dst = *src;
- }
+ if (tp->oc > 0) {
+ char *src;
+ tp->ol -= tp->oc;
+ src = tp->tp + tp->tll - tp->oc;
+ while (tp->oc-- > 0)
+ *tp->po++ = *src++;
+ tp->oc = 0;
}
tp_invariants(tp);
@@ -71,17 +69,11 @@ tp_terminer(struct tampon *tp)
{
tp_invariants(tp);
- if (tp->nl > 0) {
- *tp->po = '\0';
- } else {
- char *fin;
- if ((tp->nt = utf8_incomplet(tp->tp, tp->po-1)) < 0) {
- tp->nt = 0;
+ if (tp->ol == 0) {
+ if ((tp->oc = utf8_acouper(tp->tp, tp->po-1)) < 0) {
+ tp->oc = 0;
return -1;
}
- fin = tp->po - tp->nt;
- *tp->po = *fin;
- *fin = '\0';
}
tp_invariants(tp);
@@ -89,9 +81,17 @@ tp_terminer(struct tampon *tp)
return 0;
}
+size_t
+tp_longueur(struct tampon *tp)
+{
+ tp_invariants(tp);
+
+ return tp->tll - tp->ol - tp->oc;
+}
+
/* nombre d'octets en trop */
static int
-utf8_incomplet(char *base, char *fin)
+utf8_acouper(char *base, char *fin)
{
int i, n;
unsigned char c;
diff --git a/tampon.h b/tampon.h
@@ -4,14 +4,15 @@
struct tampon {
char *tp; /* début du tampon */
char *po; /* prochain octet libre */
- int nt; /* nombre d'octets écrits en trop */
+ int oc; /* nombre d'octets coupés */
size_t tll; /* taille du tampon */
- size_t nl; /* nombre d'octets libres */
+ size_t ol; /* nombre d'octets libres */
};
-int tp_init(struct tampon *tp, size_t taille);
-int tp_ecr(struct tampon *tp, char c);
-int tp_terminer(struct tampon *tp);
-void tp_reinit(struct tampon *tp);
+int tp_init(struct tampon *tp, size_t taille);
+int tp_ecr(struct tampon *tp, char c);
+int tp_terminer(struct tampon *tp);
+size_t tp_longueur(struct tampon *tp);
+void tp_reinit(struct tampon *tp);
#endif