00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <algorithm>
00011 #include <celutil/debug.h>
00012 #include <celutil/util.h>
00013 #include <celengine/celestia.h>
00014 #include <celengine/astro.h>
00015 #include <celengine/parser.h>
00016 #include "destination.h"
00017
00018 using namespace std;
00019
00020
00021 Destination::Destination() :
00022 name(""),
00023 target(""),
00024 distance(0.0),
00025 description("")
00026 {
00027 }
00028
00029
00030 DestinationList* ReadDestinationList(istream& in)
00031 {
00032 Tokenizer tokenizer(&in);
00033 Parser parser(&tokenizer);
00034 DestinationList* destinations = new DestinationList();
00035
00036 while (tokenizer.nextToken() != Tokenizer::TokenEnd)
00037 {
00038 if (tokenizer.getTokenType() != Tokenizer::TokenBeginGroup)
00039 {
00040 DPRINTF(0, "Error parsing destinations file.\n");
00041 for_each(destinations->begin(), destinations->end(), deleteFunc<Destination*>());
00042 delete destinations;
00043 return NULL;
00044 }
00045 tokenizer.pushBack();
00046
00047 Value* destValue = parser.readValue();
00048 if (destValue == NULL || destValue->getType() != Value::HashType)
00049 {
00050 DPRINTF(0, "Error parsing destination.\n");
00051 for_each(destinations->begin(), destinations->end(), deleteFunc<Destination*>());
00052 delete destinations;
00053 if (destValue != NULL)
00054 delete destValue;
00055 return NULL;
00056 }
00057
00058 Hash* destParams = destValue->getHash();
00059 Destination* dest = new Destination();
00060
00061 if (!destParams->getString("Name", dest->name))
00062 {
00063 DPRINTF(1, "Skipping unnamed destination\n");
00064 delete dest;
00065 }
00066 else
00067 {
00068 destParams->getString("Target", dest->target);
00069 destParams->getString("Description", dest->description);
00070 destParams->getNumber("Distance", dest->distance);
00071
00072
00073 string distanceUnits;
00074 if (destParams->getString("DistanceUnits", distanceUnits))
00075 {
00076 if (!compareIgnoringCase(distanceUnits, "km"))
00077 dest->distance = astro::kilometersToLightYears(dest->distance);
00078 else if (!compareIgnoringCase(distanceUnits, "au"))
00079 dest->distance = astro::AUtoLightYears(dest->distance);
00080 }
00081
00082 destinations->insert(destinations->end(), dest);
00083 }
00084
00085 delete destValue;
00086 }
00087
00088 return destinations;
00089 }