Update of /cvsroot/objecthandler/ObjectHandler/ohxl/Conversions
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv17350/ohxl/Conversions
Added Files:
all.hpp matrixtooper.hpp opertomatrix.hpp opertoscalar.cpp
opertoscalar.hpp opertovector.hpp scalartooper.cpp
scalartooper.hpp validations.cpp validations.hpp
vectortooper.hpp
Log Message:
consolidate conversion functions in preparation for implementation of coercion
--- NEW FILE: all.hpp ---
/*
Copyright (C) 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_all_hpp
#define ohxl_conversions_all_hpp
#include <ohxl/Conversions/opertoscalar.hpp>
#include <ohxl/Conversions/opertovector.hpp>
#include <ohxl/Conversions/opertomatrix.hpp>
#include <ohxl/Conversions/scalartooper.hpp>
#include <ohxl/Conversions/vectortooper.hpp>
#include <ohxl/Conversions/matrixtooper.hpp>
#include <ohxl/Conversions/validations.hpp>
#endif
--- NEW FILE: scalartooper.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_scalartooper_hpp
#define ohxl_conversions_scalartooper_hpp
#include <oh/objhandlerdefines.hpp>
#include <xlsdk/xlsdkdefines.hpp>
#include <string>
#include <boost/any.hpp>
namespace ObjHandler {
DLL_API void scalarToXloper(XLOPER &xLong, const long &value);
DLL_API void scalarToXloper(XLOPER &xDouble, const double &value);
DLL_API void scalarToXloper(XLOPER &xBoolean, const bool &value);
DLL_API void scalarToXloper(XLOPER &xString, const std::string &value);
DLL_API void scalarToXloper(
XLOPER &xAny,
const boost::any &value,
const bool &expandVectors = true);
}
#endif
--- NEW FILE: opertomatrix.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_opertomatrix_hpp
#define ohxl_conversions_opertomatrix_hpp
#include <ohxl/Conversions/opertoscalar.hpp>
#include <vector>
namespace ObjHandler {
template <class T>
std::vector<std::vector<T> > operToMatrix(const OPER &xMatrix) {
OPER xTemp;
bool needToFree = false;
try {
std::vector<std::vector<T> > ret;
if (xMatrix.xltype & xltypeErr)
throw Exception("input value is #NULL (xltypeErr)");
if (xMatrix.xltype & (xltypeMissing | xltypeNil))
return ret;
const OPER *xMulti;
if (xMatrix.xltype == xltypeMulti)
xMulti = &xMatrix;
else {
Excel(xlCoerce, &xTemp, 2, &xMatrix, TempInt(xltypeMulti));
xMulti = &xTemp;
needToFree = true;
}
for (int i=0; i<xMulti->val.array.rows; i++) {
std::vector<T> row;
for (int j=0; j<xMulti->val.array.columns; j++) {
T value;
operToScalar(value, xMulti->val.array.lparray[i * xMulti->val.array.columns + j]);
row.push_back(value);
}
ret.push_back(row);
}
if (needToFree)
Excel(xlFree, 0, 1, &xTemp);
return ret;
} catch (const std::exception &e) {
if (needToFree)
Excel(xlFree, 0, 1, &xTemp);
std::ostringstream msg;
msg << "operToMatrix: " << e.what();
throw Exception(msg.str());
}
}
template <class T>
std::vector<std::vector<T> > fpToMatrix(const FP &fpMatrix) {
std::vector<std::vector<T> > ret;
for (int i=0; i<fpMatrix.rows; i++) {
std::vector<T> row;
for (int j=0; j<fpMatrix.columns; j++)
row.push_back(fpMatrix.array[i * fpMatrix.columns + j]);
ret.push_back(row);
}
return ret;
}
}
#endif
--- NEW FILE: opertoscalar.cpp ---
/*
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2004, 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ohxl/Conversions/opertoscalar.hpp>
#include <oh/exception.hpp>
#include <oh/utilities.hpp>
#include <sstream>
namespace ObjHandler {
DLL_API void operToScalar(long &ret, const OPER &xScalar, const long &defaultValue) {
try {
//if (xScalar.xltype & xltypeErr)
// throw Exception("input value is #NULL (xltypeErr)");
//if (xScalar.xltype & (xltypeMissing | xltypeNil))
if (xScalar.xltype & (xltypeMissing | xltypeNil | xltypeErr))
ret = defaultValue;
else if (xScalar.xltype == xltypeNum)
ret = xScalar.val.num;
else {
OPER xLong;
Excel(xlCoerce, &xLong, 2, &xScalar, TempInt(xltypeInt));
ret = xLong.val.w;
}
} catch (const std::exception &e) {
std::ostringstream msg;
msg << "operToScalar cannot coerce to long: " << e.what();
throw Exception(msg.str());
}
}
DLL_API void operToScalar(double &ret, const OPER &xScalar, const double &defaultValue) {
try {
//if (xScalar.xltype & xltypeErr)
// throw Exception("input value is #NULL (xltypeErr)");
//if (xScalar.xltype & (xltypeMissing | xltypeNil))
if (xScalar.xltype & (xltypeMissing | xltypeNil | xltypeErr))
ret = defaultValue;
else if (xScalar.xltype == xltypeNum)
ret = xScalar.val.num;
else {
OPER xDouble;
Excel(xlCoerce, &xDouble, 2, &xScalar, TempInt(xltypeNum));
ret = xDouble.val.num;
}
} catch (const std::exception &e) {
std::ostringstream msg;
msg << "operToScalar cannot coerce to double: " << e.what();
throw Exception(msg.str());
}
}
DLL_API void operToScalar(bool &ret, const OPER &xScalar, const bool &defaultValue) {
try {
//if (xScalar.xltype & xltypeErr)
// throw Exception("input value is #NULL (xltypeErr)");
//if (xScalar.xltype & (xltypeMissing | xltypeNil))
if (xScalar.xltype & (xltypeMissing | xltypeNil | xltypeErr))
ret = defaultValue;
else if (xScalar.xltype == xltypeBool)
ret = xScalar.val.boolean != 0;
else {
OPER xBool;
Excel(xlCoerce, &xBool, 2, &xScalar, TempInt(xltypeBool));
ret = xBool.val.boolean != 0;
}
} catch (const std::exception &e) {
std::ostringstream msg;
msg << "operToScalar cannot coerce to bool: " << e.what();
throw Exception(msg.str());
}
}
DLL_API void operToScalar(std::string &ret, const OPER &xScalar, const std::string &defaultValue) {
OPER xTemp;
bool needToFree = false;
try {
//if (xScalar.xltype & xltypeErr)
// throw Exception("input value is #NULL (xltypeErr)");
//if (xScalar.xltype & (xltypeMissing | xltypeNil))
if (xScalar.xltype & (xltypeMissing | xltypeNil | xltypeErr)) {
ret = defaultValue;
return;
}
const OPER *xString;
if (xScalar.xltype == xltypeStr)
xString = &xScalar;
else {
Excel(xlCoerce, &xTemp, 2, &xScalar, TempInt(xltypeStr));
xString = &xTemp;
needToFree = true;
}
//if (xString->val.str[0])
// ret.assign(xString->val.str + 1, xString->val.str[0]);
// expirimental workaround for apparent bug in Excel API
// where the value for the string length wraps around the byte
int stringLength = xString->val.str[0];
if (stringLength < 0) stringLength += 257;
if (stringLength)
ret.assign(xString->val.str + 1, stringLength);
if (needToFree)
Excel(xlFree, 0, 1, &xTemp);
} catch (const std::exception &e) {
if (needToFree)
Excel(xlFree, 0, 1, &xTemp);
std::ostringstream msg;
msg << "operToScalar cannot coerce to string: " << e.what();
throw Exception(msg.str());
}
}
DLL_API void operToScalar(boost::any &ret, const OPER &xScalar) {
//if (xScalar.xltype & xltypeErr)
// throw Exception("input value is #NULL (xltypeErr)");
//if (xScalar.xltype & (xltypeMissing | xltypeNil))
if (xScalar.xltype & (xltypeMissing | xltypeNil | xltypeErr))
ret = boost::any();
//else if (xScalar.xltype & xltypeErr)
// ret = InvalidInput;
else if (xScalar.xltype == xltypeNum)
ret = xScalar.val.num;
else if (xScalar.xltype == xltypeBool)
ret = xScalar.val.boolean != 0;
else if (xScalar.xltype == xltypeStr) {
std::string value;
operToScalar(value, xScalar);
ret = value;
} else
throw Exception("operToScalar cannot coerce to boost::any: unexpected datatype");
}
void operToOper(OPER *xTarget, const OPER *xSource) {
if (xSource->xltype == xltypeNum) {
xTarget->xltype = xltypeNum;
xTarget->val.num = xSource->val.num;
return;
} else if (xSource->xltype == xltypeStr) {
int len = xSource->val.str[0];
xTarget->val.str = new char[ len + 1 ];
if (!xTarget->val.str)
throw Exception("operToOper: error calling new");
xTarget->xltype = xltypeStr | xlbitDLLFree;
xTarget->val.str[0] = len;
if (len)
strncpy(xTarget->val.str + 1, xSource->val.str + 1, len);
return;
} else if (xSource->xltype == xltypeErr) {
xTarget->xltype = xltypeErr;
xTarget->val.err = xSource->val.err;
return;
} else if (xSource->xltype == xltypeNil) {
xTarget->xltype = xltypeNil;
return;
} else {
std::ostringstream msg;
msg << "operToOper: unexpected OPER type: " << xSource->xltype;
throw Exception(msg.str());
}
}
}
--- NEW FILE: matrixtooper.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_matrixtooper_hpp
#define ohxl_conversions_matrixtooper_hpp
#include <ohxl/Conversions/scalartooper.hpp>
#include <vector>
namespace ObjHandler {
template <class T>
void matrixToXloper(XLOPER &xMatrix, const std::vector<std::vector<T> > &vv) {
if (vv.empty() || vv[0].empty()) {
//xMatrix.xltype = xltypeNum;
//xMatrix.val.num = 0;
xMatrix.xltype = xltypeErr;
xMatrix.val.err = xlerrNA;
return;
}
xMatrix.val.array.rows = vv.size();
xMatrix.val.array.columns = vv[0].size();
xMatrix.val.array.lparray = new XLOPER[xMatrix.val.array.rows * xMatrix.val.array.columns];
if (!xMatrix.val.array.lparray)
throw Exception("matrixToXloper: error on call to new");
xMatrix.xltype = xltypeMulti | xlbitDLLFree;
for (unsigned int i=0; i<vv.size(); i++) {
std::vector<T> v = vv[i];
for (unsigned int j=0; j<v.size(); j++)
scalarToXloper(xMatrix.val.array.lparray[i * v.size() + j], v[j]);
}
}
}
#endif
--- NEW FILE: opertoscalar.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_opertoscalar_hpp
#define ohxl_conversions_opertoscalar_hpp
#include <oh/objhandlerdefines.hpp>
#include <xlsdk/xlsdkdefines.hpp>
#include <string>
#include <boost/any.hpp>
namespace ObjHandler {
DLL_API void operToScalar(long &ret, const OPER &xScalar, const long &defaultValue = 0);
DLL_API void operToScalar(double &ret, const OPER &xScalar, const double &defaultValue = 0.);
DLL_API void operToScalar(bool &ret, const OPER &xScalar, const bool &defaultValue = false);
DLL_API void operToScalar(std::string &ret, const OPER &xScalar, const std::string &defaultValue = "");
DLL_API void operToScalar(boost::any &ret, const OPER &xScalar);
void operToOper(OPER *xTarget, const OPER *xSource);
}
#endif
--- NEW FILE: vectortooper.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_vectortooper_hpp
#define ohxl_conversions_vectortooper_hpp
#include <ohxl/Conversions/scalartooper.hpp>
#include <ohxl/functioncall.hpp>
#include <vector>
namespace ObjHandler {
template <class T>
void vectorToXloper(XLOPER &xVector, const std::vector<T> &v) {
if (v.empty()) {
//xVector.xltype = xltypeNum;
//xVector.val.num = 0;
xVector.xltype = xltypeErr;
xVector.val.err = xlerrNA;
return;
}
if (FunctionCall::instance().getCallerDimensions() == Row) {
xVector.val.array.columns = v.size();
xVector.val.array.rows = 1;
} else {
xVector.val.array.rows = v.size();
xVector.val.array.columns = 1;
}
xVector.val.array.lparray = new XLOPER[v.size()];
if (!xVector.val.array.lparray)
throw Exception("vectorToXloper: error on call to new");
xVector.xltype = xltypeMulti | xlbitDLLFree;
for (unsigned int i=0; i<v.size(); i++)
scalarToXloper(xVector.val.array.lparray[i], v[i]);
}
}
#endif
--- NEW FILE: scalartooper.cpp ---
/*
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2004, 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ohxl/Conversions/scalartooper.hpp>
#include <ohxl/Conversions/vectortooper.hpp>
#include <ohxl/Conversions/matrixtooper.hpp>
#include <oh/exception.hpp>
#include <oh/utilities.hpp>
#include <sstream>
namespace ObjHandler {
DLL_API void scalarToXloper(XLOPER &xLong, const long &value) {
xLong.xltype = xltypeNum;
xLong.val.num = value;
}
DLL_API void scalarToXloper(XLOPER &xDouble, const double &value) {
xDouble.xltype = xltypeNum;
xDouble.val.num = value;
}
DLL_API void scalarToXloper(XLOPER &xBoolean, const bool &value) {
xBoolean.xltype = xltypeBool;
xBoolean.val.boolean = value;
}
DLL_API void scalarToXloper(XLOPER &xString, const std::string &value) {
int len = __min(XL_MAX_STR_LEN, value.length());
xString.val.str = new char[ len + 1 ];
if (!xString.val.str)
throw Exception("stringToXloper: error calling new");
xString.xltype = xltypeStr | xlbitDLLFree;
xString.val.str[0] = len;
if (len)
strncpy(xString.val.str + 1, value.c_str(), len);
}
DLL_API void scalarToXloper(
XLOPER &xAny,
const boost::any &value,
const bool &expandVectors) {
if (value.type() == typeid(XLOPER)) {
XLOPER xTemp = boost::any_cast<XLOPER>(value);
Excel(xlCoerce, &xAny, 1, &xTemp);
} else if (value.type() == typeid(XLOPER*)) {
XLOPER *xTemp = boost::any_cast<XLOPER*>(value);
Excel(xlCoerce, &xAny, 1, xTemp);
} else if (value.type() == typeid(int)) {
xAny.xltype = xltypeNum;
xAny.val.num = boost::any_cast<int>(value);
} else if (value.type() == typeid(long)) {
xAny.xltype = xltypeNum;
xAny.val.num = boost::any_cast<long>(value);
} else if (value.type() == typeid(double)) {
xAny.xltype = xltypeNum;
xAny.val.num = boost::any_cast<double>(value);
} else if (value.type() == typeid(bool)) {
xAny.xltype = xltypeBool;
xAny.val.boolean = boost::any_cast<bool>(value);
} else if (value.type() == typeid(std::string)) {
std::string s = boost::any_cast<std::string>(value);
scalarToXloper(xAny, s);
} else if (value.type() == typeid(std::vector<long>)) {
if (expandVectors) {
std::vector<long> v= boost::any_cast< std::vector<long> >(value);
vectorToXloper(xAny, v);
} else {
scalarToXloper(xAny, std::string(VECTOR));
}
} else if (value.type() == typeid(std::vector<double>)) {
if (expandVectors) {
std::vector<double> v= boost::any_cast< std::vector<double> >(value);
vectorToXloper(xAny, v);
} else {
scalarToXloper(xAny, std::string(VECTOR));
}
} else if (value.type() == typeid(std::vector<bool>)) {
if (expandVectors) {
std::vector<bool> v= boost::any_cast< std::vector<bool> >(value);
vectorToXloper(xAny, v);
} else {
scalarToXloper(xAny, std::string(VECTOR));
}
} else if (value.type() == typeid(std::vector< std::string >)) {
if (expandVectors) {
std::vector< std::string > v= boost::any_cast< std::vector< std::string > >(value);
vectorToXloper(xAny, v);
} else {
scalarToXloper(xAny, std::string(VECTOR));
}
} else if (value.type() == typeid(std::vector< boost::any >)) {
if (expandVectors) {
std::vector< boost::any > v= boost::any_cast< std::vector< boost::any > >(value);
vectorToXloper(xAny, v);
} else {
scalarToXloper(xAny, std::string(VECTOR));
}
} else if (value.type() == typeid(std::vector< std::vector< long > >)) {
if (expandVectors) {
std::vector< std::vector<long> >vv = boost::any_cast< std::vector< std::vector<long> > >(value);
vectorToXloper(xAny, vv);
} else {
scalarToXloper(xAny, std::string(MATRIX));
}
} else if (value.type() == typeid(std::vector< std::vector< double > >)) {
if (expandVectors) {
std::vector< std::vector< double > >vv = boost::any_cast< std::vector< std::vector< double > > >(value);
matrixToXloper(xAny, vv);
} else {
scalarToXloper(xAny, std::string(MATRIX));
}
} else if (value.type() == typeid(std::vector< std::vector< bool > >)) {
if (expandVectors) {
std::vector< std::vector< bool > >vv = boost::any_cast< std::vector< std::vector< bool > > >(value);
// FIXME VC8 compilation fails on this line
// support for matrix of bools not presently required
//matrixToXloper(xAny, vv);
} else {
scalarToXloper(xAny, std::string(MATRIX));
}
} else if (value.type() == typeid(std::vector< std::vector< std::string > >)) {
if (expandVectors) {
std::vector< std::vector< std::string > >vv = boost::any_cast< std::vector< std::vector< std::string > > >(value);
matrixToXloper(xAny, vv);
} else {
scalarToXloper(xAny, std::string(MATRIX));
}
} else if (value.type() == typeid(std::vector< std::vector< boost::any > >)) {
if (expandVectors) {
std::vector< std::vector< boost::any > >vv = boost::any_cast< std::vector< std::vector< boost::any > > >(value);
matrixToXloper(xAny, vv);
} else {
scalarToXloper(xAny, std::string(MATRIX));
}
} else {
xAny.xltype = xltypeErr;
xAny.val.err = xlerrValue;
}
}
}
--- NEW FILE: opertovector.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_opertovector_hpp
#define ohxl_conversions_opertovector_hpp
#include <ohxl/Conversions/opertoscalar.hpp>
#include <vector>
namespace ObjHandler {
template <class T>
std::vector<T> operToVector(const OPER &xVector) {
OPER xTemp;
bool needToFree = false;
try {
std::vector<T> ret;
if (xVector.xltype & xltypeErr)
throw Exception("input value is #NULL (xltypeErr)");
if (xVector.xltype & (xltypeMissing | xltypeNil))
return ret;
const OPER *xMulti;
if (xVector.xltype == xltypeMulti)
xMulti = &xVector;
else {
Excel(xlCoerce, &xTemp, 2, &xVector, TempInt(xltypeMulti));
xMulti = &xTemp;
needToFree = true;
}
for (int i=0; i<xMulti->val.array.rows * xMulti->val.array.columns; i++) {
T value;
operToScalar(value, xMulti->val.array.lparray[i]);
ret.push_back(value);
}
if (needToFree)
Excel(xlFree, 0, 1, &xTemp);
return ret;
} catch (const std::exception &e) {
if (needToFree)
Excel(xlFree, 0, 1, &xTemp);
std::ostringstream msg;
msg << "operToVector: " << e.what();
throw Exception(msg.str());
}
}
template <class T>
std::vector<T> fpToVector(const FP &fpVector) {
std::vector<T> ret;
for (int i=0; i<fpVector.rows * fpVector.columns; i++)
ret.push_back(fpVector.array[i]);
return ret;
}
}
#endif
--- NEW FILE: validations.hpp ---
/*
Copyright (C) 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef ohxl_conversions_validations_hpp
#define ohxl_conversions_validations_hpp
#include <oh/objhandlerdefines.hpp>
#include <xlsdk/xlsdkdefines.hpp>
#include <ohxl/functioncall.hpp>
#include <vector>
#include <string>
#include <boost/any.hpp>
namespace ObjHandler {
DLL_API void validateRange(const OPER *xRange, const std::string &name);
DLL_API void validateReference(const XLOPER *xReference, const std::string &name);
DLL_API void stringToChar(char *c, const std::string &s);
void operToOper(OPER *xTarget, const OPER *xSource);
}
#endif
--- NEW FILE: validations.cpp ---
/*
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2004, 2005, 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ohxl/Conversions/validations.hpp>
#include <oh/exception.hpp>
#include <sstream>
namespace ObjHandler {
bool validateMulti(const OPER *xMulti) {
for (int i=0; i<xMulti->val.array.rows * xMulti->val.array.columns; i++)
if (xMulti->val.array.lparray[i].xltype == xltypeErr)
return false;
return true;
}
DLL_API void validateRange(const OPER *xRange, const std::string &name) {
if (xRange->xltype == xltypeErr) {
std::ostringstream msg;
msg << "parameter '" << name << "' has error value";
throw Exception(msg.str());
} else if (xRange->xltype == xltypeMulti) {
if (!validateMulti(xRange)) {
std::ostringstream msg;
msg << "parameter '" << name << "' has error value";
throw Exception(msg.str());
}
}
}
DLL_API void validateReference(const XLOPER *xReference, const std::string &name) {
if (xReference->xltype != xltypeMissing
&& xReference->xltype != xltypeRef
&& xReference->xltype != xltypeSRef) {
std::ostringstream msg;
msg << "parameter '" << name << "' is not a valid range reference";
throw Exception(msg.str());
}
}
DLL_API void stringToChar(char *c, const std::string &value) {
int len = __min(XL_MAX_STR_LEN, value.length());
strncpy(c, value.c_str(), len);
c[len] = 0;
}
}
|