* \e$B8=:_!"\e(Bnkf \e$B$O\e(B SorceForge \e$B$K$F%a%s%F%J%s%9$,B3$1$i$l$F$$$^$9!#\e(B
* http://sourceforge.jp/projects/nkf/
***********************************************************************/
-/* $Id: nkf.c,v 1.137 2007/10/01 14:29:21 naruse Exp $ */
+/* $Id: nkf.c,v 1.138 2007/10/01 19:55:25 naruse Exp $ */
#define NKF_VERSION "2.0.8"
#define NKF_RELEASE_DATE "2007-10-01"
#define COPY_RIGHT \
static char *get_backup_filename(const char *suffix, const char *filename);
#endif
-static int nlmode_f = 0; /* CR, LF, CRLF */
-static nkf_char prev_cr = 0;
+static int nlmode_f = 0; /* CR, LF, CRLF */
+static int input_nextline = 0; /* 0: unestablished, EOF: MIXED */
+static nkf_char prev_cr = 0; /* CR or 0 */
#ifdef EASYWIN /*Easy Win */
static int end_check;
#endif /*Easy Win */
/* base64_count = 0; */
}
- if (nlmode_f) {
+ if (nlmode_f || guess_f) {
o_nlconv = oconv; oconv = nl_conv;
}
if (rot_f) {
SEND;
}
}
- if (!nlmode_f) {
- if (prev_cr && c1 == LF) nlmode_f = CRLF;
- else nlmode_f = c1;
- }
} else if (c1 == DEL && input_mode == X0208) {
/* CP5022x */
c2 = c1;
void nl_conv(nkf_char c2, nkf_char c1)
{
- if (prev_cr) {
+ if (guess_f && input_nextline != EOF) {
+ if (c2 == 0 && c1 == LF) {
+ if (!input_nextline) input_nextline = prev_cr ? CRLF : LF;
+ else if (input_nextline != (prev_cr ? CRLF : LF)) input_nextline = EOF;
+ } else if (c2 == 0 && c1 == CR && input_nextline == LF) input_nextline = EOF;
+ else if (!prev_cr);
+ else if (!input_nextline) input_nextline = CR;
+ else if (input_nextline != CR) input_nextline = EOF;
+ }
+ if (prev_cr || c2 == 0 && c1 == LF) {
prev_cr = 0;
- if (! (c2==0&&c1==LF)) {
- nl_conv(0,LF);
- }
- }
- if (c2) {
- (*o_nlconv)(c2,c1);
- } else if (c1==CR) {
- prev_cr = c1;
- } else if (c1==LF) {
- if (nlmode_f==CRLF) {
- (*o_nlconv)(0,CR);
- } else if (nlmode_f==CR) {
- (*o_nlconv)(0,CR);
- return;
- }
- (*o_nlconv)(0,LF);
- } else if (c1!='\032' || nlmode_f!=LF){
- (*o_nlconv)(c2,c1);
+ if (nlmode_f != LF) (*o_nlconv)(0, CR);
+ if (nlmode_f != CR) (*o_nlconv)(0, LF);
}
+ if (c2 == 0 && c1 == CR) prev_cr = CR;
+ else if (c2 != 0 || c1 != LF) (*o_nlconv)(c2, c1);
}
/*
{
char *codename = "BINARY";
char *str_nlmode = NULL;
- if (!input_codename || *input_codename) {
- if (!input_codename) {
- codename = "ASCII";
- } else {
- codename = input_codename;
- }
- if (nlmode_f == CR) str_nlmode = "CR";
- else if (nlmode_f == LF) str_nlmode = "LF";
- else if (nlmode_f == CRLF) str_nlmode = "CRLF";
- else if (nlmode_f == EOF) str_nlmode = "MIXED NL";
- }
if (filename != NULL) printf("%s: ", filename);
- if (str_nlmode != NULL) printf("%s (%s)\n", codename, str_nlmode);
- else printf("%s\n", codename);
+ if (input_codename && !*input_codename) {
+ printf("BINARY\n");
+ } else {
+ printf("%s%s\n",
+ (input_codename ? input_codename : "ASCII"),
+ input_nextline == CR ? " (CR)" :
+ input_nextline == LF ? " (LF)" :
+ input_nextline == CRLF ? " (CRLF)" :
+ input_nextline == EOF ? " (MIXED NL)" :
+ "");
+ }
}
#endif /*WIN32DLL*/
#
# nkf test program for nkf-2
#
-# $Id: nkf_test.pl,v 1.19 2007/09/12 04:56:53 naruse Exp $
+# $Id: nkf_test.pl,v 1.20 2007/10/01 19:55:25 naruse Exp $
#
# Shinji KONO <kono@ie.u-ryukyu.ac.jp>
# Sun Aug 18 12:25:40 JST 1996
printf "%-40s", "test_data/bugs10904";
&test("$nkf -Mj",$example{'test_data/bugs10904'},$example{'test_data/bugs10904.ans'});
-# end
+printf "%-40s", "Guess NL/NONE"; &test("$nkf --guess","none", "ASCII\n");
+printf "%-40s", "Guess NL/LF"; &test("$nkf --guess","\n", "ASCII (LF)\n");
+printf "%-40s", "Guess NL/LFLF"; &test("$nkf --guess","\n\n", "ASCII (LF)\n");
+printf "%-40s", "Guess NL/LFCR"; &test("$nkf --guess","\n\r", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/LFCRLF"; &test("$nkf --guess","\n\r\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/LF.LF"; &test("$nkf --guess","\n.\n", "ASCII (LF)\n");
+printf "%-40s", "Guess NL/LF.CR"; &test("$nkf --guess","\n.\r", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/LF.CRLF"; &test("$nkf --guess","\n.\r\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CR"; &test("$nkf --guess","\r", "ASCII (CR)\n");
+printf "%-40s", "Guess NL/CRCR"; &test("$nkf --guess","\r\r", "ASCII (CR)\n");
+printf "%-40s", "Guess NL/CRCRLF"; &test("$nkf --guess","\r\r\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CR.LF"; &test("$nkf --guess","\r.\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CR.CR"; &test("$nkf --guess","\r.\r", "ASCII (CR)\n");
+printf "%-40s", "Guess NL/CR.CRLF"; &test("$nkf --guess","\r.\r\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLF"; &test("$nkf --guess","\r\n", "ASCII (CRLF)\n");
+printf "%-40s", "Guess NL/CRLFLF"; &test("$nkf --guess","\r\n\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLFCR"; &test("$nkf --guess","\r\n\r", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLFCRLF"; &test("$nkf --guess","\r\n\r\n", "ASCII (CRLF)\n");
+printf "%-40s", "Guess NL/CRLF.LF"; &test("$nkf --guess","\r\n.\n", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLF.CR"; &test("$nkf --guess","\r\n.\r", "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLF.CRLF"; &test("$nkf --guess","\r\n.\r\n", "ASCII (CRLF)\n");
+# end