⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.c

📁 ORACLE编程的好东西,纯C写的OCI封装.很好用,支持数据池.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
   +----------------------------------------------------------------------+   
   |                                                                      |
   |                     OCILIB - C Driver for Oracle                     |
   |                                                                      |
   |                      (C Wrapper for Oracle OCI)                      |
   |                                                                      |
   +----------------------------------------------------------------------+
   |                      Website : http://ocilib.net                     |
   +----------------------------------------------------------------------+
   |               Copyright (c) 2007-2008 Vincent ROGIER                 |
   +----------------------------------------------------------------------+
   | This library is free software; you can redistribute it and/or        |
   | modify it under the terms of the GNU Library General Public          |
   | License as published by the Free Software Foundation; either         |
   | version 2 of the License, or (at your option) any later version.     |
   |                                                                      |
   | This library 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 GNU    |
   | Library General Public License for more details.                     |
   |                                                                      |
   | You should have received a copy of the GNU Library General Public    |
   | License along with this library; if not, write to the Free           |
   | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   |
   +----------------------------------------------------------------------+
   |          Author: Vincent ROGIER <vince.rogier@gmail.com>             |
   +----------------------------------------------------------------------+ 
*/

/* ------------------------------------------------------------------------ *
 * $Id: file.c, v 3.0.0 2008/10/09 09:54 Vince $
 * ------------------------------------------------------------------------ */

#include "ocilib_internal.h"

/* ************************************************************************ *
 *                             PRIVATE FUNCTIONS
 * ************************************************************************ */

/* ------------------------------------------------------------------------ *
 * OCI_FileInit
 * ------------------------------------------------------------------------ */

 OCI_File * OCI_FileInit(OCI_Connection *con, OCI_File **pfile, 
                         OCILobLocator *handle, ub4 type)
{
    OCI_File *file = NULL;
    boolean res    = TRUE;

    OCI_CHECK(pfile == NULL, NULL);

    if (*pfile == NULL)
        *pfile = (OCI_File *) OCI_MemAlloc(OCI_IPC_FILE, sizeof(*file), 1, TRUE);

    if (*pfile != NULL)
    {
        file = *pfile;
    
        file->type   = type;
        file->con    = con;
        file->handle = handle;
        file->offset = 1;

        /* reset file info */

        if (file->dir != NULL)
            file->dir[0] = 0;

        if (file->name != NULL)
            file->name[0] = 0;

        if (file->handle == NULL)
        {
            /* allocate handle for non fetched file (local file object) */

            file->hstate = OCI_OBJECT_ALLOCATED;

            res = (OCI_SUCCESS == OCI_DescriptorAlloc((dvoid *) OCILib.env, 
                                                     (dvoid **) (void *) &file->handle,
                                                     (ub4) OCI_DTYPE_LOB, 
                                                     (size_t) 0, (dvoid **) NULL));
        }
        else
            file->hstate = OCI_OBJECT_FETCHED_CLEAN;
    }
    else
        res = FALSE;

    /* check for failure */

    if (res == FALSE)
    {
        OCI_FileFree(file);
        file = NULL;
    }

    return file;
}

/* ------------------------------------------------------------------------ *
 * OCI_FileGetInfo
 * ------------------------------------------------------------------------ */

boolean OCI_FileGetInfo(OCI_File *file)
{
    boolean res  = TRUE;
    void *ostr1  = NULL;
    void *ostr2  = NULL;
    int  osize1  = 0;
    int  osize2  = 0;
    ub2  usize1  = 0;
    ub2  usize2  = 0;

    OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);

    /* directory name */

    if (file->dir == NULL)
    {
        if (res == TRUE)
        {
            file->dir = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
                                               OCI_SIZE_DIRECTORY + 1, TRUE);

            res = (file->dir != NULL);
         }
    }
    else
        file->dir[0] = 0;

    /* file name */

    if (file->name == NULL)
    {
        if (res == TRUE)
        {
            file->name = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
                                                OCI_SIZE_FILENAME + 1, TRUE);

            res = (file->name != NULL);
        }
    }
    else
        file->name[0] = 0;

    /* retrieve name */

    if (res == TRUE)
    {
        osize1 = OCI_SIZE_DIRECTORY  * sizeof(mtext);
        ostr1  = OCI_GetInputMetaString(file->dir, &osize1);

        osize2 = OCI_SIZE_FILENAME  * sizeof(mtext);
        ostr2  = OCI_GetInputMetaString(file->name, &osize1);
     
        usize1 = (ub2) osize1;
        usize2 = (ub2) osize2;

        OCI_CALL2
        (
            res, file->con, 
            
            OCILobFileGetName(OCILib.env, file->con->err, file->handle,
                              (OraText *) ostr1, (ub2*) &usize1,
                              (OraText *) ostr2, (ub2*) &usize2)
        )

        osize1 = (int) usize1;
        osize2 = (int) usize2;

        OCI_GetOutputMetaString(ostr1, file->dir,  &osize1);
        OCI_GetOutputMetaString(ostr2, file->name, &osize2);

        OCI_ReleaseMetaString(ostr1);
        OCI_ReleaseMetaString(ostr2);
    }

    return res;
}

