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

📄 string.c

📁 oci的源码,可以在任何平台上编译,相当方便实用
💻 C
📖 第 1 页 / 共 2 页
字号:
}

/* ------------------------------------------------------------------------ *
 * OCI_ConvertString
 * ------------------------------------------------------------------------ */

void OCI_ConvertString(void *str, int char_count, int size_char_in, 
                       int size_char_out)
{
    OCI_MoveString(str, str, char_count, size_char_in, size_char_out);
}

/* ------------------------------------------------------------------------ *
 *  
 * ------------------------------------------------------------------------ */

void OCI_CopyString(void *src, void *dest, int *size, int size_char_in,
                    int size_char_out)
{
    if ((src == NULL) || (dest == NULL) || (size == NULL))
        return;

    if (size_char_out == size_char_in)
    {
        memcpy(dest, src, (size_t) *size);
        memset((void*) (((char*) dest) + (*size)), 0, size_char_out);
    }
    else
        OCI_GetOutputString(src, dest, size, size_char_in, size_char_out);
}

/* ------------------------------------------------------------------------ *
 * OCI_ReleaseMetaString
 * ------------------------------------------------------------------------ */

void OCI_ReleaseMetaString(void *str)
{
    if (str == NULL)
        return;

#ifdef OCI_CHECK_METASTRINGS 

    free(str);

#endif
}

/* ------------------------------------------------------------------------ *
 * OCI_ReleaseDataString
 * ------------------------------------------------------------------------ */

void OCI_ReleaseDataString(void *str)
{
    if (str == NULL)
        return;

#ifdef OCI_CHECK_DATASTRINGS 

    free(str);

#endif
}


/* ------------------------------------------------------------------------ *
 * OCI_StringFromStringPtr
 * ------------------------------------------------------------------------ */

void * OCI_StringFromStringPtr(OCIString *str, void **buf, int *buflen)
{
    void *tmp = NULL;
    void *ret = NULL;  
  
    int olen  = 0;
    int osize = 0;
    int esize = 0;
    int msize = 0;

    OCI_CHECK(buf    == NULL, NULL);
    OCI_CHECK(buflen == NULL, NULL);

    tmp = OCIStringPtr(OCILib.env, str);

    if (tmp != NULL)
    {

#if defined(OCI_CHARSET_MIXED)

        /* tmp is ANSI and must be converted to UTF16 */

        esize  = 1;
        msize  = (int) sizeof(dtext);
        olen   = (int) strlen((char* ) tmp);
        osize  = olen * esize;
        
#elif defined(OCI_CHECK_DATASTRINGS)

        /* tmp is UTF16 and might be converted to UTF32 on unixes */

        
        esize  = (int) sizeof(odtext);
        msize  = (int) sizeof(dtext);
        olen   = (int) OCI_StringLength(tmp, sizeof(odtext));
        osize  = olen * esize;

#else

    OCI_NOT_USED(esize);

#endif

        /* do we need to use a buffer */

        if (olen > 0)
        {
            /* do we need to allocate/reallocate the buffer */
            
            if ((*buf) == NULL)
            {           
                *buflen = (olen+1) * msize;
                *buf    = OCI_MemAlloc(OCI_IPC_STRING, msize, olen+1, FALSE);
            }
            else if ((*buflen) < ((olen+1) * msize))
            {
                *buflen = (olen+1) * msize;
                *buf    = OCI_MemRealloc(*buf, OCI_IPC_STRING, msize, olen+1);
            }
        }

#if defined(OCI_CHARSET_MIXED)

        mbstowcs(*buf, tmp, olen + OCI_CVT_CHAR);

        memset( (void*) (((char*) *buf) + (olen*msize)), 0, msize);
        
        ret = *buf;

#elif defined(OCI_CHECK_DATASTRINGS)
  
        OCI_GetOutputDataString(tmp, *buf, &osize);
        
        memset( (void*) (((char*) *buf) + (osize)), 0, msize);
                
        ret = *buf;

#else

        osize = 0;
        ret   = tmp;

#endif

    }
    else
    {
        ret = tmp;
    }

    return ret;
}

/* ------------------------------------------------------------------------ *
 * OCI_StringFromStringPtr
 * ------------------------------------------------------------------------ */

