3 // $Id: compileroutput.cpp,v 1.3 2005/06/08 08:08:06 nilu Exp $
5 // CppTest - A C++ Unit Testing Framework
6 // Copyright (c) 2003 Niklas Lundell
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
30 #include "cpptest-compileroutput.h"
31 #include "cpptest-source.h"
39 // Checks for output format modifiers.
42 check_format(const string
& format
,
43 string::size_type
& pos
,
47 if (format
.compare(pos
, mod
.size(), mod
) == 0)
50 throw Test::CompilerOutput::InvalidFormat(format
);
57 } // anonymous namespace
59 /// Constructs a compiler output handler.
61 /// \param format Pre-defined compiler output format.
62 /// \param stream Stream to output to.
64 CompilerOutput::CompilerOutput(Format format
, ostream
& stream
)
68 static const char* table
[] =
70 "%file:%line: %text", // Generic
71 "Error cpptest %file %line: %text", // BCC
72 "%file:%line: %text", // GCC
73 "%file(%line) : %text" // MSVC
76 _format
= table
[format
];
79 /// Constructs a compiler output handler.
81 /// \param format %Output format to use.
82 /// \param stream Stream to output to.
84 /// \exception InvalidFormat Invalid format specified.
86 CompilerOutput::CompilerOutput(const string
& format
, ostream
& stream
)
91 int expr(0), file(0), line(0);
93 for (string::size_type pos
= 0;
94 (pos
= _format
.find_first_of('%', pos
)) != string::npos
; )
97 if (check_format(_format
, pos
, "expr", expr
)) ;
98 else if (check_format(_format
, pos
, "file", file
)) ;
99 else if (check_format(_format
, pos
, "line", line
)) ;
101 throw InvalidFormat(format
);
104 if (!expr
&& !file
&& !line
)
105 throw InvalidFormat(format
);
109 CompilerOutput::assertment(const Source
& s
)
112 string::size_type pos
;
114 fmt
.reserve(fmt
.size() + 128);
116 if ((pos
= fmt
.find("%file")) != string::npos
)
117 fmt
.replace(pos
, 5, s
.file());
119 if ((pos
= fmt
.find("%text")) != string::npos
)
120 fmt
.replace(pos
, 5, s
.message());
122 if ((pos
= fmt
.find("%line")) != string::npos
)
126 fmt
.replace(pos
, 5, ss
.str());
129 _stream
<< fmt
<< endl
;