-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.c
413 lines (368 loc) · 9.51 KB
/
export.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* export.c
*
* CAVE WALL - Colored Ascii Visual Editor Without Any Loathsome Limitations
* Image export routines
*
* Copyright (C) 2002 Michal Miszewski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "export.h"
#include "config.h"
#include "cursor.h"
#include "color.h"
#include "status.h"
FILE *fd;
extern char lastname[500];
int realxsize;
int realysize;
/* opens a file */
int open_file(char filename[], int mode)
{
int ret = 0;
if (mode == 1) fd = fopen(filename, "w"); else fd = fopen(filename, "a+");
if (fd == NULL) /* error message on status */
{
ret = 1;
status_error();
}
return ret;
}
/* finds the real end of line, without unimportant spaces */
int end(char *buf)
{
int last;
for (last = strlen(buf) - 1; last >= 0; last--)
if (buf[last] != ' ') break;
return last;
}
/* ansi text export */
/* sorry because of the obscure code */
void export_ansi(void)
{
int x, y, was = 0, waszero;
short color, bgnd, attrnum;
short lastcolor = EDEFCOLOR;
unsigned short i;
chtype lastbgnd = EDEFBGND, lastattr = EDEFATTR;
chtype ch;
chtype attr;
char sbuf[10];
char *buf; /* line buffer */
buf = (char *)malloc(sizeof(char) * CBUF);
if (buf == NULL)
{
status_message("Error: Can't allocate memory.");
return;
}
for (y = 0; y < realysize; y++)
{
strcpy(sbuf, "");
strcpy(buf, "");
for (x = 0; x <= realxsize; x++)
{
if (x != realxsize) ch = mvwinch(editp, y, x);
else /* we are outside the screen */
ch = ' ' | EDEFATTR | COLOR_PAIR(find_pair(EDEFCOLOR, EDEFBGND));
/* if something goes wrong */
if (pair_content(PAIR_NUMBER(ch & A_COLOR), &color, &bgnd) == ERR)
{
color = EDEFCOLOR;
bgnd = EDEFBGND; // !!! shouldn't it be chtype ???
}
attr= ch & A_ATTRONLY;
was = 0;
waszero = 0;
/* if any codes are needed */
if ((color != lastcolor) || (bgnd != lastbgnd) || (attr != lastattr))
{
sprintf(sbuf, "\x1b[");
strcat(buf, sbuf);
if (attr != lastattr) /* the attribute has changed */
{
if (lastattr != 0) /* clear all attributes before */
{ /* setting another one */
sprintf(sbuf, "0;");
strcat(buf, sbuf);
waszero = 1; /* inform that attributes were cleared */
}
for (i=1; i<NUMATTRS; i++)
{
if (attr & avals[i])
{
switch (avals[i]) /* changes curses attr names into ansi */
{
case A_BOLD: { attrnum = 1; break; }
case A_UNDERLINE: { attrnum = 4; break; }
case A_BLINK: { attrnum = 5; break; }
case A_REVERSE: { attrnum = 7; break; }
default: { attrnum = 13; } // non-existant value
}
if (attrnum!=13)
{
sprintf(sbuf, "%d;", attrnum);
strcat(buf, sbuf);
}
}
}
if ((attr == 0) || (waszero != 0))
{
/* force adding color codes by changing last colors/bgnds to
* non-existant values */
lastcolor = 20;
lastbgnd = 20;
}
was = 1;
}
if ((color != lastcolor)) /* the color has changed */
{
sprintf(sbuf, "%d;", 30 + color);
strcat(buf, sbuf);
was = 1;
}
if ((bgnd != lastbgnd)) /* the background color has changed */
{
sprintf(sbuf, "%d", 40 + bgnd);
strcat(buf, sbuf);
was = 1;
}
if (was == 1) /* the code was generated, must be ended with 'm' */
{
sprintf(sbuf, "m");
strcat(buf, sbuf);
}
}
if (x != realxsize) /* if we are not outside the screen */
{
sprintf(sbuf, "%c", (char)ch);
strcat(buf, sbuf);
}
lastcolor = color;
lastbgnd = bgnd;
lastattr = attr;
}
/* remove all unneeded spaces */
buf[end(buf)+1] = '\0';
strcpy(sbuf, "");
/* all attributes off at the end of line ( ESC[0m ) */
if ((strlen(buf) != 0) && (was != 0))
{
sprintf(sbuf, "\x1b[0m");
strcat(buf, sbuf);
}
fputs(buf, fd);
fputc('\n', fd);
lastcolor = EDEFCOLOR;
lastbgnd = EDEFBGND;
lastattr = EDEFATTR;
}
free(buf);
}
/* plain text export */
void export_txt(void)
{
int x, y;
char *buf; /* line buffer */
buf = (char *)malloc(sizeof(char) * CBUF);
if (buf == NULL)
{
status_message("Error: Can't allocate memory.");
return;
}
for (y = 0; y < realysize; y++)
{
for (x = 0; x < realxsize; x++) buf[x] = mvwinch(editp, y, x);
buf[x] = '\0';
buf[end(buf) + 1] = '\0';
fputs(buf, fd);
fputc('\n', fd);
}
free(buf);
}
/* finds real number of rows */
int findrealysize(void)
{
int x, y;
int maxy=0;
for (y=realysize-1; y>=0; y--)
{
for (x = 0; x < realxsize; x++)
if (mvwinch(editp, y, x)!=(' ' | COLOR_PAIR(find_pair(DEFCOLOR,DEFBGND)) | DEFATTR))
{
maxy=y;
x=realxsize;
y=-1;
}
}
return maxy+1;
}
/* html text export */
void export_html_css(void)
{
int x, y;
short color, bgnd;
chtype attr = EDEFATTR, lastattr = EDEFATTR;
short lastcolor = EDEFCOLOR, lastbgnd = EDEFBGND;
chtype ch;
bool firsttime=true;
char sbuf[20];
char *buf; /* line buffer */
buf = (char *)malloc(sizeof(char) * CBUF);
if (buf == NULL)
{
status_message("Error: Can't allocate memory.");
return;
}
fputs("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"cavewall.css\"/></head><body bgcolor=\"black\">", fd);
fputs("<pre>", fd);
for (y = 0; y < realysize; y++)
{
strcpy(sbuf, "");
strcpy(buf, "");
for (x = 0; x < realxsize; x++)
{
ch = mvwinch(editp, y, x);
/* if something goes wrong */
if (pair_content(PAIR_NUMBER(ch & A_COLOR), &color, &bgnd) == ERR)
{
color = EDEFCOLOR;
bgnd = EDEFBGND; // !!! shouldn't it be chtype ???
}
attr = (ch & A_ATTRONLY);
if (attr & A_REVERSE)
{
short a = color;
color = bgnd;
bgnd = a;
}
if ((color != lastcolor) || (bgnd != lastbgnd) || (attr != lastattr) || (firsttime))
{
if (firsttime!=true)
{
sprintf(sbuf, "</span><span class=");
strcat(buf, sbuf);
}
else
{
sprintf(sbuf, "<span class=");
strcat(buf, sbuf);
firsttime=false;
}
switch (bgnd)
{
case COLOR_WHITE: sprintf(sbuf, "\"b7 c"); break;
case COLOR_BLACK: sprintf(sbuf, "\"b0 c"); break;
case COLOR_BLUE: sprintf(sbuf, "\"b1 c"); break;
case COLOR_GREEN: sprintf(sbuf, "\"b2 c"); break;
case COLOR_CYAN: sprintf(sbuf, "\"b3 c"); break;
case COLOR_RED: sprintf(sbuf, "\"b4 c"); break;
case COLOR_MAGENTA: sprintf(sbuf, "\"b5 c"); break;
case COLOR_YELLOW: sprintf(sbuf, "\"b6 c"); break;
default: sprintf(sbuf, "\"b0 c"); break;
}
strcat(buf, sbuf);
if (attr & A_BOLD)
{
sprintf(sbuf, "b");
strcat(buf, sbuf);
}
switch (color)
{
case COLOR_WHITE: sprintf(sbuf, "7"); break;
case COLOR_BLACK: sprintf(sbuf, "0"); break;
case COLOR_BLUE: sprintf(sbuf, "1"); break;
case COLOR_GREEN: sprintf(sbuf, "2"); break;
case COLOR_CYAN: sprintf(sbuf, "3"); break;
case COLOR_RED: sprintf(sbuf, "4"); break;
case COLOR_MAGENTA: sprintf(sbuf, "5"); break;
case COLOR_YELLOW: sprintf(sbuf, "6"); break;
default: sprintf(sbuf, "7"); break;
}
strcat(buf, sbuf);
if (attr & A_BLINK)
{
sprintf(sbuf, " bl");
strcat(buf, sbuf);
}
if (attr & A_UNDERLINE)
{
sprintf(sbuf, " ul");
strcat(buf, sbuf);
}
sprintf(sbuf, "\">");
strcat(buf, sbuf);
lastcolor = color;
lastbgnd = bgnd;
lastattr= attr;
}
if (((char)ch != '\"') && ((char)ch!='<') && ((char)ch!='>') && ((char)ch!='&'))
sprintf(sbuf, "%c", (char)ch);
else
switch ((char)ch)
{
case '\"': sprintf(sbuf, """); break;
case '<': sprintf(sbuf, "<"); break;
case '>': sprintf(sbuf, ">"); break;
case '&': sprintf(sbuf, "&"); break;
}
strcat(buf, sbuf);
}
/* remove all unneeded spaces */
buf[end(buf)+1] = '\0';
strcpy(sbuf, "");
fputs(buf, fd);
fputs("<br>", fd);
}
fputs("</pre>", fd);
fputs("</body></html>", fd);
free(buf);
}
void export(void)
{
char filename[200] = "";
char realname[200] = "";
int format, a, wr = 1;
realxsize=truexsize;
realysize=trueysize;
realysize=findrealysize();
format = status_question("Image export. Which format", "ANSI Text HTML Cancel", 1);
if (format != 4) /* check this after adding new filters! */
{
status_input("File name", lastname, filename, 60);
if (strncmp(filename, "", 200) != 0)
{
for (a = 0; a < strlen(filename); a++)
if (filename[a] == '~')
{
status_message("~ not allowed; enter full pathname.");
return;
}
realpath(filename, realname);
if (access(realname, F_OK) == 0)
wr = status_question("File Exists. Overwrite", "Yes No Append", 1);
if (wr != 2)
{
if (open_file(realname, wr) == 0)
{
cursor_up();
switch (format)
{
case 1: { export_ansi(); break; }
case 2: { export_txt(); break; }
case 3: { export_html_css(); break; }
}
fclose(fd);
strcpy(lastname, filename);
cursor_down();
}
}
}
}
}