]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Support CR/LF line terminators in Python sources even on Unix.
authorDaiki Ueno <ueno@gnu.org>
Tue, 16 Apr 2013 06:17:14 +0000 (15:17 +0900)
committerDaiki Ueno <ueno@gnu.org>
Mon, 22 Apr 2013 03:09:56 +0000 (12:09 +0900)
gettext-tools/src/ChangeLog
gettext-tools/src/x-python.c

index 4b92aeb871236c122afb371e06402e5f74d146cf..9493a5ed76212ab86ae4748b8aa825703540316a 100644 (file)
@@ -1,3 +1,9 @@
+2013-04-22  Daiki Ueno  <ueno@gnu.org>
+
+       Support CR/LF line terminators in Python sources even on Unix.
+       * x-python.c (phase0_getc, phase0_ungetc): New functions.
+       (phase1_getc): Use them instead of calling getc/ungetc directly.
+
 2013-04-22  Daiki Ueno  <ueno@gnu.org>
 
        Support explicit string concatenation in Python.
index cdca2551c97122a9117fc183495ee46eec4e3d3d..a3a106cfc1a92c1a6ed4377b7f4ecd3c05aef795 100644 (file)
@@ -149,6 +149,46 @@ static int line_number;
 static FILE *fp;
 
 
+/* 0. Terminate line by \n, regardless whether the external
+   representation of a line terminator is CR (Mac), and CR/LF
+   (DOS/Windows), as Python treats them equally.  */
+static int
+phase0_getc ()
+{
+  int c;
+
+  c = getc (fp);
+  if (c == EOF)
+    {
+      if (ferror (fp))
+        error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
+               real_file_name);
+      return EOF;
+    }
+
+  if (c == '\r')
+    {
+      int c1 = getc (fp);
+
+      if (c1 != EOF && c1 != '\n')
+        ungetc (c1, fp);
+
+      /* Seen line terminator CR or CR/LF.  */
+      return '\n';
+    }
+
+  return c;
+}
+
+/* Supports only one pushback character, and not '\n'.  */
+static inline void
+phase0_ungetc (int c)
+{
+  if (c != EOF)
+    ungetc (c, fp);
+}
+
+
 /* 1. line_number handling.  */
 
 /* Maximum used, roughly a safer MB_LEN_MAX.  */
@@ -165,17 +205,7 @@ phase1_getc ()
   if (phase1_pushback_length)
     c = phase1_pushback[--phase1_pushback_length];
   else
-    {
-      c = getc (fp);
-
-      if (c == EOF)
-        {
-          if (ferror (fp))
-            error (EXIT_FAILURE, errno, _("error while reading \"%s\""),
-                   real_file_name);
-          return EOF;
-        }
-    }
+    c = phase0_getc ();
 
   if (c == '\n')
     ++line_number;