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

"Kevin J. McCarthy" <[email protected]>
Newsgroups gmane.mail.mutt.devel
Message-ID <[email protected]>
This will be used to add built-in textmode conversion for the
"classic" pgp backend.
---
 muttlib.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 protos.h  |  2 ++
 2 files changed, 65 insertions(+)

diff --git a/muttlib.c b/muttlib.c
index 494c27d0..a6294476 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -2437,6 +2437,69 @@ int mutt_atolofft (const char *str, LOFF_T *dst, int flags)
   return rc;
 }
 
+static int normalize_line_endings (const char *infile, FILE *ifp, const char *outfile,
+                                   FILE *ofp, int tocrlf)
+{
+  char *buf = NULL;
+  size_t blen = 0, olen;
+  int has_nl;
+
+  if (infile)
+    if (!(ifp = safe_fopen (infile, "r")))
+      mutt_perror (infile);
+  if (!ifp)
+    return -1;
+
+  if (outfile)
+    if (!(ofp = safe_fopen (outfile, "w")))
+      mutt_perror (outfile);
+  if (!ofp)
+  {
+    if (infile)
+      safe_fclose (&ifp);
+    return -1;
+  }
+
+  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);
+    }
+  }
+
+  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
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.