Skip to content

Commit

Permalink
Update satcat dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Mar 27, 2023
1 parent a1d0a75 commit 4c775f4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 54 deletions.
72 changes: 41 additions & 31 deletions lib/satcat/satcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@
#include "satcat.h"

#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

static bool isopstat(char c);
static int parse_int(char *str, unsigned len);
static double parse_dbl(char *str, unsigned len, unsigned loc_pt);
static void parse_date(struct SCDate *date, char *str);
static scbool isopstat(char c);
static int parse_int(const char *str, unsigned len);
static double parse_dbl(const char *str, unsigned len, unsigned loc_pt);
static void parse_date(const char *str, struct SCDate *date);

void sc_parse(struct SatCat *sc, char *str)
void sc_parse(struct SatCat *sc, const char *str)
{
memcpy(sc->id, str, 11);
#ifdef SC_CSTRING
sc->id[11] = '\0';
#endif
sc->catnum = parse_int(str + 13, 5);
sc->mul_names = (str[19] == 'M');
sc->payload = (str[20] == '*');
sc->opstat = str[21];
memcpy(sc->name, str + 23, 24);
#ifdef SC_CSTRING
sc->name[24] = '\0';
#endif
memcpy(sc->source, str + 49, 5);
parse_date(&sc->launch_date, str + 56);
#ifdef SC_CSTRING
sc->source[5] = '\0';
#endif
parse_date(str + 56, &sc->launch_date);
memcpy(sc->launch_site, str + 68, 5);
if (str[75] != ' ') {
parse_date(&sc->decay_date, str + 75);
parse_date(str + 75, &sc->decay_date);
} else {
sc->decay_date.year = 0;
sc->decay_date.month = 0;
Expand All @@ -46,34 +54,29 @@ void sc_parse(struct SatCat *sc, char *str)
else
sc->radar_cs = 0.0;
memcpy(sc->status_code, str + 129, 3);
#ifdef SC_CSTRING
sc->status_code[3] = '\0';
#endif
}

bool sc_validate(char *str)
scbool sc_validate(const char *str)
{
const char *fmt = "nnnn-nnnaaa nnnnn M*O aaaaaaaaaaaaaaaaaaaaaaaa aaaaa nnnn-nn-nn aaaaa nnnn-nn-nn NNNNn.n NNn.n NNNNNn NNNNNn NNn.nnnn aaa";
const char *fmt = "nnnn-nnnaaa nnnnn M*O aaaaaaaaaaaaaaaaaaaaaaaa aaaaa nnnn-nn-nn aaaaa lnnn-nn-nn NNNNn.n NNn.n NNNNNn NNNNNn LNn.nnnn aaa";
unsigned i;

/* Validate Length */
for (i = 0; i < 132; i++) {
/* Validate Length */
if (str[i] == '\0')
return 0;
}

/* Validate Contents */
for (i = 0; i < 132; i++) {
if (i == 75) {
/* Validate Contents */
switch (fmt[i]) {
case 'l':
if (!strncmp(" ", str + 75, 10)) {
i += 9;
continue;
}
} else if (i == 119) {
if (!strncmp(" N/A ", str + 119, 8)) {
i += 7;
continue;
}
}

switch (fmt[i]) {
__attribute__((fallthrough));
case 'n':
if (!isdigit(str[i]))
return 0;
Expand All @@ -91,8 +94,15 @@ bool sc_validate(char *str)
if (!isopstat(str[i]))
return 0;
break;
case 'L':
if (!strncmp(" N/A ", str + 119, 8)) {
i += 7;
continue;
}
__attribute__((fallthrough));
case 'N':
if ((fmt[i - 1] == 'N' && str[i - 1] != ' ' && str[i] == ' ') || (str[i] != ' ' && !isdigit(str[i])))
if ((fmt[i - 1] == 'N' && str[i - 1] != ' ' && str[i] == ' ')
|| (str[i] != ' ' && !isdigit(str[i])))
return 0;
break;
default:
Expand All @@ -103,7 +113,7 @@ bool sc_validate(char *str)
return 1;
}

bool isopstat(char c)
scbool isopstat(const char c)
{
switch (c) {
case '+':
Expand All @@ -121,23 +131,23 @@ bool isopstat(char c)
}
}

int parse_int(char *str, unsigned len)
int parse_int(const char *str, const unsigned len)
{
char buf[8];
memcpy(buf, str, len);
buf[len] = '\0';
return strtol(buf, NULL, 10);
}

double parse_dbl(char *str, unsigned len, unsigned loc_pt)
double parse_dbl(const char *str, const unsigned len, const unsigned loc_pt)
{
unsigned decim_places = len - loc_pt - 1;
int integral = parse_int(str, loc_pt);
int fract = parse_int(str + loc_pt + 1, decim_places);
const unsigned decim_places = len - loc_pt - 1;
const int integral = parse_int(str, loc_pt);
const int fract = parse_int(str + loc_pt + 1, decim_places);
return integral + fract / pow(10.0, decim_places);
}

void parse_date(struct SCDate *date, char *str)
void parse_date(const char *str, struct SCDate *date)
{
date->year = parse_int(str, 4);
date->month = parse_int(str + 5, 2);
Expand Down
27 changes: 17 additions & 10 deletions lib/satcat/satcat.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@

#ifdef __STDC_VERSION__
#include <stdbool.h>
typedef bool scbool;
#else
typedef int bool;
typedef int scbool;
#endif

#ifdef SC_CSTRING
#define SC_TERM 1
#else
#define SC_TERM 0
#endif

struct SCDate {
Expand All @@ -22,25 +29,25 @@ struct SCDate {
};

struct SatCat {
char id[12]; /* International Designator */ /* NOTE: NULL-Terminated */
char id[11 + SC_TERM]; /* International Designator */
unsigned catnum; /* NORAD Catalog Number */
bool mul_names; /* Multiple Name Flag */
bool payload; /* Payload Flag */
scbool mul_names; /* Multiple Name Flag */
scbool payload; /* Payload Flag */
char opstat; /* Operational Status Code */
char name[24]; /* Satellite Name */
char source[5]; /* Source or Ownership */
char name[24 + SC_TERM]; /* Satellite Name */
char source[5 + SC_TERM]; /* Source or Ownership */
struct SCDate launch_date; /* Launch Date */
char launch_site[5]; /* Launch Site */
char launch_site[5 + SC_TERM]; /* Launch Site */
struct SCDate decay_date; /* Decay Date [Optional] */
double period; /* Orbital Period (minutes) */
double inc_deg; /* Inclination (degrees) */
unsigned apogee; /* Apogee Altitude (kilometers) */
unsigned perigee; /* Perigee Altitude (kilometers) */
double radar_cs; /* Radar Cross Section (meters^2) [Optional] */
char status_code[3]; /* Orbital Status Code */
char status_code[3 + SC_TERM]; /* Orbital Status Code */
};

void sc_parse(struct SatCat *sc, char *str);
bool sc_validate(char *str);
void sc_parse(struct SatCat *sc, const char *str);
scbool sc_validate(const char *str);

#endif /* SATCAT_H */
8 changes: 6 additions & 2 deletions lib/satcat/satcat_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const char *sc_status_str(const char code)
}
}

const char *sc_source_str(const sc_code_t code){
const char *sc_source_str(const sc_code_t code)
{
switch (code) {
case SCSRC_AB :
return "Arab Satellite Communications Organization";
Expand Down Expand Up @@ -162,6 +163,8 @@ const char *sc_source_str(const sc_code_t code){
return "Morroco";
case SCSRC_MALA:
return "Malaysia";
case SCSRC_MDA :
return "Republic of Moldova";
case SCSRC_MEX :
return "Mexico";
case SCSRC_MMR :
Expand Down Expand Up @@ -275,7 +278,8 @@ const char *sc_source_str(const sc_code_t code){
}
}

const char *sc_launch_site_str(const sc_code_t code){
const char *sc_launch_site_str(const sc_code_t code)
{
switch (code) {
case SCSITE_AFETR:
return "Air Force Eastern Test Range, Florida, USA";
Expand Down
13 changes: 3 additions & 10 deletions lib/satcat/satcat_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@
#ifndef SATCAT_CODE_H
#define SATCAT_CODE_H

#ifdef __cplusplus
extern "C"{
#endif

#ifdef _ISOC99_SOURCE
#include <stdint.h>
typedef uint64_t sc_code_t;
#else
#include <limits.h>
#if (UINT_MAX >= 0xFFFFFFFFFF)
#if (UINT_MAX >= 0xFFFFFFFFFFUL)
typedef unsigned sc_code_t;
#elif (ULONG_MAX >= 0xFFFFFFFFFF)
#elif (ULONG_MAX >= 0xFFFFFFFFFFUL)
typedef unsigned long sc_code_t;
#else
typedef unsigned long long sc_code_t;
Expand Down Expand Up @@ -113,6 +109,7 @@ const char *sc_launch_site_str(sc_code_t code);
#define SCSRC_LUXE (sc_code_t)138602370380UL
#define SCSRC_MA (sc_code_t)137977938253UL
#define SCSRC_MALA (sc_code_t)138534469965UL
#define SCSRC_MDA (sc_code_t)137980101709UL
#define SCSRC_MEX (sc_code_t)137981609293UL
#define SCSRC_MMR (sc_code_t)137981218125UL
#define SCSRC_MNG (sc_code_t)137980497485UL
Expand Down Expand Up @@ -204,8 +201,4 @@ const char *sc_launch_site_str(sc_code_t code);
#define SCSITE_YAVNE (sc_code_t)297667019097UL
#define SCSITE_YUN (sc_code_t)137980958041UL

#ifdef __cplusplus
}
#endif

#endif /* SATCAT_CODE_H */
1 change: 1 addition & 0 deletions src/util/satcat_code_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const sc_code_t SCSRC[NUM_SCSRC] = {
SCSRC_LUXE,
SCSRC_MA,
SCSRC_MALA,
SCSRC_MDA,
SCSRC_MEX,
SCSRC_MMR,
SCSRC_MNG,
Expand Down
2 changes: 1 addition & 1 deletion src/util/satcat_code_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "satcat_code.h"

#define NUM_SCSTAT 8
#define NUM_SCSRC 117
#define NUM_SCSRC 118
#define NUM_SCSITE 34

extern const char SCSTAT[NUM_SCSTAT];
Expand Down

0 comments on commit 4c775f4

Please sign in to comment.