Re: [PATCH 1/6] Add library functions to convert between CRLF and LF.

Alejandro Colomar via Mutt-dev <[email protected]>
Newsgroups gmane.mail.mutt.devel
Message-ID <abqqyUGNZslXPHWf@devuan>
Hi Kevin,

On 2026-03-18T10:50:35+0800, Kevin J. McCarthy wrote:
> +  while ((buf = mutt_read_line (buf, &blen, ifp, NULL, MUTT_EOL)) != NULL)
> +  {
> +    has_nl = 0;
> +    olen = mutt_strlen (buf);
> +    if ((olen > 0) && buf[olen - 1] == '\n')
> +    {
> +      has_nl = 1;
> +      buf[olen - 1] = '\0';
> +      if ((olen > 1) && buf[olen - 2] == '\r')
> +        buf[olen - 2] = '\0';
> +    }
> +    fputs (buf, ofp);
> +    if (has_nl)
> +    {
> +      if (tocrlf)
> +        fputc ('\r', ofp);
> +      fputc ('\n', ofp);
> +    }

Just a comment about readability:

If we had a strsuffix() function, this could be written as something
like:

	while (...) {
		nl = strsuffix(buf, "\r\n") ?: strsuffix(buf, "\n");
		if (nl != NULL)
			strcpy(nl, "");

		fputs(buf, ofp);
		if (nl != NULL) {
			if (tocrlf)
				fputc('\r', ofp);
			fputc('\n', ofp);
		}
	}

I've found strsuffix() quite useful for a few things.  It's rarely used,
but when it's used, it improves readability significantly.


Have a lovely day!
Alex

> +  }
> +
> +  if (infile)
> +    safe_fclose (&ifp);
> +  if (outfile)
> +    safe_fclose (&ofp);
> +  FREE (&buf);
> +
> +  return 0;
> +}
> +
> +int mutt_convert_to_crlf (const char *infile, FILE *ifp, const char *outfile, FILE *ofp)
> +{
> +  return normalize_line_endings (infile, ifp, outfile, ofp, 1);
> +}
> +
> +int mutt_convert_to_lf (const char *infile, FILE *ifp, const char *outfile, FILE *ofp)
> +{
> +  return normalize_line_endings (infile, ifp, outfile, ofp, 0);
> +}
> +
> +
>  
>  /************************************************************************
>   * These functions are transplanted from lib.c, in order to modify them *
> diff --git a/protos.h b/protos.h
> index a641034a..71497f95 100644
> --- a/protos.h
> +++ b/protos.h
> @@ -323,6 +323,8 @@ int mutt_command_complete (char *, size_t, int, int);
>  int mutt_var_value_complete (char *, size_t, int);
>  int mutt_complete (char *, size_t);
>  int mutt_compose_attachment (BODY *a);
> +int mutt_convert_to_crlf (const char *, FILE *, const char *, FILE *);
> +int mutt_convert_to_lf (const char *, FILE *, const char *, FILE *);
>  int mutt_copy_body (FILE *, BODY **, BODY *);
>  int mutt_decode_save_attachment (FILE *, BODY *, const char *, int, int);
>  int mutt_display_message (HEADER *h);
> -- 
> 2.53.0
> 

-- 
<https://www.alejandro-colomar.es>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmm6rNUACgkQ64mZXMKQ
wqllyw//cOwiMnxvg5OGkAhwMKflIv5FIb3lhAhAFvsmiQ21mJva1aVaqgltUP5U
sA/BWxJtxXe3VAtw37d5fP9PvtfW/AcZftSV1923/uetI4U2fHFaQ47oSGm6sS87
eTkDjKh4P5St3Mqx9qu46ZPiw+uzVsss4uRC0Vw2cUmFiGA9bF3On9FjpBvxXChR
+GI073EAZVEpvGQRD51cX1I7YgwufRlOQWZq6e1KKgKuJwPIFjZNQD7pxXEnNaLq
rAaY+PtW8tbRPqzOMmf/hWZZnDpszfXnZNZ37RneZjWPzuIbDSNE5ifOsvIiZJR0
RuMoZkLgPFklk7xCUzclSGFVuPC/DLrTWwg9xrIQkpJdXnvnME8GpNHY3eB1GMPE
P0BpM0Ac+2yUJe0mKCt8Gh03RiLr8XVa45emAyJak1EjPNP2jGvdn43ODwRx0agX
ZEHB6/89BKhV5TvbbRXJtV2edJSDZ0IVY6f5o9W7cQ5md5/9glAaxBg+qOhSyWR1
g5dYZtgsfg5o6TV+EoFAfj87+/2TdW6Z3aLl9RJcIwO8GhctPiJB030xPZ3YcZxy
+nQCKfDU4g/kilKZvDJfNCEYcTpFDA8yisUe9CpaM4hT+zmuDBZidgcS4VKZTjI2
RNw/DqZIaue6Auf3n4P16aic4zZ1+OHUvMx089EYDAbKEnUpvsM=
=m39C
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.