-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSkyCatalog.cpp
More file actions
707 lines (566 loc) · 13.9 KB
/
Copy pathSkyCatalog.cpp
File metadata and controls
707 lines (566 loc) · 13.9 KB
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
/*
* SkyCatalog.cpp
*
* Copyright (c) 2017 by Sebastien MARCHAND (Web:www.marscaper.com, Email:sebastien@marscaper.com)
*/
/*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SkyCatalog.hpp"
char *newPathByAppendingPaths(char *path, char *path1, char *path2, char *path3)
{
int bufferSize = strlen(path)+1;
if( path1 )
{
bufferSize += strlen(path1)+1;
}
if( path2 )
{
bufferSize += strlen(path2)+1;
}
if( path3 )
{
bufferSize += strlen(path3)+1;
}
// Create a new empty string
char *fullPath = (char*)calloc(bufferSize,sizeof(char));
// Copy first path
strcpy(fullPath, path);
if( path1 )
{
// Add / separator
strncpy(&fullPath[strlen(fullPath)],"/",1);
// Copy path
strncpy(&fullPath[strlen(fullPath)],path1,strlen(path1));
}
if( path2 )
{
// Add / separator
strncpy(&fullPath[strlen(fullPath)],"/",1);
// Copy path
strncpy(&fullPath[strlen(fullPath)],path2,strlen(path2));
}
if( path3 )
{
// Add / separator
strncpy(&fullPath[strlen(fullPath)],"/",1);
// Copy path
strncpy(&fullPath[strlen(fullPath)],path3,strlen(path3));
}
return fullPath;
}
void readLine(File *file, char *string, int stringLength)
{
//get bytes from file
int strIndex = 0;
char inputChar = file->read();
while (inputChar != '\n' && inputChar != EOF && strIndex<stringLength)
{
string[strIndex] = inputChar;
strIndex++;
inputChar = file->read();
}
// Clip string if needed
if( strIndex == stringLength )
{
strIndex--;
}
// Set null terminated string char for security
string[strIndex] = '\0';
}
#if USE_SD_FAT
int countFile(SdFat *sd, char *path, char *cntName)
#else
int countFile(SDClass *sd, char *path, char *cntName)
#endif
{
int count = -1;
// Simple test to check if directory contains "star.cnt"
char *cntPath = newPathByAppendingPaths(path,cntName,NULL,NULL);
// Open file
File cntFile = sd->open(cntPath);
// Free string path
free(cntPath);
// Does file exist?
if(cntFile)
{
char string[6];
readLine(&cntFile, string, 6);
// Convert to int value
count = atoi(string);
// Close file
cntFile.close();
}
return count;
}
SkyCatalog::SkyCatalog()
{
#if USE_SD_FAT
_sd = new SdFat();
#else
_sd = &SD;
#endif
}
SkyCatalog::~SkyCatalog()
{
delete _sd;
free(_dbDirectory);
closeObjectList();
closeConstellationList();
}
bool SkyCatalog::initDatabase(int sd_pin,char *directory)
{
// Init SD
#if USE_SD_FAT
if (!_sd->begin(sd_pin, SPI_FULL_SPEED))
#else
if (!_sd->begin(sd_pin))
#endif
{
return false;
}
// Simple test to check if directory contains "star.cnt"
if( countFile(_sd,directory,"star.cnt") == -1 )
{
return false;
}
// Copy db path
_dbDirectory = (char*)malloc(strlen(directory)+1*sizeof(char));
strcpy(_dbDirectory, directory);
return true;
}
SkyDbDescrStruct SkyCatalog::databaseDescription()
{
SkyDbDescrStruct dbDescr;
dbDescr.starCount = countFile(_sd,_dbDirectory,"star.cnt");
dbDescr.messierCount = countFile(_sd,_dbDirectory,"messier.cnt");
dbDescr.ngcCount = countFile(_sd,_dbDirectory,"ngc.cnt");
dbDescr.icCount = countFile(_sd,_dbDirectory,"ic.cnt");
return dbDescr;
}
SkyObjectStruct SkyCatalog::object(char *consAbrv, long objIndex, DatabaseObjectType objType)
{
// Create path
char fileName[11];
sprintf(fileName, "%06d.txt\0",objIndex);
// Get type directory according to objType
char *type = NULL;
switch (objType)
{
case DatabaseObjectStar:
{
type = "star";
break;
}
case DatabaseObjectMessier:
{
type = "messier";
break;
}
case DatabaseObjectNGC:
{
type = "ngc";
break;
}
case DatabaseObjectIC:
{
type = "ic";
break;
}
}
// Create path to object directory
char *path;
if( consAbrv )
{
// Filter by constellation
// Add "const" directory to Abbreviation path
consAbrv = newPathByAppendingPaths("const", consAbrv, NULL, NULL);
path = newPathByAppendingPaths(_dbDirectory,consAbrv,type,fileName);
// Remove allocated path
free(consAbrv);
}
else
{
// All objects
path = newPathByAppendingPaths(_dbDirectory,type,fileName,NULL);
}
// Open file
File objectFile = _sd->open(path);
// Delete path
free(path);
// Return object from file
SkyObjectStruct object = readObject(&objectFile,objIndex,objType);
objectFile.close();
return object;
}
bool SkyCatalog::openObjectList(char *consAbrv, DatabaseObjectType objectType)
{
if( !_dbDirectory )
{
return false;
}
_currentObjetListType = objectType;
// Get type directory according to objType
char *type = NULL;
switch (objectType)
{
case DatabaseObjectStar:
{
type = "star";
break;
}
case DatabaseObjectMessier:
{
type = "messier";
break;
}
case DatabaseObjectNGC:
{
type = "ngc";
break;
}
case DatabaseObjectIC:
{
type = "ic";
break;
}
}
// Create path to object directory
char *path;
if( consAbrv )
{
// Filter by constellation
path = newPathByAppendingPaths(_dbDirectory, "const", consAbrv, type);
}
else
{
// All objects
path = newPathByAppendingPaths(_dbDirectory, type, NULL, NULL);
}
// No file
_fileObjectIndex = 0;
// Close old directory if open
if( _currentObjectDir ) _currentObjectDir.close();
// Open directory
_currentObjectDir = _sd->open(path);
// Remove allocated path
free(path);
return _currentObjectDir;
}
SkyObjectStruct SkyCatalog::readObject(File *objectFile, long objIndex, DatabaseObjectType objType)
{
SkyObjectStruct skyObject;
// Clean object
memset(&skyObject,0,sizeof(SkyObjectStruct));
if( *objectFile != false )
{
char tmpString[12];
int tmpStringSize = sizeof(tmpString);
// Valid index
skyObject.index = objIndex;
// Name
readLine(objectFile, skyObject.name, sizeof(skyObject.name));
// Designation
readLine(objectFile, skyObject.designation, sizeof(skyObject.designation));
// Const Abbreviation
readLine(objectFile, skyObject.constAbrv, sizeof(skyObject.constAbrv));
// R.A
readLine(objectFile, tmpString, tmpStringSize);
skyObject.ra = atof(tmpString);
// Dec
readLine(objectFile, tmpString, tmpStringSize);
skyObject.dec = atof(tmpString);
// Visual magnitude
readLine(objectFile, tmpString, tmpStringSize);
skyObject.magnitude = atof(tmpString);
if( objType == DatabaseObjectStar )
{
// drift RA per year
readLine(objectFile, tmpString, tmpStringSize);
skyObject.dRa = atof(tmpString);
// drift Dec per year
readLine(objectFile, tmpString, tmpStringSize);
skyObject.dDec = atof(tmpString);
// Parallax
readLine(objectFile, tmpString, tmpStringSize);
skyObject.parallax = atof(tmpString);
}
else
{
skyObject.dRa = NAN;
skyObject.dDec = NAN;
skyObject.parallax = NAN;
}
}
return skyObject;
}
bool SkyCatalog::currentObjectInList(SkyObjectStruct *object)
{
bool res = false;
if( _currentObjectFile )
{
// Get filename
#if USE_SD_FAT
char name[14];
_currentObjectFile.getName(name,14);
#else
char *name = _currentObjectFile.name();
#endif
// Extension should be txt
int extIdx = strlen(name)-4;
if( strstr(strlwr(name+extIdx), "txt"))
{
// Remove extension
name[extIdx] = '\0';
// Read object
*object = readObject(&_currentObjectFile, atoi(name), _currentObjetListType);
res = true;
}
}
else
{
// Clean object
memset(object,0,sizeof(SkyObjectStruct));
}
return res;
}
bool SkyCatalog::gotoNextObjectInList()
{
bool res = false;
if( _currentObjectDir )
{
if( _currentObjectFile )
{
_currentObjectFile.close();
}
// Go to next file if exists
File nextFile = _currentObjectDir.openNextFile();
// Is there a valid file?
if( nextFile )
{
_currentObjectFile = nextFile;
_fileObjectIndex++;
res = true;
}
}
return res;
}
bool SkyCatalog::gotoPreviousObjectInList()
{
bool res = false;
if( (_fileObjectIndex-1) && _currentObjectDir )
{
// If file already open. Close it.
if( _currentObjectFile )
{
_currentObjectFile.close();
}
// Go to beginning
unsigned int index = 1;
_currentObjectDir.rewindDirectory();
_currentObjectFile = _currentObjectDir.openNextFile();
// Scroll down
while (index != _fileObjectIndex-1 )
{
// Close current
_currentObjectFile.close();
// Open next
_currentObjectFile = _currentObjectDir.openNextFile();
index++;
}
// Is there a valid file?
if( _currentObjectFile )
{
_fileObjectIndex = index;
res = true;
}
else
{
// No valid file
_fileObjectIndex = 0;
}
}
return res;
}
void SkyCatalog::closeObjectList()
{
_fileObjectIndex = 0;
_currentObjetListType = SkyObjectNone;
if( _currentObjectDir ) _currentObjectDir.close();
if( _currentObjectFile ) _currentObjectFile.close();
}
#pragma mark -
#pragma mark Constellation
bool SkyCatalog::openConstellationList()
{
if( !_dbDirectory )
{
return false;
}
char *path = newPathByAppendingPaths(_dbDirectory, "const", NULL, NULL);
// No file
_fileConstIndex = 0;
// Close old directory if open
if( _currentConstDir ) _currentConstDir.close();
// Open directory
_currentConstDir = _sd->open(path);
// Remove allocated path
free(path);
return _currentConstDir;
}
bool SkyCatalog::gotoNextConstellationInList()
{
bool res = false;
if( _currentConstDir )
{
if( _currentConstFile )
{
_currentConstFile.close();
}
// Go to next file if exists
File nextFile = _currentConstDir.openNextFile();
// Is there a valid file?
if( nextFile )
{
_currentConstFile = nextFile;
_fileConstIndex++;
res = true;
}
}
return res;
}
bool SkyCatalog::gotoPreviousConstellationInList()
{
bool res = false;
if( (_fileConstIndex-1) && _currentConstDir )
{
// If file already open. Close it.
if( _currentConstFile )
{
_currentConstFile.close();
}
// Go to beginning
unsigned int index = 1;
_currentConstDir.rewindDirectory();
_currentConstFile = _currentConstDir.openNextFile();
// Scroll down
while (index != _fileConstIndex-1 )
{
// Close current
_currentConstFile.close();
// Open next
_currentConstFile = _currentConstDir.openNextFile();
index++;
}
// Is there a valid file?
if( _currentConstFile )
{
_fileConstIndex = index;
res = true;
}
else
{
// No valid file
_fileConstIndex = 0;
}
}
return res;
}
SkyConstStruct SkyCatalog::constellation(char *consAbrv)
{
SkyConstStruct object;
// Get translation path
char *tslPath;
switch (_language)
{
case SkyTranslateLatin:
tslPath = newPathByAppendingPaths(_dbDirectory, "const", consAbrv, "lat.tsl");
break;
case SkyTranslateFrench:
tslPath = newPathByAppendingPaths(_dbDirectory, "const", consAbrv, "fra.tsl");
break;
default:
tslPath = newPathByAppendingPaths(_dbDirectory, "const", consAbrv, "eng.tsl");
break;
}
File tslFile = _sd->open(tslPath,FILE_READ);
if( tslFile )
{
// Read translation
readLine(&tslFile, object.fullName, sizeof(object.fullName));
tslFile.close();
}
else
{
// Use abbreviation instead
strcpy(object.fullName, consAbrv);
}
strcpy(object.abrv, consAbrv);
free(tslPath);
return object;
}
bool SkyCatalog::currentConstellationInList(SkyConstStruct *object)
{
bool res = false;
if( _currentConstFile )
{
// Get filename
#if USE_SD_FAT
char name[14];
_currentConstFile.getName(name,14);
strcpy(object->abrv, name);
#else
strcpy(object->abrv, _currentConstFile.name());
#endif
// Get translation path
char *tslPath;
switch (_language)
{
case SkyTranslateLatin:
tslPath = newPathByAppendingPaths(_dbDirectory, "const", object->abrv, "lat.tsl");
break;
case SkyTranslateFrench:
tslPath = newPathByAppendingPaths(_dbDirectory, "const", object->abrv, "fra.tsl");
break;
default:
tslPath = newPathByAppendingPaths(_dbDirectory, "const", object->abrv, "eng.tsl");
break;
}
File tslFile = _sd->open(tslPath,FILE_READ);
if( tslFile )
{
// Read translation
readLine(&tslFile, object->fullName, sizeof(object->fullName));
tslFile.close();
}
else
{
// Use abbreviation instead
strcpy(object->fullName, object->abrv);
}
free(tslPath);
}
else
{
// Clean object
memset(object,0,sizeof(SkyConstStruct));
}
return res;
}
void SkyCatalog::closeConstellationList()
{
_fileConstIndex = 0;
if( _currentConstDir ) _currentConstDir.close();
if( _currentConstFile ) _currentConstFile.close();
}