destination.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // destination.cpp
  2. //
  3. // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. #include <algorithm>
  10. #include <celutil/debug.h>
  11. #include <celutil/util.h>
  12. #include <celengine/celestia.h>
  13. #include <celengine/astro.h>
  14. #include <celengine/parser.h>
  15. #include "destination.h"
  16. using namespace std;
  17. Destination::Destination() :
  18.     name(""),
  19.     target(""),
  20.     distance(0.0),
  21.     description("")
  22. {
  23. }
  24. DestinationList* ReadDestinationList(istream& in)
  25. {
  26.     Tokenizer tokenizer(&in);
  27.     Parser parser(&tokenizer);
  28.     DestinationList* destinations = new DestinationList();
  29.     while (tokenizer.nextToken() != Tokenizer::TokenEnd)
  30.     {
  31.         if (tokenizer.getTokenType() != Tokenizer::TokenBeginGroup)
  32.         {
  33.             DPRINTF(0, "Error parsing destinations file.n");
  34.             for_each(destinations->begin(), destinations->end(), deleteFunc<Destination*>());
  35.             delete destinations;
  36.             return NULL;
  37.         }
  38.         tokenizer.pushBack();
  39.         Value* destValue = parser.readValue();
  40.         if (destValue == NULL || destValue->getType() != Value::HashType)
  41.         {
  42.             DPRINTF(0, "Error parsing destination.n");
  43.             for_each(destinations->begin(), destinations->end(), deleteFunc<Destination*>());
  44.             delete destinations;
  45.             if (destValue != NULL)
  46.                 delete destValue;
  47.             return NULL;
  48.         }
  49.         Hash* destParams = destValue->getHash();
  50.         Destination* dest = new Destination();
  51.         
  52.         if (!destParams->getString("Name", dest->name))
  53.         {
  54.             DPRINTF(1, "Skipping unnamed destinationn");
  55.             delete dest;
  56.         }
  57.         else
  58.         {
  59.             destParams->getString("Target", dest->target);
  60.             destParams->getString("Description", dest->description);
  61.             destParams->getNumber("Distance", dest->distance);
  62.             // Default unit of distance is the light year
  63.             string distanceUnits;
  64.             if (destParams->getString("DistanceUnits", distanceUnits))
  65.             {
  66.                 if (!compareIgnoringCase(distanceUnits, "km"))
  67.                     dest->distance = astro::kilometersToLightYears(dest->distance);
  68.                 else if (!compareIgnoringCase(distanceUnits, "au"))
  69.                     dest->distance = astro::AUtoLightYears(dest->distance);
  70.             }
  71.             destinations->insert(destinations->end(), dest);
  72.         }
  73.         delete destValue;
  74.     }
  75.     return destinations;
  76. }