summaryrefslogtreecommitdiffstats
path: root/Diaspora/Main.cpp
blob: 7de2bfc685ce879a213873295a9251224da701f1 (plain) (blame)
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Main.h"
#include <versekey.h>
#include <gbfthml.h>
#include <latin1utf8.h>
#include <thmlgbf.h>
#include <thmlolb.h>
#include <thmlplain.h>
#include <localemgr.h>

#include <rawtext.h>
#include <rawcom.h>
#include <ztext.h>
#include <zcom.h>
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TMainForm *MainForm;

VerseKey* getVerseBounds (SWModule* mod, VerseKey* key) {
        zCom* zcom;
        RawCom* rawcom;
        zText* ztext;
        RawText* rawtext;

        key->LowerBound(mod->Key());
        key->UpperBound(mod->Key());

        char testament = ((VerseKey*)&mod->Key())->Testament();
        unsigned short otherIndex, index = ((VerseKey*)&mod->Key())->Index();

        long origStart, otherStart;
        unsigned short origSize, otherSize;

        zcom = dynamic_cast<zCom*>(mod);
        if (zcom) {

        zcom->findoffset(testament, index, &origStart, &origSize);
/*
        //work backwards to find first verse outside our block
        otherIndex = index - 1;
        zcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize) {
                otherIndex--;
                zcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->LowerBound() -= (index - otherIndex - 1);
*/
        //work forwards to find first verse outside our block
        otherIndex = index + 1;
        zcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize && index != otherIndex) {
                otherIndex++;
                zcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->UpperBound() += (otherIndex - index - 1);

        }
        else {
                ztext = dynamic_cast<zText*>(mod);
                if (ztext) {

        ztext->findoffset(testament, index, &origStart, &origSize);
/*
        //work backwards to find first verse outside our block
        otherIndex = index - 1;
        ztext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (otherStart == origStart && otherSize == origSize) {
                otherIndex--;
                ztext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->LowerBound() -= (index - otherIndex - 1);
*/
        //work forwards to find first verse outside our block
        otherIndex = index + 1;
        ztext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize && index != otherIndex) {
                otherIndex++;
                ztext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->UpperBound() += (otherIndex - index - 1);


                }
                else {
                        rawcom = dynamic_cast<RawCom*>(mod);
                        if (rawcom) {

        rawcom->findoffset(testament, index, &origStart, &origSize);
/*
        //work backwards to find first verse outside our block
        otherIndex = index - 1;
        rawcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize) {
                otherIndex--;
                rawcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->LowerBound() -= (index - otherIndex - 1);
*/
        //work forwards to find first verse outside our block
        otherIndex = index + 1;
        rawcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize) {
                otherIndex++;
                rawcom->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->UpperBound() += (otherIndex - index - 1);

                        }
                        else {
                                rawtext = dynamic_cast<RawText*>(mod);
                                if (rawtext) {

        rawtext->findoffset(testament, index, &origStart, &origSize);
/*
        //work backwards to find first verse outside our block
        otherIndex = index - 1;
        rawtext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize) {
                otherIndex--;
                rawtext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->LowerBound() -= (index - otherIndex - 1);
*/
        //work forwards to find first verse outside our block
        otherIndex = index + 1;
        rawtext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        while (origSize && otherStart == origStart && otherSize == origSize) {
                otherIndex++;
                rawtext->findoffset(testament, otherIndex, &otherStart, &otherSize);
        }
        key->UpperBound() += (otherIndex - index - 1);


                                }
                        }
                }
        }
        return key;
}

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
}
//----------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
	ModMap::iterator it;
	SWModule *target;
        manager = new SWMgr();

	for (it = manager->Modules.begin(); it != manager->Modules.end(); it++) {
		target = it->second;
		if (!strcmp(target->Type(), "Biblical Texts")) {
                        modlist->Items->Add(target->Name());
//			if (descriptions) *output << target->Description();
		}
	}
	for (it = manager->Modules.begin(); it != manager->Modules.end(); it++) {
		target = it->second;
		if (!strcmp(target->Type(), "Commentaries")) {
                        modlist->Items->Add(target->Name());
//			if (descriptions) *output << target->Description();
		}
	}
	for (it = manager->Modules.begin(); it != manager->Modules.end(); it++) {
		target = it->second;
		if (!strcmp(target->Type(), "Lexicons / Dictionaries")) {
                        modlist->Items->Add(target->Name());
//			if (descriptions) *output << target->Description();
		}
	}

}

