aplat

Documents structurés pour Unix
git clone git://git.asteride.xyz/~ldp/aplat.git
Journaux | Fichiers | Références | LISEZ-MOI | LICENCE

commit 9c76abfb1444c0a8547382b0cfae64c47161e8d7
parent 85e997142da429c02ed7e9ae48cc9f3a661a1ae1
Auteur: Loïc Daignault-Pichette <loic@asteride.xyz>
Date:   Fri,  7 Jun 2024 14:19:02 -0400

création des fichiers tampon.[ch]

Diffstat:
MMakefile | 4++--
Maplat.c | 136+------------------------------------------------------------------------------
Atampon.c | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atampon.h | 17+++++++++++++++++
4 files changed, 146 insertions(+), 137 deletions(-)

diff --git a/Makefile b/Makefile @@ -14,8 +14,8 @@ include config.mk all: aplat -aplat: aplat.o - ${CC} ${COPTS} ${LDOPTS} -o "$@" aplat.o ${BIBS} +aplat: aplat.o tampon.o + ${CC} ${COPTS} ${LDOPTS} -o "$@" aplat.o tampon.o ${BIBS} clean: rm -rf *.o *.core diff --git a/aplat.c b/aplat.c @@ -1,7 +1,5 @@ #include <assert.h> #include <stdio.h> -#include <stdlib.h> -#include <string.h> #ifdef __OpenBSD__ #include <unistd.h> @@ -11,6 +9,7 @@ #include <locale.h> #include "coroutines.h" +#include "tampon.h" #define _(c) gettext(c) @@ -25,20 +24,7 @@ #define DRAP_FRM 1 #define DRAP_OUV 2 -struct tampon { - char *tp; /* début du tampon */ - char *po; /* prochain octet libre */ - int nt; /* nombre d'octets écrits en trop */ - size_t tll; /* taille du tampon */ - size_t nl; /* nombre d'octets libres */ -}; - static void utilisation(FILE *fichier); -static int tp_init(struct tampon *tp, size_t taille); -static int tp_ecr(struct tampon *tp, char c); -static int tp_terminer(struct tampon *tp); -static void tp_reinit(struct tampon *tp); -static int utf8_incomplet(char *base, char *fin); static int aplatir(FILE *f); static int obt_jeton(FILE *fichier, struct tampon *tp); static int echapper(FILE *fichier, struct tampon *tp); @@ -264,126 +250,6 @@ obt_jeton(FILE *f, struct tampon *tp) /*cr_sortir(0, EOF);*/ } -#define tp_invariants(t) \ - (assert((t)), \ - assert((t)->tp), \ - assert((t)->po), \ - assert((t)->po >= (t)->tp), \ - assert((t)->tll >= (t)->nl)) - -static int -tp_init(struct tampon *tp, size_t tll) -{ - assert(tp); - assert(tll >= 1); - - if ((tp->tp = malloc(tll+1)) == NULL) - return -1; - tp->po = tp->tp; - tp->tll = tp->nl = tll; - tp->nt = 0; - - tp_invariants(tp); - - return 0; -} - -static int -tp_ecr(struct tampon *tp, char c) -{ - tp_invariants(tp); - - if (tp->nl > 0) { - tp->nl--; - *tp->po++ = c; - tp_invariants(tp); - return (unsigned char) c; - } - - return -1; -} - -static void -tp_reinit(struct tampon *tp) -{ - tp_invariants(tp); - - tp->nl = 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; - } - } - - tp_invariants(tp); -} - -static int -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; - return -1; - } - fin = tp->po - tp->nt; - *tp->po = *fin; - *fin = '\0'; - } - - tp_invariants(tp); - - return 0; -} - -/* nombre d'octets en trop */ -static int -utf8_incomplet(char *base, char *fin) -{ - int i, n; - unsigned char c; - - assert(base); - assert(fin); - assert(base <= fin); - - if (((unsigned char) *base >> 6) == 0x2) - return -1; - - for (i = 1; ((unsigned char) *fin >> 6) == 0x2; fin--) - i++; - c = *fin; - if (c <= 0x7F) /* un octet */ - n = 1; - else if (c >= 0xF0) /* quatre octets */ - n = 4; - else if (c >= 0xE0) /* trois octets */ - n = 3; - else if (c >= 0xC2) /* deux octets */ - n = 2; - else /* séquence invalide */ - n = 0;; - - if (i < n) - return i; - else if (i == n) - return 0; - else - return n - i; -} - static int echapper(FILE *f, struct tampon *tp) { diff --git a/tampon.c b/tampon.c @@ -0,0 +1,126 @@ +#include <assert.h> +#include <stdlib.h> + +#include "tampon.h" + +#define tp_invariants(t) \ + (assert((t)), \ + assert((t)->tp), \ + assert((t)->po), \ + assert((t)->po >= (t)->tp), \ + assert((t)->tll >= (t)->nl)) + +static int utf8_incomplet(char *base, char *fin); + +int +tp_init(struct tampon *tp, size_t tll) +{ + assert(tp); + assert(tll >= 1); + + if ((tp->tp = malloc(tll+1)) == NULL) + return -1; + tp->po = tp->tp; + tp->tll = tp->nl = tll; + tp->nt = 0; + + tp_invariants(tp); + + return 0; +} + +int +tp_ecr(struct tampon *tp, char c) +{ + tp_invariants(tp); + + if (tp->nl > 0) { + tp->nl--; + *tp->po++ = c; + tp_invariants(tp); + return (unsigned char) c; + } + + return -1; +} + +void +tp_reinit(struct tampon *tp) +{ + tp_invariants(tp); + + tp->nl = 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; + } + } + + tp_invariants(tp); +} + +int +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; + return -1; + } + fin = tp->po - tp->nt; + *tp->po = *fin; + *fin = '\0'; + } + + tp_invariants(tp); + + return 0; +} + +/* nombre d'octets en trop */ +static int +utf8_incomplet(char *base, char *fin) +{ + int i, n; + unsigned char c; + + assert(base); + assert(fin); + assert(base <= fin); + + if (((unsigned char) *base >> 6) == 0x2) + return -1; + + for (i = 1; ((unsigned char) *fin >> 6) == 0x2; fin--) + i++; + c = *fin; + if (c <= 0x7F) /* un octet */ + n = 1; + else if (c >= 0xF0) /* quatre octets */ + n = 4; + else if (c >= 0xE0) /* trois octets */ + n = 3; + else if (c >= 0xC2) /* deux octets */ + n = 2; + else /* séquence invalide */ + n = 0;; + + if (i < n) + return i; + else if (i == n) + return 0; + else + return n - i; +} diff --git a/tampon.h b/tampon.h @@ -0,0 +1,17 @@ +#ifndef TAMPON_H +#define TAMPON_H + +struct tampon { + char *tp; /* début du tampon */ + char *po; /* prochain octet libre */ + int nt; /* nombre d'octets écrits en trop */ + size_t tll; /* taille du tampon */ + size_t nl; /* 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); + +#endif