static PHB_FILE s_fileOpen( PHB_FILE_FUNCS pFuncs, const char * pszName, const char * pszDefExt, HB_USHORT uiExFlags, const char * pPaths, PHB_ITEM pError ) { PHB_IOUSR pIO = ( PHB_IOUSR ) pFuncs; PHB_FILE pFile = NULL; PHB_ITEM pFileItm; s_pushMethod( pIO, IOUSR_OPEN ); hb_vmPushString( pszName, strlen( pszName ) ); if( pszDefExt ) hb_vmPushString( pszDefExt, strlen( pszDefExt ) ); else hb_vmPushNil(); hb_vmPushInteger( uiExFlags ); if( pPaths ) hb_vmPushString( pPaths, strlen( pPaths ) ); else hb_vmPushNil(); if( pError ) hb_vmPush( pError ); else hb_vmPushNil(); hb_vmDo( 5 ); pFileItm = hb_stackReturnItem(); if( ! HB_IS_NIL( pFileItm ) ) pFile = s_fileNew( pIO, hb_itemNew( pFileItm ) ); return pFile; }
static PHB_FILE s_fileOpen( PHB_FILE_FUNCS pFuncs, const char * pszName, const char * pszDefExt, HB_FATTR nExFlags, const char * pPaths, PHB_ITEM pError ) { PHB_FILE pFile = NULL; HB_ERRCODE errcode = 0; int iPort, iTimeout, iBaud, iParity, iSize, iStop, iFlow; HB_BOOL fRead, fWrite; HB_SYMBOL_UNUSED( pFuncs ); HB_SYMBOL_UNUSED( pszDefExt ); HB_SYMBOL_UNUSED( pPaths ); fRead = fWrite = HB_TRUE; iPort = s_filePortParams( pszName, &iTimeout, &iBaud, &iParity, &iSize, &iStop, &iFlow ); if( iPort > 0 ) { if( hb_comOpen( iPort ) == 0 && hb_comInit( iPort, iBaud, iParity, iSize, iStop ) == 0 && hb_comFlowControl( iPort, NULL, iFlow ) == 0 ) { switch( nExFlags & ( FO_READ | FO_WRITE | FO_READWRITE ) ) { case FO_READ: fWrite = HB_FALSE; break; case FO_WRITE: fRead = HB_FALSE; break; } pFile = s_fileNew( iPort, iTimeout, fRead, fWrite ); } else errcode = hb_comGetError( iPort ); } else errcode = HB_COM_ERR_WRONGPORT; hb_fsSetError( errcode ); if( pError ) { hb_errPutFileName( pError, pszName ); if( pFile == NULL ) { hb_errPutOsCode( pError, errcode ); hb_errPutGenCode( pError, ( HB_ERRCODE ) EG_OPEN ); } } return pFile; }
static PHB_FILE s_fileOpen( PHB_FILE_FUNCS pFuncs, const char * pszName, const char * pszDefExt, HB_FATTR nExFlags, const char * pPaths, PHB_ITEM pError ) { const char * pszHost = pszName + FILE_PREFIX_LEN, * ptr; PHB_FILE pFile = NULL; HB_ERRCODE errcode = 0; HB_SIZE nLen = 0; int iPort = 0; HB_MAXINT timeout = -1; HB_SYMBOL_UNUSED( pFuncs ); HB_SYMBOL_UNUSED( pszDefExt ); HB_SYMBOL_UNUSED( pPaths ); if( ( ptr = strchr( pszHost, ':' ) ) != NULL && ptr != pszHost ) { nLen = ptr - pszHost; ++ptr; while( HB_ISDIGIT( * ptr ) ) iPort = iPort * 10 + ( * ptr++ - '0' ); if( * ptr == ':' ) { ++ptr; while( HB_ISDIGIT( * ptr ) ) timeout = HB_MAX( timeout, 0 ) * 10 + ( * ptr++ - '0' ); } if( * ptr != 0 && * ptr != ':' ) iPort = 0; } if( iPort > 0 ) { char * pszAddr, * pszIpAddr; hb_socketAutoInit(); pszAddr = hb_strndup( pszHost, nLen ); pszIpAddr = hb_socketResolveAddr( pszAddr, HB_SOCKET_AF_INET ); hb_xfree( pszAddr ); if( pszIpAddr ) { HB_SOCKET sd = hb_socketOpen( HB_SOCKET_PF_INET, HB_SOCKET_PT_STREAM, 0 ); if( sd != HB_NO_SOCKET ) { void * pSockAddr; unsigned uiLen; if( hb_socketInetAddr( &pSockAddr, &uiLen, pszIpAddr, iPort ) ) { hb_socketSetKeepAlive( sd, HB_TRUE ); if( hb_socketConnect( sd, pSockAddr, uiLen, timeout ) == 0 ) { PHB_SOCKEX sock; switch( nExFlags & ( FO_READ | FO_WRITE | FO_READWRITE ) ) { case FO_READ: hb_socketShutdown( sd, HB_SOCKET_SHUT_WR ); break; case FO_WRITE: hb_socketShutdown( sd, HB_SOCKET_SHUT_RD ); break; } sock = hb_sockexNew( sd, NULL, NULL ); if( sock ) { hb_sockexSetShutDown( sock, HB_TRUE ); hb_sockexSetAutoFlush( sock, HB_TRUE ); pFile = s_fileNew( sock, timeout ); sd = HB_NO_SOCKET; } } hb_xfree( pSockAddr ); } if( sd != HB_NO_SOCKET ) { errcode = hb_socketGetError(); hb_socketClose( sd ); } } hb_xfree( pszIpAddr ); } if( errcode == 0 && pFile == NULL ) errcode = hb_socketGetError(); } else errcode = HB_SOCKET_ERR_WRONGADDR; hb_fsSetError( errcode ); if( pError ) { hb_errPutFileName( pError, pszName ); if( pFile == NULL ) { hb_errPutOsCode( pError, errcode ); hb_errPutGenCode( pError, ( HB_ERRCODE ) EG_OPEN ); } } return pFile; }
static PHB_FILE hb_fileFromSocket( PHB_SOCKEX sock, HB_MAXINT timeout ) { return sock && hb_sockexGetHandle( sock ) != HB_NO_SOCKET ? s_fileNew( sock, timeout ) : NULL; }