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

destination.cpp

Go to the documentation of this file.
00001 // destination.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 #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             // Default unit of distance is the light year
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 }

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