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

starname.cpp

Go to the documentation of this file.
00001 //
00002 // C++ Implementation: starname
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: Toti <root@totibox>, (C) 2005
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 
00013 #include <celengine/constellation.h>
00014 #include <celengine/starname.h>
00015 
00016 using namespace std;
00017 
00018 
00019 uint32 StarNameDatabase::findCatalogNumberByName(const string& name) const
00020 {
00021     string priName   = name;
00022     string altName;
00023 
00024     // See if the name is a Bayer or Flamsteed designation
00025     string::size_type pos  = name.find(' ');
00026     if (pos != 0 && pos != string::npos && pos < name.length() - 1)
00027     {
00028         string prefix(name, 0, pos);
00029         string conName(name, pos + 1, string::npos);
00030         Constellation* con  = Constellation::getConstellation(conName);
00031         if (con != NULL)
00032     {
00033             char digit  = ' ';
00034             int len     = prefix.length();
00035 
00036             // If the first character of the prefix is a letter
00037             // and the last character is a digit, we may have
00038             // something like 'Alpha2 Cen' . . . Extract the digit
00039             // before trying to match a Greek letter.
00040             if (len > 2 && isalpha(prefix[0]) && isdigit(prefix[len - 1]))
00041             {
00042                 --len;
00043                 digit   = prefix[len];
00044     }
00045 
00046             // We have a valid constellation as the last part
00047             // of the name.  Next, we see if the first part of
00048             // the name is a greek letter.
00049             const string& letter = Greek::canonicalAbbreviation(string(prefix, 0, len));
00050             if (letter != "")
00051             {
00052                 // Matched . . . this is a Bayer designation
00053                 if (digit == ' ')
00054                 {
00055                     priName  = letter + ' ' + con->getAbbreviation();
00056                     // If 'let con' doesn't match, try using
00057                     // 'let1 con' instead.
00058                     altName  = letter + '1' + ' ' + con->getAbbreviation();
00059                 }
00060     else
00061     {
00062                     priName  = letter + digit + ' ' + con->getAbbreviation();
00063                 }
00064             }
00065             else
00066         {
00067                 // Something other than a Bayer designation
00068                 priName  = prefix + ' ' + con->getAbbreviation();
00069             }
00070         }
00071     }
00072 
00073     uint32 catalogNumber   = getCatalogNumberByName(priName);
00074     if (catalogNumber != Star::InvalidCatalogNumber)
00075         return catalogNumber;
00076 
00077     priName        += " A";  // try by appending an A
00078     catalogNumber   = getCatalogNumberByName(priName);
00079     if (catalogNumber != Star::InvalidCatalogNumber)
00080         return catalogNumber;
00081 
00082     // If the first search failed, try using the alternate name
00083     if (altName.length() != 0)
00084     {
00085         catalogNumber   = getCatalogNumberByName(altName);
00086         if (catalogNumber == Star::InvalidCatalogNumber)
00087         {
00088             altName        += " A";
00089             catalogNumber   = getCatalogNumberByName(altName);
00090         }   // Intentional fallthrough.
00091     }
00092 
00093     return catalogNumber;
00094 }
00095 
00096 
00097 StarNameDatabase* StarNameDatabase::readNames(istream& in)
00098 {
00099     StarNameDatabase* db = new StarNameDatabase();
00100     bool failed = false;
00101     string s;
00102 
00103     while (!failed)
00104     {
00105         uint32 catalogNumber = Star::InvalidCatalogNumber;
00106 
00107         in >> catalogNumber;
00108         if (in.eof())
00109             break;
00110         if (in.bad())
00111         {
00112             failed = true;
00113             break;
00114         }
00115 
00116         // in.get(); // skip a space (or colon);
00117 
00118         string name;
00119         getline(in, name);
00120         if (in.bad())
00121         {
00122             failed = true;
00123             break;
00124         }
00125 
00126         // Iterate through the string for names delimited
00127         // by ':', and insert them into the star database. Note that
00128         // db->add() will skip empty names.
00129         string::size_type startPos = 0; 
00130         while (startPos != string::npos)
00131         {
00132             ++startPos;
00133             string::size_type next = name.find(':', startPos);
00134             string::size_type length = string::npos;
00135 
00136             if (next != string::npos)
00137                 length = next - startPos;
00138 
00139             db->add(catalogNumber, name.substr(startPos, length));
00140             startPos = next;
00141         }
00142     }
00143 
00144     if (failed)
00145     {
00146         delete db;
00147         return NULL;
00148     }
00149     else
00150     {
00151         return db;
00152     }
00153 }

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