VR-Forces 4.3.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stringRepHelper.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2013 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 #pragma once
6 #pragma warning (disable:4996)
7 
9 #include "tdbutil/tdbUnits.h"
13 #include "vrfutil/rwVector.h"
15 #include "vrfutil/rwReal.h"
16 #include "vrfutil/rwString.h"
17 #include "vrfutil/rwInt.h"
18 
20 {
21 public:
23  {
24 
25  }
26 
27  virtual ~DtStringRepHelper()
28  {
29 
30  }
31 
32  virtual bool isDisplayableSymbol(const DtString& s) const
33  {
34  return true;
35  }
36 
37  virtual DtString stringRepSingleVariable(const DtString variableName, const DtRwVariableBindings& variables, const DtRwVariableBindings& variableDataTypes = DtRwVariableBindings("no-types")) const
38  {
39  const DtReaderWriter* rw = variables.findVariableBinding(variableName);
40  if (rw)
41  {
42  return stringRep(rw, variableDataTypes, false);
43  }
44  else
45  {
46  return DtString::nullString();
47  }
48  }
49 
50  virtual DtString stringRep(const DtReaderWriter* readerWriter, const DtRwVariableBindings& variableDataTypes, const bool printVariableName = true ) const
51  {
52  DtString label;
53  if (printVariableName)
54  {
55  label += DtStringRepHelper::convertToLuaSymbol(readerWriter->name().c_str());
56  }
57 
58  if (isDisplayableSymbol(label))
59  {
60  if (printVariableName)
61  {
62  label += "=";
63  }
64 
66  const DtRwString* str = dynamic_cast<const DtRwString*>(readerWriter);
67  bool pp = true;
68 
69  if (str && (str->findString("<?xml ") == 0))
70  {
72 
73  if (rec.fromXml(*str))
74  {
75  label += rec.prettyPrint();
76  pp = false;
77  }
78  }
79 
80  if (pp)
81  {
84  const DtReaderWriter* type = variableDataTypes.findVariableBinding(readerWriter->name().c_str());
85 
86  if (type)
87  {
88  DtString dataType = *(dynamic_cast<const DtRwString*>(type));
89  if ((dataType == DtScriptedTaskLocationVariable) ||
91  {
92  label += formatLocation(*(dynamic_cast<const DtRwVector*>(readerWriter)));
93  }
94  else if (dataType == DtScriptedTaskOffsetLocationVariable)
95  {
96  label += formatOffset(*(dynamic_cast<const DtRwVector*>(readerWriter)));
97  }
98  else if (dataType == DtScriptedTaskRangeVariable)
99  {
100  label += formatDistance(*(dynamic_cast<const DtRwReal*>(readerWriter)));
101  }
102  else if (dataType == DtScriptedTaskHeadingVariable)
103  {
104  label += formatHeading(*(dynamic_cast<const DtRwReal*>(readerWriter)));
105  }
106  else if (dataType == DtScriptedTaskRateVariable)
107  {
108  label += formatRate(*(dynamic_cast<const DtRwReal*>(readerWriter)));
109  }
110  else if (dataType == DtScriptedTaskTurnRateVariable)
111  {
112  label += formatTurnRate(*(dynamic_cast<const DtRwReal*>(readerWriter)));
113  }
114  else if (dataType == DtScriptedTaskTimeVariable)
115  {
116  label += formatTime(*(dynamic_cast<const DtRwInt*>(readerWriter)));
117  }
118  else if (dataType == DtScriptedTaskDateTimeVariable)
119  {
120  label += formatDateTime(static_cast<time_t>((*(dynamic_cast<const DtRwReal*>(readerWriter)))));
121  }
122  else
123  {
124  label += readerWriter->prettyPrint();
125  }
126  }
127  else
128  {
129  label += readerWriter->prettyPrint();
130  }
131  }
132  return label;
133  }
134  else
135  {
136  return DtString::nullString();
137  }
138  }
139 
140  virtual DtString stringRep(const DtRwVariableBindings& variables, const DtRwVariableBindings& variableDataTypes) const
141  {
142  DtString rep = "";
143  std::vector<DtReaderWriterRegistryData *>::const_iterator iter = variables.registry().registryData().begin();
144  std::vector<DtReaderWriterRegistryData *>::const_iterator endIter = variables.registry().registryData().end();
145 
146  for(; iter != endIter;)
147  {
148  bool added = false;
149  const DtReaderWriter* readerWriter = (*iter)->readerWriter();
150  DtString label = stringRep(readerWriter, variableDataTypes);
151  if (!label.isEmpty())
152  {
153  rep += label;
154  added = true;
155  }
156  ++iter;
157 
158  if ((iter != endIter) && added)
159  {
160  rep += "; ";
161  }
162  }
163  return rep;
164  }
165 
166  virtual DtString convertToLuaSymbol(const DtString& inputString) const
167  {
168  DtString result;
169  for(int i = 0; i < inputString.size(); ++i)
170  {
171  char c = inputString[i];
172  if (c == '-')
173  {
174  result += '_';
175  }
176  else if (c == ' ')
177  {
178  continue;
179  }
180  else
181  {
182  result += c;
183  }
184  }
185  return result;
186  }
187 
188  virtual DtString formatTime(int time) const
189  {
190  int days = 0, hours = 0, minutes = 0, seconds = 0;
191 
192  days = time / (24 * 3600);
193  time -= (days * 24 * 3600);
194 
195  hours = time / 3600;
196  time -= hours * 3600;
197 
198  minutes = time / 60;
199  time -= minutes * 60;
200 
201  seconds = time;
202 
203  DtString rep;
204 
205  rep = DtString(days) + "d:" + DtString(hours) + "h:" + DtString(minutes) + "m:" + DtString(seconds) + "s";
206 
207  return rep;
208  }
209 
210  virtual DtString formatDateTime(time_t time) const
211  {
212  struct tm* timeptr = gmtime(&time);
213  return asctime(timeptr);
214  }
215 
216  virtual DtString formatLocation(const DtVector& loc) const
217  {
218  return formatSourceToDisplay(loc);
219  }
220 
221 
222  virtual DtString formatDistance(double d) const
223  {
224  DtString rep;
225  DtString unitLabel;
226  DtString distance = formatSourceToDisplay(d, DtUnitDisplayDistance, DtUnitMeters, unitLabel);
227 
228  rep = distance;
229 
230  if (unitLabel.length())
231  {
232  rep += " (";
233  rep += unitLabel;
234  rep += ")";
235  }
236 
237  return rep;
238  }
239 
240  virtual DtString formatOffset(const DtVector& loc) const
241  {
242  DtString rep = "{";
243 
244  rep += formatDistance(loc.x());
245  rep += ", ";
246  rep += formatDistance(loc.y());
247  rep += ", ";
248  rep += formatDistance(loc.z());
249  rep += "}";
250 
251  return rep;
252  }
253 
254  virtual DtString formatHeading(double d) const
255  {
256  DtString rep;
257  DtString unitLabel;
258  DtString heading = formatSourceToDisplay(d, DtUnitDisplayOrientation, DtUnitRadians, unitLabel);
259 
260  rep = heading;
261 
262  if (unitLabel.length())
263  {
264  rep += " (";
265  rep += unitLabel;
266  rep += ")";
267  }
268 
269  return rep;
270  }
271 
272  virtual DtString formatRate(double d) const
273  {
274  DtString rep;
275  DtString unitLabel;
276  DtString heading = formatSourceToDisplay(d, DtUnitDisplayVelocity, DtUnitMetersPerSecond, unitLabel);
277 
278  rep = heading;
279 
280  if (unitLabel.length())
281  {
282  rep += " (";
283  rep += unitLabel;
284  rep += ")";
285  }
286 
287  return rep;
288  }
289 
290  virtual DtString formatTurnRate(double d) const
291  {
292  DtString rep;
293  DtString unitLabel;
294  DtString heading = formatSourceToDisplay(d, DtUnitDisplayRotationalVelocity, DtUnitRadians, unitLabel);
295 
296  rep = heading;
297 
298  if (unitLabel.length())
299  {
300  rep += " (";
301  rep += unitLabel;
302  rep += ")";
303  }
304 
305  return rep;
306  }
307 
308 };

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)