/* ************************************************************************ *
 *                            PUBLIC FUNCTIONS
 * ************************************************************************ */

/* ------------------------------------------------------------------------ *
 * OCI_FileCreate
 * ------------------------------------------------------------------------ */

OCI_File * OCI_API OCI_FileCreate(OCI_Connection *con, unsigned int type)
{
    OCI_File *file = NULL;

    OCI_CHECK_INITIALIZED(NULL);

    OCI_CHECK_PTR(OCI_IPC_CONNECTION, con, NULL);

    file = OCI_FileInit(con, &file, NULL, type);

    OCI_RESULT(file != NULL);

    return file;
}

/* ------------------------------------------------------------------------ *
 * OCI_FileFree
 * ------------------------------------------------------------------------ */

boolean OCI_API OCI_FileFree(OCI_File *file)
{
    boolean res = TRUE;

    OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
    OCI_CHECK_OBJECT_FETCHED(file, FALSE);

    OCI_FREE(file->dir);
    OCI_FREE(file->name);

    if (file->hstate == OCI_OBJECT_ALLOCATED)
    {
        OCI_DescriptorFree((dvoid *) file->handle, (ub4) OCI_DTYPE_LOB);
    }

    OCI_FREE(file);

    OCI_RESULT(res);

    return res;
}

/* ------------------------------------------------------------------------ *
 * OCI_FileSeek
 * ------------------------------------------------------------------------ */

boolean OCI_API OCI_FileSeek(OCI_File *file, big_uint offset, unsigned int mode)
{
    boolean res   = TRUE;
    big_uint size = 0;

    OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);

    size = OCI_FileGetSize(file);

    if ((mode == OCI_SEEK_CUR && (offset + file->offset-1) > size))
        res = FALSE;
    else if (mode == OCI_SEEK_SET)
        file->offset  = offset + 1;
    else if (mode == OCI_SEEK_END)
        file->offset  = size-offset + 1;
    else if (mode == OCI_SEEK_CUR)
        file->offset += offset;
    else
        res = FALSE;

    OCI_RESULT(res);

    return res;
}

/* ------------------------------------------------------------------------ *
 * OCI_FileGetOffset
 * ------------------------------------------------------------------------ */

big_uint OCI_API OCI_FileGetOffset(OCI_File *file)
{
    OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);

    OCI_RESULT(TRUE);

    return file->offset - 1;
}

/* ------------------------------------------------------------------------ *
 * OCI_FileRead
 * ------------------------------------------------------------------------ */

unsigned int OCI_API OCI_FileRead(OCI_File *file, void *buffer, unsigned int len)
{
    boolean res  = TRUE;
    ub4 size_in  = 0;
    ub4 size_out = 0;

    OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);

    size_out = size_in = len;

#ifdef OCI_LOB2_API_ENABLED

    if (OCILib.use_lob_ub8)
    {
        ub8 size_char = (ub8) len;
        ub8 size_byte = (ub8) size_in;
        
        OCI_CALL2
        (
            res, file->con, 
            
            OCILobRead2(file->con->cxt, file->con->err,
                        file->handle, &size_byte,
                        &size_char, (ub8) file->offset, 
                        buffer, (ub8) size_in, 
                        (ub1) OCI_ONE_PIECE, (dvoid *) NULL,
                        NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT)
        )
    }

    else

#endif

    {
        ub4 offset = (ub4) file->offset;

        OCI_CALL2
        (

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -