forked from CruiserOne/Astrolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.cpp
2667 lines (2444 loc) · 87.4 KB
/
io.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
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
/*
** Astrolog (Version 7.70) File: io.cpp
**
** IMPORTANT NOTICE: Astrolog and all chart display routines and anything
** not enumerated below used in this program are Copyright (C) 1991-2024 by
** Walter D. Pullen ([email protected], http://www.astrolog.org/astrolog.htm).
** Permission is granted to freely use, modify, and distribute these
** routines provided these credits and notices remain unmodified with any
** altered or distributed versions of the program.
**
** The main ephemeris databases and calculation routines are from the
** library SWISS EPHEMERIS and are programmed and copyright 1997-2008 by
** Astrodienst AG. Use of that source code is subject to license for Swiss
** Ephemeris Free Edition at https://www.astro.com/swisseph/swephinfo_e.htm.
** This copyright notice must not be changed or removed by any user of this
** program.
**
** Additional ephemeris databases and formulas are from the calculation
** routines in the program PLACALC and are programmed and Copyright (C)
** 1989,1991,1993 by Astrodienst AG and Alois Treindl ([email protected]). The
** use of that source code is subject to regulations made by Astrodienst
** Zurich, and the code is not in the public domain. This copyright notice
** must not be changed or removed by any user of this program.
**
** The original planetary calculation routines used in this program have
** been copyrighted and the initial core of this program was mostly a
** conversion to C of the routines created by James Neely as listed in
** 'Manual of Computer Programming for Astrologers', by Michael Erlewine,
** available from Matrix Software.
**
** Atlas composed using data from https://www.geonames.org/ licensed under a
** Creative Commons Attribution 4.0 License. Time zone changes composed using
** public domain TZ database: https://data.iana.org/time-zones/tz-link.html
**
** The PostScript code within the core graphics routines are programmed
** and Copyright (C) 1992-1993 by Brian D. Willoughby ([email protected]).
**
** More formally: This program is free software; you can redistribute it
** and/or modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of the
** License, or (at your option) any later version. This program is
** distributed in the hope that it will be useful and inspiring, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details, a copy of which is in the
** LICENSE.HTM file included with Astrolog, and at http://www.gnu.org
**
** Initial programming 8/28-30/1991.
** X Window graphics initially programmed 10/23-29/1991.
** PostScript graphics initially programmed 11/29-30/1992.
** Last code change made 4/22/2024.
*/
#include "astrolog.h"
#ifdef PC
#include <io.h>
#else
#include <dirent.h>
#endif
/*
******************************************************************************
** File IO Routines.
******************************************************************************
*/
// Open the file indicated by the given string and return the file's stream
// pointer, or NULL if the file couldn't be found or opened. All parts of the
// program which open files to read call this routine. We look in several
// various locations and directories for the file before giving up.
FILE *FileOpen(CONST char *szFile, int nFileMode, char *szPath)
{
FILE *file;
char szFileT[cchSzMax], szExe[cchSzMax], sz[cchSzMax], szMode[3], *pch;
#ifdef ENVIRON
char *env;
#endif
int i, j;
// Some file types we want to open as binary instead of Ascii.
sprintf(szMode, "r%s", nFileMode >= 2 ? "b" : "");
// Get directory containing Astrolog executable.
#ifdef WIN
GetModuleFileName(wi.hinst, szExe, cchSzMax);
#else
sprintf(szExe, "%s", is.szProgName != NULL ? is.szProgName : "");
#endif
for (pch = szExe; *pch; pch++)
;
while (pch > szExe && *pch != chDirSep)
pch--;
if (*pch == chDirSep)
pch++;
*pch = chNull;
for (i = 0; i <= 1; i++) {
if (i <= 0)
sprintf(szFileT, "%s", szFile);
else {
if (nFileMode > 1)
break;
sprintf(szFileT, "%s.as", szFile);
}
// First look for the file in the directory of the Astrolog executable.
sprintf(sz, "%s", szExe);
for (pch = sz; *pch; pch++)
;
sprintf(pch, "%s", szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
// Next look for the file in the current directory.
sprintf(sz, "%s", szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
// Next look in the directories indicated by the -Yi switch.
for (j = 0; j < 10; j++) {
pch = us.rgszPath[j];
if (FSzSet(pch)) {
if ((FCapCh(*pch) || FUncapCh(*pch) || FNumCh(*pch)) &&
!(pch[1] == ':')) {
// If dir is relative path, then prepend the path to executable.
sprintf(sz, "%s", szExe);
for (pch = sz; *pch; pch++)
;
} else
pch = sz;
sprintf(pch, "%s%c%s", us.rgszPath[j], chDirSep, szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
}
}
#ifdef ENVIRON
// Next look for the file in the directory indicated by the version
// specific system environment variable.
sprintf(sz, "%s%s", ENVIRONVER, szVerCore);
env = getenv(sz);
if (env && *env) {
sprintf(sz, "%s%c%s", env, chDirSep, szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
}
// Next look in the directory in the general environment variable.
env = getenv(ENVIRONALL);
if (env && *env) {
sprintf(sz, "%s%c%s", env, chDirSep, szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
}
// Next look in directory in the version prefix environment variable.
env = getenv(ENVIRONVER);
if (env && *env) {
sprintf(sz, "%s%c%s", env, chDirSep, szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
}
#endif
// Finally look in one of several compile time specified directories.
sprintf(sz, "%s%c%s", nFileMode == 0 ? DEFAULT_DIR :
(nFileMode == 1 ? CHART_DIR : EPHE_DIR), chDirSep, szFileT);
file = fopen(sz, szMode);
if (file != NULL)
goto LDone;
}
if (file == NULL && FOdd(nFileMode)) {
// If file was never found, print an error (unless we were looking for a
// certain file type, e.g. the optional astrolog.as file).
sprintf(sz, "File '%s' not found.", szFile);
PrintError(sz);
}
LDone:
if (file != NULL && szPath != NULL) {
sprintf(szPath, "%s", sz);
fclose(file);
}
return file;
}
// Read an 8 bit byte from a file.
byte BRead(FILE *file)
{
return getc(file);
}
// Read a 16 bit word from a file.
word WRead(FILE *file)
{
byte b1, b2;
b1 = getbyte(); b2 = getbyte();
return WFromBB(b1, b2);
}
// Read a 32 bit long from a file.
dword LRead(FILE *file)
{
byte b1, b2, b3, b4;
b1 = getbyte(); b2 = getbyte(); b3 = getbyte(); b4 = getbyte();
return LFromWW(WFromBB(b1, b2), WFromBB(b3, b4));
}
// This is Astrolog's generic file processing routine, which handles chart
// info files, position files, and config files. Given a file name or a file
// handle, run through each line as a series of command switches.
flag FProcessSwitchFile(CONST char *szFile, FILE *file)
{
char szLine[cchSzLine], *argv[MAXSWITCHES], ch;
int argc, i;
flag fHaveFile, fRet = fFalse;
fHaveFile = (file != NULL);
if (!fHaveFile) {
file = FileOpen(szFile, 0, NULL);
if (file == NULL)
goto LDone;
}
is.fileIn = file;
// All files have to begin with the -@ switch file type identifier.
ch = getc(file); ungetc(ch, file);
if (ch != '@') {
sprintf(szLine,
"The command file '%s' is not in any valid format (character %d).",
szFile, (int)ch);
PrintWarning(szLine);
goto LDone;
}
loop {
while (!feof(file) && (ch = getbyte()) < ' ')
;
if (feof(file))
break;
for (szLine[0] = ch, i = 1; i < cchSzLine-1 && !feof(file); i++) {
ch = getbyte();
if (!((uchar)ch >= ' ' || ch == chTab))
break;
szLine[i] = ch;
}
if (ch != '\n')
ch = getbyte();
// Windows Notepad files sometimes have character 255 at end of file.
if (i > 0 && (uchar)szLine[i-1] == 255)
i--;
szLine[i] = chNull;
argc = NParseCommandLine(szLine, argv);
if (!FProcessSwitches(argc, argv))
goto LDone;
}
fRet = fTrue;
LDone:
is.fileIn = NULL;
if (!fHaveFile && file != NULL)
fclose(file);
return fRet;
}
// Take the current chart information, and write it out to the file as
// indicated by the -o switch. This is only executed at the end of program
// execution if the -o switch is in effect.
flag FOutputData(void)
{
char sz[cchSzMax], *pch;
FILE *file;
int i, j, iMax;
real dst, rT;
if (us.fNoWrite)
return fFalse;
// Write other file formats if -od, -ol, -oa, -oq, or -ox is in effect.
if (us.nWriteFormat == 'd')
return FOutputSettings();
else if (us.nWriteFormat == 'l')
return FOutputChartList();
else if (us.nWriteFormat == 'a')
return FOutputAAFFile();
else if (us.nWriteFormat == 'q')
return FOutputQuickFile();
#ifdef SWISSGRAPH
else if (us.nWriteFormat == 'x')
return FOutputDaedalusStar();
#endif
file = fopen(is.szFileOut, "w"); // Create and open the file for output.
if (file == NULL) {
sprintf(sz, "File '%s' can not be created.", is.szFileOut);
PrintError(sz);
return fFalse;
}
if (us.nWriteFormat != '0') {
// Write the chart information to the file.
if (Mon < 1) {
fclose(file);
PrintError("Can't output chart with no time/space to file.");
return fFalse;
}
if (us.fWriteOld) {
dst = Dst;
if (dst == dstAuto)
dst = (real)is.fDst;
fprintf(file, "%d\n%d\n%d\n%.2f\n%.2f\n%.2f\n%.2f\n", Mon, Day, Yea,
DegToDec(Tim), DegToDec(Zon-dst), DegToDec(Lon), DegToDec(Lat));
} else {
fprintf(file, "@AI%s ; %s chart info.\n", szVerCore, szAppName);
i = us.fAnsiChar;
us.fAnsiChar = fFalse;
fprintf(file, "%cqb %.3s %d %d %s %s ", chSwitch, szMonth[Mon],
Day, Yea, SzTim(Tim), Dst == 0.0 ? "ST" : (Dst == 1.0 ? "DT" :
Dst == dstAuto ? "Autodetect" : SzZone(Dst)));
fprintf(file, "%s %s\n", SzZone(Zon), SzLocation(Lon, Lat));
// Don't want double quotes within double quoted string parameters.
fprintf(file, "%czi \"", chSwitch);
if (FSzSet(ciMain.nam))
for (pch = ciMain.nam; *pch; pch++)
putc(*pch != '"' ? *pch : '\'', file);
fprintf(file, "\" \"");
if (FSzSet(ciMain.loc))
for (pch = ciMain.loc; *pch; pch++)
putc(*pch != '"' ? *pch : '\'', file);
fprintf(file, "\"\n");
us.fAnsiChar = i;
}
} else {
// However, if the -o0 switch is in effect, then write the actual
// positions of the planets and houses to the file instead.
if (us.fWriteOld) {
for (i = 1; i <= oNorm; i++) {
j = (int)planet[i];
fprintf(file, "%.3s: %2d %2d %10.7f\n", szObjName[i],
j%30, j/30+1, RFract(planet[i])*60.0); // Position
rT = planetalt[i];
fprintf(file, "[%c]: %3d %12.8f\n", // Altitude
ret[i] >= 0.0 ? 'D' : chRet, (int)(RSgn(rT)*
RFloor(RAbs(rT))), (rT-(real)(int)rT)*60.0); // Retrograde?
if (i == oNod)
i = oFor-1;
else if (i == oFor)
i = oMC -1;
else if (i == oMC)
i = oAsc-1;
else if (i == oAsc)
i = oVtx-1;
else if (i == oVtx) // Skip minor cusps to write Uranians
i = us.fUranian ? uranLo-1 : cObj;
}
for (i = 1; i <= cSign/2; i++) { // Write first six cusp positions
j = (int)chouse[i];
fprintf(file, "H_%c: %2d %2d %10.7f\n",
'a'+i-1, j%30, j/30+1, RFract(chouse[i])*60.0);
}
} else {
fprintf(file, "@AP%s ; %s chart positions.\n", szVerCore, szAppName);
// Don't want double quotes within double quoted string parameters.
fprintf(file, "%czi \"", chSwitch);
if (FSzSet(ciMain.nam))
for (pch = ciMain.nam; *pch; pch++)
putc(*pch != '"' ? *pch : '\'', file);
fprintf(file, "\" \"");
if (FSzSet(ciMain.loc))
for (pch = ciMain.loc; *pch; pch++)
putc(*pch != '"' ? *pch : '\'', file);
fprintf(file, "\"\n");
iMax = Max(is.nObj, cuspHi);
for (i = 0; i <= iMax; i++) if (!ignore[i] || FCusp(i)) {
fprintf(file, "%cYF ", chSwitch);
fprintf(file, i != oUrT && i != oAlr ? "%-4.4s" : "%-6.6s",
szObjName[i]);
rT = FBetween(i, cuspLo-1+4, cuspLo-1+9) ?
chouse[i-(cuspLo-1)] : planet[i];
j = (int)rT;
fprintf(file, "%3d %.3s %12.9f,",
j%30, szSignName[j/30+1], RFract(rT)*60.0);
rT = planetalt[i] < 0.0 && planetalt[i] > -1.0 ?
planetalt[i] : RFract(RAbs(planetalt[i]));
fprintf(file, "%4d %13.9f,", (int)planetalt[i], rT*60.0);
rT = i > oNorm ? 999.0 : (i == oMoo && !us.fEphemFiles ? 0.0026 :
PtLen(space[i]));
fprintf(file, " %13.9f %13.9f\n", ret[i], rT);
}
}
}
// Now write any extra strings that were on the command line after the -o
// specification but before the next switch, to the file as comments.
for (i = 0; i < is.cszComment; i++)
fprintf(file, "%s%s\n", us.fWriteOld ? "" : "; ", is.rgszComment[i]);
fclose(file);
return fTrue;
}
// Load an Astrological Exchange Format (AAF) file into the default set of
// chart information, given a file name or a file handle.
flag FProcessAAFFile(CONST char *szFile, FILE *file)
{
char szLine[cchSzLine], sz[cchSzMax], *pch, *sz1, *sz2, ch;
int i, grf;
flag fHaveFile, fRet = fFalse;
fHaveFile = (file != NULL);
if (!fHaveFile) {
file = FileOpen(szFile, 0, NULL);
if (file == NULL)
goto LDone;
}
is.fileIn = file;
do {
grf = 0;
while (grf != 3) {
while (!feof(file) && (ch = getc(file)) < ' ')
;
if (feof(file))
break;
for (szLine[0] = ch, i = 1; i < cchSzLine-1 && !feof(file) &&
(uchar)(szLine[i] = getc(file)) >= ' '; i++)
;
szLine[i] = chNull;
if (szLine[0] == chNull)
continue;
if (szLine[0] != '#') {
sprintf(szLine,
"The AAF file '%s' has a line not starting with '#' (character %d).",
szFile, (int)ch);
PrintWarning(szLine);
goto LDone;
}
if (szLine[1] == ':') // Skip over comment lines
continue;
// Input row #1.
if (FEqRgch(szLine, "#A93:", 5, fFalse)) {
sz1 = pch = szLine + 5;
AdvancePast(',');
pch[-1] = chNull;
sz2 = pch;
AdvancePast(',');
pch[-1] = chNull;
if (*sz1 && !FEqSz(sz1, "*"))
sprintf(sz, "%s %s", sz2, sz1);
else
sprintf(sz, "%s", sz2);
ciCore.nam = SzClone(sz);
AdvancePast(',');
DD = NFromSz(pch);
AdvancePast('.');
MM = NParseSz(pch, pmMon);
AdvancePast('.');
YY = NParseSz(pch, pmYea);
AdvancePast(',');
sz1 = pch;
AdvancePast(',');
pch[-1] = chNull;
TT = RParseSz(sz1, pmTim);
sz1 = pch;
AdvancePast(',');
pch[-1] = chNull;
sz2 = pch;
// Convert something like "Seattle, WA (USA)" to "Seattle, WA, USA".
AdvancePast('(');
if (pch[-1] == '(') {
pch[-2] = ','; pch[-1] = ' ';
for (; *pch; pch++)
if (*pch == ')')
*pch = chNull;
}
if (*sz1)
sprintf(sz, "%s, %s", sz1, sz2);
else
sprintf(sz, "%s", sz2);
ciCore.loc = SzClone(sz);
grf |= 1;
// Input row #2.
} else if (FEqRgch(szLine, "#B93:", 5, fFalse)) {
pch = szLine + 5;
AdvancePast(',');
AA = RParseSz(pch, pmLon);
AdvancePast(',');
OO = RParseSz(pch, pmLat);
AdvancePast(',');
ZZ = RParseSz(pch, pmZon);
AdvancePast(',');
if (*pch == 'D') // Double Daylight Saving time
SS = 2.0;
else if (*pch == 'L') // Local Mean Time
SS = 0.0;
else
SS = RParseSz(pch, pmDst);
grf |= 2;
} else {
sprintf(szLine,
"The AAF file '%s' has a line that can't be parsed.", szFile);
PrintWarning(szLine);
goto LDone;
}
}
if (!FValidMon(MM) || !FValidDay(DD, MM, YY) || !FValidYea(YY) ||
!FValidTim(TT) || !FValidZon(ZZ) || !FValidLon(OO) || !FValidLat(AA)) {
PrintWarning("Values in AAF file are out of range.");
goto LDone;
}
if (!FAppendCIList(&ciCore))
goto LDone;
} while (!feof(file));
fRet = fTrue;
LDone:
is.fileIn = NULL;
if (!fHaveFile)
fclose(file);
return fRet;
}
// Write the current chart information to an Astrological Exchange Format
// (AAF) file, as indicated by the -oa switch.
flag FOutputAAFFile(void)
{
char sz[cchSzMax], *pch;
FILE *file;
int nSav;
flag fSav;
if (us.fNoWrite)
return fFalse;
file = fopen(is.szFileOut, "w"); // Create and open the file for output.
if (file == NULL) {
sprintf(sz, "AAF file '%s' can not be created.", is.szFileOut);
PrintError(sz);
return fFalse;
}
fprintf(file, "#: %s %s\n", szAppName, szVersionCore);
// Output row #1.
fprintf(file, "#A93:*,");
if (FSzSet(ciMain.nam))
for (pch = ciMain.nam; *pch; pch++)
putc(*pch == ',' ? ';' : *pch, file);
fSav = us.fEuroTime; us.fEuroTime = fTrue;
fprintf(file, ",*,%d.%d.%d,%s,", Day, Mon, Yea, SzTim(Tim));
us.fEuroTime = fSav;
if (FSzSet(ciMain.loc))
for (pch = ciMain.loc; *pch && *pch != ','; pch++)
putc(*pch, file);
putc(',', file);
if (!*pch)
putc('*', file);
else {
// Convert commas to other characters, since they delimit fields.
for (pch++; *pch == ' '; pch++)
;
for (; *pch && *pch != ','; pch++)
putc(*pch, file);
if (*pch == ',') {
// Convert something like "Seattle, WA, USA" to "Seattle, WA (USA)".
fprintf(file, " (");
for (pch++; *pch == ' '; pch++)
;
for (; *pch; pch++)
putc(*pch == ',' ? ';' : *pch, file);
putc(')', file);
}
}
// Output row #2.
nSav = us.fAnsiChar; us.fAnsiChar = 4;
fprintf(file, "\n#B93:%f,%s,%s,", JulianDayFromTime(is.T),
SzLocation(Lon, Lat), Zon != zonLMT && Zon != zonLAT ? SzZone(Zon) : "*");
us.fAnsiChar = nSav;
fprintf(file, "%c\n", Zon == zonLMT || Zon == zonLAT ? 'L' :
(Dst != 0.0 ? '1' : '0'));
fprintf(file, "#: AAF end\n");
fclose(file);
return fTrue;
}
#define FZonDst(sz) ((sz)[1] == 'D' || (sz)[1] == 'W')
// Load a Quick*Chart format file into the default set of chart information,
// given a file name or a file handle.
flag FProcessQuickFile(CONST char *szFile, FILE *file)
{
char szLine[cchSzMax], sz[cchSzDef];
int i;
flag fHaveFile, fRet = fFalse;
fHaveFile = (file != NULL);
if (!fHaveFile) {
file = FileOpen(szFile, 0, NULL);
if (file == NULL)
goto LDone;
}
is.fileIn = file;
do {
fgets(szLine, cchSzMax, file);
if (feof(file)) {
fRet = fTrue;
goto LDone;
}
// Parse line of 100 characters.
CopyRgb((pbyte)szLine, (pbyte)sz, 23);
for (i = 23-1; i >= 0 && sz[i] <= ' '; i--)
;
sz[i+1] = chNull;
ciCore.nam = SzClone(sz);
CopyRgchToSz(szLine+23, 3, sz, cchSzDef);
MM = NParseSz(sz, pmMon);
DD = NParseSz(szLine+23+3, pmDay);
CopyRgchToSz(szLine+23+7, 5, sz, cchSzDef);
YY = NParseSz(sz, pmYea);
CopyRgchToSz(szLine+23+12, 24-12, sz, cchSzDef);
TT = RParseSz(sz, pmTim);
if (FEqRgch(&szLine[23+24], "LMT", 3, fTrue))
ZZ = zonLMT;
else {
CopyRgchToSz(szLine+23+24+3, 9-3, sz, cchSzDef);
ZZ = RParseSz(sz, pmZon);
}
CopyRgchToSz(szLine+23+24+9, 10, sz, cchSzDef);
OO = RParseSz(sz, pmLon);
CopyRgchToSz(szLine+23+24+9+10, 9, sz, cchSzDef);
AA = RParseSz(sz, pmLat);
CopyRgb((pbyte)(szLine+23+24+9+10+9), (pbyte)sz, 25);
for (i = 25-1; i >= 0 && sz[i] <= ' '; i--)
;
sz[i+1] = chNull;
ciCore.loc = SzClone(sz);
SS = 0.0;
if (FZonDst(&szLine[23+24])) {
ZZ += 1.0; SS += 1.0;
}
if (!FValidMon(MM) || !FValidDay(DD, MM, YY) || !FValidYea(YY) ||
!FValidTim(TT) || !FValidZon(ZZ) || !FValidLon(OO) || !FValidLat(AA)) {
PrintWarning("Values in Quick*Chart file are out of range.");
goto LDone;
}
if (!FAppendCIList(&ciCore))
goto LDone;
} while (!feof(file));
fRet = fTrue;
LDone:
is.fileIn = NULL;
if (!fHaveFile)
fclose(file);
return fRet;
}
// Write the current chart information or current chart list to an Quick*Chart
// format file, as indicated by the -oq switch.
flag FOutputQuickFile(void)
{
char sz[cchSzMax];
FILE *file = NULL;
real rOff, rDst;
int i, iMax, j, fSav1;
flag fRet = fFalse, fSav2;
CI *pci;
if (us.fNoWrite)
goto LDone;
file = fopen(is.szFileOut, "w"); // Create and open the file for output.
if (file == NULL) {
sprintf(sz, "Quick*Chart file '%s' can not be created.", is.szFileOut);
PrintError(sz);
goto LDone;
}
iMax = (is.cci > 1 ? is.cci : 1);
for (i = 0; i < iMax; i++) {
pci = iMax <= 1 ? &ciMain : &is.rgci[i];
if (!FBetween(pci->yea, -9999, 99999)) {
PrintWarning("Can't save this chart to Quick*Chart format because the "
"Year field is more than 5 characters long.");
goto LDone;
}
// Output line of 101 characters.
fprintf(file, "%-23.23s", pci->nam);
fprintf(file, "%c%c%c", szMonth[pci->mon][0],
ChCap(szMonth[pci->mon][1]), ChCap(szMonth[pci->mon][2]));
fSav1 = us.fAnsiChar; us.fAnsiChar = 4;
fSav2 = is.fSeconds; is.fSeconds = fTrue;
fprintf(file, "%3d,%5d%s", pci->day, pci->yea, SzTim(pci->tim));
us.fAnsiChar = fSav1; is.fSeconds = fSav2;
rDst = (pci->dst == dstAuto ? (real)is.fDst : pci->dst);
rOff = (pci->zon == zonLMT || pci->zon == zonLAT ? pci->lon / 15.0 :
pci->zon) - rDst;
if (pci->zon == zonLMT || pci->zon == zonLAT)
sprintf(sz, "LMT");
else {
sprintf(sz, " ");
// Search for the 3 character time zone string that best matches offset.
for (j = 0; j < cZone; j++)
if (FSameR(rZon[j], rOff) && CchSz(szZon[j]) == 3 &&
FZonDst(szZon[j]) == (rDst == 1.0)) {
sprintf(sz, "%s", szZon[j]);
break;
}
}
fprintf(file, " %s%c%02d:%02d", sz, rOff < 0.0 ? '-' : '+',
(int)RAbs(rOff), (int)(RFract(RAbs(rOff))*60.0+rRound/60.0));
fprintf(file, "%03d%c%02d'%02d", (int)RAbs(pci->lon), pci->lon < 0.0 ?
'E' : 'W', (int)(RFract(RAbs(pci->lon))*60.0+rRound/60.0), 0);
fprintf(file, " %02d%c%02d'%02d", (int)RAbs(pci->lat), pci->lat < 0.0 ?
'S' : 'N', (int)(RFract(RAbs(pci->lat))*60.0+rRound/60.0), 0);
fprintf(file, " %-25.25s \n", pci->loc);
}
fRet = fTrue;
LDone:
if (file != NULL)
fclose(file);
return fRet;
}
// Load a Astrodatabank XML format file into the default set of chart
// information, given a file name or a file handle.
flag FProcessADBFile(CONST char *szFile, FILE *file)
{
char szLine[cchSzLine], sz[cchSzDef], szLoc1[cchSzDef], szLoc2[cchSzDef],
ch, *pch, *pch2;
int i, grf, cchSz = (us.szADB == NULL ? 0 : CchSz(us.szADB));
flag fHaveFile, fDidOne = fFalse, fDidStart, fDidEnd, fDidLon, fRet = fFalse;
fHaveFile = (file != NULL);
if (!fHaveFile) {
file = FileOpen(szFile, 0, NULL);
if (file == NULL)
goto LDone;
}
is.fileIn = file;
do {
#ifdef EXPRESS
if (!us.fExpOff && FSzSet(us.szExpADB)) {
for (i = us.iExpADB; i < us.iExpADB + us.cExpADB; i++)
if (!ExpSetN(i, 0))
goto LDone;
}
#endif
fDidStart = fDidEnd = fFalse;
grf = 0;
while (!fDidEnd) {
while (!feof(file) && (ch = getc(file)) < ' ')
;
if (feof(file))
break;
for (szLine[0] = ch, i = 1; i < cchSzLine-1 && !feof(file) &&
(uchar)(szLine[i] = getc(file)) >= ' '; i++)
;
szLine[i] = chNull;
fDidLon = fFalse;
for (pch = szLine; *pch; pch++) {
if (FEqRgch(pch, "<adb_entry", 10, fFalse))
fDidStart = fTrue;
else if (!fDidStart)
continue;
if (FEqRgch(pch, "</adb_entry>", 12, fFalse))
fDidEnd = fTrue;
if ((grf & 1) == 0 && FEqRgch(pch, "imonth=\"", 8, fFalse)) {
MM = atoi(pch + 8);
grf |= 1;
}
if ((grf & 2) == 0 && FEqRgch(pch, "iday=\"", 6, fFalse)) {
DD = atoi(pch + 6);
grf |= 2;
}
if ((grf & 4) == 0 && FEqRgch(pch, "iyear=\"", 7, fFalse)) {
YY = atoi(pch + 7);
grf |= 4;
}
if ((grf & 8) == 0 && FEqRgch(pch, "sbtime_ampm=\"", 13, fFalse)) {
pch += 13;
for (pch2 = pch; *pch2 && *pch2 != '"'; pch2++)
;
CopyRgchToSz(pch, pch2 - pch, sz, cchSzDef);
if (*sz)
TT = RParseSz(sz, pmTim);
else
TT = 12.0; // Some records are "unknown, 12:00 used"
grf |= 8;
}
if ((grf & 16) == 0 && FEqRgch(pch, "ctimetype=\"", 11, fFalse)) {
if (pch[11] != 'l') {
SS = pch[11] == 'd' ? 1.0 : 0.0;
grf |= 16;
} else {
SS = 0.0; ZZ = zonLMT;
grf |= (16 | 32);
}
}
if ((grf & 32) == 0 && FEqRgch(pch, "stmerid=\"", 9, fFalse)) {
pch += 9;
for (pch2 = pch; *pch2 && *pch2 != '"'; pch2++)
;
CopyRgchToSz(pch, pch2 - pch, sz, cchSzDef);
ZZ = RParseSz(sz, pmZon);
grf |= 32;
}
if ((grf & 64) == 0 && FEqRgch(pch, "slong=\"", 7, fFalse)) {
OO = RParseSz(pch + 7, pmLon);
grf |= 64;
fDidLon = fTrue;
}
if ((grf & 128) == 0 && FEqRgch(pch, "slati=\"", 7, fFalse)) {
AA = RParseSz(pch + 7, pmLat);
grf |= 128;
}
if ((grf & 256) == 0 && FEqRgch(pch, "<sflname>", 9, fFalse)) {
pch += 9;
while (*pch == ' ')
pch++;
for (pch2 = pch; *pch2 && *pch2 != '<'; pch2++)
;
CopyRgchToSz(pch, pch2 - pch, sz, cchSzDef);
ConvertSzFromUTF8(sz);
ciCore.nam = SzClone(sz);
grf |= 256;
}
if ((grf & 512) == 0 && fDidLon && FEqRgch(pch, "\">", 2, fFalse)) {
pch += 2;
for (pch2 = pch; *pch2 && *pch2 != '<'; pch2++)
;
CopyRgchToSz(pch, pch2 - pch, szLoc1, cchSzDef);
grf |= 512;
}
if ((grf & 1024) == 0 && FEqRgch(pch, "<country", 8, fFalse)) {
for (pch2 = pch + 8; *pch2 && *pch2 != '>'; pch2++)
;
pch = pch2 + (*pch2 == '>');
for (pch2 = pch; *pch2 && *pch2 != '<'; pch2++)
;
CopyRgchToSz(pch, pch2 - pch, szLoc2, cchSzDef);
sprintf(sz, "%s, %s", szLoc1, szLoc2);
ConvertSzFromUTF8(sz);
ciCore.loc = SzClone(sz);
grf |= 1024;
}
if (cchSz > 0 && (grf & 2048) == 0 &&
FEqRgch(pch, us.szADB, cchSz, fFalse))
grf |= 2048;
#ifdef EXPRESS
if (!us.fExpOff && FSzSet(us.szExpADB)) {
for (i = us.iExpADB; i < us.iExpADB + us.cExpADB; i++) {
pch2 = ExpGetString(i);
if (FSzSet(pch2) && FEqRgch(pch2, pch, CchSz(pch2), fFalse)) {
if (!ExpSetN(i, NExpGet(i) + 1))
goto LDone;
}
}
}
#endif
}
}
if ((grf & 2047) != 2047) {
if (grf == 0 && fDidOne) {
fRet = fTrue;
goto LDone;
}
if (grf == 0)
PrintWarning("Couldn't find any charts in Astrodatabank file.");
else
PrintWarning("Couldn't detect all fields in Astrodatabank file.");
goto LDone;
}
ZZ += SS;
if (!FValidMon(MM) || !FValidDay(DD, MM, YY) || !FValidYea(YY) ||
!FValidTim(TT) || !FValidZon(ZZ) || !FValidLon(OO) || !FValidLat(AA)) {
PrintWarning("Values in Astrodatabank file are out of range.");
goto LDone;
}
if (cchSz > 0 && (grf & 2048) == 0)
continue;
#ifdef EXPRESS
// Skip current chart record if AstroExpression says to do so.
if (!us.fExpOff && FSzSet(us.szExpADB)) {
if (!NParseExpression(us.szExpADB))
continue;
}
#endif
if (!FAppendCIList(&ciCore))
goto LDone;
fDidOne = fTrue;
} while (!feof(file));
fRet = fTrue;
LDone:
is.fileIn = NULL;
if (!fHaveFile)
fclose(file);
return fRet;
}
// Load a Solar Fire chart export text file into the default set of chart
// information, given a file name or a file handle.
flag FProcessSFTextFile(CONST char *szFile, FILE *file)
{
char szLine[cchSzLine], *pch, *pch2;
int nState = -1, cch;
flag fHaveFile, fRet = fFalse;
fHaveFile = (file != NULL);
if (!fHaveFile) {
file = FileOpen(szFile, 0, NULL);
if (file == NULL)
goto LDone;
}
is.fileIn = file;
loop {
fgets(szLine, cchSzMax, file);
if (feof(file))
break;
for (pch = szLine; *pch; pch++)
;
while (pch > szLine && pch[-1] <= ' ')
*(--pch) = chNull;
if (nState <= 0) {
if (*szLine == chNull)
continue;
if (nState < 0 && FMatchSz("Charts from Solar Fire", szLine))
continue;
if (nState < 0 && FMatchSz("Created by Esoteric Technologies", szLine)) {
nState = 0;
continue;
}
if (nState == 0)
nState = 1;
else
break;
}
if (nState == 1) {
for (pch = szLine; *pch; pch++)
;
cch = pch - szLine;
if (cch > 14 && FEqRgch(pch - 14, " - Natal Chart", 14, fFalse))
pch[-14] = chNull;
else if (cch > 13 && FEqRgch(pch - 13, " - Male Chart", 13, fFalse))
pch[-13] = chNull;
else if (cch > 15 && FEqRgch(pch - 15, " - Female Chart", 15, fFalse))
pch[-15] = chNull;
ciCore.nam = SzClone(szLine);
nState = 2;
} else if (nState == 2) {
for (pch = szLine; *pch && *pch <= ' '; pch++)
;
while (*pch > ' ')
pch++;