1 /*
2 # This file is part of the Astrometry.net suite.
3 # Licensed under a 3-clause BSD style license - see LICENSE
4 */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "openngc.h"
11 #include "bl.h"
12 #include "ioutils.h"
13
14 struct ngc_name {
15 anbool is_ngc;
16 int id;
17 char* name;
18 };
19 typedef struct ngc_name ngc_name;
20
21 ngc_name ngc_names[] = {
22 #include "openngc-names.c"
23 };
24
25 ngc_entry ngc_entries[] = {
26 #include "openngc-entries.c"
27 };
28
n_names()29 static int n_names() {
30 return sizeof(ngc_names) / sizeof(ngc_name);
31 }
32
ngc_num_entries()33 int ngc_num_entries() {
34 return sizeof(ngc_entries) / sizeof(ngc_entry);
35 }
36
ngc_get_entry(int i)37 ngc_entry* ngc_get_entry(int i) {
38 if (i < 0)
39 return NULL;
40 if (i >= ngc_num_entries())
41 return NULL;
42 return ngc_entries + i;
43 }
44
ngc_get_ngcic_num(int is_ngc,int num)45 ngc_entry* ngc_get_ngcic_num(int is_ngc, int num) {
46 int i, N;
47 N = ngc_num_entries();
48 for (i=0; i<N; i++) {
49 ngc_entry* e = ngc_get_entry(i);
50 if (e->is_ngc == is_ngc && e->id == num)
51 return e;
52 }
53 return NULL;
54 }
55
ngc_get_entry_named(const char * name)56 ngc_entry* ngc_get_entry_named(const char* name) {
57 if (starts_with(name, "NGC") || starts_with(name, "IC")) {
58 int num;
59 const char* cptr;
60 anbool isngc;
61 isngc = starts_with(name, "NGC");
62 cptr = name + (isngc ? 3 : 2);
63 if (*cptr == ' ')
64 cptr++;
65 num = atoi(cptr);
66 if (!num)
67 return NULL;
68 return ngc_get_ngcic_num(isngc, num);
69 } else {
70 int i, N;
71 N = n_names();
72 for (i=0; i<N; i++) {
73 char nsname[256];
74 const char* src;
75 char* dest;
76 if (streq(name, ngc_names[i].name))
77 return ngc_get_ngcic_num(ngc_names[i].is_ngc, ngc_names[i].id);
78 // Try without spaces
79 dest = nsname;
80 src = ngc_names[i].name;
81 for (; *src; src++) {
82 if (*src == ' ')
83 continue;
84 *dest = *src;
85 dest++;
86 }
87 *dest = '\0';
88 //printf("Spaceless name: '%s'\n", nsname);
89 if (streq(name, nsname))
90 return ngc_get_ngcic_num(ngc_names[i].is_ngc, ngc_names[i].id);
91 }
92 }
93 return NULL;
94 }
95
ngc_get_name(ngc_entry * entry,int num)96 char* ngc_get_name(ngc_entry* entry, int num) {
97 int i;
98 for (i=0; i<sizeof(ngc_names)/sizeof(ngc_name); i++) {
99 if ((entry->is_ngc == ngc_names[i].is_ngc) &&
100 (entry->id == ngc_names[i].id)) {
101 if (num == 0)
102 return ngc_names[i].name;
103 else
104 num--;
105 }
106 }
107 return NULL;
108 }
109
ngc_get_names(ngc_entry * entry,sl * lst)110 sl* ngc_get_names(ngc_entry* entry, sl* lst) {
111 int i;
112 if (!lst)
113 lst = sl_new(4);
114 sl_appendf(lst, "%s %i", (entry->is_ngc ? "NGC" : "IC"), entry->id);
115 for (i=0; i<sizeof(ngc_names)/sizeof(ngc_name); i++) {
116 if ((entry->is_ngc == ngc_names[i].is_ngc) &&
117 (entry->id == ngc_names[i].id)) {
118 sl_append(lst, ngc_names[i].name);
119 }
120 }
121 return lst;
122 }
123
ngc_get_name_list(ngc_entry * entry,const char * separator)124 char* ngc_get_name_list(ngc_entry* entry, const char* separator) {
125 char* str;
126 sl* lst = ngc_get_names(entry, NULL);
127 str = sl_implode(lst, separator);
128 sl_free2(lst);
129 return str;
130 }
131
132