001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:



















































































































































































/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(OMNI_CHAR_T_HPP)
#define OMNI_CHAR_T_HPP 1
#include <omni/defs/global.hpp>
#include <cctype>
#include <cwctype>
#include <cstdlib>
#include <cwchar>

// defines the string types to use for different string processing
#if defined(OMNI_UNICODE)
    #define OMNI_CHAR_T wchar_t     // Defines a wide character type
#else
    #define OMNI_CHAR_T char     // Defines a standard character type
#endif

namespace omni {
    typedef OMNI_CHAR_T char_t;

    namespace char_util {
        inline bool is_digit(char c)
        { return (std::isdigit(c) != 0); }
        
        inline bool is_digit(wchar_t c)
        { return (std::iswdigit(c) != 0); }
        
        inline bool is_alpha(char c)
        { return (std::isalpha(c) != 0); }
        
        inline bool is_alpha(wchar_t c)
        { return (std::iswalpha(c) != 0); }
        
        template < typename std_char_t >
        std_char_t to_lower(std_char_t c)
        { OMNI_UNUSED(c); OMNI_ERR_RETV_FW("Invalid template parameter specified.", omni::exceptions::invalid_template_type(), c) }
        
        template <>
        inline char to_lower<char>(char c)
        { return static_cast<char>(std::tolower(c)); }
        
        template <>
        inline wchar_t to_lower<wchar_t>(wchar_t c)
        { return std::towlower(c); }
        
        template < typename std_char_t >
        std_char_t to_upper(std_char_t c)
        { OMNI_UNUSED(c); OMNI_ERR_RETV_FW("Invalid template parameter specified.", omni::exceptions::invalid_template_type(), c) }
        
        template <>
        inline char to_upper<char>(char c)
        { return static_cast<char>(std::toupper(c)); }
        
        template <>
        inline wchar_t to_upper<wchar_t>(wchar_t c)
        { return std::towupper(c); }

        inline char to_char(wchar_t wc)
        {
            #if defined(OMNI_WIN_API)
                char cret;
                std::size_t w = ::WideCharToMultiByte(OMNI_CODE_PAGE, 0, &wc, 1, &cret, 1, NULL, NULL);
                if (w != 1) {
                    OMNI_ERRV_FW("expected size of 1 got ", w, omni::exceptions::invalid_size())
                }
                return cret;
            #else
                int rc = std::wctob(wc);
                if (rc == EOF) {
                    OMNI_ERR_FW("invalid wchar to char ", omni::exceptions::invalid_type_cast())
                }
                return static_cast<char>(rc);
            #endif
        }

        inline char to_char(char c)
        {
            return c;
        }

        inline wchar_t to_wchar(char c)
        {
            #if defined(OMNI_WIN_API)
                wchar_t wret;
                std::size_t w = ::MultiByteToWideChar(OMNI_CODE_PAGE, 0, &c, 1, &wret, 1);
                if (w != 1) {
                    OMNI_ERRV_FW("expected size of 1 got ", w, omni::exceptions::invalid_size())
                }
                return wret;
            #else
                wint_t rc = std::btowc(c);
                if (rc == WEOF) {
                    OMNI_ERR_FW("invalid char to wchar ", omni::exceptions::invalid_type_cast())
                }
                return static_cast<wchar_t>(rc);
            #endif
        }

        inline wchar_t to_wchar(wchar_t wc)
        {
            return wc;
        }

        template < typename Cx, typename Cy >
        inline Cx to_char_t(Cy c)
        {
            return static_cast<Cx>(c);
        }

        template<>
        inline char to_char_t<char, wchar_t>(wchar_t c)
        {
            return omni::char_util::to_char(c);
        }

        template<>
        inline char to_char_t<char, char>(char c)
        {
            return c;
        }

        template<>
        inline wchar_t to_char_t<wchar_t, char>(char c)
        {
            return omni::char_util::to_wchar(c);
        }

        template<>
        inline wchar_t to_char_t<wchar_t, wchar_t>(wchar_t c)
        {
            return c;
        }
        
        namespace helpers {
            inline bool is_1or0(char v)
            { return v == '0' || v == '1'; }
            
            inline bool is_1or0(wchar_t v)
            { return v == L'0' || v == L'1'; }
            
            inline bool is_nde(char v)
            { return (v == '-' || v == '.' || v == ','); }
            
            inline bool is_nde(wchar_t v)
            { return (v == L'-' || v == L'.' || v == L','); }
            
            inline bool is_de(char v)
            { return (v == '.' || v == ','); }
            
            inline bool is_de(wchar_t v)
            { return (v == L'.' || v == L','); }
            
            inline bool is_neg(char v)
            { return (v == '-'); }
            
            inline bool is_neg(wchar_t v)
            { return (v == L'-'); }
        }
    }
}

#endif // OMNI_CHAR_T_HPP