SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Parser.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2013 Evgeniy Andreev (gsomix)
00008  */
00009 
00010 #include <stdlib.h>
00011 #include <shogun/io/Parser.h>
00012 
00013 using namespace shogun;
00014 
00015 CParser::CParser()
00016 {
00017     init();
00018 }
00019 
00020 CParser::CParser(SGVector<char> text, CTokenizer* tokenizer)
00021 {
00022     init();
00023 
00024     m_text=text;
00025 
00026     SG_REF(tokenizer);
00027     m_tokenizer=tokenizer;
00028 
00029     if (m_tokenizer!=NULL)
00030         m_tokenizer->set_text(m_text);
00031 }
00032 
00033 CParser::~CParser()
00034 {
00035     SG_UNREF(m_tokenizer);
00036 }
00037 
00038 bool CParser::has_next()
00039 {
00040     if (m_tokenizer!=NULL)
00041         return m_tokenizer->has_next();
00042 
00043     return false;
00044 }
00045 
00046 void CParser::skip_token()
00047 {
00048     index_t start=0;
00049     m_tokenizer->next_token_idx(start);
00050 }
00051 
00052 SGVector<char> CParser::read_string()
00053 {
00054     index_t start=0;
00055     index_t end=0;
00056 
00057     end=m_tokenizer->next_token_idx(start);
00058 
00059     SGVector<char> result=SGVector<char>(end-start);
00060     for (index_t i=start; i<end; i++)
00061     {
00062         result[i-start]=m_text[i];
00063     }
00064 
00065     return result;
00066 }
00067 
00068 SGVector<char> CParser::read_cstring()
00069 {
00070     index_t start=0;
00071     index_t end=0;
00072 
00073     end=m_tokenizer->next_token_idx(start);
00074 
00075     SGVector<char> result=SGVector<char>(end-start+1);
00076     for (index_t i=start; i<end; i++)
00077     {
00078         result[i-start]=m_text[i];
00079     }
00080     result[end-start]='\0';
00081 
00082     return result;
00083 }
00084 
00085 bool CParser::read_bool()
00086 {
00087     SGVector<char> token=read_cstring();
00088 
00089     if (token.vlen>0)
00090         return (bool) strtod(token.vector, NULL);
00091     else
00092         return (bool) 0L;
00093 }
00094 
00095 #define READ_INT_METHOD(fname, convf, sg_type) \
00096 sg_type CParser::fname() \
00097 { \
00098     SGVector<char> token=read_cstring(); \
00099     \
00100     if (token.vlen>0) \
00101         return (sg_type) convf(token.vector, NULL, 10); \
00102     else \
00103         return (sg_type) 0L; \
00104 }
00105 
00106 READ_INT_METHOD(read_long, strtoll, int64_t)
00107 READ_INT_METHOD(read_ulong, strtoull, uint64_t)
00108 #undef READ_INT_METHOD
00109 
00110 #define READ_REAL_METHOD(fname, convf, sg_type) \
00111 sg_type CParser::fname() \
00112 { \
00113     SGVector<char> token=read_cstring(); \
00114     \
00115     if (token.vlen>0) \
00116         return (sg_type) convf(token.vector, NULL); \
00117     else \
00118         return (sg_type) 0L; \
00119 }
00120 
00121 READ_REAL_METHOD(read_char, strtod, char)
00122 READ_REAL_METHOD(read_byte, strtod, uint8_t)
00123 READ_REAL_METHOD(read_short, strtod, int16_t)
00124 READ_REAL_METHOD(read_word, strtod, uint16_t)
00125 READ_REAL_METHOD(read_int, strtod, int32_t)
00126 READ_REAL_METHOD(read_uint, strtod, uint32_t)
00127 
00128 READ_REAL_METHOD(read_short_real, strtod, float32_t)
00129 READ_REAL_METHOD(read_real, strtod, float64_t)
00130 #ifdef HAVE_STRTOLD
00131 READ_REAL_METHOD(read_long_real, strtold, floatmax_t)
00132 #else
00133 READ_REAL_METHOD(read_long_real, strtod, floatmax_t)
00134 #endif
00135 #undef READ_REAL_METHOD
00136 
00137 void CParser::set_text(SGVector<char> text)
00138 {
00139     m_text=text;
00140 
00141     if (m_tokenizer!=NULL)
00142         m_tokenizer->set_text(m_text);
00143 }
00144 
00145 void CParser::set_tokenizer(CTokenizer* tokenizer)
00146 {
00147     SG_REF(tokenizer);
00148     SG_UNREF(m_tokenizer);
00149     m_tokenizer=tokenizer;
00150 
00151     if (m_tokenizer!=NULL)
00152         m_tokenizer->set_text(m_text);
00153 }
00154 
00155 void CParser::init()
00156 {
00157     m_text=SGVector<char>();
00158     m_tokenizer=NULL;
00159 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation