LCOV - code coverage report
Current view: top level - ugbase/common/util - string_table_stream.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 19 0
Test Date: 2025-09-21 23:31:46 Functions: 0.0 % 1 0

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2013-2015:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Martin Rupp
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 §7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : 
      33              : #ifndef STRING_TABLE_STREAM_H_
      34              : #define STRING_TABLE_STREAM_H_
      35              : 
      36              : #include <vector>
      37              : #include "table.h"
      38              : #include "string_util.h"
      39              : #include "stringify.h"
      40              : 
      41              : /// \addtogroup ugbase_common_types
      42              : /// \{
      43              : ///     Useful for printing a table like a stream to the terminal or into a file.
      44              : /**
      45              :  * Here's an example on how to use the class:
      46              :  * \code
      47              :  * #include "common/util/string_table_stream.h"
      48              :  * ...
      49              :  * ug::StringTableStream sts;
      50              :  * sts << "|" << 31.3 << ", " <<  0 << "|\n";
      51              :  * sts << "|" << 0 << ", " << 768 << "|\n";
      52              :  * std::cout << table;
      53              :  * \endcode
      54              :  *
      55              :  * And this is what the output looks like:
      56              :  * \verbatim
      57              :   | 31.3 , 0   |
      58              :   | 0    , 768 |
      59              :  * \endverbatim
      60              :  *
      61              :  * This class uses internally StringTable.
      62              :  * @sa StringTable, Table
      63              :  */
      64              : namespace ug{
      65            0 : class StringTableStream
      66              : {
      67              : private:
      68              :         StringTable s;
      69              :         size_t m_curRow;
      70              :         size_t m_curCol;
      71              : 
      72              : public:
      73              :         StringTableStream()
      74            0 :         {
      75            0 :                 m_curRow=m_curCol = 0;
      76              :         }
      77              : 
      78              :         StringTable &table() { return s; }
      79              : 
      80              :         /**
      81              :          * operator <<. sets curCol++
      82              :          * @param t anything which is ostream << -able
      83              :          * @return *this
      84              :          */
      85              :         template<typename T>
      86              :         StringTableStream &operator << (T t)
      87              :         {
      88              :                 s(m_curRow, m_curCol++) = ToString(t);
      89              :                 return *this;
      90              :         }
      91              : 
      92              :         /**
      93              :          * operator <<
      94              :          * @param c a const char *. If contains a \n, sets curRow++, curCol=0
      95              :          * @return *this
      96              :          */
      97            0 :         StringTableStream &operator << (const char *c)
      98              :         {
      99            0 :                 size_t l = strlen(c);
     100              : //              size_t begin=0;
     101              :                 std::vector<char> vc;
     102            0 :                 for(size_t i=0; i<l; i++)
     103              :                 {
     104            0 :                         if(c[i] == '\n')
     105              :                         {
     106            0 :                                 vc.push_back(0);
     107            0 :                                 s(m_curRow, m_curCol) = &vc[0];
     108              :                                 vc.clear();
     109            0 :                                 m_curRow++;
     110            0 :                                 m_curCol=0;
     111            0 :                                 if(i == l-1) return *this;
     112              :                         }
     113              :                         else
     114            0 :                                 vc.push_back(c[i]);
     115              :                 }
     116            0 :                 vc.push_back(0);
     117            0 :                 s(m_curRow, m_curCol++) = &vc[0];
     118              :                 return *this;
     119            0 :         }
     120              : 
     121              : 
     122              : 
     123              :         struct RepeatedCol
     124              :         {
     125              :                 RepeatedCol(std::string _content, size_t _number) :
     126              :                         content(_content), number(_number) {}
     127              :                 std::string content;
     128              :                 size_t number;
     129              :         };
     130              : 
     131              :         StringTableStream &operator << (RepeatedCol c)
     132              :         {
     133              :                 for(size_t i=0; i<c.number; i++)
     134              :                         *this << c.content;
     135              :                 return *this;
     136              :         }
     137              : 
     138              : 
     139              :         /**
     140              :          * \code
     141              :          * ug::StringTableStream sts;
     142              :          * sts << 1 << "\n";
     143              :          * sts << sts.emtpy_col(1) << 1 << "\n";
     144              :          * sts << sts.empty_col(2) << 1 << "\n";
     145              :          * \endcode
     146              :          * prints
     147              :          * \verbatim
     148              :          * 1
     149              :          *   1
     150              :          *     1
     151              :          * \endverbatim
     152              :          * @param nrRepeat                      number of times to repeat
     153              :          * @param contentToRepeat       what to repeat
     154              :          * @return RepeatedCol-struct for <<-ing into sts
     155              :          */
     156              :         RepeatedCol empty_col(size_t i)
     157              :         {
     158              :                 return RepeatedCol("", i);
     159              :         }
     160              : 
     161              :         /**
     162              :          * \code
     163              :          * ug::StringTableStream sts;
     164              :          * sts << 1 << sts.repeat_col(2, 0) << "\n";
     165              :          * sts << 0 << 1 << 0 << "\n";
     166              :          * sts << sts.repeat(2, 0) << 1 << "\n";
     167              :          * \endcode
     168              :          * prints
     169              :          * \verbatim
     170              :          * 1 0 0
     171              :          * 0 1 0
     172              :          * 0 0 1
     173              :          * \endverbatim
     174              :          * @param nrRepeat                      number of times to repeat
     175              :          * @param contentToRepeat       what to repeat
     176              :          * @return RepeatedCol-struct for <<-ing into sts
     177              :          */
     178              :         template<typename T>
     179              :         RepeatedCol repeat_col(size_t nrRepeat, T contentToRepeat)
     180              :         {
     181              :                 return RepeatedCol(ToString(contentToRepeat), nrRepeat);
     182              :         }
     183              : 
     184              :         template<typename T>
     185              :         void set(size_t r, size_t c, T t)
     186              :         {
     187              :                 s(r, c) = ToString(t);
     188              :         }
     189              : 
     190              :         StringTableStream &operator << (std::string str)
     191              :         {
     192            0 :                 *this << str.c_str();
     193            0 :                 return *this;
     194              :         }
     195              :         std::string to_string() const
     196              :         {
     197            0 :                 return s.to_string();
     198              :         }
     199              : 
     200              :         void clear()
     201              :         {
     202              :                 m_curRow=m_curCol = 0;
     203              :                 s.clear();
     204              :         }
     205              : };
     206              : 
     207              : inline std::ostream& operator << (std::ostream& os, const StringTableStream &sts)
     208              : {
     209              :         os << sts.to_string();
     210              :         return os;
     211              : }
     212              : 
     213              : }
     214              : #endif /* STRING_TABLE_STREAM_H_ */
        

Generated by: LCOV version 2.0-1