Skip to content

Commit 7a79cca

Browse files
committed
closes #78
adds a boolean to AssureLongScalarPar to enable catching conversion errors at this early level if necessary.
1 parent 7bf88b8 commit 7a79cca

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/basic_pro.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -967,8 +967,10 @@ namespace lib {
967967
SizeT nParam = e->NParam(1);
968968

969969
DLong lun;
970-
e->AssureLongScalarPar(0, lun);
971-
970+
e->AssureLongScalarPar(0, lun, true); //throw on an input conversion error when defining lun see #78. IDL catches this too and sets !ERR
971+
972+
if (lun==0) return; //see #78 . 0 is open and ready, but only for READ, READF. READU just returns without setting !ERR
973+
972974
istream* is = NULL;
973975
igzstream* igzs = NULL;
974976
bool f77 = false;

src/envt.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1664,10 +1664,10 @@ BaseGDL*& EnvT::GetPar(SizeT i)
16641664
/**
16651665
* @brief converts par if necessary and sets 'scalar' to Long . Chokes on !NULL, NULL and non-scalar
16661666
*/
1667-
void EnvBaseT::AssureLongScalarPar( SizeT pIx, DLong64& scalar)
1667+
void EnvBaseT::AssureLongScalarPar( SizeT pIx, DLong64& scalar, bool throwIfConversionErrorOccured)
16681668
{
16691669
BaseGDL* p = GetParDefined( pIx);
1670-
DLong64GDL* lp = static_cast<DLong64GDL*>(p->Convert2( GDL_LONG64, BaseGDL::COPY));
1670+
DLong64GDL* lp = static_cast<DLong64GDL*>(p->Convert2( GDL_LONG64, throwIfConversionErrorOccured?BaseGDL::COPY_THROWIOERROR:BaseGDL::COPY));
16711671
Guard<DLong64GDL> guard_lp( lp);
16721672
if( !lp->Scalar( scalar))
16731673
Throw("Parameter must be a scalar or 1 element array in this context: "+
@@ -1676,10 +1676,10 @@ void EnvBaseT::AssureLongScalarPar( SizeT pIx, DLong64& scalar)
16761676
/**
16771677
* @brief converts par if necessary and sets 'scalar' to Long . Chokes on !NULL, NULL and non-scalar
16781678
*/
1679-
void EnvBaseT::AssureLongScalarPar( SizeT pIx, DLong& scalar)
1679+
void EnvBaseT::AssureLongScalarPar( SizeT pIx, DLong& scalar, bool throwIfConversionErrorOccured)
16801680
{
16811681
BaseGDL* p = GetParDefined( pIx);
1682-
DLongGDL* lp = static_cast<DLongGDL*>(p->Convert2( GDL_LONG, BaseGDL::COPY));
1682+
DLongGDL* lp = static_cast<DLongGDL*>(p->Convert2( GDL_LONG, throwIfConversionErrorOccured?BaseGDL::COPY_THROWIOERROR:BaseGDL::COPY));
16831683
Guard<DLongGDL> guard_lp( lp);
16841684
if( !lp->Scalar( scalar))
16851685
Throw("Parameter must be a scalar or 1 element array in this context: "+
@@ -1688,16 +1688,16 @@ void EnvBaseT::AssureLongScalarPar( SizeT pIx, DLong& scalar)
16881688
/**
16891689
* @brief converts par if necessary and sets 'scalar' to Long . Chokes on !NULL, NULL and non-scalar
16901690
*/
1691-
void EnvT::AssureLongScalarPar( SizeT pIx, DLong64& scalar)
1691+
void EnvT::AssureLongScalarPar( SizeT pIx, DLong64& scalar, bool throwIfConversionErrorOccured)
16921692
{
1693-
EnvBaseT::AssureLongScalarPar( pIx, scalar);
1693+
EnvBaseT::AssureLongScalarPar( pIx, scalar, throwIfConversionErrorOccured);
16941694
}
16951695
/**
16961696
* @brief converts par if necessary and sets 'scalar' to Long . Chokes on !NULL, NULL and non-scalar
16971697
*/
1698-
void EnvT::AssureLongScalarPar( SizeT pIx, DLong& scalar)
1698+
void EnvT::AssureLongScalarPar( SizeT pIx, DLong& scalar, bool throwIfConversionErrorOccured)
16991699
{
1700-
EnvBaseT::AssureLongScalarPar( pIx, scalar);
1700+
EnvBaseT::AssureLongScalarPar( pIx, scalar, throwIfConversionErrorOccured);
17011701
}
17021702
/**
17031703
* @brief converts ix if necessary and sets 'scalar' to Long . scalar unchanged if !NULL, NULL. Chokes on non-scalar

src/envt.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ class EnvBaseT
344344

345345
void AssureGlobalKW( SizeT ix);
346346

347-
// converts parameter 'ix' if necessary and sets 'scalar'
348-
void AssureLongScalarPar( SizeT ix, DLong& scalar);
349-
void AssureLongScalarPar( SizeT ix, DLong64& scalar);
347+
// converts parameter 'ix' if necessary and sets 'scalar' . sets scalar to 0 if a type conversion was found, or throws depending on bool
348+
void AssureLongScalarPar( SizeT ix, DLong& scalar, bool throwIfConversionErrorOccured=false);
349+
void AssureLongScalarPar( SizeT ix, DLong64& scalar, bool throwIfConversionErrorOccured=false);
350350
// get i'th parameter
351351
// throws if not defined (ie. never returns NULL, but can optionally return a !NULL)
352352
BaseGDL*& GetParDefined(SizeT i, bool rejectNulls=true); //, const std::string& subName = "");
@@ -886,8 +886,8 @@ class EnvT: public EnvBaseT
886886
void AssureLongScalarKW( SizeT ix, DLong& scalar);
887887
void AssureLongScalarKW( SizeT ix, DLong64& scalar);
888888
// converts parameter 'ix' if necessary and sets 'scalar'
889-
void AssureLongScalarPar( SizeT ix, DLong& scalar);
890-
void AssureLongScalarPar( SizeT ix, DLong64& scalar);
889+
void AssureLongScalarPar( SizeT ix, DLong& scalar, bool throwIfConversionErrorOccured=false);
890+
void AssureLongScalarPar( SizeT ix, DLong64& scalar, bool throwIfConversionErrorOccured=false);
891891

892892
// same as for Long
893893
void AssureDoubleScalarKWIfPresent( const std::string& kw, DDouble& scalar);

0 commit comments

Comments
 (0)