OSDN Git Service

ヘルプメッセージのファイル名を修正
[beyond-jp/txtwrite.git] / txtwrite.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <fcntl.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6
7 #ifndef O_BINARY
8 #       define O_BINARY 0
9 #endif
10 #define WIDTH   80
11
12 typedef unsigned char   uint8;
13
14 static int readRecord(int fd, int max, char *text)
15 {
16         uint8 len;
17
18         read(fd, &len, 1);
19         read(fd, text, max);
20
21         memset(text + len, 0, max - len);
22         return len;
23 }
24
25 static void printRecord(const char *text, int len)
26 {
27         const char *p;
28
29         if(len <= 0)
30                 return;
31
32         printf("%.*s", WIDTH, text);
33
34         for(p = text + WIDTH; p < text + len; p += WIDTH)
35                 printf("\t%.*s", WIDTH, p);
36 }
37
38 static int decode(const char *file1, const char *file2)
39 {
40         int fd1 = -1, fd2 = -1, len;
41         char text[256];
42
43         if((fd1 = open(file1, O_RDONLY | O_BINARY)) < 0) {
44                 fprintf(stderr, "CANNOT OPEN %s\n", file1);
45                 return 1;
46         }
47
48         if(file2 == NULL) { /* LINE */
49                 while(!eof(fd1)) {
50                         len = readRecord(fd1, 80, text);
51                         printRecord(text, len);
52                         printf("\n");
53                 }
54         } else { /* ROOMS, SPECIAL */
55                 if((fd2 = open(file2, O_RDONLY | O_BINARY)) < 0) {
56                         fprintf(stderr, "CANNOT OPEN %s\n", file2);
57                         return 1;
58                 }
59
60                 while(!eof(fd1) && !eof(fd2)) {
61                         len = readRecord(fd1, 240, text);
62                         printRecord(text, len);
63                         if((len = readRecord(fd2, 240, text)) > 0) {
64                                 printf("\t");
65                                 printRecord(text, len);
66                         }
67                         printf("\n");
68                 }
69         }
70
71         if(fd1 > 0)
72                 close(fd1);
73         if(fd2 > 0)
74                 close(fd2);
75         return 0;
76 }
77
78 static int removeTab(char *text)
79 {
80         int len = strlen(text);
81         char *p;
82
83         if(text[len - 1] == '\r' || text[len - 1] == '\n') {
84                 len--;
85                 text[len] = 0;
86         }
87
88         for(p = text + WIDTH; p < text + strlen(text); p += WIDTH) {
89                 if(*p != '\t') {
90                         fprintf(stderr, "WRONG FORMAT\n");
91                         return 0;
92                 }
93                 memmove(p, p + 1, strlen(p) + 1);
94         }
95         return 1;
96 }
97
98 void writeRecord(int fd1, int fd2, const char *text)
99 {
100         int len = strlen(text);
101         const char *p = text;
102         uint8 len8;
103         char buf[1024];
104
105         memset(buf, ' ', sizeof(buf));
106         memcpy(buf, text, len);
107
108         if(fd2 > 0) {
109                 if(len > 240)
110                         len8 = 240;
111                 else
112                         len8 = len;
113                 write(fd1, &len8, 1);
114                 write(fd1, buf, 240);
115
116                 if(len > 240)
117                         len -= 240;
118                 else
119                         len = 0;
120                 len8 = len;
121                 write(fd2, &len8, 1);
122                 write(fd2, buf + 240, 240);
123         
124         } else {
125                 len8 = len;
126
127                 write(fd1, &len8, 1);
128                 write(fd1, buf, 80);
129         }
130 }
131
132 static int encode(const char *file1, const char *file2)
133 {
134         int fd1 = -1, fd2 = -1;
135         char text[1024];
136
137         if((fd1 = open(file1, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE)) < 0) {
138                 fprintf(stderr, "CANNOT OPEN %s\n", file1);
139                 return 1;
140         }
141         if(file2 != NULL) {     
142                 if((fd2 = open(file2, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE)) < 0) {
143                         fprintf(stderr, "CANNOT OPEN %s\n", file2);
144                         return 1;
145                 }
146         }
147
148         while(fgets(text, sizeof(text), stdin) != NULL) {
149                 if(!removeTab(text))
150                         return 1;
151                 writeRecord(fd1, fd2, text);
152         }
153
154         if(fd1 > 0)
155                 close(fd1);
156         if(fd2 > 0)
157                 close(fd2);
158         return 0;
159 }
160
161 int main(int argc, char *argv[])
162 {
163         if(argc <= 1) {
164                 fprintf(stderr, "usage:\ntxtwrite -d ROOMS1 ROOMS2 > rooms.txt\n");
165                 fprintf(stderr, "txtwrite -d SPECIAL1 SPECIAL2 > special.txt\n");
166                 fprintf(stderr, "txtwrite -d LINE > line.txt\n");
167                 fprintf(stderr, "txtwrite -e ROOMS1 ROOMS2 < rooms.txt\n");
168                 fprintf(stderr, "txtwrite -e SPECIAL1 SPECIAL2 < special.txt\n");
169                 fprintf(stderr, "txtwrite -e LINE < line.txt\n");
170                 return 0;
171         }
172
173         if(stricmp(argv[1], "-d") == 0) {
174                 if(argc == 3)
175                         return decode(argv[2], NULL);
176                 else if(argc == 4)
177                         return decode(argv[2], argv[3]);
178         } else if(stricmp(argv[1], "-e") == 0) {
179                 if(argc == 3)
180                         return encode(argv[2], NULL);
181                 else if(argc == 4)
182                         return encode(argv[2], argv[3]);
183         }
184
185         fprintf(stderr, "ERROR\n");
186         return 1;
187 }
188
189 /* eof */