[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gettext] [PATCH] Convert line endings when scanning Python sour
From: |
Daiki Ueno |
Subject: |
Re: [bug-gettext] [PATCH] Convert line endings when scanning Python source code |
Date: |
Tue, 16 Apr 2013 15:51:12 +0900 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) |
Daiki Ueno <address@hidden> writes:
> From 346699bfec77187eea1734d338855015a94824b6 Mon Sep 17 00:00:00 2001
> From: Daiki Ueno <address@hidden>
> Date: Tue, 16 Apr 2013 15:17:14 +0900
> Subject: [PATCH] Convert line endings when scanning Python source code
Sorry, the attached patch should be better.
>From a0b65974702320f14596dd90697cf1c539c35ed2 Mon Sep 17 00:00:00 2001
From: Daiki Ueno <address@hidden>
Date: Tue, 16 Apr 2013 15:17:14 +0900
Subject: [PATCH] Convert line endings when scanning Python source code
---
gettext-tools/src/x-python.c | 52 ++++++++++++++++++++++++++++++++++----------
1 file changed, 41 insertions(+), 11 deletions(-)
diff --git a/gettext-tools/src/x-python.c b/gettext-tools/src/x-python.c
index aa6a7d6..c7e0680 100644
--- a/gettext-tools/src/x-python.c
+++ b/gettext-tools/src/x-python.c
@@ -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;
--
1.8.1.4