SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parser.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Evgeniy Andreev (gsomix)
8  */
9 
10 #include <stdlib.h>
11 #include <shogun/io/Parser.h>
12 #include <shogun/lib/Tokenizer.h>
13 
14 using namespace shogun;
15 
17 {
18  init();
19 }
20 
22 {
23  init();
24 
25  m_text=text;
26 
27  SG_REF(tokenizer);
28  m_tokenizer=tokenizer;
29 
30  if (m_tokenizer!=NULL)
31  m_tokenizer->set_text(m_text);
32 }
33 
35 {
36  SG_UNREF(m_tokenizer);
37 }
38 
40 {
41  if (m_tokenizer!=NULL)
42  return m_tokenizer->has_next();
43 
44  return false;
45 }
46 
48 {
49  index_t start=0;
50  m_tokenizer->next_token_idx(start);
51 }
52 
54 {
55  index_t start=0;
56  index_t end=0;
57 
58  end=m_tokenizer->next_token_idx(start);
59 
60  SGVector<char> result=SGVector<char>(end-start);
61  for (index_t i=start; i<end; i++)
62  {
63  result[i-start]=m_text[i];
64  }
65 
66  return result;
67 }
68 
70 {
71  index_t start=0;
72  index_t end=0;
73 
74  end=m_tokenizer->next_token_idx(start);
75 
76  SGVector<char> result=SGVector<char>(end-start+1);
77  for (index_t i=start; i<end; i++)
78  {
79  result[i-start]=m_text[i];
80  }
81  result[end-start]='\0';
82 
83  return result;
84 }
85 
87 {
89 
90  if (token.vlen>0)
91  return (bool) strtod(token.vector, NULL);
92  else
93  return (bool) 0L;
94 }
95 
96 #define READ_INT_METHOD(fname, convf, sg_type) \
97 sg_type CParser::fname() \
98 { \
99  SGVector<char> token=read_cstring(); \
100  \
101  if (token.vlen>0) \
102  return (sg_type) convf(token.vector, NULL, 10); \
103  else \
104  return (sg_type) 0L; \
105 }
106 
107 READ_INT_METHOD(read_long, strtoll, int64_t)
108 READ_INT_METHOD(read_ulong, strtoull, uint64_t)
109 #undef READ_INT_METHOD
110 
111 #define READ_REAL_METHOD(fname, convf, sg_type) \
112 sg_type CParser::fname() \
113 { \
114  SGVector<char> token=read_cstring(); \
115  \
116  if (token.vlen>0) \
117  return (sg_type) convf(token.vector, NULL); \
118  else \
119  return (sg_type) 0L; \
120 }
121 
122 READ_REAL_METHOD(read_char, strtod, char)
123 READ_REAL_METHOD(read_byte, strtod, uint8_t)
124 READ_REAL_METHOD(read_short, strtod, int16_t)
125 READ_REAL_METHOD(read_word, strtod, uint16_t)
126 READ_REAL_METHOD(read_int, strtod, int32_t)
127 READ_REAL_METHOD(read_uint, strtod, uint32_t)
128 
129 READ_REAL_METHOD(read_short_real, strtod, float32_t)
130 READ_REAL_METHOD(read_real, strtod, float64_t)
131 #ifdef HAVE_STRTOLD
132 READ_REAL_METHOD(read_long_real, strtold, floatmax_t)
133 #else
134 READ_REAL_METHOD(read_long_real, strtod, floatmax_t)
135 #endif
136 #undef READ_REAL_METHOD
137 
139 {
140  m_text=text;
141 
142  if (m_tokenizer!=NULL)
143  m_tokenizer->set_text(m_text);
144 }
145 
147 {
148  SG_REF(tokenizer);
149  SG_UNREF(m_tokenizer);
150  m_tokenizer=tokenizer;
151 
152  if (m_tokenizer!=NULL)
153  m_tokenizer->set_text(m_text);
154 }
155 
156 void CParser::init()
157 {
158  m_text=SGVector<char>();
159  m_tokenizer=NULL;
160 }

SHOGUN Machine Learning Toolbox - Documentation