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