forked from Francesco149/TempleOSGit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport.CPP
executable file
·640 lines (608 loc) · 17 KB
/
Export.CPP
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//This is NOT Terry's work. I wrote this program to export historical
//versions of TempleOS to git.
//The goal is to obtain something easily browsable in a normal text editor
//and easy to diff in git while not loosing too much information. Docs are
//converted to plaintext but retain links and a few decorations, binary data
//is dumped to a DAT file for docs (embedded as hex strings in code in
//old versions).
//As an added bonus, I try to match each iteration's coding style.
//This code is public domain.
OptOn(OPTf_WARN_PAREN);
//Just in case the filesystem gets corrupted while I develop it
U0 BackupExport()
{
Copy("D:/Export.CPP","C:/Export.CPP");
Copy("D:/Export.CPP","D:/ExportB.CPP");
Copy("D:/Export.CPP","C:/ExportB.CPP");
}
//Appears to hang on this version...
//BackupExport
#define EXPORT_W 78
U0 ExportBinary(U8 *src_file,CDocBin *b)
{
I64 len;
U8 *buf,*buf1;
len=StrLen(src_file);
buf1=MAlloc(len+1);
StrCpy(buf1,src_file);
buf=buf1+len-1;
while (buf>buf1 && *buf!='.')
buf--;
*buf=0;
buf=MStrPrint("%s%d.DAT",buf1,b->num);
Free(buf1);
"%s\n",buf;
FileWrite(buf,b->data,b->size);
Free(buf);
}
U8 *ResolveLink(U8 *link_st)
{ //Adapted from [C:/Adam/DolDoc/DocMain.CPP.Z,3] Ed. Resolves stuff like MN:Func to the file
U8 *res,*filename=NULL,*pattern=NULL;
I64 num;
if (EdLinkCvt(link_st,&filename,&pattern,&num,0)==-1)
res=StrNew(link_st);
else if (num!=0)
res=MStrPrint("%s,%d",filename,num);
else
res=MStrPrint("%s,%s",filename,pattern);
Free(filename);
Free(pattern);
return res;
}
U8 *IndentString(I64 n)
{
U8 *s=MAlloc(n+1);
MemSet(s,' ',n);
s[n]=0;
return s;
}
U8 *StrAdd(U8 *base,U8 *addition) {
I64 base_len=StrLen(base);
I64 addition_len=StrLen(addition);
I64 required=base_len+addition_len+1;
I64 base_size=MSize(base);
U8 *res=base;
if (base_size<=required) {
while (base_size<=required)
base_size*=2;
res=MAlloc(base_size);
MemCpy(res,base,base_len);
Free(base);
}
MemCpy(res+base_len,addition,addition_len+1);
return res;
}
U8 *Wrap(U8 *base,U8 *s,I64 *line_len,I64 indent=0)
{ //Performs word wrapping on base, starting from s, line_len is state.
//Also adds indent spaces to every soft and hard newline.
//Returns new value for base.
//This is supposed to also take tabs into account but fuck that shit
//NOTE: at this point I could use [MN:SetWinHorz] SetWinHorz and append to a CDoc
I64 len;
U8 *p,*buf;
U8 *end;
U8 *lbstring;
I64 lbstring_len;
lbstring=MAlloc(indent+2);
lbstring[0]='\n';
MemSet(lbstring+1,' ',indent);
lbstring[indent+1]=0;
lbstring_len=indent+1;
s-=*line_len;
end=s+EXPORT_W;
while (StrLen(s)>EXPORT_W) {
for (p=s;*p && p<end;p++) {
if (*p=='\n') {
buf=StrNew(p+1);
if(p>s && *(p-1)=='\r')
p--;
*p=0;
len=p-base;
base=StrAdd(base,lbstring);
base=StrAdd(base,buf);
Free(buf);
p=base+len+lbstring_len-1;
s=p+1;
*line_len=0;
end=s+EXPORT_W;
}
}
if (StrLen(s)<=EXPORT_W)
break;
for (p=s+EXPORT_W-1;p>s;p--) {
if (*p==' ')
break;
}
if (*p!=' ') {
//Truncate words longer than a line
while (StrLen(s)>EXPORT_W) {
buf=StrNew(s+EXPORT_W);
s[EXPORT_W]=0;
len=s-base;
base=StrAdd(base,lbstring);
base=StrAdd(base,buf);
Free(buf);
s=base+len+EXPORT_W+lbstring_len;
*line_len=0;
}
break;
}
buf=StrNew(p+1);
*p=0;
len=p-base;
base=StrAdd(base,lbstring);
base=StrAdd(base,buf);
Free(buf);
*line_len=0;
s=base+len+lbstring_len;
}
*line_len=StrLen(s);
Free(lbstring);
return base;
}
U8 *StrToASCII(U8 *s)
{ //Convert non-standard ASCII
U8 *ext,*buf,*buf1;
I64 len;
for (buf=s;*buf;buf++) {
switch (*buf) {
case CH_SHIFT_SPACE: ext=" "; break; //Special space, not conv. to tabs
case CH_CURSOR: ext=""; break; //Cursor position
case 'pi': ext="pi"; break;
case 'inf': ext="inf"; break;
case 'theta': ext="theta"; break;
case 'phi': ext="phi"; break;
case 'omega': ext="omega"; break;
case '-': ext="-"; break;
case 'u':
case 'u':
case 'u':
case 'u':
case 'u':
case 'u': ext="u"; break;
case 'alpha': ext="alpha"; break;
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+': ext="+"; break;
case '-': ext="-"; break;
case '|':
case '|': ext="|"; break;
case '_': ext="_"; break;
case 'a':
case 'a':
case 'a':
case 'a': ext="a"; break;
case 'c':
case 'c':
case 'c': ext="c"; break;
case 'e':
case 'e':
case 'e':
case 'e': ext="e"; break;
case 'i':
case 'i':
case 'i':
case 'i': ext="i"; break;
case 'a':
case 'a':
case 'a':
case 'a': ext="a"; break;
case 'E': ext="E"; break;
case 'ae': ext="ae"; break;
case 'AE': ext="AE"; break;
case 'o':
case 'o':
case 'o':
case 'o':
case 'o':
case 'o': ext="o"; break;
case 'y': ext="y"; break;
case 'pounds': ext="pounds"; break;
case 'yen': ext="yen"; break;
case 'Pt': ext="Pt"; break;
case 'f': ext="f"; break;
case 'n':
case 'n': ext="n"; break;
case '?': ext="?"; break;
case ''':
case ''': ext="'"; break;
case '1/2': ext="1/2"; break;
case '1/4': ext="1/4"; break;
case '!': ext="!"; break;
case '<<': ext="<<"; break;
case '>>': ext=">>"; break;
case '.': ext="."; break;
case ':': ext=":"; break;
case '#': ext="#"; break;
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+':
case '+': ext="+"; break;
case '-':
case '-': ext="-"; break;
case '|':
case '|': ext="|"; break;
case 'beta': ext="beta"; break;
case 'gamma': ext="gamma"; break;
case 'SIGMA': ext="SIGMA"; break;
case 'sigma': ext="sigma"; break;
case 'upsilon': ext="upsilon"; break;
case 'delta': ext="delta"; break;
case 'phi': ext="phi"; break;
case 'ei': ext="ei"; break;
case 'eta': ext="eta"; break;
case '===': ext="==="; break;
case '+-': ext="+-"; break;
case '>=': ext=">="; break;
case '>=': ext-"<="; break;
case 'integral_top': ext="integral_top"; break;
case 'integral_bot': ext="integral_bot"; break;
case '/': ext="/"; break;
case '~=': ext="~="; break;
case 'degrees': ext="degrees"; break;
case '.':
case '.': ext="."; break;
case 'sqrt': ext="sqrt"; break;
case '^n': ext="^n"; break;
case '^2': ext="^2"; break;
case '.': ext="."; break;
case 255: ext="\\xFF"; break; //Only used in the extended chars demo
default:
if (*buf&0x80)
"%s: extended ASCII char '%c'\n",ST_WARN_ST,*buf;
goto next_char;
}
if (StrLen(ext)!=1) {
buf1=StrNew(buf+1);
*buf=0;
s=StrAdd(s,ext);
len=StrLen(s)-1;
s=StrAdd(s,buf1);
buf=s+len;
Free(buf1);
} else
*buf=*ext;
next_char:
}
return s;
}
U0 FilePassThrough(U8 *in_path,U8 *out_path)
{
I64 len;
U8 *out=FileRead(in_path,&len);
FileWrite(out_path,out,len);
Free(out);
}
Bool StrEqAny(U8 *s,...)
{ //Returns TRUE if any of the strings match s
I64 i;
for (i=0;i<argc;i++) {
if (!StrCmp(s,argv[i]))
return TRUE;
}
return FALSE;
}
U8 *StrMatchAny(U8 *s,...)
{ //Returns a pointer to the first substring matched
U8 *res;
I64 i;
for (i=0;i<argc;i++) {
if (res=StrMatch(argv[i],s))
break;
}
return res;
}
U8 *UnZExtension(U8 *out_path)
{ //Changes out_path to a non-compressed extension, returns ptr to extension.
//See [C:/Doc/Glossary.TXT.Z,1] Filename Extention Types.
U8 *ext=FileExtDot(out_path)+1;
if (StrEqAny(ext,"TXT.Z","LOG.Z","CPP.Z","HPP.Z","PRJ.Z","MAP.Z","DAT.Z",
"BIN.Z","AUT.Z","GRA.Z"))
ext[3]=0;
else if (IsDotZ(out_path))
ext[4]='C';
return ext;
}
//All these string operations are crazy slow because I don't preallocate mem
//but it's good enough for now.
//This whole thing should've been designed to do one big allocation and be
//done with it but it's too late now.
U0 ExportSingle(U8 *in_path,U8 *out_path,Bool debug=FALSE)
{
CDoc *l;
CDocEntry *cl;
CDocBin *b;
U8 *out,*buf,*ext,*lnk;
I64 len,flags=0;
Bool is_code;
I64 indent=0,line_len=0,prev_len=0;
U8 *indents=StrNew("");
//Windows stuff should just be passed through with the original extension
if (StrMatchAny(out_path,"/Windows/","/Linux/")) {
FilePassThrough(in_path,out_path);
return;
}
ext=UnZExtension(out_path);
if (StrEqAny(ext,"BIN.C","BIN","DAT","GRA","ISO","BMP")) {
FilePassThrough(in_path,out_path);
return;
}
for (buf=ext;buf>out_path && *buf!='/';buf--);
buf++;
if (StrEqAny(buf,"Bible.TXT","PCIDevices.TXT")) {
//Special case: I know these large files don't have any Doc syntax so no point
//in wasting time parsing them
//NOTE: PCIDevices contains non-ASCII but they shouldn't be converted because
//it's not TempleOS extended ASCII, it was probably created in Linux/Windows
FilePassThrough(in_path,out_path);
return;
}
l=DocRead(in_path);
if (!l) {
FilePassThrough(in_path,out_path);
return;
}
is_code=StrEqAny(ext,"CPP","HPP","PRJ","AUT");
out=StrNew("");
for (b=l->bin_root.next;b!=&l->bin_root;b=b->next) {
if (StrCmp(ext,"MAP"))
ExportBinary(out_path,b);
//MP files are symbol maps and the binary data is just debug info (line nums)
//so there's no need to create hundreds of bin files just for the line nums
}
if (*out)
out=StrAdd(out,"\n");
for (cl=l->root.next;cl!=l;cl=cl->next) {
switch (cl->type_u8) {
case DOCT_ERROR:
case DOCT_TEXT:
case DOCT_ANCHOR:
case DOCT_TREE: //Trees text includes the +] at the beginning
case DOCT_BUTTON:
if (!cl->tag)
break;
if (cl->de_flags & DOCEF_CENTER_X)
buf=IndentString((EXPORT_W-StrLen(cl->tag))/2);
else if (cl->de_flags & DOCEF_RIGHT_X)
buf=IndentString(EXPORT_W-StrLen(cl->tag));
else
buf=NULL;
if (buf) {
//This should actually go back to the beginning of the line but having text
//before a justify tag actually breaks even in J OS itself so I assume there's
//no invalid usage anywhere
out=StrAdd(out,buf);
Free(buf);
}
if (debug) {
//CL breaks everything and the file is truncated when you open it
len=StrLen(out);
out=StrAdd(out,cl->tag);
for (buf=out+len;*buf;buf++) {
if (*buf=='$') *buf='_';
}
} else
out=StrAdd(out,cl->tag);
break;
case DOCT_INS_BINARY_SIZE:
if (is_code)
buf=MStrPrint("%s__BIN_%d_SIZE",out,cl->bin_data->num);
else
buf=MStrPrint("%s%d",out,cl->bin_data->size);
Free(out); out=buf;
break;
case DOCT_INS_BINARY:
if (is_code) {
buf=MStrPrint("%s__BIN_%d",out,cl->bin_data->num);
Free(out); out=buf;
} else
out=StrAdd(out,"(binary data)");
break;
case DOCT_SPRITE:
if (is_code)
buf=MStrPrint("%s/* %s <%d> (image) */",
out,cl->tag,cl->bin_data->num);
else
buf=MStrPrint("%s%s <%d> (image)",out,cl->tag,
cl->bin_data->num);
Free(out); out=buf;
break;
case DOCT_MACRO:
if (cl->tag)
out=StrAdd(out,cl->tag);
else
out=StrAdd(out,"(macro)");
out=StrAdd(out,"\n");
if (cl->de_flags & DOCEF_RIGHT_MACRO) {
buf=MStrPrint("%s`%s`\n",out,cl->right_macro);
Free(out); out=buf;
}
if (cl->de_flags & DOCEF_LEFT_MACRO) {
buf=MStrPrint("%s`%s`\n",out,cl->left_macro);
Free(out); out=buf;
}
break;
case DOCT_LINK:
if (cl->aux_str) {
lnk=ResolveLink(cl->aux_str);
buf=MStrPrint("%s[%s] %s",out,lnk,cl->tag);
} else {
lnk=ResolveLink(cl->tag);
buf=MStrPrint("%s[%s]",out,lnk);
}
Free(lnk);
Free(out); out=buf;
break;
case DOCT_SONG: //This is used to autoplay songs in documents
out=StrAdd(out,cl->tag);
out=StrAdd(out," ");
out=StrAdd(out,cl->aux_str);
break;
case DOCT_NEW_LINE:
case DOCT_SOFT_NEW_LINE:
out=StrAdd(out,"\n");
prev_len++;
if (!(flags & DOCF_WORD_WRAP)) {
line_len=indent;
out=StrAdd(out,indents);
} else
line_len=0; //Wrap will take care of it
break;
case DOCT_TAB:
out=StrAdd(out,"\t");
break;
case DOCT_BLINK:
out=StrAdd(out,"**");
break;
case DOCT_UNDERLINE:
out=StrAdd(out,"_");
break;
case DOCT_WORD_WRAP:
if (cl->attr)
flags|=DOCF_WORD_WRAP;
else
flags&=~DOCF_WORD_WRAP;
break;
case DOCT_INDENT:
indent+=cl->attr;
//This is tricky - an indent tag effectively moves the following text, even
//if there's something before it, so if you have foo$ID,2$bar it will display
//as fbar. to handle this I'm just gonna go back to the beginning of the line
//and overwrite. Not quite the same behaviour but close enough.
//We also need to re-wrap the line.
for (buf=out+StrLen(out)-1;*buf>out && *buf!='\n';buf--)
line_len++;
buf[1]=0;
Free(indents); indents=IndentString(indent);
out=StrAdd(out,indents);
break;
case DOCT_HTML_CODE:
out=StrAdd(out,"```\n");
out=StrAdd(out,cl->tag);
out=StrAdd(out,"\n```");
break;
case DOCT_PMT: //Marks end of C:/User/Prompt> so you can't erase it
case DOCT_FOREGROUND:
case DOCT_BACKGROUND:
case DOCT_DFT_FOREGROUND:
case DOCT_DFT_BACKGROUND:
case DOCT_LEFT_MARGIN:
case DOCT_RIGHT_MARGIN:
case DOCT_CURSOR_MOVEMENT:
case DOCT_SHIFTED_X:
case DOCT_SHIFTED_Y:
case DOCT_CURSOR:
case DOCT_INVERT:
case DOCT_HEADER:
case DOCT_FOOTER:
case DOCT_PAGE_BREAK:
case DOCT_HIGHLIGHT:
case DOCT_PAGE_LEN:
break;
default:
buf=MStrPrint("%s /* Unknown token: %d */ ",out,cl->type_u8);
Free(out); out=buf;
"%s: Unknown token: %d\n",ST_WARN_ST,cl->type_u8;
break;
}
if (flags & DOCF_WORD_WRAP)
out=Wrap(out,out+prev_len,&line_len,indent);
prev_len=StrLen(out);
}
//Special case: this file just contains garbage because there's a hex view
if (!StrMatch("/Demo/MultiCore/RadixSort.CPP",out_path))
out=StrToASCII(out);
//Terry uses a mix of tabs and spaces all over the place and converting tabs
//to 8 spaces is not good enough because some formatting relies on tabs align
//so I'm just gonna leave it
FileWrite(out_path,out,StrLen(out));
Free(out);
DocDel(l);
Free(indents);
}
//Adapted from [C:/Adam/Dsk/ADskA.CPP.Z,135] CopyTree2 which is internally used by [C:/Adam/Dsk/ADskA.CPP.Z,154] CopyTree
U0 PutExportTree(CDirEntry *ent,I64 src_dir_len,I64 dst_dir_len,
U8 *dst_dir,Bool debug=FALSE)
{
U8 *path;
while (ent) {
path=MAlloc(dst_dir_len+StrLen(ent->full_name)+1);
MemCpy(path,dst_dir,dst_dir_len);
path[dst_dir_len]='/';
StrCpy(path+dst_dir_len+1,ent->full_name+src_dir_len);
"%s\n",path;
if (ent->attr & RS_ATTR_DIR) {
MkDir(path);
if (ent->sub)
PutExportTree(ent->sub,src_dir_len,dst_dir_len,dst_dir,debug);
} else
ExportSingle(ent->full_name,path,debug);
Free(path);
ent=ent->next;
}
}
//Adapted from [C:/Adam/Dsk/ADskA.CPP.Z,154] CopyTree
U0 ExportTreeImpl(U8 *src_mask="T:/*",U8 *dst_mask="D:/Git",
Bool debug=FALSE)
{
CFileAccess *fa;
CDirEntry *ent;
I64 dst_dir_len,src_dir_len;
U8 *dst_dir,*src_dir;
fa=FileAccessNew(src_mask,TRUE);
if (fa) {
ent=SysFilesFind(fa->mask,1<<FUf_RECURSE);
src_dir=CurDir;
src_dir_len=StrLen(src_dir);
Free(src_dir);
FileAccessDel(fa);
fa=FileAccessNew(dst_mask,TRUE,TRUE);
if (fa) {
dst_dir=CurDir;
dst_dir_len=StrLen(dst_dir);
PutExportTree(ent,src_dir_len,dst_dir_len,dst_dir,debug);
FileAccessDel(fa);
Free(dst_dir);
}
DirTreeDel(ent);
}
}
//This is pretty neat - instead of hogging the current window and making it
//change dir, we spawn a new terminal and programmatically send keystrokes
//to start the export.
U0 ExportTree() {
CTask *task=User("#include \"D:/Export.CPP\"
ExportTreeImpl;
ExportSingle(\"D:/Export.CPP\",\"D:/Git/Export.CPP\");
");
WinToTop(task);
}