short palmbooks[67] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 190, 220, 230, 240, 250, 260, 290, 300, 310, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730};
char* palmbooknames[67] = {"", "GEN", "EXO", "LEV", "NUM", "DEU", "JOS", "JUG", "RUT", "1SA", "2SA", "1KI", "2KI", "1CH", "2CH", "EZR", "NEH", "EST", "JOB", "PS", "PRO", "ECC", "SON", "ISA", "JER", "LAM", "EZE", "DAN", "HOS", "JOE", "AMO", "OBA", "JON", "MIC", "NAH", "HAB", "ZEP", "HAG", "ZEC", "MAL", "MAT", "MAK", "LUK", "JHN", "ACT", "ROM", "1CO", "2CO", "GAL", "EPH", "PHL", "COL", "1TS", "2TS", "1TI", "2TI", "TIT", "MON", "HEB", "JAS", "1PE", "2PE", "1JN", "2JN", "3JN", "JUD", "REV"}; 
enum {ThML, OLB, GBF, PBR, Plain};

void __fastcall TMainForm::outputClick(TObject *Sender)
{
        static GBFThML gbffilter;
        static Latin1UTF8 latin1filter;
        static ThMLOLB olboutputfilter;
        static ThMLGBF gbfoutputfilter;
        static ThMLPlain plainoutputfilter;

        int format;
        if (fmtOLB->Checked) format = OLB;
        else if (fmtGBF->Checked) format = GBF;
        else if (fmtPalm->Checked) format = PBR;
        else if (fmtPlain->Checked) format = Plain;
        else format = ThML;

        unsigned long topicnr = 0;

        if (optFootnotes->Checked)
                manager->setGlobalOption("Footnotes","On");
        else
                manager->setGlobalOption("Footnotes","Off");

        if (optHeadings->Checked)
        	manager->setGlobalOption("Headings","On");
        else
               	manager->setGlobalOption("Headings","Off");

        if (optStrongs->Checked)
        	manager->setGlobalOption("Strong's Numbers","On");
        else
        	manager->setGlobalOption("Strong's Numbers","Off");

        if (optMorph->Checked)
                manager->setGlobalOption("Morphological Tags","On");
        else
                manager->setGlobalOption("Morphological Tags","Off");

        if (optHCant->Checked)
                manager->setGlobalOption("Hebrew Cantillation","On");
        else
                manager->setGlobalOption("Hebrew Cantillation","Off");

        if (optHVowels->Checked)
        	manager->setGlobalOption("Hebrew Vowel Points","On");
        else
                manager->setGlobalOption("Hebrew Vowel Points","Off");

        if (optGAccents->Checked)
        	manager->setGlobalOption("Greek Accents","On");
        else
        	manager->setGlobalOption("Greek Accents","Off");

        if (optRoman->Checked){}

        if (format == OLB) {
        	manager->setGlobalOption("Headings","Off");
        }

        for (int i = 0; i < modlist->Items->Count; i++) {
                if (modlist->Checked[i]) {
                        if (feedback->Lines->Count > 30000) {
                                feedback->Lines->Clear();
                        }
                        AnsiString path;
                        if (format == OLB)
                                path = outputpath->Text + "\\" + modlist->Items->Strings[i] + ".exp";
                        else if (format == GBF)
                                path = outputpath->Text + "\\" + modlist->Items->Strings[i] + ".gbf";
                        else if (format == ThML)
                                path = outputpath->Text + "\\" + modlist->Items->Strings[i] + ".thml";
                        else
                                path = outputpath->Text + "\\" + modlist->Items->Strings[i] + ".txt";

                	ModMap::iterator it = manager->Modules.find(modlist->Items->Strings[i].c_str());
                	SectionMap::iterator sit;
                	ConfigEntMap::iterator eit;
                	SWModule *mod = it->second;
                        FILE *ofile = fopen(path.c_str(), "w");
                        VerseKey* vkey, vkey3;
                        SWKey* key;
                        char* version;
                        char* lang;
                        bool rtl;

                        const char* text;
                                                
                        ListKey listkey;
                        long listi;
                        bool listfinished, isAtomic;
                        VerseKey* vkey2;

                        static unsigned char testament, book, chapter, verse, verse2;
                        static bool t, b, c, v;
                        static char ldsect[3] = {0, 0, 0};

                        testament = book = chapter = verse = t = b = c = v = 0;

                       	if ((sit = manager->config->Sections.find((*it).second->Name())) != manager->config->Sections.end()) {
                		if ((eit = (*sit).second.find("SourceType")) != (*sit).second.end()) {
                			if (!stricmp((char *)(*eit).second.c_str(), "GBF")) {
                        		        mod->AddRenderFilter(&gbffilter);
                                        }
                                        else {
                                                mod->RemoveRenderFilter(&gbffilter);
                                        }
                		}
                		if ((eit = (*sit).second.find("Encoding")) == (*sit).second.end() || stricmp((char *)(*eit).second.c_str(), "UTF-8")) {
                                        if (eit == (*sit).second.end()) {
                                                mod->AddRenderFilter(&latin1filter);
                                        }
                                        else {
                                                mod->RemoveRenderFilter(&latin1filter);
                                        }
                                }
                                if ((eit = (*sit).second.find("Version")) != (*sit).second.end()) {
                                        version = (char *)(*eit).second.c_str();
                                }
                                else {
                                        version = "1.0";
                                }
                                if ((eit = (*sit).second.find("Lang")) != (*sit).second.end()) {
                                        lang = (char *)(*eit).second.c_str();
                                }
                                else {
                                        lang = "en";
                                }

                                if ((eit = (*sit).second.find("Direction")) != (*sit).second.end()) {
                                        rtl = !stricmp((char *)(*eit).second.c_str(), "RtoL");
                                }
                                else {
                                        rtl = false;
                                }
                	}




                        if (format == ThML) {
                                LocaleMgr::systemLocaleMgr.setDefaultLocaleName("en");
                                fprintf(ofile, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE ThML PUBLIC \"-\/\/CCEL\/\/DTD Theological Markup Language\/\/EN\" \"dtd\/ThML.dtd\">\n<ThML>\n<ThML.head>\n");
                                fprintf(ofile, "<electronicEdInfo>\n<publisherID>crosswire</publisherID>\n<authorID></authorID>\n<bookID>%s</bookID>\n<version>%s</version>\n<series></series>\n", modlist->Items->Strings[i].LowerCase().c_str(), version);
                                fprintf(ofile, "<DC>\n<DC.Title>%s<\/DC.Title>\n<DC.Language>%s<\/DC.Language>\n<\/DC>\n<\/electronicEdInfo>\n<\/ThML.head>\n<ThML.body>\n", (char*)mod->Description(), lang);

                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        fprintf (ofile, "<scripContext version=\"%s\" \/>\n", (char*)mod->Name());
                                        key = (*mod);
                                       	vkey = (VerseKey*)key;
                                        vkey->Headings(1);
                                        vkey->Persist(1);
                                        vkey->setLocale("en");

                                        listfinished = false;
                                        if (!range->Text.IsEmpty()) {
                                             listkey = vkey->ParseVerseList(range->Text.c_str(), "Gen 1:1", true);
                                             listi = 0;
                                        }
                                }
                                else if (!strcmp(mod->Type(), "Lexicons / Dictionaries")) {
                                        fprintf (ofile, "<glossary>\n");
                                }

                                (*mod) = TOP;
                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        if (listkey.Count() > listi) {
                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                if (!vkey) {
                                                        isAtomic = true;
                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                }
                                                else {
                                                        isAtomic = false;
                                                }
                                                mod->SetKey(vkey);
                                        }
                                        else {
                                                isAtomic = false;
                                        }
                                }


                                bool div1, div2, div3;
                                div1 = div2 = div3 = 0;


                                while (!mod->Error() && !listfinished) {
                                        if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                                text = (const char*)(*mod);
                                                if (!listkey.Count() && !strlen(text)) {
                                                        (*mod)++;
                                                        continue;
                                                }
                                                getVerseBounds(mod, &vkey3);
                                                verse2 = vkey3.UpperBound().Verse();
                                                VerseKey* vkey2 = (VerseKey*)(SWKey*)(*mod);
                                                t = vkey2->Testament() != testament;
                                                testament = vkey2->Testament();
                                                b = vkey2->Book() != book || t;
                                                book = vkey2->Book();
                                                c = vkey2->Chapter() != chapter || b;
                                                chapter = vkey2->Chapter();
                                                v = vkey2->Verse() != verse || c;
                                                verse = vkey2->Verse();

                                                if (testament) {
                                                        if (b && book) {
                                                                feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + vkey2->books[testament - 1][book - 1].name);
                                                        }

                                                        if (t || !div1) {
                                                                if (div1) {
                                                                        fprintf (ofile, "<\/div3>\n<\/div2>\n<\/div1>\n");
                                                                        div3 = false;
                                                                        div2 = false;
                                                                }
                                                                if (testament == 1) {
                                                                        fprintf (ofile, "<div1 title=\"Old Testament\" class=\"testament\" n=\"1\">\n");
                                                                        div1 = true;
                                                                }
                                                                else if (testament == 2) {
                                                                        fprintf (ofile, "<div1 title=\"New Testament\" class=\"testament\" n=\"2\">\n");
                                                                        div1 = true;
                                                                }
                                                        }
                                                        if ((b || !div2) && book) {
                                                                if (div2) {
                                                                        fprintf (ofile, "<\/div3>\n<\/div2>\n");
                                                                        div3 = false;
                                                                }
                                                                fprintf (ofile, "<div2 title=\"%s\" class=\"book\" n=\"%d\">\n<scripContext passage=\"%s\" \/>\n", vkey2->books[testament - 1][book - 1].name, book, vkey2->books[testament - 1][book - 1].name);
                                                                div2 = true;
                                                        }
                                                        if ((c || !div3) && chapter) {
                                                                if (div3) {
                                                                        fprintf (ofile, "<\/div3>\n");
                                                                }
                                                                fprintf (ofile, "<div3 title=\"Chapter %d\" class=\"chapter\" n=\"%d\">\n<scripContext passage=\"%s %d\" \/>\n",  chapter, chapter, vkey2->books[testament - 1][book - 1].name,  chapter);
                                                                div3 = true;
                                                        }
                                                }

                                                if (!strcmp(mod->Type(), "Biblical Texts")) {
                                                        if (verse == verse2 || !verse)
                                                                fprintf (ofile, "<scripture passage=\"%s\">%s<\/scripture>\n", (char*)mod->KeyText(), text);
                                                        else
                                                                fprintf (ofile, "<scripture passage=\"%s-%d\">%s<\/scripture>\n", (char*)mod->KeyText(), verse2, text);
                                                }
                                                else if (!strcmp(mod->Type(), "Commentaries")) {
                                                        if (verse == verse2 || !verse)
                                                                fprintf (ofile, "<scripCom passage=\"%s\">%s<\/scripCom>\n", (char*)mod->KeyText(), text);
                                                        else
                                                                fprintf (ofile, "<scripCom passage=\"%s-%d\">%s<\/scripCom>\n", (char*)mod->KeyText(), verse2, text);
                                                }

                                                if (isAtomic || mod->Key() >= vkey->UpperBound()) {
                                                        listi++;
                                                        if (listkey.Count() > listi) {
                                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                if (!vkey) {
                                                                        isAtomic = true;
                                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                                }
                                                                else {
                                                                        isAtomic = false;
                                                                }
                                                                mod->SetKey(vkey);
                                                        }
                                                        else {
                                                                listfinished = true;
                                                        }
                                                }
                                                else {
                                                        (*mod)++;
                                                        if (mod->Key() > vkey->UpperBound()) {
                                                                listi++;
                                                                if (listkey.Count() > listi) {
                                                                        vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                        if (!vkey) {
                                                                                isAtomic = true;
                                                                                vkey = (VerseKey*)listkey.GetElement(listi);
                                                                        }
                                                                        else {
                                                                                isAtomic = false;
                                                                        }
                                                                        mod->SetKey(vkey);
                                                                }
                                                                else {
                                                                        listfinished = true;
                                                                }
                                                        }
                                                }

                                        }
                                        else if (!strcmp(mod->Type(), "Lexicons / Dictionaries")) {
                                                fprintf (ofile, "<term>%s<\/term><def>%s<\/def>\n", (char*)mod->KeyText(), (const char*)(*mod));
                                                (*mod)++;
                                                if (ldsect[0] != *((char*)mod->KeyText()) || ldsect[1] != *((char*)mod->KeyText() + 1)) {
                                                        ldsect[0] = *((char*)mod->KeyText());
                                                        ldsect[1] = *((char*)mod->KeyText() + 1);
                                                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + ldsect);
                                                }
                                        }
                                }

                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        fprintf (ofile, "<\/div3>\n<\/div2>\n<\/div1>\n");
                                }
                                else if (!strcmp(mod->Type(), "Lexicons / Dictionaries")) {
                                        fprintf (ofile, "<\/glossary>\n");
                                }
                                fprintf(ofile, "<\/ThML.body>\n<\/ThML>\n");
                        }
                        else if (format == Plain) {
                                mod->AddRenderFilter(&plainoutputfilter);
                                LocaleMgr::systemLocaleMgr.setDefaultLocaleName("en");
                                fprintf (ofile, "");  // UTF-8 BOM -- a BOM is stupid for an 8-bit encoding, but this does serve to signal that this is UTF-8 and not ANSI

                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        key = (*mod);
                                       	vkey = (VerseKey*)key;
                                        vkey->Headings(1);
                                        vkey->Persist(1);
                                        vkey->setLocale("en");

                                        listfinished = false;
                                        if (!range->Text.IsEmpty()) {
                                             listkey = vkey->ParseVerseList(range->Text.c_str(), "Gen 1:1", true);
                                             listi = 0;
                                        }
                                }

                                (*mod) = TOP;

                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        if (listkey.Count() > listi) {
                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                if (!vkey) {
                                                        isAtomic = true;
                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                }
                                                else {
                                                        isAtomic = false;
                                                }
                                                mod->SetKey(vkey);
                                        }
                                        else {
                                                isAtomic = false;
                                        }
                                }

                                while (!mod->Error() && !listfinished) {
                                        if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                                text = (const char*)(*mod);
                                                if (!listkey.Count() && !strlen(text)) {
                                                        (*mod)++;
                                                        continue;
                                                }
                                                getVerseBounds(mod, &vkey3);
                                                verse2 = vkey3.UpperBound().Verse();
                                                VerseKey* vkey2 = (VerseKey*)(SWKey*)(*mod);
                                                testament = vkey2->Testament();
                                                b = vkey2->Book() != book;
                                                book = vkey2->Book();
                                                verse = vkey2->Verse();

                                                if (b) {
                                                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + vkey2->books[testament - 1][book - 1].name);
                                                }

                                                if (verse2 == verse || !verse)
                                                        fprintf (ofile, "%s %s\n", (char*)mod->KeyText(), text);
                                                else
                                                        fprintf (ofile, "%s-%d %s\n", (char*)mod->KeyText(), verse2, text);

                                                if (isAtomic || mod->Key() >= vkey->UpperBound()) {
                                                        listi++;
                                                        if (listkey.Count() > listi) {
                                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                if (!vkey) {
                                                                        isAtomic = true;
                                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                                }
                                                                else {
                                                                        isAtomic = false;
                                                                }
                                                                mod->SetKey(vkey);
                                                        }
                                                        else {
                                                                listfinished = true;
                                                        }
                                                }
                                                else {
                                                        (*mod)++;
                                                        if (mod->Key() > vkey->UpperBound()) {
                                                                listi++;
                                                                if (listkey.Count() > listi) {
                                                                        vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                        if (!vkey) {
                                                                                isAtomic = true;
                                                                                vkey = (VerseKey*)listkey.GetElement(listi);
                                                                        }
                                                                        else {
                                                                                isAtomic = false;
                                                                        }
                                                                        mod->SetKey(vkey);
                                                                }
                                                                else {
                                                                        listfinished = true;
                                                                }
                                                        }
                                                }
                                        }
                                        else if (!strcmp(mod->Type(), "Lexicons / Dictionaries")) {
                                                fprintf (ofile, "%s\n%s\n\n", (char*)mod->KeyText(), (const char*)(*mod));
                                                (*mod)++;
                                                if (ldsect[0] != *((char*)mod->KeyText()) || ldsect[1] != *((char*)mod->KeyText() + 1)) {
                                                        ldsect[0] = *((char*)mod->KeyText());
                                                        ldsect[1] = *((char*)mod->KeyText() + 1);
                                                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + ldsect);
                                                }
                                        }
                                }
                                mod->RemoveRenderFilter(&plainoutputfilter);                                
                        }
                        else if (format == OLB) {
                                mod->AddRenderFilter(&olboutputfilter);
                                LocaleMgr::systemLocaleMgr.setDefaultLocaleName("olb");

                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        key = (*mod);
                                       	vkey = (VerseKey*)key;
                                        vkey->Headings(0);
                                        vkey->Persist(1);
                                        vkey->setLocale("olb");

                                        listfinished = false;
                                        if (!range->Text.IsEmpty()) {
                                             listkey = vkey->ParseVerseList(range->Text.c_str(), "Gen 1:1", true);
                                             listi = 0;
                                        }
                                }

                                (*mod) = TOP;

                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        if (listkey.Count() > listi) {
                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                if (!vkey) {
                                                        isAtomic = true;
                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                }
                                                else {
                                                        isAtomic = false;
                                                }
                                                mod->SetKey(vkey);
                                        }
                                        else {
                                                isAtomic = false;
                                        }
                                }

                                while (!mod->Error() && !listfinished) {
                                        if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                                text = (const char*)(*mod);
                                                if (!listkey.Count() && !strlen(text)) {
                                                        (*mod)++;
                                                        continue;
                                                }
                                                VerseKey* vkey2 = (VerseKey*)(SWKey*)(*mod);
                                                testament = vkey2->Testament();
                                                b = vkey2->Book() != book;
                                                book = vkey2->Book();

                                                if (b) {
                                                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + vkey2->books[testament - 1][book - 1].name);
                                                }

                                                if (!strcmp(mod->Type(), "Biblical Texts"))
                                                        fprintf (ofile, "$$$ %s\n%s\n", (char*)mod->KeyText(), text);
                                                else
                                                        fprintf (ofile, "$$$ %s\n%s\n", (char*)mod->KeyText(), text);

                                                if (isAtomic || mod->Key() >= vkey->UpperBound()) {
                                                        listi++;
                                                        if (listkey.Count() > listi) {
                                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                if (!vkey) {
                                                                        isAtomic = true;
                                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                                }
                                                                else {
                                                                        isAtomic = false;
                                                                }
                                                                mod->SetKey(vkey);
                                                        }
                                                        else {
                                                                listfinished = true;
                                                        }
                                                }
                                                else {
                                                        (*mod)++;
                                                        if (mod->Key() > vkey->UpperBound()) {
                                                                listi++;
                                                                if (listkey.Count() > listi) {
                                                                        vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                        if (!vkey) {
                                                                                isAtomic = true;
                                                                                vkey = (VerseKey*)listkey.GetElement(listi);
                                                                        }
                                                                        else {
                                                                                isAtomic = false;
                                                                        }
                                                                        mod->SetKey(vkey);
                                                                }
                                                                else {
                                                                        listfinished = true;
                                                                }
                                                        }
                                                }
                                        }
                                        else if (!strcmp(mod->Type(), "Lexicons / Dictionaries")) {
                                                fprintf (ofile, "$$$ T%d\n\\\\%s\\\\\n%s\n", topicnr++, (char*)mod->KeyText(), text);
                                                (*mod)++;
                                                if (ldsect[0] != *((char*)mod->KeyText()) || ldsect[1] != *((char*)mod->KeyText() + 1)) {
                                                        ldsect[0] = *((char*)mod->KeyText());
                                                        ldsect[1] = *((char*)mod->KeyText() + 1);
                                                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + ldsect);
                                                }
                                        }
                                }
                                mod->RemoveRenderFilter(&olboutputfilter);
                        }
                        else if (format == GBF) {
                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        mod->AddRenderFilter(&gbfoutputfilter);
                                        LocaleMgr::systemLocaleMgr.setDefaultLocaleName("en");
                                        fprintf(ofile, "<H000>\n<H1>%s\n<H2>%s\n", (char*)mod->Description(), (char*)mod->Name());
                                        key = (*mod);
                                       	vkey = (VerseKey*)key;
                                        vkey->Headings(0);
                                        vkey->Persist(1);
                                        vkey->setLocale("en");

                                        listfinished = false;
                                        if (!range->Text.IsEmpty()) {
                                             listkey = vkey->ParseVerseList(range->Text.c_str(), "Gen 1:1", true);
                                             listi = 0;
                                        }

                                        (*mod) = TOP;

                                        if (listkey.Count() > listi) {
                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                if (!vkey) {
                                                        isAtomic = true;
                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                }
                                                else {
                                                        isAtomic = false;
                                                }
                                                mod->SetKey(vkey);
                                        }
                                        else {
                                                isAtomic = false;
                                        }

                                        while (!mod->Error() && !listfinished) {
                                                text = (const char*)(*mod);                                        
                                                if (!listkey.Count() && !strlen(text)) {
                                                        (*mod)++;
                                                        continue;
                                                }
                                                VerseKey* vkey2 = (VerseKey*)(SWKey*)(*mod);
                                                t = vkey2->Testament() != testament;
                                                testament = vkey2->Testament();
                                                b = vkey2->Book() != book || t;
                                                book = vkey2->Book();
                                                c = vkey2->Chapter() != chapter || b;
                                                chapter = vkey2->Chapter();
                                                v = vkey2->Verse() != verse || v;
                                                verse = vkey2->Verse();

                                                if (b) {
                                                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + vkey2->books[testament - 1][book - 1].name);
                                                }

                                                if (t) {
                                                        if (testament == 1) {
                                                                fprintf (ofile, "<BO><CM>\n");
                                                        }
                                                        else if (testament == 2) {
                                                                fprintf (ofile, "<BN><CM>\n");
                                                        }

                                                        fprintf (ofile, "<SB%d><TT>%s<CM><Tt>\n", book, vkey2->books[testament - 1][book - 1].name);
                                                        fprintf (ofile, "<SC%d>\n", chapter);
                                                        if (rtl)
                                                                fprintf (ofile, "<DR>\n");
                                                }
                                                else if (b) {
                                                        fprintf (ofile, "<SB%d><TT>%s<CM><Tt>\n", book, vkey2->books[testament - 1][book - 1].name);
                                                        fprintf (ofile, "<SC%d>\n", chapter);
                                                        if (rtl)
                                                                fprintf (ofile, "<DR>\n");
                                                }
                                                else if (c) {
                                                        fprintf (ofile, "<SC%d>\n", chapter);
                                                        if (rtl)
                                                                fprintf (ofile, "<DR>\n");
                                                }

                                                fprintf (ofile, "<SV%d>%s\n", verse, text);

                                                if (isAtomic || mod->Key() >= vkey->UpperBound()) {
                                                        listi++;
                                                        if (listkey.Count() > listi) {
                                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                if (!vkey) {
                                                                        isAtomic = true;
                                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                                }
                                                                else {
                                                                        isAtomic = false;
                                                                }
                                                                mod->SetKey(vkey);
                                                        }
                                                        else {
                                                                listfinished = true;
                                                        }
                                                }
                                                else {
                                                        (*mod)++;
                                                        if (mod->Key() > vkey->UpperBound()) {
                                                                listi++;
                                                                if (listkey.Count() > listi) {
                                                                        vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                        if (!vkey) {
                                                                                isAtomic = true;
                                                                                vkey = (VerseKey*)listkey.GetElement(listi);
                                                                        }
                                                                        else {
                                                                                isAtomic = false;
                                                                        }
                                                                        mod->SetKey(vkey);
                                                                }
                                                                else {
                                                                        listfinished = true;
                                                                }
                                                        }
                                                }
                                        }
                                        fprintf(ofile, "<ZZ>\n");
                                        mod->RemoveRenderFilter(&gbfoutputfilter);                                        
                                }
                        }
                        else if (format == PBR) {
                                if (!strcmp(mod->Type(), "Biblical Texts") || !strcmp(mod->Type(), "Commentaries")) {
                                        mod->AddRenderFilter(&plainoutputfilter);
                                        LocaleMgr::systemLocaleMgr.setDefaultLocaleName(lang);
                                        fprintf(ofile, "<PARSERINFO ENCODE=\"iso-8859-1\" WORDTYPE=\"SPCSEP\">\n\n");
                                        fprintf(ofile, "<BIBLE NAME=\"%s\" INFO=\"%s\" ALIGN=\"%s\">\n", (char*)mod->Name(), (char*)mod->Description(), rtl ? "RIGHT" : "LEFT");
                                        key = (*mod);
                                       	vkey = (VerseKey*)key;
                                        vkey->Headings(0);
                                        vkey->Persist(1);
                                        vkey->setLocale(lang);

                                        listfinished = false;
                                        if (!range->Text.IsEmpty()) {
                                             listkey = vkey->ParseVerseList(range->Text.c_str(), "Gen 1:1", true);
                                             listi = 0;
                                        }                                        

                                        (*mod) = TOP;

                                        if (listkey.Count() > listi) {
                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                if (!vkey) {
                                                        isAtomic = true;
                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                }
                                                else {
                                                        isAtomic = false;
                                                }
                                                mod->SetKey(vkey);
                                        }
                                        else {
                                                isAtomic = false;
                                        }

                                        bool div1, div2;
                                        div1 = div2 = 0;

                                        while (!mod->Error() && !listfinished) {
                                                text = (const char*)(*mod);                                        
                                                if (!listkey.Count() && !strlen(text)) {
                                                        (*mod)++;
                                                        continue;
                                                }
                                                unsigned char chapter2 = chapter;
                                                verse2 = verse;
                                                VerseKey* vkey2 = (VerseKey*)(SWKey*)(*mod);
                                                t = vkey2->Testament() != testament;
                                                testament = vkey2->Testament();
                                                b = vkey2->Book() != book || t;
                                                book = vkey2->Book();
                                                c = vkey2->Chapter() != chapter || b;
                                                chapter = vkey2->Chapter();
                                                v = vkey2->Verse() != verse || v;
                                                verse = vkey2->Verse();

                                                if (testament) {
                                                        if (b) {
                                                                feedback->Lines->Add(modlist->Items->Strings[i] + "  -  " + vkey2->books[testament - 1][book - 1].name);
                                                        }

                                                        if (b || !div1) {
                                                                if (div1) {
                                                                        fprintf(ofile, "<\/CHAPTER>\n<\/BOOK>\n");
                                                                        div2 = false;
                                                                }

                                                                int len = 2 * strlen(vkey2->books[testament - 1][book - 1].name);
                                                                char* localname = new char[len];
                                                                strcpy (localname, vkey2->books[testament - 1][book - 1].name);
                                                                latin1filter.ProcessText(localname, len, NULL);

                                                                fprintf (ofile, "<BOOK NAME=\"%s\" NUMBER=\"%d\" SHORTCUT=\"%s\">\n", localname, palmbooks[book + (39 * (testament - 1))], palmbooknames[book + (39 * (testament - 1))]);
                                                                delete localname;

                                                                div1 = true;
                                                                b = true;
                                                        }
                                                        if (c || !div2) {
                                                                if (div2) {
                                                                        fprintf(ofile, "<\/CHAPTER>\n");
                                                                }
                                                                while (chapter - chapter2 - 1 > 0) {
                                                                        fprintf (ofile, "<CHAPTER><\/CHAPTER>\n");
                                                                        chapter2++;
                                                                }
                                                                fprintf (ofile, "<CHAPTER>\n");
                                                                div2 = true;
                                                                c = true;
                                                        }
                                                }

                                                for (int j = 0; j < (verse - verse2 - 1); j++) {
                                                        fprintf (ofile, "<VERSE><\/VERSE>\n");
                                                }                                                

                                                fprintf (ofile, "<VERSE>");
                                                if (c) {
                                                        if (b) {
                                                                int len = 2 * strlen(vkey->books[testament - 1][book - 1].name);
                                                                char* localname = new char[len];
                                                                strcpy (localname, vkey->books[testament - 1][book - 1].name);
                                                                latin1filter.ProcessText(localname, len, NULL);

                                                                fprintf(ofile, "<BOOKTEXT>%s", localname);

                                                                delete localname;
                                                        }
                                                        fprintf (ofile, "<CHAPTEXT>Chapter %d<VERSTEXT>", chapter);
                                                }
                                                fprintf (ofile, "%s<\/VERSE>\n", text);

                                                if (isAtomic || mod->Key() >= vkey->UpperBound()) {
                                                        listi++;
                                                        if (listkey.Count() > listi) {
                                                                vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                if (!vkey) {
                                                                        isAtomic = true;
                                                                        vkey = (VerseKey*)listkey.GetElement(listi);
                                                                }
                                                                else {
                                                                        isAtomic = false;
                                                                }
                                                                mod->SetKey(vkey);
                                                        }
                                                        else {
                                                                listfinished = true;
                                                        }
                                                }
                                                else {
                                                        (*mod)++;
                                                        if (mod->Key() > vkey->UpperBound()) {
                                                                listi++;
                                                                if (listkey.Count() > listi) {
                                                                        vkey = SWDYNAMIC_CAST(VerseKey, listkey.GetElement(listi));
                                                                        if (!vkey) {
                                                                                isAtomic = true;
                                                                                vkey = (VerseKey*)listkey.GetElement(listi);
                                                                        }
                                                                        else {
                                                                                isAtomic = false;
                                                                        }
                                                                        mod->SetKey(vkey);
                                                                }
                                                                else {
                                                                        listfinished = true;
                                                                }
                                                        }                                                        
                                                }

                                        }

                                        fprintf(ofile, "<\/CHAPTER>\n<\/BOOK>\n<\/BIBLE>\n");
                                        mod->RemoveRenderFilter(&plainoutputfilter);
                                }
                        }

                        fclose(ofile);
                        feedback->Lines->Add(modlist->Items->Strings[i] + "  -  Complete");
                }
        }
        feedback->Lines->Add("All Modules Complete");
}
//---------------------------------------------------------------------------