Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

asterism.cpp

Go to the documentation of this file.
00001 // asterism.cpp
00002 //
00003 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 
00010 #include <algorithm>
00011 
00012 #ifndef _WIN32
00013 #ifndef MACOSX_PB
00014 #include <config.h>
00015 #endif /* MACOSX_PB */
00016 #endif /* _WIN32 */
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 }

Generated on Sat Jan 14 22:30:26 2006 for Celestia by  doxygen 1.4.1