Skip to content

Commit

Permalink
Try to fix issue about lower vs upper case in different versions of s…
Browse files Browse the repository at this point in the history
…tarbook
  • Loading branch information
knro committed Jul 17, 2019
1 parent a72d12e commit 2c40dd7
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions indi-starbook/starbook_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@ using namespace std;

constexpr char sep = '+';

starbook::DMS::DMS(string dms) : ln_dms{0, 0, 0, 0} {
starbook::DMS::DMS(string dms) : ln_dms{0, 0, 0, 0}
{
static regex pattern(R"((-?)(\d+)\+(\d+))");
smatch results;
if (regex_search(dms, results, pattern)) {
if (regex_search(dms, results, pattern))
{
neg = (unsigned short) (results[1].str().empty() ? 0 : 1);
degrees = (unsigned short) stoi(results[2].str());
minutes = (unsigned short) stoi(results[3].str());
seconds = 0;
} else {
}
else
{
throw;
}
}

ostream &starbook::operator<<(ostream &os, const starbook::DMS &obj) {
ostream &starbook::operator<<(ostream &os, const starbook::DMS &obj)
{
if (obj.neg != 0) os << "-";
os << fixed << setprecision(0) << setfill('0')
<< setw(3) << obj.degrees
Expand All @@ -51,7 +56,8 @@ ostream &starbook::operator<<(ostream &os, const starbook::DMS &obj) {
return os;
}

ostream &starbook::operator<<(ostream &os, const starbook::HMS &obj) {
ostream &starbook::operator<<(ostream &os, const starbook::HMS &obj)
{
os << fixed << setprecision(0) << setfill('0')
<< setw(2) << obj.hours
<< setw(0) << sep
Expand All @@ -61,13 +67,15 @@ ostream &starbook::operator<<(ostream &os, const starbook::HMS &obj) {
return os;
}

std::istream &starbook::operator>>(std::istream &is, starbook::HMS &obj) {
std::istream &starbook::operator>>(std::istream &is, starbook::HMS &obj)
{
unsigned short h, m, m_tenth;
std::array<char, 2> ch = {{'\0'}};
is >> h >> ch[0] >> m >> ch[1] >> m_tenth;

if (!is) return is;
if (ch[0] != sep || ch[1] != '.') {
if (ch[0] != sep || ch[1] != '.')
{
is.clear(ios_base::failbit);
return is;
}
Expand All @@ -76,12 +84,14 @@ std::istream &starbook::operator>>(std::istream &is, starbook::HMS &obj) {
}

starbook::Equ::Equ(double ra, double dec) : lnh_equ_posn{{0, 0, 0},
{0, 0, 0, 0}} {
{0, 0, 0, 0}}
{
ln_equ_posn target_d = {ra, dec};
ln_equ_to_hequ(&target_d, this);
}

ostream &starbook::operator<<(ostream &os, const starbook::Equ &obj) {
ostream &starbook::operator<<(ostream &os, const starbook::Equ &obj)
{
os << "RA=";
os << static_cast<const HMS &> (obj.ra);

Expand All @@ -90,7 +100,8 @@ ostream &starbook::operator<<(ostream &os, const starbook::Equ &obj) {
return os;
}

ostream &starbook::operator<<(ostream &os, const starbook::DateTime &obj) {
ostream &starbook::operator<<(ostream &os, const starbook::DateTime &obj)
{
os << setfill('0') << std::fixed << setprecision(0)
<< obj.years << sep
<< setw(2) << obj.months << sep
Expand All @@ -101,24 +112,27 @@ ostream &starbook::operator<<(ostream &os, const starbook::DateTime &obj) {
return os;
}

std::istream &starbook::operator>>(std::istream &is, starbook::DateTime &utc) {
std::istream &starbook::operator>>(std::istream &is, starbook::DateTime &utc)
{
int Y, M, D, h, m, s;
std::array<char, 5> ch = {{'\0'}};
is >> Y >> ch[0] >> M >> ch[1] >> D >> ch[2] >> h >> ch[3] >> m >> ch[4] >> s;

if (!is) return is;
for (char i : ch)
if (i != sep) {
if (i != sep)
{
is.clear(ios_base::failbit);
return is;
}
utc = DateTime(Y, M, D, h, m, static_cast<double>(s));
return is;
}

std::ostream &starbook::operator<<(std::ostream &os, const starbook::LnLat &obj) {
std::ostream &starbook::operator<<(std::ostream &os, const starbook::LnLat &obj)
{
lnh_lnlat_posn dms{{0, 0, 0, 0},
{0, 0, 0, 0}};
{0, 0, 0, 0}};
auto tmp = static_cast<ln_lnlat_posn>(obj);
ln_lnlat_to_hlnlat(&tmp, &dms);

Expand All @@ -130,12 +144,16 @@ std::ostream &starbook::operator<<(std::ostream &os, const starbook::LnLat &obj)
return os;
}

starbook::CommandResponse::CommandResponse(const std::string &url_like) : status(OK), raw(url_like) {
starbook::CommandResponse::CommandResponse(const std::string &url_like) : status(OK), raw(url_like)
{
if (url_like.empty()) throw runtime_error("parsing error, no payload");
if (url_like.rfind("OK", 0) == 0) {
if (url_like.rfind("OK", 0) == 0)
{
status = OK;
return;
} else if (url_like.rfind("ERROR", 0) == 0) {
}
else if (url_like.rfind("ERROR", 0) == 0)
{
if (url_like.rfind("ERROR:FORMAT", 0) == 0)
status = ERROR_FORMAT;
else if (url_like.rfind("ERROR:ILLEGAL STATE", 0) == 0)
Expand All @@ -145,20 +163,30 @@ starbook::CommandResponse::CommandResponse(const std::string &url_like) : status
else
status = ERROR_UNKNOWN;
return;
} else {
}
else
{
std::string str_remaining = url_like;
std::regex param_re(R"((\w+)=(\-?[\w\+\.]+))");
std::smatch sm;

while (regex_search(str_remaining, sm, param_re)) {
while (regex_search(str_remaining, sm, param_re))
{
std::string key = sm[1].str();
std::string value = sm[2].str();

// JM 2017-07-17: Should we make all uppercase to get around different version incompatibilities?
std::transform(key.begin(), key.end(), key.begin(), ::toupper);
std::transform(value.begin(), value.end(), value.begin(), ::toupper);

payload[key] = value;
str_remaining = sm.suffix();
}
if (payload.empty()) throw std::runtime_error("parsing error, couldn't parse any field");
if (!str_remaining.empty()) throw std::runtime_error("parsing error, couldn't parse full payload");

if (payload.empty())
throw std::runtime_error("parsing error, could not parse any field");
if (!str_remaining.empty())
throw std::runtime_error("parsing error, could not parse full payload");
status = OK;
}
}

0 comments on commit 2c40dd7

Please sign in to comment.