예제 #1
0
파일: rddcopy.c 프로젝트: Paulosnunes/core
/* Export field value into the buffer in PG accepted CSV format */
static HB_BOOL exportBufSqlVar( pgCopyContext * context, PHB_ITEM pValue, const char * szQuote, const char * szEsc )
{
   switch( hb_itemType( pValue ) )
   {
      case HB_IT_STRING:
      case HB_IT_MEMO:
      {
         HB_SIZE      nLen  = hb_itemGetCLen( pValue );
         HB_SIZE      nCnt  = 0;
         const char * szVal = hb_itemGetCPtr( pValue );

         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;

         if( context->str_trim )
         {
            while( nLen && HB_ISSPACE( szVal[ nLen - 1 ] ) )
               nLen--;
         }

         while( *szVal && nCnt++ < nLen )
         {
            /* if( *szVal == *szDelim || *szVal == *szEsc || *szVal == *szQuote )
               we don't need to escape delim in CSV mode,
               only the quote and the escape itself */

            if( *szVal == *szQuote || *szVal == *szEsc )
               if( ! addToContext( context, *szEsc ) )
                  return HB_FALSE;
            if( ( HB_UCHAR ) *szVal >= 32 )
               if( ! addToContext( context, *szVal ) )
                  return HB_FALSE;
            szVal++;
         }
         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         break;
      }

      case HB_IT_DATE:
      {
         char szDate[ 9 ];

         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         hb_itemGetDS( pValue, szDate );
         if( szDate[ 0 ] == ' ' )
         {
            if( ! addStrToContext( context, "0100-01-01" ) )
               return HB_FALSE;
         }
         else
         {
            if( ! addStrnToContext( context, &szDate[ 0 ], 4 ) ||
                ! addToContext( context, '-' ) ||
                ! addStrnToContext( context, &szDate[ 4 ], 2 ) ||
                ! addToContext( context, '-' ) ||
                ! addStrnToContext( context, &szDate[ 6 ], 2 ) )
               return HB_FALSE;
         }
         if( ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         break;
      }

      case HB_IT_TIMESTAMP:
      {
         long lDate, lTime;
         char szDateTime[ 24 ];

         hb_itemGetTDT( pValue, &lDate, &lTime );
         hb_timeStampStr( szDateTime, lDate, lTime );
         if( ! addStrToContext( context, szQuote ) ||
             ! addStrToContext( context, szDateTime ) ||
             ! addStrToContext( context, szQuote ) )
            return HB_FALSE;
         break;
      }

      case HB_IT_LOGICAL:
#if 0
         if( ! addStrToContext( context, szQuote ) || ! addToContext( context, hb_itemGetL( pValue ) ? 'Y' : 'N' ) || ! addStrToContext( context, szQuote ) )
#endif
         if( ! addToContext( context, hb_itemGetL( pValue ) ? 'Y' : 'N' ) )
            return HB_FALSE;
         break;

      case HB_IT_INTEGER:
      case HB_IT_LONG:
      case HB_IT_DOUBLE:
      {
         char szResult[ HB_MAX_DOUBLE_LENGTH ];
         int  iSize, iWidth, iDec;

         hb_itemGetNLen( pValue, &iWidth, &iDec );
         iSize = ( iDec > 0 ? iWidth + 1 + iDec : iWidth );
         if( hb_itemStrBuf( szResult, pValue, iSize, iDec ) )
         {
            int iPos = 0;
            while( iSize && HB_ISSPACE( szResult[ iPos ] ) )
            {
               iPos++;
               iSize--;
            }
            if( ! addStrnToContext( context, &szResult[ iPos ], iSize ) )
               return HB_FALSE;
         }
         else
         if( ! addToContext( context, '0' ) )
            return HB_FALSE;
         break;
      }
      /* an "M" field or the other, might be a "V" in SixDriver */
      default:
         return HB_FALSE;
   }

   return HB_TRUE;
}
예제 #2
0
파일: math.c 프로젝트: emazv72/core
/* Harbour default math error handling routine */
static int hb_matherr( HB_MATH_EXCEPTION * pexc )
{
   int mode = hb_mathGetErrMode();
   int iRet = 1;

   HB_TRACE( HB_TR_DEBUG, ( "hb_matherr(%p)", ( void * ) pexc ) );

   if( pexc == NULL || pexc->handled != 0 )
   {
      /* error already handled by other handlers ! */
      return 1;
   }

   if( mode == HB_MATH_ERRMODE_USER || mode == HB_MATH_ERRMODE_USERDEFAULT ||
       mode == HB_MATH_ERRMODE_USERCDEFAULT )
   {
      PHB_ITEM pArg1, pArg2, pError;
      PHB_ITEM pMatherrResult;

      /* create an error object */
      /* NOTE: In case of HB_MATH_ERRMODE_USER[C]DEFAULT, I am setting both
               EF_CANSUBSTITUTE and EF_CANDEFAULT to .T. here.
               This is forbidden according to the original Cl*pper docs, but
               I think this reflects the situation best here:
               The error handler can either substitute the erroneous value
               (by returning a numeric value) or choose the default error
               handling (by returning .F., as usual) [martin vogel] */
      pError = hb_errRT_New_Subst( ES_ERROR, "MATH", EG_NUMERR, pexc->type,
                                   pexc->error, pexc->funcname, 0, EF_CANSUBSTITUTE |
                                   ( mode == HB_MATH_ERRMODE_USER ? 0 : EF_CANDEFAULT ) );

      /* Assign the new array to the object data item. */
      /* NOTE: Unfortunately, we cannot decide whether one or two parameters
               have been used when the math function has been called, so we
               always take two */
      pArg1 = hb_itemPutND( NULL, pexc->arg1 );
      pArg2 = hb_itemPutND( NULL, pexc->arg2 );
      hb_errPutArgs( pError, 2, pArg1, pArg2 );
      hb_itemRelease( pArg1 );
      hb_itemRelease( pArg2 );

      /* launch error codeblock */
      pMatherrResult = hb_errLaunchSubst( pError );
      hb_errRelease( pError );

      if( pMatherrResult )
      {
         if( HB_IS_NUMERIC( pMatherrResult ) )
         {
            pexc->retval = hb_itemGetND( pMatherrResult );
            hb_itemGetNLen( pMatherrResult, &pexc->retvalwidth, &pexc->retvaldec );
            pexc->handled = 1;
         }
         hb_itemRelease( pMatherrResult );
      }
   }

   /* math exception not handled by Harbour error routine above ? */
   if( pexc->handled == 0 )
   {
      switch( mode )
      {
         case HB_MATH_ERRMODE_USER:
            /* user failed to handle the math exception, so quit the app
               [yes, that's the meaning of this mode !!] */
            iRet = 0;
            hb_vmRequestQuit();
            break;

         case HB_MATH_ERRMODE_DEFAULT:
         case HB_MATH_ERRMODE_USERDEFAULT:
            /* return 1 to suppress C RTL error msgs, but leave error
               handling to the calling Harbour routine */
            break;

         case HB_MATH_ERRMODE_CDEFAULT:
         case HB_MATH_ERRMODE_USERCDEFAULT:
            /* use the correction value supplied in pexc->retval */
            pexc->handled = 1;
            break;
      }
   }

   return iRet;  /* error handling successful */
}
예제 #3
0
파일: dbsql.c 프로젝트: cwanderlei/hbtest3
/* Export field value into the buffer in SQL format */
static HB_BOOL hb_exportBufSqlVar( PHB_FILEBUF pFileBuf, PHB_ITEM pValue,
                                   const char * szDelim, const char * szEsc )
{
   switch( hb_itemType( pValue ) )
   {
      case HB_IT_STRING:
      {
         HB_SIZE nLen = hb_itemGetCLen( pValue );
         HB_SIZE nCnt = 0;
         const char *szVal = hb_itemGetCPtr( pValue );

         hb_addStrToFBuffer( pFileBuf, szDelim );
         while( nLen && HB_ISSPACE( szVal[ nLen - 1 ] ) )
            nLen--;

         while( *szVal && nCnt++ < nLen )
         {
            if( *szVal == *szDelim || *szVal == *szEsc )
               hb_addToFBuffer( pFileBuf, *szEsc );
            if( ( HB_UCHAR ) *szVal >= 32 )
               hb_addToFBuffer( pFileBuf, *szVal );
            else
            {
               /* printf( "%d %c", *szVal, *szVal ); */
            }
            szVal++;
         }
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;
      }

      case HB_IT_DATE:
      {
         char szDate[ 9 ];

         hb_addStrToFBuffer( pFileBuf, szDelim );
         hb_itemGetDS( pValue, szDate );
         if( szDate[ 0 ] == ' ' )
         {
            hb_addStrToFBuffer( pFileBuf, "0100-01-01" );
         }
         else
         {
            hb_addStrnToFBuffer( pFileBuf, &szDate[0], 4 );
            hb_addToFBuffer( pFileBuf, '-' );
            hb_addStrnToFBuffer( pFileBuf, &szDate[4], 2 );
            hb_addToFBuffer( pFileBuf, '-' );
            hb_addStrnToFBuffer( pFileBuf, &szDate[6], 2 );
         }
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;
      }

      case HB_IT_TIMESTAMP:
      {
         long lDate, lTime;
         char szDateTime[ 24 ];

         hb_itemGetTDT( pValue, &lDate, &lTime );
         hb_timeStampStr( szDateTime, lDate, lTime );
         hb_addStrToFBuffer( pFileBuf, szDelim );
         hb_addStrToFBuffer( pFileBuf, szDateTime );
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;
      }

      case HB_IT_LOGICAL:
         hb_addStrToFBuffer( pFileBuf, szDelim );
         hb_addToFBuffer( pFileBuf, hb_itemGetL( pValue ) ? 'Y' : 'N' );
         hb_addStrToFBuffer( pFileBuf, szDelim );
         break;

      case HB_IT_INTEGER:
      case HB_IT_LONG:
      case HB_IT_DOUBLE:
      {
         char szResult[ HB_MAX_DOUBLE_LENGTH ];
         int iSize, iWidth, iDec;

         hb_itemGetNLen( pValue, &iWidth, &iDec );
         iSize = ( iDec > 0 ? iWidth + 1 + iDec : iWidth );
         if( hb_itemStrBuf( szResult, pValue, iSize, iDec ) )
         {
            int iPos = 0;
            while( iSize && HB_ISSPACE( szResult[ iPos ] ) )
            {
               iPos++;
               iSize--;
            }
            hb_addStrnToFBuffer( pFileBuf, &szResult[ iPos ], iSize );
         }
         else
            hb_addToFBuffer( pFileBuf, '0' );
         break;
      }
      /* an "M" field or the other, might be a "V" in SixDriver */
      default:
         /* We do not want MEMO contents */
         return HB_FALSE;
   }
   return HB_TRUE;
}
예제 #4
0
파일: math.c 프로젝트: emazv72/core
static int hb_matherrblock( HB_MATH_EXCEPTION * pexc )
{
   PHB_MATHERRDATA pMathErr = hb_mathErrData();
   int retval;

   /* call codeblock for both case: handled and unhandled exceptions */

   if( pMathErr->block )
   {
      PHB_ITEM pArray, pRet;
      PHB_ITEM pType, pFuncname, pError, pArg1, pArg2, pRetval, pHandled;
      const char * funcname = pexc->funcname;

      if( funcname == HB_ERR_FUNCNAME )
      {
         PHB_SYMB pSym = hb_itemGetSymbol( hb_stackBaseItem() );
         if( pSym )
            funcname = pSym->szName;
      }

      pType = hb_itemPutNI( NULL, pexc->type );
      pFuncname = hb_itemPutC( NULL, funcname );
      pError = hb_itemPutC( NULL, pexc->error );
      pArg1 = hb_itemPutND( NULL, pexc->arg1 );
      pArg2 = hb_itemPutND( NULL, pexc->arg2 );
      pRetval = hb_itemPutNDLen( NULL, pexc->retval, pexc->retvalwidth, pexc->retvaldec );
      pHandled = hb_itemPutL( NULL, pexc->handled );

      pArray = hb_itemArrayNew( 2 );
      hb_itemArrayPut( pArray, 1, pRetval );
      hb_itemArrayPut( pArray, 2, pHandled );

      /* launch error codeblock that can
         a) change the members of the array = {dRetval, lHandled} to set the
            return value of the math C RTL routine and the <exception handled
            flag> and it
         b) can return an integer value to set the return value of matherr().
         NOTE that these values are only used if lHandled was .F. and is set
         to .T. within the codeblock */
      pRet = hb_itemDo( pMathErr->block, 6, pType, pFuncname, pError, pArg1, pArg2, pArray );

      hb_itemRelease( pType );
      hb_itemRelease( pFuncname );
      hb_itemRelease( pError );
      hb_itemRelease( pArg1 );
      hb_itemRelease( pArg2 );
      hb_itemRelease( pRetval );
      hb_itemRelease( pHandled );

      if( pexc->handled )
      {
         /* math exception has already been handled, so codeblock call above
            was only informative */
         retval = 1;
      }
      else
      {
         /* exception handled by codeblock ? */
         pHandled = hb_itemArrayGet( pArray, 2 );
         if( pHandled )
         {
            pexc->handled = hb_itemGetL( pHandled );
            hb_itemRelease( pHandled );
         }

         if( pexc->handled )
         {
            /* YES ! */
            /* extract retval for math routine and matherr() */
            pRetval = hb_itemArrayGet( pArray, 1 );
            if( pRetval )
            {
               pexc->retval = hb_itemGetND( pRetval );
               hb_itemGetNLen( pRetval, &pexc->retvalwidth, &pexc->retvaldec );
               hb_itemRelease( pRetval );
            }
            if( pRet && HB_IS_NUMERIC( pRet ) )
            {
               retval = hb_itemGetNI( pRet );  /* block may also return 0 to force C math lib warnings */
               hb_itemRelease( pRet );
            }
            else
            {
               retval = 1;  /* default return value to suppress C math lib warnings */
            }
         }
         else
         {
            /* NO ! */
            retval = 1;
         }
      }
      hb_itemRelease( pArray );
   }
   else
   {
      retval = 1;  /* default return value to suppress C math lib warnings */
   }

   if( pMathErr->prevHandler )
   {
      if( pexc->handled )
      {
         /* the error is handled, so simply inform the previous handler */
         ( *pMathErr->prevHandler )( pexc );
      }
      else
      {
         /* else go on error handling within previous handler */
         retval = ( *pMathErr->prevHandler )( pexc );
      }
   }
   return retval;
}