comparison src/coding.c @ 19173:04ed7c3f5cee

(detect_eol_type): If EOL representation does not seem consistent, use no conversion.
author Richard M. Stallman <rms@gnu.org>
date Tue, 05 Aug 1997 18:19:33 +0000
parents 8fa6e23f8d22
children 917138730635
comparison
equal deleted inserted replaced
19172:f3580f4e3587 19173:04ed7c3f5cee
2701 2701
2702 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC 2702 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2703 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF, 2703 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2704 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */ 2704 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */
2705 2705
2706 #define MAX_EOL_CHECK_COUNT 3
2707
2706 int 2708 int
2707 detect_eol_type (src, src_bytes) 2709 detect_eol_type (src, src_bytes)
2708 unsigned char *src; 2710 unsigned char *src;
2709 int src_bytes; 2711 int src_bytes;
2710 { 2712 {
2711 unsigned char *src_end = src + src_bytes; 2713 unsigned char *src_end = src + src_bytes;
2712 unsigned char c; 2714 unsigned char c;
2713 2715 int total = 0; /* How many end-of-lines are found so far. */
2714 while (src < src_end) 2716 int eol_type = CODING_EOL_UNDECIDED;
2717 int this_eol_type;
2718
2719 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
2715 { 2720 {
2716 c = *src++; 2721 c = *src++;
2717 if (c == '\n') 2722 if (c == '\n' || c == '\r')
2718 return CODING_EOL_LF;
2719 else if (c == '\r')
2720 { 2723 {
2721 if (src < src_end && *src == '\n') 2724 total++;
2722 return CODING_EOL_CRLF; 2725 if (c == '\n')
2726 this_eol_type = CODING_EOL_LF;
2727 else if (src >= src_end || *src != '\n')
2728 this_eol_type = CODING_EOL_CR;
2723 else 2729 else
2724 return CODING_EOL_CR; 2730 this_eol_type = CODING_EOL_CRLF, src++;
2731
2732 if (eol_type == CODING_EOL_UNDECIDED)
2733 /* This is the first end-of-line. */
2734 eol_type = this_eol_type;
2735 else if (eol_type != this_eol_type)
2736 /* The found type is different from what found before.
2737 We had better not decode end-of-line. */
2738 return CODING_EOL_LF;
2725 } 2739 }
2726 } 2740 }
2727 return CODING_EOL_UNDECIDED; 2741
2742 return (total ? eol_type : CODING_EOL_UNDECIDED);
2728 } 2743 }
2729 2744
2730 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC 2745 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2731 is encoded. If it detects an appropriate format of end-of-line, it 2746 is encoded. If it detects an appropriate format of end-of-line, it
2732 sets the information in *CODING. */ 2747 sets the information in *CODING. */