00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <algorithm>
00011
00012 #ifndef _WIN32
00013 #ifndef MACOSX_PB
00014 #include <config.h>
00015 #endif
00016 #endif
00017
00018 #include <celutil/util.h>
00019 #include <celutil/debug.h>
00020 #include "parser.h"
00021 #include "asterism.h"
00022
00023 using namespace std;
00024
00025
00026 Asterism::Asterism(string _name) : name(_name)
00027 {
00028 i18nName = _(_name.c_str());
00029 }
00030
00031 Asterism::~Asterism()
00032 {
00033 }
00034
00035
00036 string Asterism::getName(bool i18n) const
00037 {
00038 return i18n?i18nName:name;
00039 }
00040
00041 int Asterism::getChainCount() const
00042 {
00043 return chains.size();
00044 }
00045
00046 const Asterism::Chain& Asterism::getChain(int index) const
00047 {
00048 return *chains[index];
00049 }
00050
00051 void Asterism::addChain(Asterism::Chain& chain)
00052 {
00053 chains.insert(chains.end(), &chain);
00054 }
00055
00056
00057 AsterismList* ReadAsterismList(istream& in, const StarDatabase& stardb)
00058 {
00059 AsterismList* asterisms = new AsterismList();
00060 Tokenizer tokenizer(&in);
00061 Parser parser(&tokenizer);
00062
00063 while (tokenizer.nextToken() != Tokenizer::TokenEnd)
00064 {
00065 if (tokenizer.getTokenType() != Tokenizer::TokenString)
00066 {
00067 DPRINTF(0, "Error parsing asterism file.\n");
00068 for_each(asterisms->begin(), asterisms->end(), deleteFunc<Asterism*>());
00069 delete asterisms;
00070 return NULL;
00071 }
00072
00073 string name = tokenizer.getStringValue();
00074 Asterism* ast = new Asterism(name);
00075
00076 Value* chainsValue = parser.readValue();
00077 if (chainsValue == NULL || chainsValue->getType() != Value::ArrayType)
00078 {
00079 DPRINTF(0, "Error parsing asterism %s\n", name.c_str());
00080 for_each(asterisms->begin(), asterisms->end(), deleteFunc<Asterism*>());
00081 delete asterisms;
00082 return NULL;
00083 }
00084
00085 Array* chains = chainsValue->getArray();
00086
00087 for (int i = 0; i < (int) chains->size(); i++)
00088 {
00089 if ((*chains)[i]->getType() == Value::ArrayType)
00090 {
00091 Array* a = (*chains)[i]->getArray();
00092 Asterism::Chain* chain = new Asterism::Chain();
00093 for (Array::const_iterator iter = a->begin(); iter != a->end(); iter++)
00094 {
00095 if ((*iter)->getType() == Value::StringType)
00096 {
00097 Star* star = stardb.find((*iter)->getString());
00098 if (star != NULL)
00099 chain->insert(chain->end(), star->getPosition());
00100 }
00101 }
00102
00103 ast->addChain(*chain);
00104 }
00105 }
00106
00107 asterisms->insert(asterisms->end(), ast);
00108
00109 delete chainsValue;
00110 }
00111
00112 return asterisms;
00113 }