bump product version to 4.1.6.2
[LibreOffice.git] / reportdesign / source / core / misc / reportformula.cxx
blob94158d8d00eb7d10bf7ea70183258c8962c44a05
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "reportformula.hxx"
22 #include <rtl/ustrbuf.hxx>
24 //........................................................................
25 namespace rptui
27 //........................................................................
29 using ::com::sun::star::uno::Any;
31 namespace
33 //----------------------------------------------------------------
34 const OUString& lcl_getExpressionPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
36 static OUString s_sPrefix( "rpt:" );
37 if ( _pTakeLengthOrNull )
38 *_pTakeLengthOrNull = s_sPrefix.getLength();
39 return s_sPrefix;
42 //----------------------------------------------------------------
43 const OUString& lcl_getFieldPrefix( sal_Int32* _pTakeLengthOrNull = NULL )
45 static OUString s_sPrefix( "field:" );
46 if ( _pTakeLengthOrNull )
47 *_pTakeLengthOrNull = s_sPrefix.getLength();
48 return s_sPrefix;
52 //====================================================================
53 //= ReportFormula
54 //====================================================================
55 //--------------------------------------------------------------------
56 ReportFormula::ReportFormula( const OUString& _rFormula )
57 :m_eType( Invalid )
59 impl_construct( _rFormula );
62 //--------------------------------------------------------------------
63 ReportFormula::ReportFormula( const BindType _eType, const OUString& _rFieldOrExpression )
64 :m_eType( _eType )
66 switch ( m_eType )
68 case Expression:
70 if ( _rFieldOrExpression.indexOf( lcl_getExpressionPrefix() ) == 0 )
71 m_sCompleteFormula = _rFieldOrExpression;
72 else
73 m_sCompleteFormula = lcl_getExpressionPrefix() + _rFieldOrExpression;
75 break;
77 case Field:
79 OUStringBuffer aBuffer;
80 aBuffer.append( lcl_getFieldPrefix() );
81 aBuffer.appendAscii( "[" );
82 aBuffer.append( _rFieldOrExpression );
83 aBuffer.appendAscii( "]" );
84 m_sCompleteFormula = aBuffer.makeStringAndClear();
86 break;
87 default:
88 OSL_FAIL( "ReportFormula::ReportFormula: illegal bind type!" );
89 return;
92 m_sUndecoratedContent = _rFieldOrExpression;
94 //--------------------------------------------------------------------
95 ReportFormula::~ReportFormula()
98 //--------------------------------------------------------------------
99 void ReportFormula::impl_construct( const OUString& _rFormula )
101 m_sCompleteFormula = _rFormula;
103 sal_Int32 nPrefixLen( -1 );
104 // is it an ordinary expression?
105 if ( m_sCompleteFormula.indexOf( lcl_getExpressionPrefix( &nPrefixLen ) ) == 0 )
107 m_eType = Expression;
108 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen );
109 return;
112 /// does it refer to a field?
113 if ( m_sCompleteFormula.indexOf( lcl_getFieldPrefix( &nPrefixLen ) ) == 0 )
115 if ( ( m_sCompleteFormula.getLength() >= nPrefixLen + 2 )
116 && ( m_sCompleteFormula[ nPrefixLen ] == '[' )
117 && ( m_sCompleteFormula[ m_sCompleteFormula.getLength() - 1 ] == ']' )
120 m_eType = Field;
121 m_sUndecoratedContent = m_sCompleteFormula.copy( nPrefixLen + 1, m_sCompleteFormula.getLength() - nPrefixLen - 2 );
122 return;
126 m_eType = Invalid;
129 //--------------------------------------------------------------------
130 OUString ReportFormula::getBracketedFieldOrExpression() const
132 bool bIsField = ( getType() == Field );
133 OUStringBuffer aFieldContent;
134 if ( bIsField )
135 aFieldContent.appendAscii( "[" );
136 aFieldContent.append( getUndecoratedContent() );
137 if ( bIsField )
138 aFieldContent.appendAscii( "]" );
140 return aFieldContent.makeStringAndClear();
142 //--------------------------------------------------------------------
143 const OUString& ReportFormula::getUndecoratedContent() const
145 return m_sUndecoratedContent;
147 const OUString& ReportFormula::getCompleteFormula() const { return m_sCompleteFormula; }
148 bool ReportFormula::isValid() const { return getType() != Invalid; }
149 ReportFormula& ReportFormula::operator=(class ReportFormula const & _rHd)
151 if ( this == &_rHd )
152 return *this;
153 m_eType = _rHd.m_eType;
154 m_sCompleteFormula = _rHd.m_sCompleteFormula;
155 m_sUndecoratedContent = _rHd.m_sUndecoratedContent;
156 return *this;
158 //--------------------------------------------------------------------
159 OUString ReportFormula::getEqualUndecoratedContent() const
161 OUStringBuffer aBuffer;
162 aBuffer.appendAscii( "=" );
163 aBuffer.append( getUndecoratedContent() );
164 return aBuffer.makeStringAndClear();
167 //........................................................................
168 } // namespace rptui
169 //........................................................................
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */