Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
NEWLINE.cpp
Go to the documentation of this file.
1// checking for newline (CR or CR-LF)
2
3
25#include <stdio.h>
26#include <stddef.h>
27
28
29int main ()
30{
31 // determine the byte sequence that corresponds to the '\n'
32 // source character, i.e., one of
33 // <CR> -- Apple II and Mac OS
34 // <LF> -- UNIX systems
35 // <CR><LF> -- CP/M, DOS, Windows
36 // <LF><CR> -- unknown
37
38 char fname [256];
39 if (0 == tmpnam (fname))
40 return 1;
41
42 FILE *fp = fopen (fname, "w");
43 if (0 == fp)
44 return 2;
45
46 fprintf (fp, "%s", "a\nb");
47 fclose (fp);
48
49 fp = fopen (fname, "rb");
50 if (0 == fp)
51 return 3;
52
53 char buffer [32] = "";
54
55 size_t nbytes = fread (buffer, 1, sizeof buffer, fp);
56
57 fclose (fp);
58 remove (fname);
59
60 char *pc;
61
62 for (pc = buffer; *pc && *pc != '\n'; ++pc);
63
64 if ('\0' == *pc) {
65
66 for (pc = buffer; *pc && *pc != '\r'; ++pc);
67
68 if ('\0' == *pc) {
69
70#if 'A' == '\x41' // ASCII
71
72 // no newline?
73 return 4;
74
75
76#elif 'A' == '\xc1' // EBCDIC
77
78
79 for (pc = buffer; *pc && *pc != '\x15'; ++pc);
80
81 if ('\0' == *pc) {
82 // no newline?
83 return 4;
84 }
85
86 printf ("#define _RWSTD_NEWLINE \"\\x15\"\n");
87 printf ("#define _RWSTD_NEWLINE_NEL\n");
88
89#endif // ASCII or EBCDIC
90
91 }
92 else {
93
94 printf ("#define _RWSTD_NEWLINE \"\\r\"\n");
95 printf ("#define _RWSTD_NEWLINE_CR\n");
96 }
97 }
98 else {
99 if ('\r' == pc [1]) {
100 printf ("#define _RWSTD_NEWLINE \"\\n\\r\"\n");
101 printf ("#define _RWSTD_NEWLINE_LF_CR\n");
102 }
103 if ('\r' == pc [-1]) {
104 printf ("#define _RWSTD_NEWLINE \"\\r\\n\"\n");
105 printf ("#define _RWSTD_NEWLINE_CR_LF\n");
106 }
107 else {
108 printf ("#define _RWSTD_NEWLINE \"\\n\"\n");
109 printf ("#define _RWSTD_NEWLINE_LF\n");
110 }
111 }
112
113 return 0;
114}
#define buffer
Definition 17.names.cpp:453
IMPORT_C SInt32 printf(const Char *,...)
int main()
Definition NEWLINE.cpp:29
#define FILE