trader  v0.1a
A framework to build trading applications
fileoutputstream.h
1 #pragma once
2 
3 namespace std
4 {
5  inline ostringstream &cendl(ostringstream &os)
6  {
7  os << std::endl;
8  os << ";";
9  return os;
10  }
11  inline std::ostringstream &dot(std::ostringstream &os)
12  {
13  if (os.str().length())
14  {
15  os << ".";
16  }
17  return os;
18  }
19 
20  inline std::ostringstream &operator<<(std::ostringstream &os, std::ostringstream &(*_Pfn)(std::ostringstream &))
21  {
22  return ((*_Pfn)(os));
23  }
24 } // namespace std
25 
26 namespace trader
27 {
28 
29  using Poco::DirectoryIterator;
30  using Poco::File;
31  using Poco::Path;
32  using Poco::Util::AbstractConfiguration;
33  using Poco::Util::Application;
34  using Poco::Util::HelpFormatter;
35  using Poco::Util::Option;
36  using Poco::Util::OptionCallback;
37  using Poco::Util::OptionSet;
38  using namespace Poco;
39  using namespace std;
40 
41  class ApiFileOutputStream;
42  class ApiStreamBuffer;
43 
44  class tabs
45  {
46  size_t _n;
47 
48  public:
49  explicit tabs(size_t n)
50  : _n(n)
51  {
52  }
53  size_t getn() const { return _n; }
54 
55  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const tabs &obj);
56  friend ApiStreamBuffer &operator<<(ApiStreamBuffer &os, const tabs &obj);
57  friend std::ostringstream &operator<<(std::ostringstream &os, const tabs &obj);
58  friend std::ostream &operator<<(ostream &os, const tabs &obj);
59  };
60 
62  {
63  public:
64  ApiFileOutputStream &operator++()
65  {
66  indentation++;
67  return *this;
68  }
69 
70  ApiFileOutputStream &operator--()
71  {
72 
73  poco_assert(indentation > 0);
74  indentation--;
75  return *this;
76  }
77 
78  ApiFileOutputStream(const string &path, ios::openmode mode = ios::out | ios::trunc)
79  : indentation(0)
80  , stream(path, mode)
81  {
82  }
83 
84  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const char *text);
85  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, string &str);
86  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const string &str);
87  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const Int32 &num);
88  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os,
90  friend ApiFileOutputStream &endl(ApiFileOutputStream &os);
91  friend ApiFileOutputStream &cendl(ApiFileOutputStream &os);
92 
93  Int32 indentation;
94  Poco::FileOutputStream stream;
95  ostringstream tempStream;
96  };
97 
98  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const char *text)
99  {
100  os.tempStream << text;
101  return os;
102  }
103 
104  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, string &str)
105  {
106  os.tempStream << str;
107  return os;
108  }
109 
110  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const string &str)
111  {
112  os.tempStream << str;
113  return os;
114  }
115 
116  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const Int32 &num)
117  {
118  os.tempStream << num;
119  return os;
120  }
121 
123  {
124  return ((*_Pfn)(os));
125  }
126 
127  inline ApiFileOutputStream &endl(ApiFileOutputStream &os)
128  {
129  os.stream << std::endl;
130  os.stream << tabs(os.indentation);
131  os.stream << os.tempStream.str();
132 #ifdef DEBUG
133  cout << os.tempStream.str() << std::endl;
134 #endif
135  os.tempStream.str("");
136  os.tempStream.clear();
137  return os;
138  }
139 
140  inline ApiFileOutputStream &cendl(ApiFileOutputStream &os)
141  {
142  os.stream << std::endl;
143  os.stream << tabs(os.indentation);
144  os.stream << os.tempStream.str();
145  os.stream << ";";
146 #ifdef DEBUG
147  cout << os.tempStream.str() << std::endl;
148 #endif
149  os.tempStream.str("");
150  os.tempStream.clear();
151  return os;
152  }
153 
155  {
156  public:
157  ApiStreamBuffer &operator++()
158  {
159  indentation++;
160  return *this;
161  }
162 
163  ApiStreamBuffer &operator--()
164  {
165 
166  poco_assert(indentation > 0);
167  indentation--;
168  return *this;
169  }
170 
171  ApiStreamBuffer(ApiFileOutputStream &fileStream) { indentation = fileStream.indentation; }
172 
173  friend ApiStreamBuffer &operator<<(ApiStreamBuffer &os, const char *text);
174  friend ApiStreamBuffer &operator<<(ApiStreamBuffer &os, string &str);
175  friend ApiStreamBuffer &operator<<(ApiStreamBuffer &os, const string &str);
176  friend ApiStreamBuffer &operator<<(ApiStreamBuffer &os, ApiStreamBuffer &(*_Pfn)(ApiStreamBuffer &));
177  friend ApiStreamBuffer &endl(ApiStreamBuffer &os);
178  friend ApiStreamBuffer &cendl(ApiStreamBuffer &os);
179 
180  inline ApiStreamBuffer &dot(ApiStreamBuffer &os)
181  {
182  os.tempStream << ".";
183  return os;
184  }
185 
186  std::string str() { return tempStream.str(); }
187 
188  Int32 indentation;
189  ostringstream tempStream;
190  };
191 
192  inline ApiStreamBuffer &operator<<(ApiStreamBuffer &os, const char *text)
193  {
194  os.tempStream << text;
195  return os;
196  }
197 
198  inline ApiStreamBuffer &operator<<(ApiStreamBuffer &os, string &str)
199  {
200  os.tempStream << str;
201  return os;
202  }
203 
204  inline ApiStreamBuffer &operator<<(ApiStreamBuffer &os, const string &str)
205  {
206  os.tempStream << str;
207  return os;
208  }
209 
210  inline ApiStreamBuffer &operator<<(ApiStreamBuffer &os, ApiStreamBuffer &(*_Pfn)(ApiStreamBuffer &))
211  {
212  return ((*_Pfn)(os));
213  }
214 
215  inline ApiStreamBuffer &endl(ApiStreamBuffer &os)
216  {
217  os.tempStream << std::endl;
218  os.tempStream << tabs(os.indentation);
219  return os;
220  }
221 
222  inline ApiStreamBuffer &cendl(ApiStreamBuffer &os)
223  {
224  os.tempStream << ";";
225  os.tempStream << std::endl;
226  os.tempStream << tabs(os.indentation);
227  return os;
228  }
229 
230  inline ApiStreamBuffer &dot(ApiStreamBuffer &os)
231  {
232  os.tempStream << ".";
233  return os;
234  }
235 
236  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const tabs &obj)
237  {
238  size_t n = obj.getn();
239  for (size_t i = 0; i < n; ++i)
240  os.tempStream << "\t";
241  return os;
242  }
243 
244  inline ApiStreamBuffer &operator<<(ApiStreamBuffer &os, const tabs &obj)
245  {
246  size_t n = obj.getn();
247  for (size_t i = 0; i < n; ++i)
248  os.tempStream << "\t";
249  return os;
250  }
251 
252  inline std::ostringstream &operator<<(std::ostringstream &os, const tabs &obj)
253  {
254  size_t n = obj.getn();
255  for (size_t i = 0; i < n; ++i)
256  os << "\t";
257  return os;
258  }
259 
260  inline std::ostream &operator<<(ostream &os, const tabs &obj)
261  {
262  size_t n = obj.getn();
263  for (size_t i = 0; i < n; ++i)
264  os << "\t";
265  return os;
266  }
267 
268  class temp_name
269  {
270  size_t _n;
271 
272  public:
273  explicit temp_name(size_t n)
274  : _n(n)
275  {
276  }
277  size_t getn() const { return _n; }
278  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const temp_name &obj)
279  {
280  os.tempStream << "obj" << obj.getn();
281  return os;
282  }
283  friend std::ostringstream &operator<<(std::ostringstream &os, const temp_name &obj)
284  {
285  os << "obj" << obj.getn();
286  return os;
287  }
288  };
289 
291  {
292  std::string _str;
293 
294  public:
295  explicit clean_name(std::string &str)
296  : _str(str)
297  {
298  }
299  explicit clean_name(const char *str)
300  : _str(str)
301  {
302  }
303  string getstr() const { return _str; }
304  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const clean_name &obj);
305  friend std::ostringstream &operator<<(std::ostringstream &os, const clean_name &obj);
306  template < class _Traits >
307  friend basic_ostream< char, _Traits > &operator<<(basic_ostream< char, _Traits > &_Ostr, const clean_name &obj);
308  };
309 
310  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const clean_name &obj)
311  {
312  string newStr = obj.getstr();
313  if (newStr.length())
314  {
315  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
316  }
317  os.tempStream << newStr;
318  return os;
319  }
320  inline std::ostringstream &operator<<(std::ostringstream &os, const clean_name &obj)
321  {
322  string newStr = obj.getstr();
323  if (newStr.length())
324  {
325  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
326  }
327  os << newStr;
328  return os;
329  }
330 
331  template < class _Traits >
332  inline basic_ostream< char, _Traits > &operator<<(basic_ostream< char, _Traits > &_Ostr, const clean_name &obj)
333  {
334  string newStr = obj.getstr();
335  if (newStr.length())
336  {
337  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
338  }
339  _Ostr << newStr;
340  return _Ostr;
341  }
342 
343  class var_name
344  {
345  std::string _str;
346 
347  public:
348  explicit var_name(std::string &str)
349  : _str(str)
350  {
351  }
352  explicit var_name(const std::string &str)
353  : _str(str)
354  {
355  }
356  explicit var_name(const char *str)
357  : _str(str)
358  {
359  }
360  string getstr() const { return _str; }
361  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const var_name &obj);
362  friend std::ostringstream &operator<<(std::ostringstream &os, const var_name &obj);
363  template < class _Traits >
364  friend basic_ostream< char, _Traits > &operator<<(basic_ostream< char, _Traits > &_Ostr, const var_name &obj);
365  };
366 
367  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const var_name &obj)
368  {
369  string newStr = obj.getstr();
370  if (newStr.length())
371  {
372  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
373  std::transform(newStr.begin(), newStr.begin() + 1, newStr.begin(), ::tolower);
374  }
375  os.tempStream << newStr;
376  return os;
377  }
378 
379  inline std::ostringstream &operator<<(std::ostringstream &os, const var_name &obj)
380  {
381  string newStr = obj.getstr();
382  if (newStr.length())
383  {
384  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
385  std::transform(newStr.begin(), newStr.begin() + 1, newStr.begin(), ::tolower);
386  }
387  os << newStr;
388  return os;
389  }
390 
391  template < class _Traits >
392  inline basic_ostream< char, _Traits > &operator<<(basic_ostream< char, _Traits > &_Ostr, const var_name &obj)
393  {
394  string newStr = obj.getstr();
395  if (newStr.length())
396  {
397  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
398  std::transform(newStr.begin(), newStr.begin() + 1, newStr.begin(), ::tolower);
399  }
400  _Ostr << newStr;
401  return _Ostr;
402  }
403 
404  class type_name
405  {
406  std::string _str;
407 
408  public:
409  explicit type_name(std::string &str)
410  : _str(str)
411  {
412  }
413  explicit type_name(const std::string &str)
414  : _str(str)
415  {
416  }
417  explicit type_name(const char *str)
418  : _str(str)
419  {
420  }
421  string getstr() const { return _str; }
422  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const type_name &obj);
423  friend std::ostringstream &operator<<(std::ostringstream &os, const type_name &obj);
424  template < class _Traits >
425  friend basic_ostream< char, _Traits > &operator<<(basic_ostream< char, _Traits > &_Ostr, const type_name &obj);
426  };
427 
428  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const type_name &obj)
429  {
430  string newStr = obj.getstr();
431  if (newStr.length())
432  {
433  if (!os.tempStream.str().length())
434  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
435  std::transform(newStr.begin(), newStr.begin() + 1, newStr.begin(), ::toupper);
436  }
437  os.tempStream << newStr;
438  return os;
439  }
440 
441  inline std::ostringstream &operator<<(std::ostringstream &os, const type_name &obj)
442  {
443  string newStr = obj.getstr();
444  if (newStr.length())
445  {
446  if (!os.str().length())
447  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
448  std::transform(newStr.begin(), newStr.begin() + 1, newStr.begin(), ::toupper);
449  }
450  os << newStr;
451  return os;
452  }
453 
454  template < class _Traits >
455  inline basic_ostream< char, _Traits > &operator<<(basic_ostream< char, _Traits > &_Ostr, const type_name &obj)
456  {
457  string newStr = obj.getstr();
458  if (newStr.length())
459  {
460  if (!_Ostr.width())
461  newStr.erase(std::remove(newStr.begin(), newStr.end(), ':'), newStr.end());
462  std::transform(newStr.begin(), newStr.begin() + 1, newStr.begin(), ::toupper);
463  }
464  _Ostr << newStr;
465  return _Ostr;
466  }
467 
468  class comment
469  {
470  std::string _str;
471 
472  public:
473  explicit comment(std::string &str)
474  : _str(str)
475  {
476  }
477  explicit comment(const std::string &str)
478  : _str(str)
479  {
480  }
481  explicit comment(const char *str)
482  : _str(str)
483  {
484  }
485  string getstr() const { return _str; }
486  friend ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const comment &obj);
487  };
488 
489  inline ApiFileOutputStream &operator<<(ApiFileOutputStream &os, const comment &obj)
490  {
491  string commentStr = obj.getstr();
492  if (commentStr.length())
493  {
494  string buffer = commentStr;
495  std::size_t found = buffer.find("\n");
496  while (found != std::string::npos)
497  {
498  string temp = buffer.substr(0, found);
499  os.tempStream << "// ";
500  os.tempStream << temp;
501  os << endl;
502  if (found + 1 < buffer.length())
503  {
504  buffer = buffer.substr(found + 1, buffer.length() - (found + 1));
505  }
506  found = buffer.find("\n");
507  }
508  if (buffer.length())
509  {
510  os.tempStream << "// ";
511  os.tempStream << buffer;
512  os << endl;
513  }
514  }
515  return os;
516  }
517 
519  {
520  public:
521  enum Type
522  {
523  ARRAY = 0,
524  OBJECT,
525  MAP,
526  VAR,
527  NUM_TYPES
528  };
529 
530  struct TypePair
531  {
532  Type type;
533  std::string name;
534  };
535 
537 
539  {
540  varNameStream << other.varNameStream.str();
541  typeNameStream << other.typeNameStream.str();
542  prefixStream << other.prefixStream.str();
543  typeStack = other.typeStack;
544  }
545 
546  const char *getTypeString(Type type)
547  {
548  static const char *TypeString[] = {"Array", "Object", "Map", ""};
549  return TypeString[(Int32)type];
550  }
551 
552  void updateStack(const char *text)
553  {
554  string str(text);
555  for (UInt32 typeIdx = 0; typeIdx < NUM_TYPES; typeIdx++)
556  {
557  const char *testString = getTypeString((Type)typeIdx);
558  std::size_t found = str.find(testString);
559  if (found != std::string::npos)
560  {
561  TypePair tPair;
562  tPair.type = (Type)typeIdx;
563  tPair.name = text;
564  typeStack.push_back(tPair);
565  break;
566  }
567  }
568  }
569 
570  friend expansionstringstream &operator<<(expansionstringstream &os, const char *text);
572  friend expansionstringstream &operator<<(expansionstringstream &os, string &str);
573  friend expansionstringstream &operator<<(expansionstringstream &os, const string &str);
574  friend expansionstringstream &operator<<(expansionstringstream &os, expansionstringstream::Type type);
575  friend expansionstringstream &operator<<(expansionstringstream &os,
577 
578  std::string prefix_str() { return prefixStream.str(); }
579 
580  std::string var_name_str() { return varNameStream.str(); }
581 
582  std::string type_name_str() { return typeNameStream.str(); }
583 
584  std::string debug_str()
585  {
586  std::ostringstream tempStream;
587  std::vector< TypePair >::reverse_iterator rit = typeStack.rbegin();
588  tempStream << "/*Take 1" << std::endl;
589  for (; rit != typeStack.rend(); ++rit)
590  {
591  tempStream << "." << var_name(rit->name);
592  }
593  tempStream << std::endl;
594  for (; rit != typeStack.rend(); ++rit)
595  {
596  tempStream << var_name(rit->name);
597  }
598  tempStream << std::endl;
599  tempStream << "*/" << std::endl;
600  return typeNameStream.str();
601  }
602 
603  std::string debug_str_2()
604  {
605  std::string temp;
606 
607  bool arrayMapEncountered = false;
608  std::vector< TypePair >::reverse_iterator rit = typeStack.rbegin();
609  for (; rit != typeStack.rend(); ++rit)
610  {
611  std::vector< TypePair >::reverse_iterator previous = rit + 1;
612  bool previousArrayOrMap = false;
613  if (previous != typeStack.rend())
614  {
615  if (previous->type == ARRAY || previous->type == MAP)
616  {
617  previousArrayOrMap = true;
618  }
619  }
620  std::ostringstream tempStr;
621  if (arrayMapEncountered)
622  {
623  tempStr << type_name(rit->name) << temp;
624  temp = tempStr.str();
625  }
626  else if (rit->type == VAR)
627  {
628  tempStr << var_name(rit->name);
629  temp = tempStr.str();
630  }
631  else if (rit->type == MAP || rit->type == ARRAY)
632  {
633  if (temp.length() && !previousArrayOrMap)
634  {
635  tempStr << type_name(rit->name) << std::dot << temp;
636  }
637  else
638  {
639  tempStr << type_name(rit->name);
640  }
641  temp = tempStr.str();
642  arrayMapEncountered = true;
643  }
644  else
645  {
646  if (rit + 1 == typeStack.rend())
647  {
648  tempStr << var_name(rit->name) << std::dot << temp;
649  temp = tempStr.str();
650  }
651  }
652  }
653  std::ostringstream tempStr;
654  tempStr << var_name(temp);
655  temp = tempStr.str();
656  return temp;
657  }
658 
659  std::string debug_stack_str()
660  {
661  std::ostringstream tempStr;
662  std::vector< TypePair >::iterator it = typeStack.begin();
663  for (; it != typeStack.end(); ++it)
664  {
665  tempStr << getTypeString(it->type) << ".";
666  }
667  return tempStr.str();
668  }
669 
670  bool wasPreviousPrevious(Type type)
671  {
672  if (typeStack.size() > 1)
673  {
674  auto &lastType = typeStack[typeStack.size() - 2];
675  return (lastType.type == type ? true : false);
676  }
677  return false;
678  }
679 
680  bool wasPrevious(Type type)
681  {
682  if (typeStack.size())
683  {
684  auto &lastType = typeStack.back();
685  return (lastType.type == type ? true : false);
686  }
687  return false;
688  }
689 
690  bool has(Type type)
691  {
692  struct special_compare : public std::unary_function< Type, bool >
693  {
694  explicit special_compare(const Type &baseline)
695  : baseline(baseline)
696  {
697  }
698  bool operator()(const TypePair &arg) { return arg.type == baseline; }
699  Type baseline;
700  };
701 
702  std::vector< TypePair >::iterator it = find_if(typeStack.begin(), typeStack.end(), special_compare(type));
703  return (it != typeStack.end() ? true : false);
704  }
705 
706  ostringstream varNameStream;
707  ostringstream typeNameStream;
708  ostringstream prefixStream;
709  vector< TypePair > typeStack;
710  };
711 
712  inline expansionstringstream &operator<<(expansionstringstream &os, const char *text)
713  {
714  bool previousWasArray =
715  os.wasPrevious(expansionstringstream::ARRAY) || os.wasPrevious(expansionstringstream::MAP);
716  os.updateStack(text);
717  bool newIsObject = os.wasPrevious(expansionstringstream::OBJECT);
718  if (!previousWasArray || !newIsObject)
719  {
720  if (!os.varNameStream.str().length())
721  {
722  os.varNameStream << var_name(text);
723  }
724  else
725  {
726  os.varNameStream << clean_name(text);
727  }
728  if (!os.typeNameStream.str().length())
729  {
730  os.typeNameStream << type_name(text);
731  }
732  else
733  {
734  os.typeNameStream << text;
735  }
736  if (!os.prefixStream.str().length())
737  {
738  os.prefixStream << var_name(text);
739  }
740  else
741  {
742  os.prefixStream << std::dot << var_name(text);
743  }
744  }
745  return os;
746  }
747 
749  {
750  if (os.prefixStream.str().length())
751  {
752  os.prefixStream << ".";
753  }
754  return os;
755  }
756 
757  inline expansionstringstream &operator<<(expansionstringstream &os, string &str)
758  {
759  os << str.c_str();
760  return os;
761  }
762 
763  inline expansionstringstream &operator<<(expansionstringstream &os, const string &str)
764  {
765  os << str.c_str();
766  return os;
767  }
768 
769  inline expansionstringstream &operator<<(expansionstringstream &os, expansionstringstream::Type type)
770  {
771  os << os.getTypeString(type);
772  return os;
773  }
774 
775  inline expansionstringstream &operator<<(expansionstringstream &os,
777  {
778  return ((*_Pfn)(os));
779  }
780 
781 } // namespace trader
Definition: fileoutputstream.h:268
Definition: fileoutputstream.h:61
Definition: fileoutputstream.h:154
Definition: fileoutputstream.h:530
Definition: fileoutputstream.h:3
Definition: fileoutputstream.h:518
Definition: fileoutputstream.h:468
Definition: fileoutputstream.h:44
Definition: fileoutputstream.h:290
Definition: fileoutputstream.h:404
Definition: fileoutputstream.h:343
Definition: app.h:7