gloox  1.0.13
iodata.cpp
1 /*
2  Copyright (c) 2015 by Jakob Schröter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "iodata.h"
15 
16 #include "util.h"
17 
18 namespace gloox
19 {
20 
21  static const char* ioTypes[] = {
22  "io-schemata-get",
23  "input",
24  "getStatus",
25  "getOutput",
26  "io-schemata-result",
27  "output",
28  "error",
29  "status"
30  };
31 
32  static inline IOData::Type ioType( const std::string& type )
33  {
34  return (IOData::Type)util::lookup( type, ioTypes );
35  }
36 
39  m_in( 0 ), m_out( 0 ), m_error( 0 ),
40  m_type( type )
41  {
42  m_status.elapsed = -1;
43  m_status.remaining = -1;
44  m_status.percentage = -1;
45  }
46 
47  IOData::IOData( const Tag* tag )
49  m_in( 0 ), m_out( 0 ), m_error( 0 ),
50  m_type( TypeInvalid )
51  {
52  if( !tag || !( tag->name() == "iodata" && tag->hasAttribute( XMLNS, XMLNS_IODATA ) ) )
53  return;
54 
55  m_status.elapsed = -1;
56  m_status.remaining = -1;
57  m_status.percentage = -1;
58 
59  m_type = ioType( tag->findAttribute( "type" ) );
60  Tag* m = 0;
61  switch( m_type )
62  {
63  case TypeInput:
64  m = tag->findChild( "in" );
65  if( m )
66  m_in = m->clone();
67  break;
69  m = tag->findChild( "desc" );
70  if( m )
71  m_desc = m->cdata();
72 
73  m = tag->findChild( "out" );
74  if( m )
75  m_out = m->clone();
76 
77  m = tag->findChild( "in" );
78  if( m )
79  m_in = m->clone();
80  break;
81  case TypeOutput:
82  m = tag->findChild( "out" );
83  if( m )
84  m_out = m->clone();
85  break;
86  case TypeError:
87  m = tag->findChild( "error" );
88  if( m )
89  m_error = m->clone();
90  break;
91  case TypeStatus:
92  m = tag->findChild( "status" );
93  if( m )
94  {
95  Tag* t = m->findChild( "elapsed" );
96  if( t )
97  m_status.elapsed = atoi( t->cdata().c_str() );
98 
99  t = m->findChild( "remaining" );
100  if( t )
101  m_status.remaining = atoi( t->cdata().c_str() );
102 
103  t = m->findChild( "percentage" );
104  if( t )
105  m_status.percentage = atoi( t->cdata().c_str() );
106 
107  t = m->findChild( "information" );
108  if( t )
109  m_status.info = t->cdata();
110  }
111  break;
112  case TypeIoSchemataGet:
113  case TypeGetStatus:
114  case TypeGetOutput:
115  default:
116  break;
117  }
118 
119  }
120 
122  {
123  delete m_in;
124  delete m_out;
125  delete m_error;
126  }
127 
128  Tag* IOData::tag() const
129  {
130  if( m_type == TypeInvalid )
131  return 0;
132 
133  Tag* i = new Tag( "iodata" );
134  i->setXmlns( XMLNS_IODATA );
135  i->addAttribute( "type", util::lookup( m_type, ioTypes ) );
136 
137  Tag* t = 0;
138  switch( m_type )
139  {
140  case TypeInput:
141  i->addChild( m_in );
142  break;
144  i->addChild( m_in );
145  i->addChild( m_out );
146  new Tag( i, "desc", m_desc );
147  break;
148  case TypeOutput:
149  i->addChild( m_out );
150  break;
151  case TypeError:
152  i->addChild( m_error );
153  break;
154  case TypeStatus:
155  t = new Tag( i, "status" );
156  if( m_status.elapsed >= 0 )
157  new Tag( t, "elapsed", util::int2string( m_status.elapsed ) );
158  if( m_status.remaining >= 0 )
159  new Tag( t, "remaining", util::int2string( m_status.remaining ) );
160  if( m_status.percentage >= 0 )
161  new Tag( t, "percentage", util::int2string( m_status.percentage ) );
162  if( m_status.info.length() )
163  new Tag( t, "information", m_status.info );
164  break;
165  case TypeIoSchemataGet:
166  case TypeGetStatus:
167  case TypeGetOutput:
168  default:
169  break;
170  }
171 
172  return i;
173  }
174 
176  {
177  IOData* i = new IOData( m_type );
178  i->m_status = m_status;
179  i->m_desc = m_desc;
180 
181  if( m_in )
182  i->m_in = m_in->clone();
183  if( m_out )
184  i->m_out = m_out->clone();
185  if( m_error )
186  i->m_error = m_error->clone();
187 
188  return i;
189  }
190 
191  void IOData::setIn( Tag* in )
192  {
193  if( !in )
194  return;
195 
196  delete m_in;
197 
198  m_in = new Tag( "in" );
199  m_in->addChild( in );
200  }
201 
202  void IOData::setOut( Tag* out )
203  {
204  if( !out )
205  return;
206 
207  delete m_out;
208 
209  m_out = new Tag( "out" );
210  m_out->addChild( out );
211  }
212 
213  void IOData::setError( Tag* error )
214  {
215  if( !error )
216  return;
217 
218  delete m_error;
219 
220  m_error = new Tag( "error" );
221  m_error->addChild( error );
222  }
223 
224 
225 }
226