boolean OCI_StringToStringPtr(OCIString **str, OCIError *err, void *value, 
                              void **buf, int *buflen)
{
    boolean res = TRUE;
    void *ostr  = NULL;  
    int osize   = 0;

#ifdef OCI_CHARSET_MIXED

    int olen    = 0;
    int esize   = 0;

#endif

    OCI_CHECK(str    == NULL, FALSE);
    OCI_CHECK(buf    == NULL, FALSE);
    OCI_CHECK(buflen == NULL, FALSE);

#ifdef OCI_CHARSET_MIXED

    /* value is UTF16 and must be converted to ANSI */
 
    esize  = (int) 1;
    olen   = (int) dtslen((dtext*) value);
    osize  = olen;

    /* do we need to use a buffer */

    if (olen > 0)
    {
        /* do we need to allocate/reallocate the buffer */

        if ((*buf) == NULL)
        {           
            *buflen = (olen+1) * esize;
            *buf    = OCI_MemAlloc(OCI_IPC_STRING, esize, olen+1, FALSE);

        }
        else if ((*buflen) < ((olen+1) * esize))
        {
            *buflen = (olen+1) * esize;
            *buf    = OCI_MemRealloc(*buf, OCI_IPC_STRING, esize, olen+1);
        }

    }    
    
    wcstombs((char *) *buf, (dtext *) value, olen + OCI_CVT_CHAR);

    ostr = *buf;
    
#else
    
    osize  = -1;
    ostr   = OCI_GetInputDataString(value, &osize);

#endif

    OCI_CALL3
    (
        res, err, 

        OCIStringAssignText(OCILib.env, err, (oratext *) ostr, (ub4) osize, str)
    )

    OCI_ReleaseDataString(ostr);

    return res;
}

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

/* ------------------------------------------------------------------------ *
 * ocistrdup
 * ------------------------------------------------------------------------ */

char * ocistrdup(const char * src)
{
    char *dst;

    OCI_CHECK(src == NULL, NULL)

    dst = (char *) malloc((strlen(src) + 1) * sizeof(*dst));

    if (dst != NULL)
        strcpy(dst, src);

    return dst;
}

/* ------------------------------------------------------------------------ *
 * ocistrcasecmp
 * ------------------------------------------------------------------------ */

int ocistrcasecmp(const char *str1, const char *str2)
{
    if (str1 == NULL && str2 == NULL)
        return 0;

    if (str1 == NULL)
        return 1;

    if (str2 == NULL)
        return -1;

    while (((*str1) != 0) && 
           ((*str2) != 0) &&
           (tolower((int)(*str1)) == tolower((int)(*str2))))
    {
        str1++;
        str2++;
    }

    return (tolower((int) (*str1)) - tolower((int) (*str2)));
}

/* ------------------------------------------------------------------------ *
 * ocisprintf
 * ------------------------------------------------------------------------ */

int ocisprintf(char *str, int size, const char *format, ...)
{
    va_list args;
    int n;

    va_start(args, format);

    n = vsnprintf(str, (size_t) size, format, args);

    va_end(args);

    return n;
}

#ifdef OCI_INCLUDE_WCHAR

/* ------------------------------------------------------------------------ *
 * ociwcsdup
 * ------------------------------------------------------------------------ */

wchar_t * ociwcsdup(const wchar_t * src)
{
    wchar_t *dst;

    OCI_CHECK(src == NULL, NULL)

    dst = (wchar_t *) malloc((wcslen(src)+1) * sizeof(wchar_t));

    if (dst != NULL)
        wcscpy(dst, src);

    return dst;
}

/* ------------------------------------------------------------------------ *
 * ociwcscasecmp
 * ------------------------------------------------------------------------ */

int ociwcscasecmp(const wchar_t *str1, const wchar_t *str2)
{
     if (str1 == NULL && str2 == NULL)
        return 0;

    if (str1 == NULL)
        return 1;

    if (str2 == NULL)
        return -1;

    while ((*str1 != 0) && (*str2 != 0) &&
           (towlower((wint_t)*str1) == towlower((wint_t)*str2)))
    {
        str1++;
        str2++;
    }

    return (towlower((wint_t) *str1) - towlower((wint_t) *str2));
}

#endif

⌨️ 快捷键说明

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