PageRenderTime 67ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/Applications/CLI/BRAINSTools/BRAINSCommonLib/PrettyPrintTable.h

https://github.com/pieper/Slicer4
C Header | 155 lines | 128 code | 15 blank | 12 comment | 17 complexity | 3b867f399cfe97c603eda693fcd2bcdc MD5 | raw file
  1. #ifndef __PrettyPrintTable_h
  2. #define __PrettyPrintTable_h
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <string>
  6. #include <ostream>
  7. #if defined( _WIN32 ) || defined( _WIN64 )
  8. // Windows uses a different function name for this behavior.
  9. # define SNPRINTF_FUNC _snprintf
  10. #else
  11. # define SNPRINTF_FUNC snprintf
  12. #endif
  13. /**
  14. * \class PrettyPrintTable
  15. * \author Kent Williams
  16. * Simple class to print out column-aligned tables
  17. */
  18. class PrettyPrintTable
  19. {
  20. public:
  21. private:
  22. typedef std::vector< std::string > rowType;
  23. typedef std::vector< rowType > tableType;
  24. tableType m_Table;
  25. unsigned int m_Pad;
  26. bool m_rightJustify;
  27. public:
  28. PrettyPrintTable():m_Pad(1),
  29. m_rightJustify(false)
  30. {}
  31. void setTablePad(unsigned int pad)
  32. {
  33. this->m_Pad = pad;
  34. }
  35. void leftJustify(void)
  36. {
  37. m_rightJustify = false;
  38. }
  39. void rightJustify(void)
  40. {
  41. m_rightJustify = true;
  42. }
  43. void add(const unsigned int row, const unsigned int column, const char *const s)
  44. {
  45. // Make sure the table has enough rows.
  46. if ( m_Table.size() <= row )
  47. { // Add empty rows
  48. m_Table.resize(row + 1);
  49. }
  50. // For each row, make sure that it now has enough columns.
  51. for ( unsigned int q = 0; q < m_Table.size(); q++ )
  52. {
  53. if ( m_Table[q].size() <= column )
  54. {
  55. m_Table[q].resize( column + 1, std::string("") );
  56. }
  57. }
  58. m_Table[row][column] = s;
  59. }
  60. void add(const unsigned int row, const unsigned int column, const std::string & s)
  61. {
  62. add( row, column, s.c_str() );
  63. }
  64. void add(const unsigned int row, const unsigned int column, const int x, const char *printf_format = 0)
  65. {
  66. const char *format(printf_format == 0 ? "%d" : printf_format);
  67. char buf[4096];
  68. SNPRINTF_FUNC(buf, 4096, format, x);
  69. this->add(row, column, buf);
  70. }
  71. void add(const unsigned int row, const unsigned int column, const unsigned int x, const char *printf_format = 0)
  72. {
  73. const char *format(printf_format == 0 ? "%d" : printf_format);
  74. char buf[4096];
  75. SNPRINTF_FUNC(buf, 4096, format, x);
  76. this->add(row, column, buf);
  77. }
  78. void add(const unsigned int row, const unsigned int column, const double x, const char *printf_format = 0)
  79. {
  80. const char *format(printf_format == 0 ? "%lf" : printf_format);
  81. char buf[4096];
  82. SNPRINTF_FUNC(buf, 4096, format, x);
  83. this->add(row, column, buf);
  84. }
  85. void Print(std::ostream & output)
  86. {
  87. typedef std::vector< unsigned int > ColWidthsType;
  88. ColWidthsType colWidths(m_Table[0].size(), 0);
  89. // find largest columns
  90. for ( unsigned i = 0; i < m_Table.size(); i++ )
  91. {
  92. for ( unsigned j = 0; j < m_Table[i].size(); j++ )
  93. {
  94. if ( colWidths[j] < m_Table[i][j].size() )
  95. {
  96. colWidths[j] = m_Table[i][j].size();
  97. }
  98. }
  99. }
  100. for ( unsigned i = 0; i < m_Table.size(); i++ )
  101. {
  102. for ( unsigned j = 0; j < m_Table[i].size(); j++ )
  103. {
  104. // if right justify, output leading blanks
  105. if ( m_rightJustify )
  106. {
  107. int count = colWidths[j]
  108. - m_Table[i][j].size();
  109. while ( count-- )
  110. {
  111. output << " ";
  112. }
  113. }
  114. unsigned int k(0);
  115. for ( k = 0; k < m_Table[i][j].size(); k++ )
  116. {
  117. output << m_Table[i][j][k];
  118. }
  119. unsigned int limit;
  120. // if right justify, just output pad
  121. if ( m_rightJustify )
  122. {
  123. limit = this->m_Pad;
  124. k = 0;
  125. }
  126. else
  127. {
  128. // print column fill + pad
  129. limit = colWidths[j] + this->m_Pad;
  130. }
  131. for (; k < limit; k++ )
  132. {
  133. output << " ";
  134. }
  135. }
  136. output << std::endl;
  137. }
  138. }
  139. };
  140. #endif // PrettyPrintTable_h