SHOGUN  v3.0.0
 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 
13 using namespace shogun;
14 
16 {
17  init();
18 }
19 
21 {
22  init();
23 
24  m_text=text;
25 
26  SG_REF(tokenizer);
27  m_tokenizer=tokenizer;
28 
29  if (m_tokenizer!=NULL)
30  m_tokenizer->set_text(m_text);
31 }
32 
34 {
35  SG_UNREF(m_tokenizer);
36 }
37 
39 {
40  if (m_tokenizer!=NULL)
41  return m_tokenizer->has_next();
42 
43  return false;
44 }
45 
47 {
48  index_t start=0;
49  m_tokenizer->next_token_idx(start);
50 }
51 
53 {
54  index_t start=0;
55  index_t end=0;
56 
57  end=m_tokenizer->next_token_idx(start);
58 
59  SGVector<char> result=SGVector<char>(end-start);
60  for (index_t i=start; i<end; i++)
61  {
62  result[i-start]=m_text[i];
63  }
64 
65  return result;
66 }
67 
69 {
70  index_t start=0;
71  index_t end=0;
72 
73  end=m_tokenizer->next_token_idx(start);
74 
75  SGVector<char> result=SGVector<char>(end-start+1);
76  for (index_t i=start; i<end; i++)
77  {
78  result[i-start]=m_text[i];
79  }
80  result[end-start]='\0';
81 
82  return result;
83 }
84 
86 {
88 
89  if (token.vlen>0)
90  return (bool) strtod(token.vector, NULL);
91  else
92  return (bool) 0L;
93 }
94 
95 #define READ_INT_METHOD(fname, convf, sg_type) \
96 sg_type CParser::fname() \
97 { \
98  SGVector<char> token=read_cstring(); \
99  \
100  if (token.vlen>0) \
101  return (sg_type) convf(token.vector, NULL, 10); \
102  else \
103  return (sg_type) 0L; \
104 }
105 
106 READ_INT_METHOD(read_long, strtoll, int64_t)
107 READ_INT_METHOD(read_ulong, strtoull, uint64_t)
108 #undef READ_INT_METHOD
109 
110 #define READ_REAL_METHOD(fname, convf, sg_type) \
111 sg_type CParser::fname() \
112 { \
113  SGVector<char> token=read_cstring(); \
114  \
115  if (token.vlen>0) \
116  return (sg_type) convf(token.vector, NULL); \
117  else \
118  return (sg_type) 0L; \
119 }
120 
121 READ_REAL_METHOD(read_char, strtod, char)
122 READ_REAL_METHOD(read_byte, strtod, uint8_t)
123 READ_REAL_METHOD(read_short, strtod, int16_t)
124 READ_REAL_METHOD(read_word, strtod, uint16_t)
125 READ_REAL_METHOD(read_int, strtod, int32_t)
126 READ_REAL_METHOD(read_uint, strtod, uint32_t)
127 
128 READ_REAL_METHOD(read_short_real, strtod, float32_t)
129 READ_REAL_METHOD(read_real, strtod, float64_t)
130 #ifdef HAVE_STRTOLD
131 READ_REAL_METHOD(read_long_real, strtold, floatmax_t)
132 #else
133 READ_REAL_METHOD(read_long_real, strtod, floatmax_t)
134 #endif
135 #undef READ_REAL_METHOD
136 
138 {
139  m_text=text;
140 
141  if (m_tokenizer!=NULL)
142  m_tokenizer->set_text(m_text);
143 }
144 
146 {
147  SG_REF(tokenizer);
148  SG_UNREF(m_tokenizer);
149  m_tokenizer=tokenizer;
150 
151  if (m_tokenizer!=NULL)
152  m_tokenizer->set_text(m_text);
153 }
154 
155 void CParser::init()
156 {
157  m_text=SGVector<char>();
158  m_tokenizer=NULL;
159 }

SHOGUN Machine Learning Toolbox - Documentation