Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GoKomi.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file GoKomi.cpp
00003 */
00004 //----------------------------------------------------------------------------
00005 
00006 #include "SgSystem.h"
00007 #include "GoKomi.h"
00008 
00009 #include <sstream>
00010 
00011 using namespace std;
00012 
00013 //----------------------------------------------------------------------------
00014 
00015 namespace {
00016 
00017 string GetInvalidKomiErrorMessage(float komi)
00018 {
00019     ostringstream buffer;
00020     buffer << "Invalid komi value: " << komi;
00021     return buffer.str();
00022 }
00023 
00024 } // namespace
00025 
00026 //----------------------------------------------------------------------------
00027 
00028 GoKomi::InvalidKomi::InvalidKomi(float komi)
00029     : SgException(GetInvalidKomiErrorMessage(komi))
00030 {
00031 }
00032 
00033 GoKomi::InvalidKomi::InvalidKomi(const string& komi)
00034     : SgException("Invalid komi value: " + komi)
00035 {
00036 }
00037 
00038 //----------------------------------------------------------------------------
00039 
00040 GoKomi::GoKomi(const std::string& komi)
00041 {
00042     {
00043         istringstream buffer(komi);
00044         string trimmedString;
00045         buffer >> trimmedString;
00046         if (! buffer)
00047         {
00048             m_isUnknown = true;
00049             m_value = 0;
00050             return;
00051         }
00052     }
00053     {
00054         istringstream buffer(komi);
00055         float value;
00056         buffer >> value;
00057         if (! buffer)
00058             throw InvalidKomi(komi);
00059         *this = GoKomi(value);
00060     }
00061 }
00062 
00063 string GoKomi::ToString() const
00064 {
00065     if (m_isUnknown)
00066         return "";
00067     if (m_value % 2 == 0)
00068     {
00069         ostringstream buffer;
00070         buffer << (m_value / 2);
00071         return buffer.str();
00072     }
00073     else if (m_value == -1)
00074         return "-0.5";
00075     else
00076     {
00077         ostringstream buffer;
00078         buffer << (m_value / 2) << ".5";
00079         return buffer.str();
00080     }
00081 }
00082 
00083 //----------------------------------------------------------------------------


17 Jun 2010 Doxygen 1.4.7